import { isBefore, parseISO } from 'date-fns'; import { SubscriptionDetails } from '../types/subscription'; /** Class representing a subscription. */ export class Subscription { public source = ''; public id = ''; public number = ''; public accountId = ''; public accountNumber = ''; public status = ''; public effectiveStartDate = ''; public effectiveEndDate = ''; public renewalDate = ''; public trial = false; public ratePlans: [SubscriptionRatePlan?] = []; public displayName = ''; public futureRatePlan?: SubscriptionFutureRatePlan; public subscriptionTerm?: SubscriptionTerm; public isSingleTerm = false; public isOutOfTerm = false; public offerId = ''; public offer: OfferData = {}; public stepUp: StepUpData = {}; constructor(data?: Partial) { if (data) { Object.assign(this, data); } } } /** Class representing the Offer data from subs graphql */ export class OfferData { public offerId? = ''; public discount? = null; constructor(data?: Partial) { if (data) { Object.assign(this, data); } } } /** Class representing the StepUp data from subs graphql */ export class StepUpData { public isInStepUp? = false; public priceBeforeStepUp? = null; public priceAfterStepUp? = null; constructor(data?: Partial) { if (data) { Object.assign(this, data); } } } /** Class representing an event in a user's subscription history. */ export class SubscriptionHistoryModel { public eventEffectiveDate = ''; public eventDate = ''; public eventType = ''; public subscriptionBefore?: SubscriptionDetails; public subscriptionAfter?: SubscriptionDetails; constructor(data?: Partial) { if (data) { Object.assign(this, data); } } get isFutureStepUp() { // If the type is stepup and today is still before the effective date the user is in step up. return this.eventType.toLowerCase() === 'stepup' && isBefore(new Date(), parseISO(this.eventEffectiveDate)); } get isPriceFreeze() { return this.eventType.toLowerCase() === 'undostepup'; } get isFutureTransition() { return this.eventType.toLowerCase() === 'transition' && isBefore(new Date(), parseISO(this.eventEffectiveDate)); } get isTransition() { return this.eventType.toLowerCase() === 'transition'; } } /** Class representing a rate plan */ export class SubscriptionRatePlan { public id = ''; public productName = ''; public productCode = ''; public term = ''; public option = ''; public termStartDate = ''; public ratePlanCharges: [SubscriptionRatePlanCharge?] = []; constructor(data?: Partial) { if (data) { Object.assign(this, data); } } public getTermName(): string { switch (this.term) { case 'P1M': return 'Monthly'; case 'P3M': return 'Quarterly'; case 'P1Y': return 'Annual'; default: return ''; } } public getTermUnit(): string { switch (this.term) { case 'P1M': return 'month'; case 'P3M': return 'quarter'; case 'P1Y': return 'year'; default: return 'term'; } } } /** Class representing a rate plan charge */ export class SubscriptionRatePlanCharge { public name = ''; public description = ''; public type = ''; public productType = ''; public productCode = ''; public effectiveStartDate = ''; public effectiveEndDate = ''; public chargedThroughDate = ''; public price = 0; public currency = 'GBP'; constructor(data?: Partial) { if (data) { Object.assign(this, data); } } } /** Class representing a rate plan */ export class SubscriptionFutureRatePlan { public id = ''; public productName = ''; public productCode = ''; public term = ''; public option = ''; public ratePlanCharge?: SubscriptionRatePlanCharge; constructor(data?: Partial) { if (data) { Object.assign(this, data); } } public getTermUnit(): string { switch (this.term) { case 'P1M': return 'month'; case 'P3M': return 'quarter'; case 'P1Y': return 'year'; default: return 'term'; } } } /** Class representing a subscription term */ export class SubscriptionTerm { public iso8601Duration = ''; public billingPeriod = ''; public termType = ''; public autoRenewTerm = false; public termPeriodType = ''; public termEndDate = ''; public termStartDate = ''; constructor(data?: Partial) { if(data){ Object.assign(this, data); } } }