Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/view-models/src/billing/subscription-overview.vm.ts
T

192 lines
7.1 KiB
TypeScript

import type { VMState } from "../index.js";
import { buildSubscriptionBannerVM, type SubscriptionBannerVM, type SubscriptionBannerStatus } from "./subscription-banner.vm.js";
import type { SeatUsageVM } from "./seat-usage.vm.js";
export type SubscriptionOverviewPricePeriod = "once" | "daily" | "weekly" | "monthly" | "yearly" | "quarterly" | "semiAnnual" | "unknown";
export type SubscriptionOverviewProductState = "active" | "cancelled" | "ended" | "scheduled";
export type SubscriptionOverviewPriceInput = {
readonly amount: number | null;
readonly currency: string | null;
readonly billingPeriod: number | null;
readonly quantity?: number | null;
};
export type SubscriptionOverviewProductInput = {
readonly id: string;
readonly titleKey: string;
readonly code?: string | null;
readonly kind?: "base_plan" | "recurring_add_on" | "one_time_setup" | "contract_override" | null;
readonly acquisition?: {
readonly productKind: "base_plan" | "recurring_add_on" | "one_time_setup" | "contract_override";
readonly status: string;
readonly lagoSubscriptionId: string | null;
} | null;
readonly quantity?: number | null;
readonly cancelledAt?: string | null;
readonly endsAt?: string | null;
readonly currentPeriodStart?: string | null;
readonly currentPeriodEnd?: string | null;
readonly prices: readonly SubscriptionOverviewPriceInput[];
};
export type SubscriptionOverviewEntitlementInput = {
readonly code: string;
readonly titleKey: string;
readonly enabled: boolean;
readonly value: number;
readonly used: number;
readonly remaining: number | "unlimited";
};
export type SubscriptionOverviewPriceVM = {
readonly amount: number | null;
readonly currency: string | null;
readonly period: SubscriptionOverviewPricePeriod;
readonly periodLabelKey: string;
readonly quantity: number;
};
export type SubscriptionOverviewProductVM = {
readonly id: string;
readonly titleKey: string;
readonly code: string | null;
readonly quantity: number;
readonly state: SubscriptionOverviewProductState;
readonly stateLabelKey: string;
readonly currentPeriodEnd: string | null;
readonly canCancel: boolean;
readonly prices: readonly SubscriptionOverviewPriceVM[];
};
export type SubscriptionOverviewEntitlementVM = SubscriptionOverviewEntitlementInput & {
readonly labelKey: string;
};
export type SubscriptionOverviewData = {
readonly status: SubscriptionBannerStatus;
readonly statusLabelKey: string;
readonly trialEndsAt: string | null;
readonly trialDaysRemaining: number | null;
readonly planCode: string | null;
readonly planLabel: string | null;
readonly banner: SubscriptionBannerVM;
readonly seatUsage: SeatUsageVM;
readonly products: readonly SubscriptionOverviewProductVM[];
readonly entitlements: readonly SubscriptionOverviewEntitlementVM[];
readonly contactHref: string;
readonly emptyTitleKey: string;
readonly emptyDescriptionKey: string;
};
export type SubscriptionOverviewVM = VMState<SubscriptionOverviewData>;
export type BuildSubscriptionOverviewVMInput = {
readonly hasSubscription: boolean;
readonly status: string | null | undefined;
readonly trialEndsAt?: string | null;
readonly planCode?: string | null;
readonly seatUsage: SeatUsageVM;
readonly products: readonly SubscriptionOverviewProductInput[];
readonly entitlements: readonly SubscriptionOverviewEntitlementInput[];
readonly canCancel: boolean;
readonly contactHref: string;
readonly now?: string;
};
const PERIODS: Readonly<Record<number, SubscriptionOverviewPricePeriod>> = {
0: "once",
1: "daily",
2: "weekly",
3: "monthly",
4: "yearly",
5: "quarterly",
6: "semiAnnual",
};
function normalizeStatus(status: string | null | undefined): SubscriptionBannerStatus {
if (status === "active" || status === "past_due" || status === "cancelled" || status === "frozen") return status;
return "trial";
}
function productState(product: SubscriptionOverviewProductInput, now: number): SubscriptionOverviewProductState {
if (product.endsAt && Date.parse(product.endsAt) <= now) return "ended";
if (product.cancelledAt) return "cancelled";
if (product.currentPeriodStart && Date.parse(product.currentPeriodStart) > now) return "scheduled";
return "active";
}
function priceVM(price: SubscriptionOverviewPriceInput): SubscriptionOverviewPriceVM {
const period = price.billingPeriod === null ? "unknown" : (PERIODS[price.billingPeriod] ?? "unknown");
return {
amount: price.amount,
currency: price.currency?.toUpperCase() ?? null,
period,
periodLabelKey: `billing:subscriptionOverview.period.${period}`,
quantity: Math.max(1, price.quantity ?? 1),
};
}
function remainingTrialDays(trialEndsAt: string | null | undefined, now: number): number | null {
if (!trialEndsAt) return null;
const end = Date.parse(trialEndsAt);
if (!Number.isFinite(end)) return null;
return Math.max(0, Math.ceil((end - now) / (24 * 60 * 60 * 1000)));
}
function isCancellableRecurringAddOn(product: SubscriptionOverviewProductInput, state: SubscriptionOverviewProductState): boolean {
const acquisition = product.acquisition;
return (
state === "active" &&
product.kind === "recurring_add_on" &&
acquisition?.productKind === "recurring_add_on" &&
acquisition.status === "active" &&
Boolean(acquisition.lagoSubscriptionId?.trim())
);
}
export function buildSubscriptionOverviewVM(input: BuildSubscriptionOverviewVMInput): SubscriptionOverviewVM {
if (!input.hasSubscription) return { status: "empty" };
const status = normalizeStatus(input.status);
const now = Date.parse(input.now ?? new Date().toISOString());
const products = input.products.map((product): SubscriptionOverviewProductVM => {
const state = productState(product, now);
return {
id: product.id,
titleKey: product.titleKey,
code: product.code?.trim() || null,
quantity: Math.max(1, product.quantity ?? 1),
state,
stateLabelKey: `billing:subscriptionOverview.productState.${state}`,
currentPeriodEnd: product.currentPeriodEnd ?? product.endsAt ?? null,
canCancel: input.canCancel && isCancellableRecurringAddOn(product, state),
prices: product.prices.map(priceVM),
};
});
const planCode = input.planCode?.trim() || null;
const trialEndsAt = input.trialEndsAt ?? null;
const trialDaysRemaining = status === "trial" ? remainingTrialDays(trialEndsAt, now) : null;
return {
status: "ready",
data: {
status,
statusLabelKey: `billing:subscriptionOverview.status.${status}`,
trialEndsAt,
trialDaysRemaining,
planCode,
planLabel: products.find((product) => product.state !== "ended")?.titleKey ?? planCode,
banner: buildSubscriptionBannerVM({ status, hasSubscription: true, trialEndsAt, now: input.now }),
seatUsage: input.seatUsage,
products,
entitlements: [...input.entitlements]
.sort((left, right) => left.titleKey.localeCompare(right.titleKey))
.map((entitlement) => ({ ...entitlement, labelKey: entitlement.titleKey })),
contactHref: input.contactHref,
emptyTitleKey: "billing:subscriptionOverview.empty.title",
emptyDescriptionKey: "billing:subscriptionOverview.empty.description",
},
};
}