- items.priority (migration + pb_schema.json): Recommended order in Shop blocks, higher first, then by name. Set under Services & products, which now lists items in that order. - Shop block: choose its order (Recommended, Most popular, price up/down, name) and optionally let visitors re-sort with a "Sort by" menu. sortItems() in shared/items.ts is the one place items are ordered. - Most popular: GET /api/shop/popular returns item ids by units sold in paid orders over the last 90 days (ids only, no sales figures). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HA82gWt7pNdAsocjxujY3m
85 lines
3.7 KiB
TypeScript
85 lines
3.7 KiB
TypeScript
// Services and their durations — pure helpers shared by the admin editor,
|
||
// the Shop block and the checkout. Which duration costs what is decided
|
||
// (and charged) by the shop hooks in PocketBase; this only reads and shows it.
|
||
|
||
import type { BundlePart, ItemDuration, ItemRecord, ShopSort } from './types';
|
||
|
||
/** A service's `durations` field (JSON, possibly hand-edited) → valid entries, shortest first, one per length. */
|
||
export function parseDurations(raw: unknown): ItemDuration[] {
|
||
const list = Array.isArray(raw) ? raw : [];
|
||
const seen = new Set<number>();
|
||
return list
|
||
.map((d) => ({ minutes: Math.floor(Number(d?.minutes)), price: Number(d?.price) }))
|
||
.filter((d) => d.minutes > 0 && Number.isFinite(d.price) && d.price >= 0 && !seen.has(d.minutes) && seen.add(d.minutes))
|
||
.sort((a, b) => a.minutes - b.minutes);
|
||
}
|
||
|
||
/** `45` → "45 min", `60` → "1 hour", `120` → "2 hours", `90` → "1 h 30 min". */
|
||
export function formatDuration(minutes: number) {
|
||
if (minutes < 60) return `${minutes} min`;
|
||
const hours = Math.floor(minutes / 60);
|
||
const rest = minutes % 60;
|
||
if (!rest) return `${hours} ${hours === 1 ? 'hour' : 'hours'}`;
|
||
return `${hours} h ${rest} min`;
|
||
}
|
||
|
||
/** The lowest price among a service's durations (what "from …" shows), or null without any. */
|
||
export function lowestPrice(durations: ItemDuration[]) {
|
||
return durations.length ? Math.min(...durations.map((d) => d.price)) : null;
|
||
}
|
||
|
||
/** A stored code `ABCD2345` → `ABCD-2345`, as shown to people (the shop hooks format it the same way). */
|
||
export function formatCode(code: string) {
|
||
return code.length === 8 ? `${code.slice(0, 4)}-${code.slice(4)}` : code;
|
||
}
|
||
|
||
/**
|
||
* What a bundle's parts cost bought one by one (each service at the part's
|
||
* length), to show next to the bundle price — or null when a part isn't on
|
||
* sale (not in `items`, or a length the service no longer has).
|
||
*/
|
||
export function bundleListPrice(parts: BundlePart[], items: ItemRecord[]) {
|
||
let total = 0;
|
||
for (const part of parts) {
|
||
const item = items.find((candidate) => candidate.id === part.item);
|
||
if (!item || item.type === 'bundle') return null;
|
||
const unit = item.type === 'item_with_time' ? item.durations.find((d) => d.minutes === part.minutes)?.price : item.price;
|
||
if (unit == null) return null;
|
||
total += unit * part.count;
|
||
}
|
||
return Math.round(total * 100) / 100;
|
||
}
|
||
|
||
/** The Shop block's sort choices, in menu order, with what visitors see. */
|
||
export const SHOP_SORTS: { value: ShopSort; label: string }[] = [
|
||
{ value: 'recommended', label: 'Recommended' },
|
||
{ value: 'popular', label: 'Most popular' },
|
||
{ value: 'price_asc', label: 'Price: low to high' },
|
||
{ value: 'price_desc', label: 'Price: high to low' },
|
||
{ value: 'name', label: 'Name (A–Z)' }
|
||
];
|
||
|
||
const byName = (a: ItemRecord, b: ItemRecord) => a.name.localeCompare(b.name, 'en-GB', { sensitivity: 'base' });
|
||
const byPriority = (a: ItemRecord, b: ItemRecord) => b.priority - a.priority || byName(a, b);
|
||
|
||
/**
|
||
* `items` in the Shop block's `sort` order (a new array). Ties — and, for
|
||
* "Most popular", items that haven't sold lately — fall back to Recommended,
|
||
* so the admin's priorities still decide whatever the numbers don't.
|
||
* Services sort by their lowest price (what "from …" shows).
|
||
*/
|
||
export function sortItems(items: ItemRecord[], sort: ShopSort = 'recommended') {
|
||
const list = [...items];
|
||
switch (sort) {
|
||
case 'popular':
|
||
return list.sort((a, b) => (a.popularRank ?? Infinity) - (b.popularRank ?? Infinity) || byPriority(a, b));
|
||
case 'price_asc':
|
||
return list.sort((a, b) => a.price - b.price || byPriority(a, b));
|
||
case 'price_desc':
|
||
return list.sort((a, b) => b.price - a.price || byPriority(a, b));
|
||
case 'name':
|
||
return list.sort(byName);
|
||
default:
|
||
return list.sort(byPriority);
|
||
}
|
||
}
|