All files / src/api/subscription zuora.js

81.82% Statements 18/22
100% Branches 0/0
91.67% Functions 11/12
81.82% Lines 18/22

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    2x             2x 2x     2x                                         1x                     1x   1x       1x       1x     6x             5x     5x           1x           1x                   2x                             1x               1x             5x                
// @flow
 
import {
  type Caller,
  SubscriptionAPI,
  SubscriptionLinkAPI,
  type SubscriptionLinkValues,
  type Subscription,
} from '.';
import callGQConsole from '../../service/gq-console';
import logger from '../../logger';
import { type GleipnirConf as SubscriptionConf } from '../../service/gleipnir';
 
const UpdateAction = {
  Cancelled: 'Cancelled',
  Active: 'Active',
  Expired: 'Expired',
};
 
/**
 * CRUD for zuora's subscription Link
 */
 
function legacyInto(sub: any): Subscription {
  return {
    id: sub.id,
    customerId: sub.customer_id,
    productCode: sub.product_code,
    active: sub.is_active,
    supportSubscriptionId: sub.support_subscription_id,
  };
}
 
function intoGetValues(response: any): SubscriptionLinkValues {
  return {
    subscriptionNumber: response.subscriptionNumber,
    email: response.email,
    expiryTime: response.expiryTime,
    accountNumber: response.accountNumber,
    productCode: response.productCode,
  };
}
 
export function gqConsoleZuoraLink(call: Caller): SubscriptionLinkAPI {
  async function validate(link) {
    const response = await call('GET', `zuora-subscription-link/${link}`);
 
    return intoGetValues(response);
  }
 
  async function create(data) {
    const { subscriptionLink } = await call('POST', `zuora-subscription-link`, {
      data,
    });
 
    return subscriptionLink;
  }
 
  return {
    validate,
    create,
  };
}
 
export function gqConsoleSubscription(conf: SubscriptionConf): SubscriptionAPI {
  logger.silly(`Legacy subscription conf=${JSON.stringify(conf)}`);
 
  function call(method, command, data) {
    return callGQConsole(conf, method, command, data);
  }
 
  async function get() {
    const {
      objects: [sub],
    } = await call('GET', 'aws-marketplace');
 
    return legacyInto(sub);
  }
 
  async function create({ customerId, productCode }) {
    await call('POST', `aws-marketplace`, {
      data: {
        customer_id: customerId,
        product_code: productCode,
      },
    });
 
    return get();
  }
  async function update({ subscriptionNumber, productCode, action }) {
    const response = await call(
      'PUT',
      `aws-marketplace/update-zuora-subscription`,
      {
        data: {
          customer_id: subscriptionNumber,
          product_code: productCode,
          action,
        },
      },
    );
    return { result: response.result };
  }
 
  async function subscribe({ subscriptionNumber, productCode }) {
    return update({
      subscriptionNumber,
      productCode,
      action: UpdateAction.Active,
    });
  }
 
  async function unsubscribe({ subscriptionNumber, productCode }) {
    return update({
      subscriptionNumber,
      productCode,
      action: UpdateAction.Cancelled,
    });
  }
 
  return {
    get,
    create,
    link: gqConsoleZuoraLink(call),
    subscribe,
    unsubscribe,
  };
}