The public (internet-facing) app logged in as the PocketBase superuser and held the Stripe secret key, so any bug in it could become full control of the database. Baskets, slot holds, Stripe PaymentIntents, the webhook that creates orders, and the payment-result lookup now run in PocketBase JS hooks (pocketbase/pb_hooks/shop, routes under /api/shop/*, plus a cron that releases expired holds every minute). The public app's routes keep their URLs but only proxy to those routes and manage the basket cookie; it has no PocketBase or Stripe secret. Booking emails are still rendered and sent by the public app (same renderer as the admin preview) from order data the webhook hook returns; it records the outcome via a narrow endpoint guarded by INTERNAL_API_SECRET. Also: slot add and order creation are now transactional; shop listings load anonymously (already public via API rules); shop API errors always reach the browser as JSON; the superuser client and stripe package are removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KgF3Kf47LjPGHKQHszpUAv
52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
import type PocketBase from 'pocketbase';
|
|
import { getFileUrl } from '../pb';
|
|
import type { ItemRecord, SlotRecord } from '../types';
|
|
|
|
/**
|
|
* Active items and future available slots, 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: the API rules expose active items,
|
|
* available slots and media publicly. Errors propagate.
|
|
*/
|
|
export async function loadShopData(pb: PocketBase): Promise<{ items: ItemRecord[]; slots: SlotRecord[] }> {
|
|
const [itemRecords, slotRecords] = await Promise.all([
|
|
pb.collection('items').getFullList({ filter: 'active = true', sort: 'name', expand: 'image' }),
|
|
pb.collection('slots').getFullList({
|
|
filter: pb.filter("status = 'available' && start >= {:now}", { now: new Date() }),
|
|
sort: 'start'
|
|
})
|
|
]);
|
|
|
|
const items = itemRecords.map((record): ItemRecord => {
|
|
const media = record.expand?.image;
|
|
return {
|
|
id: record.id,
|
|
name: record.name,
|
|
description: record.description || '',
|
|
price: record.price,
|
|
image: record.image || '',
|
|
imageUrl: media ? getFileUrl('media', media.id, media.file) : '',
|
|
type: record.type,
|
|
stock: record.stock ?? null,
|
|
duration: record.duration ?? null,
|
|
active: record.active
|
|
};
|
|
});
|
|
|
|
const slots = slotRecords.map(
|
|
(record): SlotRecord => ({
|
|
id: record.id,
|
|
item: record.item,
|
|
start: record.start,
|
|
end: record.end || '',
|
|
status: record.status,
|
|
heldUntil: record.heldUntil || null,
|
|
order: record.order || null,
|
|
notes: record.notes || ''
|
|
})
|
|
);
|
|
|
|
return { items, slots };
|
|
}
|