Files
AndreaSlopFotografie/web/config/offerings.ts
T
SluijsensandClaude Sonnet 5.5 f9180f231f Lets the price banner work with empty fields
Only the banner title is required; description, price, price note, features
and the button label are left out when empty (the button then says Neem contact
op).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-12 09:42:35 +01:00

130 lines
4.2 KiB
TypeScript

// Turns the answer of GET /api/v1/offerings (the CMS) into what the site
// shows: the shoot packages (cards) and at most one banner. Pure functions, so
// they are tested without a network. The build step (fetchOfferings.ts) uses
// them; the site itself never calls the API (prices are in the HTML).
//
// Rule of the design: a bad answer never breaks the build and never leads to
// an empty or wrong price. Anything that is not clearly valid is dropped, and
// when no valid card is left the whole answer is rejected (null), so the
// caller keeps the last known list (the snapshot).
export interface OfferingPackage {
id: string;
name: string;
price: number; // whole euros
features: string[];
priceNote?: string;
description?: string;
ctaLabel?: string;
featured?: boolean;
}
// Only the title is required: the banner hides every field that is empty
export interface OfferingBanner {
title: string;
description?: string;
priceText?: string;
priceNote?: string;
features: string[];
ctaLabel?: string;
}
export interface SiteOfferings {
packages: OfferingPackage[];
banner: OfferingBanner | null;
}
const SLUG = /^[a-z0-9-]+$/;
// "€ 175", "€ 1.250", "175,-", "€ 175,00" → whole euros. Anything else
// ("op aanvraag", "€ 12,50", "€ 0") has no usable price and gives null.
export function parsePriceText(text: unknown): number | null {
if (typeof text !== 'string') return null;
const match = /^\D*?(\d{1,3}(?:\.\d{3})+|\d+)(?:,(?:-|0{1,2}))?\D*$/.exec(
text.trim(),
);
if (!match) return null;
const euros = Number(match[1].replace(/\./g, ''));
return Number.isInteger(euros) && euros > 0 ? euros : null;
}
const asText = (value: unknown): string | undefined =>
typeof value === 'string' && value.trim() !== '' ? value.trim() : undefined;
function asFeatures(value: unknown): string[] | null {
if (!Array.isArray(value)) return null;
const features = value.map(asText);
return features.length > 0 && features.every((f) => f !== undefined)
? (features as string[])
: null;
}
interface RawOffering {
[key: string]: unknown;
}
const isObject = (value: unknown): value is RawOffering =>
typeof value === 'object' && value !== null && !Array.isArray(value);
function toPackage(raw: RawOffering): OfferingPackage | null {
const id = asText(raw.slug);
const name = asText(raw.title);
const price = parsePriceText(raw.price);
const features = asFeatures(raw.features);
if (!id || !SLUG.test(id) || !name || price === null || !features) {
return null;
}
return {
id,
name,
price,
features,
priceNote: asText(raw.priceNote),
description: asText(raw.description),
ctaLabel: asText(raw.ctaLabel),
featured: raw.featured === true ? true : undefined,
};
}
function toBanner(raw: RawOffering): OfferingBanner | null {
const title = asText(raw.title);
if (!title) return null;
return {
title,
description: asText(raw.description),
priceText: asText(raw.price),
priceNote: asText(raw.priceNote),
features: asFeatures(raw.features) ?? [],
ctaLabel: asText(raw.ctaLabel),
};
}
const orderOf = (raw: RawOffering) =>
typeof raw.displayOrder === 'number' ? raw.displayOrder : 0;
// null: the answer cannot be used (not a list, or no valid card)
export function mapOfferings(answer: unknown): SiteOfferings | null {
if (!Array.isArray(answer)) return null;
const rows = answer
.filter(isObject)
.map((raw, index) => ({ raw, index }))
.sort((a, b) => orderOf(a.raw) - orderOf(b.raw) || a.index - b.index)
.map(({ raw }) => raw);
const seen = new Set<string>();
const packages: OfferingPackage[] = [];
let banner: OfferingBanner | null = null;
for (const raw of rows) {
if (raw.layout === 'banner') {
// The CMS allows one banner per site; the first usable one wins
banner ??= toBanner(raw);
continue;
}
const pkg = toPackage(raw);
if (!pkg || seen.has(pkg.id)) continue;
seen.add(pkg.id);
packages.push(pkg);
}
return packages.length > 0 ? { packages, banner } : null;
}