All files / src/api/subscription index.js

83.33% Statements 20/24
100% Branches 2/2
91.67% Functions 11/12
83.33% Lines 20/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191    2x 2x     2x                                     2x                                                                                                                             1x                     1x   1x       1x       1x     6x             8x     6x           1x           1x                             2x                             1x               1x           8x 2x     6x                
// @flow
 
import logger from '../../logger';
import callGleipnir, {
  type GleipnirConf as SubscriptionConf,
} from '../../service/gleipnir';
import { gqConsoleSubscription } from './zuora';
 
export type Caller = (
  method: string,
  command: string,
  data?: any,
) => Promise<any>;
 
/**
 * A description of your subscription.
 */
export type Subscription = {
  id: number,
  customerId: string,
  productCode: string,
  active: boolean,
  supportSubscriptionId: string,
};
 
const UpdateAction = {
  SUBSCRIBE: 0,
  UNSUBSCRIBE: 1,
};
 
export type SubscriptionLinkValues = {
  subscriptionNumber: string,
  email: string,
  expiryTime: string,
  accountNumber: string,
  productCode: string,
};
 
export interface SubscriptionLinkAPI {
  validate(link: string): Promise<SubscriptionLinkValues>;
  create(values: SubscriptionLinkValues): Promise<string>;
}
 
/**
 * Parameters for creating a new subscription.
 */
export type SubscriptionCreateValues = {
  customerId: string,
  productCode: string,
};
 
export type SubscriptionUpdateValues = {
  subscriptionNumber: string,
  productCode: string,
  action: number,
};
 
export type UpdateResultValue = {
  result: string,
};
 
export type SubscriptionValues = {
  subscriptionNumber: string,
  productCode: string,
};
 
/**
 * The Subscription API
 */
export interface SubscriptionAPI {
  get(): Promise<Subscription>;
  create(values: SubscriptionCreateValues): Promise<Subscription>;
  link: SubscriptionLinkAPI;
  subscribe(values: SubscriptionValues): Promise<UpdateResultValue>;
  unsubscribe(values: SubscriptionValues): Promise<UpdateResultValue>;
}
 
function into(sub: any): Subscription {
  return {
    id: sub.id,
    customerId: sub.customerId,
    productCode: sub.productCode,
    active: sub.isActive,
    supportSubscriptionId: sub.supportSubscriptionId,
  };
}
 
function intoGetValues(response: any): SubscriptionLinkValues {
  return {
    subscriptionNumber: response.subscriptionNumber,
    email: response.email,
    expiryTime: response.expiryTime,
    accountNumber: response.accountNumber,
    productCode: response.productCode,
  };
}
 
export function subscriptionLink(call: Caller): SubscriptionLinkAPI {
  async function validate(link) {
    const response = await call('GET', `subscription-link/${link}`);
 
    return intoGetValues(response);
  }
 
  async function create(data) {
    const { link } = await call('POST', `subscription-link`, {
      data,
    });
 
    return link;
  }
 
  return {
    validate,
    create,
  };
}
 
export default function subscription(conf: SubscriptionConf): SubscriptionAPI {
  logger.silly(`Subscription conf=${JSON.stringify(conf)}`);
 
  function call(method, command, data) {
    return callGleipnir(conf, method, command, data);
  }
 
  async function get() {
    const {
      objects: [sub],
    } = await call('GET', 'subscriptions');
 
    return into(sub);
  }
 
  async function create({ customerId, productCode }) {
    await call('POST', `subscriptions`, {
      data: {
        customer_id: customerId,
        product_code: productCode,
      },
    });
 
    return get();
  }
 
  async function update({
    subscriptionNumber,
    productCode,
    action,
  }: SubscriptionUpdateValues) {
    const response = await call(
      'PUT',
      `subscriptions/${subscriptionNumber}/${productCode}`,
      {
        data: {
          customer_id: subscriptionNumber,
          product_code: productCode,
          action,
        },
      },
    );
    return { result: response.result };
  }
 
  async function subscribe({ subscriptionNumber, productCode }) {
    return update({
      subscriptionNumber,
      productCode,
      action: UpdateAction.SUBSCRIBE,
    });
  }
 
  async function unsubscribe({ subscriptionNumber, productCode }) {
    return update({
      subscriptionNumber,
      productCode,
      action: UpdateAction.UNSUBSCRIBE,
    });
  }
  if (conf.legacy) {
    return gqConsoleSubscription(conf);
  }
 
  return {
    get,
    create,
    link: subscriptionLink(call),
    subscribe,
    unsubscribe,
  };
}