Admin slots: places, blocked/full states, edits limited once booked

Calendar, day panel and list show available / in a basket / booked /
full / blocked (with what blocks it) and places taken. Services, Generate
and single slots take places (default from the service); Copy keeps
them. Once any place is taken only places (not below taken) and notes
can change. All reads go through the shop hook's slot details.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VaXE9U7AmW3dfD2w6cbLfk
This commit is contained in:
agent 2026-10-24 08:26:37 +00:00
parent 5c6dff9e18
commit a22d9f2348
7 changed files with 231 additions and 172 deletions

View file

@ -6,7 +6,7 @@
import BulkActions from './BulkActions.svelte';
import { bulkForm } from './bulkForm.svelte';
type Service = { id: string; name: string; duration: number; bufferMinutes: number };
type Service = { id: string; name: string; duration: number; bufferMinutes: number; capacity: number };
let {
open = $bindable(false),
@ -23,6 +23,7 @@
weekdays: [1, 2, 3, 4, 5],
windows: [{ from: '09:00', to: '17:00' }] as TimeWindow[],
bufferMinutes: '',
capacity: '',
notes: ''
});
@ -50,8 +51,10 @@
function selectService(id: string) {
draft.item = id;
// The service's own get-ready time is the default; it can be overridden here.
draft.bufferMinutes = String(items.find((item) => item.id === id)?.bufferMinutes ?? 0);
// The service's own get-ready time and places are the defaults; both can be overridden here.
const item = items.find((item) => item.id === id);
draft.bufferMinutes = String(item?.bufferMinutes ?? 0);
draft.capacity = String(item?.capacity ?? 1);
}
function addWindow() {
@ -109,11 +112,18 @@
<button class="btn btn-ghost btn-sm mt-1 w-fit" type="button" onclick={addWindow}>+ Add hours (e.g. after a lunch break)</button>
</div>
<label class="fieldset">
<span class="fieldset-legend">Get-ready time (minutes)</span>
<input class="input w-32" type="number" min="0" step="1" name="bufferMinutes" bind:value={draft.bufferMinutes} />
<span class="label">Gap after each appointment. Defaults to the service’s own get-ready time.</span>
</label>
<div class="grid gap-x-4 md:grid-cols-2">
<label class="fieldset">
<span class="fieldset-legend">Get-ready time (minutes)</span>
<input class="input w-32" type="number" min="0" step="1" name="bufferMinutes" bind:value={draft.bufferMinutes} />
<span class="label">Gap after each appointment. Defaults to the service’s own.</span>
</label>
<label class="fieldset">
<span class="fieldset-legend">Places per slot</span>
<input class="input w-32" type="number" min="1" max="500" step="1" name="capacity" bind:value={draft.capacity} />
<span class="label">More than 1 for a group session. Defaults to the service’s own.</span>
</label>
</div>
{#if service}
<p class="rounded-box bg-base-200 px-4 py-2 text-sm">

View file

@ -1,56 +1,34 @@
// Server helpers for the /slots bulk actions (generate, copy week, clear).
// Planning is pure and lives in $shared/slots; this file does the PocketBase
// reads/writes around it.
// Server helpers for the /slots page. Planning is pure and lives in
// $shared/slots; this file does the PocketBase reads/writes around it.
import { instantMs, type PlannedSlot } from '$lib/slots';
import type { SlotDetail } from '$lib/slots';
import type { createPocketBase } from '$lib/pb';
type Pb = ReturnType<typeof createPocketBase>;
export type ExistingSlot = PlannedSlot & {
id: string;
item: string;
status: string;
heldUntil: string;
notes: string;
};
/** How many PocketBase writes a bulk action runs at once. */
const WRITE_CONCURRENCY = 5;
/** Held by a basket whose hold hasn't expired yet (expired holds are released lazily). */
export function isHoldActive(slot: Record<string, any> | null) {
if (slot?.status !== 'held') return false;
const until = instantMs(String(slot.heldUntil || ''));
return Number.isNaN(until) || until > Date.now();
/**
* Slots with their places and blocking, from the shop hook — the same
* availability rules the public shop uses (pocketbase/pb_hooks/shop/lib.js).
* Either slots starting in `[from, to)` (optionally one service) or one slot by id.
*/
export async function loadSlotDetails(pb: Pb, query: { from: string; to: string; item?: string } | { id: string }) {
const { slots } = await pb.send<{ slots: SlotDetail[] }>('/api/shop/slots', {
query: { detail: 1, ...query },
requestKey: null
});
return slots;
}
/**
* Slots starting in `[fromIso, toIso)`, optionally for one service. A slot
* without an end gets one from its service's duration, so overlap checks
* always have a real range.
*/
export async function loadSlotsBetween(pb: Pb, fromIso: string, toIso: string, itemId = ''): Promise<ExistingSlot[]> {
const filter = itemId
? pb.filter('start >= {:from} && start < {:to} && item = {:item}', { from: new Date(fromIso), to: new Date(toIso), item: itemId })
: pb.filter('start >= {:from} && start < {:to}', { from: new Date(fromIso), to: new Date(toIso) });
const records = await pb.collection('slots').getFullList({ filter, sort: 'start', expand: 'item' });
return records.map((record) => {
const start = String(record.start || '');
const duration = Number(record.expand?.item?.duration) || 0;
const end = record.end ? String(record.end) : new Date(instantMs(start) + duration * 60_000).toISOString();
return {
id: record.id,
item: String(record.item || ''),
start,
end,
status: String(record.status || 'available'),
heldUntil: record.heldUntil ? String(record.heldUntil) : '',
notes: String(record.notes || '')
};
});
/** One slot's details, or null if it doesn't exist (any other failure throws). */
export async function loadSlotDetail(pb: Pb, id: string) {
return (await loadSlotDetails(pb, { id }))[0] ?? null;
}
export const seatsTaken = (slot: Pick<SlotDetail, 'booked' | 'held'>) => slot.booked + slot.held;
/** Run `fn` over `values` a few at a time; returns how many succeeded. */
export async function runInBatches<T>(values: T[], label: string, fn: (value: T) => Promise<unknown>) {
let succeeded = 0;