import { SubscriptionPlanDto, SubscriptionTrialDto, SubscriptionTrialValidityDto, Days, defaultPackageUntilWebhookReceived, EpochMillis, Milliseconds } from "@upload-io/upload-shared-iso-lite"; export interface SubscriptionPlanAndTrialsDto { // The tier the user has actually paid for, or the free plan (if we decide to do one): if there are no active // trials, this tier will take effect. Undefined if we don't offer a free plan and the user hasn't paid for a // tier yet, at which point if no active trials exist, the user gets a 'trial expired' error. base: undefined | SubscriptionPlanDto; // Array allows us to drop the user down to a lesser trial after the current trial finishes. trials: Array>; } export namespace SubscriptionPlanAndTrialsDto { export const defaultPlan = (now: EpochMillis): SubscriptionPlanAndTrialsDto => startTrials(defaultPackageUntilWebhookReceived.plans, now); export const noAccess: SubscriptionPlanAndTrialsDto = { base: undefined, trials: [] }; export function startTrials( plans: SubscriptionPlanAndTrialsDto, now: EpochMillis ): SubscriptionPlanAndTrialsDto { function daysToMilliseconds(days: Days): Milliseconds { return Milliseconds(days * 24 * 60 * 60 * 1000); } return { ...plans, trials: plans.trials.map( (x): SubscriptionTrialDto => ({ ...x, validity: { start: EpochMillis(now.valueOf()), end: EpochMillis(now.valueOf() + daysToMilliseconds(x.validity).valueOf()) } }) ) }; } }