const _get = require('lodash.get'); import { SubscriptionHistoryItem } from '../types/subscription'; import { Subscription, SubscriptionHistoryModel, SubscriptionRatePlan, SubscriptionRatePlanCharge, SubscriptionFutureRatePlan, SubscriptionTerm, } from '../models/subscription'; import { getOfferDisplayName, offersMap } from '../helpers/offer'; /** * Map the subscription data from membership * @param data * @param overrides */ export function mapSubscription(data: any, overrides: offersMap = {}) { const subscription = new Subscription({ source: data.source, id: data.id, number: data.number, accountId: data.accountId, accountNumber: data.accountNumber, status: data.status, effectiveStartDate: data.effectiveStartDate, effectiveEndDate: data.effectiveEndDate, renewalDate: data.renewalDate, trial: data.trial, displayName: getOfferDisplayName(data.offerId, overrides), isSingleTerm: data.isSingleTerm, isOutOfTerm: data.isOutOfTerm, offerId: data.offerId, offer: data.offer, stepUp: data.stepUp }); if (Array.isArray(data.ratePlans)) { subscription.ratePlans = data.ratePlans.map(mapSubscriptionRatePlan); } if (data.futureRatePlan) { subscription.futureRatePlan = mapSubscriptionFutureRatePlan(data.futureRatePlan); } if(data.subscriptionTerm) { subscription.subscriptionTerm = mapSubscriptionTerm(data.subscriptionTerm); } return subscription; } /** * Map the subscription history data from membership * @param data */ export function mapSubscriptionHistory(data: Array) { const history = data .filter(event => event.event_outcome !== 'failure') .map((item) => { return new SubscriptionHistoryModel({ eventEffectiveDate: item.event_effective_date, eventDate: item.event_date, eventType: item.event_type, subscriptionBefore: _get(item, 'event_data.message.subscriptionBefore'), subscriptionAfter: _get(item, 'event_data.message.subscriptionAfter') }); }); //HINT: The history is returned by membership API and it should be sorted from most recent to to oldest. const freezeIndex = history.findIndex(item => item.isPriceFreeze); const stepUpIndex = history.findIndex(item => item.isFutureStepUp); const transitionIndex = history.findIndex(item => item.isTransition); const orderingArray = [ { originalIndex: freezeIndex, event: 'freeze' }, { originalIndex: stepUpIndex, event: 'stepUp' }, { originalIndex: transitionIndex, event: 'transition' }, ]; // filter out events that don't exist, then order by originalIndex const filteredArray = orderingArray.filter(event => event.originalIndex !== -1) .sort((a, b) => a.originalIndex - b.originalIndex); const hasFrozenPrice = freezeIndex >= 0 && filteredArray[0].event === 'freeze' && freezeIndex < stepUpIndex; //there must have been a step up first const isInStepUp = stepUpIndex >=0 && filteredArray[0].event === 'stepUp'; return { isInStepUp, hasFrozenPrice, history, stepUpEvent: history[stepUpIndex], freezeEvent: history[freezeIndex] }; } /** * Map the ratePlan data from membership * @param data */ export function mapSubscriptionRatePlan(data: any) { const ratePlan = new SubscriptionRatePlan({ id: data.id, productName: data.productName, productCode: data.productCode, term: data.term, option: data.option, termStartDate: data.termStartDate }); if (Array.isArray(data.ratePlanCharges)) { ratePlan.ratePlanCharges = data.ratePlanCharges.map(mapSubscriptionRatePlanCharge); } return ratePlan; } /** * Map the ratePlanCharge data from membership * @param data */ export function mapSubscriptionRatePlanCharge(data: any) { return new SubscriptionRatePlanCharge({ name: data.name, description: data.description, type: data.type, productType: data.productType, productCode: data.productCode, effectiveStartDate: data.effectiveStartDate, effectiveEndDate: data.effectiveEndDate, chargedThroughDate: data.chargedThroughDate, price: data.price, currency: data.currency }); } /** * Map the ratePlan data from membership * @param data */ export function mapSubscriptionFutureRatePlan(data: any) { const ratePlan = new SubscriptionFutureRatePlan({ id: data.id, productName: data.productName, productCode: data.productCode, term: data.term, option: data.option }); if (Array.isArray(data.ratePlanCharges)) { ratePlan.ratePlanCharge = mapSubscriptionRatePlanCharge(data.ratePlanCharges[0]); } return ratePlan; } /** * Map the subscription term data from membership * @param data */ export function mapSubscriptionTerm(data: any) { if(data instanceof Object){ const subscriptionTerm = new SubscriptionTerm({ iso8601Duration: data.iso8601Duration, billingPeriod: data.billingPeriod, termType: data.termType, autoRenewTerm: data.autoRenewTerm, termPeriodType: data.termPeriodType, termEndDate: data.termEndDate, termStartDate: data.termStartDate, }); return subscriptionTerm; } else { const subscriptionTerm = new SubscriptionTerm({ iso8601Duration: data, termType: 'Evergreen', autoRenewTerm: true }); return subscriptionTerm; } }