File size: 1,827 Bytes
1e92f2d | 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 | import type { PlanIntroductoryOffer, PricedAPIPlan, PricedAPISitePlan } from '../../types';
/**
* Unpacks an intro offer from an API plan to the respective `PlanIntroductoryOffer` structure for the store.
*/
const unpackIntroOffer = (
plan: PricedAPISitePlan | PricedAPIPlan
): PlanIntroductoryOffer | null => {
// these aren't grouped or separated. so no introductory offer if missing or malformed raw_price
if (
typeof plan.introductory_offer_raw_price_integer !== 'number' ||
! plan.introductory_offer_interval_count
) {
return null;
}
/**
* An offer is complete if:
* 1. an expiry date is set on the plan that exceeds the offer end date, OR
* 2. the current Date exceeds the offer end date
*/
const isOfferComplete =
'expiry' in plan
? Boolean(
plan.expiry &&
plan.introductory_offer_end_date &&
new Date( plan.expiry ) > new Date( plan.introductory_offer_end_date )
)
: Boolean(
plan.introductory_offer_end_date &&
new Date() > new Date( plan.introductory_offer_end_date )
);
return {
formattedPrice: plan.introductory_offer_formatted_price as string,
rawPrice: {
monthly:
/**
* IMPORTANT:
* we make the raw assumption that the interval unit is either "year" or "month"
* to compute monthly/full price
*/
'year' === plan.introductory_offer_interval_unit
? parseFloat(
(
plan.introductory_offer_raw_price_integer /
( plan.introductory_offer_interval_count * 12 )
).toFixed( 2 )
)
: plan.introductory_offer_raw_price_integer,
full: plan.introductory_offer_raw_price_integer,
},
intervalUnit: plan.introductory_offer_interval_unit as string,
intervalCount: plan.introductory_offer_interval_count,
isOfferComplete,
};
};
export default unpackIntroOffer;
|