/** * MITHOME-130: hosting plans — lifecycle gate, price and view model. * * The product structure follows the central catalogue contract (mitplatform * PLATFM-14, docs/architecture/product-catalog-contract.md). The Payload * `products` collection is an interim mirror of it; reading goes through * src/lib/payload-content.ts so the source can be swapped in one place. * * Pure functions only — no Payload import, unit-testable. */ import { contentLocale } from './translation-gate' import type { Locale } from './i18n' export const PRODUCT_STATUSES = ['draft', 'internal', 'publishable', 'active', 'retired'] as const export type ProductStatus = (typeof PRODUCT_STATUSES)[number] export const PRODUCT_STATUS_LABELS: Record = { draft: 'Tervezet', internal: 'Belső', publishable: 'Publikálható', active: 'Aktív (éles)', retired: 'Kivezetett', } export const PRODUCT_TYPES = ['hosting', 'managed-service'] as const export type ProductType = (typeof PRODUCT_TYPES)[number] export const PRODUCT_TYPE_LABELS: Record = { hosting: 'Tárhely', 'managed-service': 'Menedzselt szolgáltatás', } export const BILLING_PERIODS = ['yearly', 'monthly', 'one-time'] as const export type BillingPeriod = (typeof BILLING_PERIODS)[number] export const BILLING_PERIOD_LABELS: Record = { yearly: 'Éves', monthly: 'Havi', 'one-time': 'Egyszeri', } export const DEFAULT_VAT_RATE_PERCENT = 27 /** * DECISION (user): production shows only active products; staging shows * everything except retired ones so drafts can be reviewed there. */ export function showableStatuses(deployEnv: string | undefined = process.env.NEXT_PUBLIC_DEPLOY_ENV): ProductStatus[] { return deployEnv === 'staging' ? PRODUCT_STATUSES.filter((s) => s !== 'retired') : ['active'] } /** Gross price from the stored net amount (whole forints). */ export function grossAmount(netAmount: number, vatRatePercent: number = DEFAULT_VAT_RATE_PERCENT): number { return Math.round(netAmount * (1 + vatRatePercent / 100)) } /** "14 900 Ft" (hu) / "14,900 Ft" (en). */ export function formatHuf(amount: number, locale: Locale): string { const number = new Intl.NumberFormat(locale === 'hu' ? 'hu-HU' : 'en-GB', { maximumFractionDigits: 0 }).format(amount) return `${number} Ft` } type Localized = T | null | undefined export type ProductDoc = { id: string | number slug: string name: string productType?: string | null status?: string | null sortOrder?: number | null highlighted?: boolean | null enTranslationStatus?: string | null public?: { audience?: Localized shortDescription?: Localized features?: { value: string }[] | null ctaText?: Localized } | null pricing?: { netAmount?: number | null; billingPeriod?: string | null } | null serviceDefinition?: { storageGb?: number | null mailStorageGb?: number | null domains?: number | null domainAliases?: number | null databases?: number | null mailboxes?: number | null backup?: Localized ssl?: boolean | null spamFilter?: boolean | null } | null } export type HostingPlanView = { id: string slug: string name: string highlighted: boolean audience: string shortDescription: string features: string[] ctaText: string netAmount: number billingPeriod: BillingPeriod service: { storageGb?: number mailStorageGb?: number domains?: number domainAliases?: number databases?: number mailboxes?: number backup?: string ssl: boolean spamFilter: boolean } } const orUndefined = (value: number | null | undefined) => (value ?? undefined) function toView(doc: ProductDoc): HostingPlanView { const s = doc.serviceDefinition ?? {} const period = doc.pricing?.billingPeriod return { id: String(doc.id), slug: doc.slug, name: doc.name, highlighted: doc.highlighted === true, audience: doc.public?.audience ?? '', shortDescription: doc.public?.shortDescription ?? '', features: (doc.public?.features ?? []).map((f) => f.value), ctaText: doc.public?.ctaText ?? '', netAmount: doc.pricing?.netAmount ?? 0, billingPeriod: (BILLING_PERIODS as readonly string[]).includes(period ?? '') ? (period as BillingPeriod) : 'yearly', service: { storageGb: orUndefined(s.storageGb), mailStorageGb: orUndefined(s.mailStorageGb), domains: orUndefined(s.domains), domainAliases: orUndefined(s.domainAliases), databases: orUndefined(s.databases), mailboxes: orUndefined(s.mailboxes), backup: s.backup ?? undefined, ssl: s.ssl === true, spamFilter: s.spamFilter === true, }, } } /** * Hosting products for `locale`, gated and ordered. The translation gate * works per product: `requested` holds the docs read in the requested * locale, `fallback` the same docs in the default locale (only needed for * English). */ export function hostingPlanViews( locale: Locale, requested: readonly ProductDoc[], fallback: readonly ProductDoc[] = [], statuses: readonly string[] = showableStatuses(), ): HostingPlanView[] { const fallbackById = new Map(fallback.map((doc) => [String(doc.id), doc])) return requested .filter((doc) => doc.productType === 'hosting' && statuses.includes(doc.status ?? '')) .filter((doc) => typeof doc.pricing?.netAmount === 'number') .map((doc) => (contentLocale(locale, doc.enTranslationStatus) === locale ? doc : fallbackById.get(String(doc.id)) ?? doc)) .sort((a, b) => (a.sortOrder ?? 0) - (b.sortOrder ?? 0)) .map(toView) }