Bundles: keep parts from being deleted out from under them
Bundle parts are JSON, so deleting a service left bundles pointing at nothing (shown as "Item" in the shop). An onRecordDelete hook on items now refuses while a bundle contains it or a usable code still has sessions of it to book. A bundle with an inactive part is no longer sold or issued; the admin items list marks it "Not on sale" and shows how many codes are waiting on an inactive service. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hy2vMnHJYP7QGsiH21zHxW
This commit is contained in:
parent
c7e019131a
commit
338aef9e01
5 changed files with 109 additions and 13 deletions
|
|
@ -24,6 +24,12 @@ export function issuePass(pb: Pb, body: { itemId: string; email: string; recipie
|
|||
return pb.send<{ pass: PassSummary }>('/api/shop/passes/issue', { method: 'POST', body, requestKey: null });
|
||||
}
|
||||
|
||||
/** Item id → how many usable codes still have sessions of it to book. */
|
||||
export async function codesWaiting(pb: Pb) {
|
||||
const { waiting } = await pb.send<{ waiting: Record<string, number> }>('/api/shop/passes/waiting', { requestKey: null });
|
||||
return waiting;
|
||||
}
|
||||
|
||||
/** The hook's own message (e.g. "Choose a bundle"), or `fallback`. */
|
||||
export function hookError(e: unknown, fallback: string) {
|
||||
if (e instanceof ClientResponseError && e.status >= 400 && e.status < 500 && e.response?.message) return String(e.response.message);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import { fail } from '@sveltejs/kit';
|
||||
import { attempt } from '$lib/server/actions';
|
||||
import { ClientResponseError } from 'pocketbase';
|
||||
import { attempt, pbErrorMessage } from '$lib/server/actions';
|
||||
import { codesWaiting } from '$lib/server/passes';
|
||||
import { parseDurations } from '$shared/items';
|
||||
import { mapItemRecord } from '$shared/server/shop';
|
||||
import type { BundlePart, ItemDuration, ItemType } from '$lib/types';
|
||||
|
|
@ -119,11 +121,14 @@ function allItems(pb: App.Locals['pb']) {
|
|||
}
|
||||
|
||||
export const load: PageServerLoad = async ({ locals }) => {
|
||||
const records = await locals.pb
|
||||
.collection('items')
|
||||
.getFullList({ sort: 'name', expand: 'image' })
|
||||
.catch(() => []);
|
||||
return { items: records.map(mapItemRecord) };
|
||||
const [records, waiting] = await Promise.all([
|
||||
locals.pb
|
||||
.collection('items')
|
||||
.getFullList({ sort: 'name', expand: 'image' })
|
||||
.catch(() => []),
|
||||
codesWaiting(locals.pb).catch(() => ({}) as Record<string, number>)
|
||||
]);
|
||||
return { items: records.map(mapItemRecord), waiting };
|
||||
};
|
||||
|
||||
export const actions: Actions = {
|
||||
|
|
@ -148,8 +153,14 @@ export const actions: Actions = {
|
|||
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 past orders, switch it to inactive instead.', () => locals.pb.collection('items').delete(id));
|
||||
if (!result.ok) return result.failure;
|
||||
try {
|
||||
await locals.pb.collection('items').delete(id);
|
||||
} catch (e) {
|
||||
// In a bundle, or still to be booked with a code: the shop hook says which.
|
||||
if (e instanceof ClientResponseError && e.response?.data?.item?.code === 'in_use') return fail(400, { message: String(e.response.message) });
|
||||
console.error('[action] Could not delete the item', e);
|
||||
return fail(400, { message: pbErrorMessage(e, 'Could not delete the item — if it has past orders, switch it to inactive instead.') });
|
||||
}
|
||||
return { ok: true };
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -67,7 +67,18 @@
|
|||
/** "3× Massage (1 hour), 1× Oil" for the table. */
|
||||
function bundleList(item: ItemRecord) {
|
||||
const names = new Map(items.map((i) => [i.id, i.name]));
|
||||
return item.bundleParts.map((p) => `${p.count}× ${names.get(p.item) ?? '?'}${p.minutes ? ` (${formatDuration(p.minutes)})` : ''}`).join(', ');
|
||||
return item.bundleParts.map((p) => `${p.count}× ${names.get(p.item) ?? 'deleted item'}${p.minutes ? ` (${formatDuration(p.minutes)})` : ''}`).join(', ');
|
||||
}
|
||||
|
||||
/** Why an active bundle isn't on sale (the shop hides it and won't sell it), or ''. */
|
||||
function bundleProblem(item: ItemRecord) {
|
||||
for (const part of item.bundleParts) {
|
||||
const partItem = items.find((i) => i.id === part.item);
|
||||
if (!partItem) return 'One of its parts was deleted — edit the bundle.';
|
||||
if (!partItem.active) return `“${partItem.name}” is inactive.`;
|
||||
if (part.minutes && !partItem.durations.some((d) => d.minutes === part.minutes)) return `“${partItem.name}” is no longer offered for ${formatDuration(part.minutes)}.`;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
/** What the parts cost on their own, to compare with the bundle price. */
|
||||
|
|
@ -153,6 +164,8 @@
|
|||
<thead><tr><th>Name</th><th>Type</th><th>Price</th><th>Stock</th><th>Duration</th><th>Status</th><th></th></tr></thead>
|
||||
<tbody>
|
||||
{#each items as item (item.id)}
|
||||
{@const problem = item.type === 'bundle' && item.active ? bundleProblem(item) : ''}
|
||||
{@const waiting = !item.active && item.type === 'item_with_time' ? (data.waiting[item.id] ?? 0) : 0}
|
||||
<tr>
|
||||
<td>
|
||||
<div class="flex items-center gap-3">
|
||||
|
|
@ -174,7 +187,17 @@
|
|||
{bundleList(item)} · {item.validMonths ? `valid ${item.validMonths} months` : 'no expiry'}
|
||||
{:else}—{/if}
|
||||
</td>
|
||||
<td><span class="badge" class:badge-success={item.active}>{item.active ? 'Active' : 'Inactive'}</span></td>
|
||||
<td>
|
||||
{#if problem}
|
||||
<span class="badge badge-warning whitespace-nowrap">Not on sale</span>
|
||||
<span class="mt-1 block text-xs text-base-content/60">{problem}</span>
|
||||
{:else}
|
||||
<span class="badge" class:badge-success={item.active}>{item.active ? 'Active' : 'Inactive'}</span>
|
||||
{/if}
|
||||
{#if waiting}
|
||||
<span class="mt-1 block text-xs text-base-content/70">{waiting === 1 ? '1 code has' : `${waiting} codes have`} sessions of this left to book — they can’t while it’s inactive.</span>
|
||||
{/if}
|
||||
</td>
|
||||
<td class="text-right"><button class="btn btn-sm" type="button" onclick={() => editItem(item)}>Edit</button></td>
|
||||
</tr>
|
||||
{:else}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue