- Slot and timed-event times were parsed in the server's zone (UTC in the container) while the form sent the admin's local wall-clock time, so every save shifted them by the UTC offset (10:00 → 12:00 in Oslo, again on each save). Inputs are now interpreted in SITE_TIMEZONE (shared helpers with DST handling), the same setting the booking emails use. - PocketBase failures in actions (duplicate, validation, relation in use, DB down) now return a readable `fail()` instead of a 500 page. - Pages: 404 for unknown slugs, slug filters built with pb.filter (quotes broke them), duplicate-slug check on create/rename (no unique index), invalid block JSON rejected instead of throwing; save returns a result so the editor can stay on the page. - Globals: only create a missing record on 404 — any other error (DB hiccup) used to create duplicates; save reports success/failure. - Slots: status is never taken from the form; re-read before update so a slot booked or held since the page loaded isn't overwritten; don't delete actively held slots. - Orders: load all order lines in one request instead of one per order. - Layout exposes site timezone and shop currency for display. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KgF3Kf47LjPGHKQHszpUAv
78 lines
3.3 KiB
TypeScript
78 lines
3.3 KiB
TypeScript
import { fail } from '@sveltejs/kit';
|
|
import { getFileUrl } from '$lib/pb';
|
|
import { attempt } from '$lib/server/actions';
|
|
import type { ItemRecord, ItemType } from '$lib/types';
|
|
import type { Actions, PageServerLoad } from './$types';
|
|
|
|
function itemPayload(data: FormData) {
|
|
const type: ItemType = String(data.get('type')) === 'item_with_time' ? 'item_with_time' : 'item';
|
|
const rawStock = String(data.get('stock') ?? '').trim();
|
|
const rawDuration = String(data.get('duration') ?? '').trim();
|
|
const stock = type === 'item' && rawStock !== '' ? parseInt(rawStock, 10) : null;
|
|
const duration = type === 'item_with_time' && rawDuration !== '' ? parseInt(rawDuration, 10) : null;
|
|
|
|
return {
|
|
name: String(data.get('name') || '').trim(),
|
|
description: String(data.get('description') || '').trim(),
|
|
price: parseFloat(String(data.get('price') || '')),
|
|
image: String(data.get('image') || '').trim(),
|
|
type,
|
|
stock: Number.isNaN(stock) ? null : stock,
|
|
duration: Number.isNaN(duration) ? null : duration,
|
|
active: String(data.get('active')) === 'true'
|
|
};
|
|
}
|
|
|
|
function normalizeItem(record: Record<string, any>): ItemRecord {
|
|
const media = record.expand?.image;
|
|
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: record.type === 'item_with_time' ? 'item_with_time' : 'item',
|
|
stock: record.stock ?? null,
|
|
duration: record.duration ?? null,
|
|
active: record.active !== false
|
|
};
|
|
}
|
|
|
|
export const load: PageServerLoad = async ({ locals }) => {
|
|
const records = await locals.pb
|
|
.collection('items')
|
|
.getFullList({ sort: 'name', expand: 'image' })
|
|
.catch(() => []);
|
|
const items = records.map((record) => normalizeItem(record));
|
|
return { items };
|
|
};
|
|
|
|
export const actions: Actions = {
|
|
create: async ({ request, locals }) => {
|
|
const data = await request.formData();
|
|
const payload = itemPayload(data);
|
|
if (!payload.name || Number.isNaN(payload.price) || payload.price < 0) return fail(400, { message: 'Name and a valid price are required.' });
|
|
const result = await attempt('Could not create the item.', () => locals.pb.collection('items').create(payload));
|
|
if (!result.ok) return result.failure;
|
|
return { ok: true };
|
|
},
|
|
update: async ({ request, locals }) => {
|
|
const data = await request.formData();
|
|
const id = String(data.get('id') || '');
|
|
const payload = itemPayload(data);
|
|
if (!id) return fail(400, { message: 'Item id is required.' });
|
|
if (!payload.name || Number.isNaN(payload.price) || payload.price < 0) return fail(400, { message: 'Name and a valid price are required.' });
|
|
const result = await attempt('Could not save the item.', () => locals.pb.collection('items').update(id, payload));
|
|
if (!result.ok) return result.failure;
|
|
return { ok: true };
|
|
},
|
|
delete: async ({ request, locals }) => {
|
|
const data = await request.formData();
|
|
const id = String(data.get('id') || '');
|
|
if (!id) return fail(400, { message: 'Item id is required.' });
|
|
const result = await attempt('Could not delete the item — if it has slots or past orders, switch it to inactive instead.', () => locals.pb.collection('items').delete(id));
|
|
if (!result.ok) return result.failure;
|
|
return { ok: true };
|
|
}
|
|
};
|