Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/services/billing/registration-plan-selection.server.ts
T

90 lines
3.8 KiB
TypeScript

import { loadSelfServiceSubscriptionCatalog } from "./self-service-subscription.server";
import { adviseRegistrationPlan, getBillingCatalogPlan } from "@hivegrid/entitlements";
export type RegistrationPlanOption = {
readonly planId: string;
readonly planCode: string;
readonly titleKey: string;
readonly descriptionKey: string;
readonly priceId: string;
readonly amountCents: number;
readonly baseAmountCents: number;
readonly currency: string;
readonly billingPeriod: number;
readonly includedOfficeSeatQuantity: number;
readonly includedFieldSeatQuantity: number;
readonly includedStorageGb: number;
readonly storagePerAdditionalSeatGb: number;
readonly extraOfficeSeatQuantity: number;
readonly extraFieldSeatQuantity: number;
readonly recommended: boolean;
};
export type RegistrationPlanSelection = {
readonly plans: readonly RegistrationPlanOption[];
readonly selectedPriceId: string;
readonly recommendedPriceId: string;
readonly officeSeatQuantity: number;
readonly fieldSeatQuantity: number;
};
export async function loadRegistrationPlanSelection(
preferredPlanOrPrice?: string | null,
seatQuantities: { readonly officeSeatQuantity: number; readonly fieldSeatQuantity: number } = {
officeSeatQuantity: 1,
fieldSeatQuantity: 0,
}
): Promise<RegistrationPlanSelection> {
const catalog = await loadSelfServiceSubscriptionCatalog();
const advice = adviseRegistrationPlan(seatQuantities);
const plans = catalog.plans.flatMap((plan) => {
const price = plan.prices.find((candidate) => candidate.billingPeriod === 3);
const canonicalPlan = getBillingCatalogPlan(plan.code);
const advisedOption = advice.options.find((candidate) => candidate.planCode === plan.code);
if (!canonicalPlan || !advisedOption) return [];
return price
? [
{
planId: plan.id,
planCode: plan.code,
titleKey: plan.titleKey,
descriptionKey: plan.descriptionKey,
priceId: price.id,
amountCents: advisedOption.breakdown.recurringNetAmountCents,
baseAmountCents: price.unitAmountCents,
currency: price.currency,
billingPeriod: price.billingPeriod,
includedOfficeSeatQuantity: canonicalPlan.includedSeats.office,
includedFieldSeatQuantity: canonicalPlan.includedSeats.field,
includedStorageGb: canonicalPlan.includedStorageGb,
storagePerAdditionalSeatGb: canonicalPlan.storagePerAdditionalSeatGb,
extraOfficeSeatQuantity: advisedOption.breakdown.extraOfficeSeatQuantity,
extraFieldSeatQuantity: advisedOption.breakdown.extraFieldSeatQuantity,
recommended: advisedOption.recommended,
} satisfies RegistrationPlanOption,
]
: [];
});
if (plans.length === 0) {
throw new Error("No canonical registration plan is available");
}
const recommended = plans.find((plan) => plan.planCode === advice.recommendedPlanCode);
if (!recommended) throw new Error("Recommended registration plan is unavailable");
const preferred = preferredPlanOrPrice?.trim() ?? "";
const selected = plans.find((plan) => plan.priceId === preferred || plan.planId === preferred || plan.planCode === preferred) ?? recommended;
return {
plans,
selectedPriceId: selected.priceId,
recommendedPriceId: recommended.priceId,
officeSeatQuantity: advice.officeSeatQuantity,
fieldSeatQuantity: advice.fieldSeatQuantity,
};
}
export async function resolveRegistrationSubscriptionPrice(selectedPriceId: string | null | undefined): Promise<RegistrationPlanOption | null> {
const normalized = selectedPriceId?.trim() ?? "";
if (!normalized) return null;
const selection = await loadRegistrationPlanSelection();
return selection.plans.find((plan) => plan.priceId === normalized) ?? null;
}