Bundle cards show what the parts cost one by one, crossed out when it's more than the bundle price. Bundles with a part that isn't on sale are left out of the shop data, so their parts always have names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hy2vMnHJYP7QGsiH21zHxW
76 lines
3.8 KiB
TypeScript
76 lines
3.8 KiB
TypeScript
import type PocketBase from 'pocketbase';
|
|
import { getFileUrl } from '../pb';
|
|
import { bundleListPrice, parseDurations } from '../items';
|
|
import type { BookableTime, BundlePart, ItemRecord, ItemType } from '../types';
|
|
|
|
/** A bundle's `bundleParts` field (JSON) → valid parts. */
|
|
export function parseBundleParts(raw: unknown): BundlePart[] {
|
|
return (Array.isArray(raw) ? raw : [])
|
|
.map((p) => ({ item: String(p?.item || ''), minutes: p?.minutes ? Math.floor(Number(p.minutes)) : null, count: Math.floor(Number(p?.count)) || 0 }))
|
|
.filter((p) => p.item && p.count > 0);
|
|
}
|
|
|
|
/** An `items` record (with `expand.image`) → `ItemRecord`. */
|
|
export function mapItemRecord(record: Record<string, any>): ItemRecord {
|
|
const media = record.expand?.image;
|
|
const type: ItemType = record.type === 'item_with_time' || record.type === 'bundle' ? record.type : 'item';
|
|
return {
|
|
id: record.id,
|
|
name: String(record.name || ''),
|
|
description: String(record.description || ''),
|
|
price: Number(record.price) || 0,
|
|
image: String(record.image || ''),
|
|
imageUrl: media ? getFileUrl('media', media.id, media.file) : '',
|
|
type,
|
|
// null = unlimited: PocketBase reads an empty number back as 0, so `trackStock` says whether it counts.
|
|
stock: type === 'item' && record.trackStock ? Number(record.stock) || 0 : null,
|
|
durations: type === 'item_with_time' ? parseDurations(record.durations) : [],
|
|
bundleParts: type === 'bundle' ? parseBundleParts(record.bundleParts) : [],
|
|
validMonths: type === 'bundle' ? Number(record.validMonths) || null : null,
|
|
bufferMinutes: record.bufferMinutes ?? null,
|
|
capacity: record.capacity ?? null,
|
|
active: record.active !== false
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Active items, shaped for the Shop/Checkout blocks. Used by the public
|
|
* layout and the admin draft preview so both render the same shop. Works
|
|
* with an anonymous client (active items and media are public). Bookable
|
|
* times are fetched by the Shop block itself, per service and duration
|
|
* (see `loadBookableTimes`). Errors propagate.
|
|
*/
|
|
export async function loadShopData(pb: PocketBase): Promise<{ items: ItemRecord[] }> {
|
|
const records = await pb.collection('items').getFullList({ filter: 'active = true', sort: 'name', expand: 'image' });
|
|
const items = records.map(mapItemRecord);
|
|
// A bundle with a part that isn't on sale (inactive, deleted, or a length
|
|
// the service dropped) can't be bought — the shop hook refuses it too.
|
|
return { items: items.filter((item) => item.type !== 'bundle' || bundleListPrice(item.bundleParts, items) !== null) };
|
|
}
|
|
|
|
export type BookableTimesQuery = { item: string; minutes: string; from: string; to: string };
|
|
|
|
/**
|
|
* Times a customer can book for one service and duration, from the shop
|
|
* hook (GET /api/shop/slots), which applies every availability rule. When
|
|
* none start in [from, to), `next` is the first one after (or null).
|
|
*/
|
|
export function loadBookableTimes(pb: PocketBase, query: BookableTimesQuery) {
|
|
return pb.send<{ times: BookableTime[]; next: string | null }>('/api/shop/slots', { query, requestKey: null });
|
|
}
|
|
|
|
/**
|
|
* GET /api/slots in both apps (the Shop block's time picker): forwards
|
|
* ?item=&minutes=&from=&to= to the shop hook. A PocketBase error keeps its
|
|
* status and message; PocketBase being down is a 503.
|
|
*/
|
|
export async function bookableTimesResponse(pb: PocketBase, url: URL): Promise<Response> {
|
|
const param = (key: string) => url.searchParams.get(key) ?? '';
|
|
try {
|
|
return Response.json(await loadBookableTimes(pb, { item: param('item'), minutes: param('minutes'), from: param('from'), to: param('to') }));
|
|
} catch (e) {
|
|
const status = Number((e as { status?: number })?.status) || 0;
|
|
const message = (e as { response?: { message?: string } })?.response?.message;
|
|
return Response.json({ message: message || 'Could not load the available times — please try again' }, { status: status >= 400 ? status : 503 });
|
|
}
|
|
}
|