Files
crosscode-platform/apps/events/frontend/src/lib/pricing.ts
T
joshuaandClaude Opus 5.5 8360a78026 Type the at-the-door page and share pricing helpers
Each door panel and modal gets typed props; unused setError/setInfo/
onCreated props are dropped. The current early-bird price helpers move
to src/lib/pricing.ts (shared with manual registration), dead fallbacks
for a legacy registration 'options' field are removed, and the refund
history now shows a refund's reason (stored in its status field) instead
of a 'reason' field that never exists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 11:10:01 +02:00

60 lines
3.2 KiB
TypeScript

import type { EarlyBirdTier, EventOption, OptionVariant, RegistrationOption } from "@/types";
// Effective unit price for a registration option. Uses priceSnapshot when available
// (authoritative backend price, variant-aware). Falls back to a deadline-only early-bird
// calculation for legacy rows without a snapshot: the cheapest tier still open both at
// `referenceTime` (e.g. the last payment) and at `atTime`.
export function registrationOptionUnitPrice(opt: RegistrationOption, referenceTime: string | Date | null, atTime: Date): number {
if (opt.priceSnapshot !== null && opt.priceSnapshot !== undefined) {
return Number(opt.priceSnapshot);
}
const eo = opt.eventOption;
const variantId: string | null = opt.variantId || null;
// Use variant price as base if available, otherwise option base
const base = (opt.variant?.price !== null && opt.variant?.price !== undefined)
? Number(opt.variant.price)
: Number(eo?.price || 0);
const allTiers = Array.isArray(eo?.earlyBirdTiers) ? eo.earlyBirdTiers.slice() : [];
// Filter: variant options use only their variant-specific tiers; plain options use option-level tiers
const tiers = variantId
? allTiers.filter((t) => t.variantId === variantId)
: allTiers.filter((t) => !t.variantId);
if (tiers.length === 0) return base;
const t = atTime ? new Date(atTime) : new Date();
const ref = referenceTime ? new Date(referenceTime) : t;
const applicable = tiers
.map((x) => ({ ...x, deadlineAt: new Date(x.deadline) }))
.filter((x) => (ref < x.deadlineAt) && (t < x.deadlineAt))
.sort((a, b) => a.deadlineAt.getTime() - b.deadlineAt.getTime() || (a.order || 0) - (b.order || 0) || a.price - b.price);
if (applicable.length === 0) return base;
const price = Number(applicable[0].price);
return (price >= 0) ? price : base;
}
// The earliest-deadline early-bird tier that's still open, or undefined when none are.
function currentTier(tiers: EarlyBirdTier[]): EarlyBirdTier | undefined {
const now = new Date();
return tiers
.map((t) => ({ ...t, deadlineAt: new Date(t.deadline) }))
.filter((t) => now < t.deadlineAt)
.sort((a, b) => a.deadlineAt.getTime() - b.deadlineAt.getTime() || (a.order || 0) - (b.order || 0) || a.price - b.price)[0];
}
// What an event option costs right now: its current early-bird price, else its base price.
export function optionUnitNow(opt: EventOption): number {
const base = opt.price || 0;
const tiers = (Array.isArray(opt.earlyBirdTiers) ? opt.earlyBirdTiers : []).filter((t) => !t.variantId);
if (tiers.length === 0) return base;
return currentTier(tiers)?.price ?? base;
}
// Same for a variant: its own tiers if it has any, otherwise the option's.
export function variantUnitNow(opt: EventOption, variant: OptionVariant): number {
const base = variant.price !== null && variant.price !== undefined ? variant.price : opt.price || 0;
const allTiers = Array.isArray(opt.earlyBirdTiers) ? opt.earlyBirdTiers : [];
const variantTiers = allTiers.filter((t) => t.variantId === variant.id);
const tiers = variantTiers.length > 0 ? variantTiers : allTiers.filter((t) => !t.variantId);
if (tiers.length === 0) return base;
return currentTier(tiers)?.price ?? base;
}