Admin: lengths and prices per service; windows and group sessions on /slots

Services get rows of length + price. /slots creates windows (date, hours,
services, start-times mode) and group sessions (service, length, start,
places); the day view shows free time and bookings. Generate/copy/clear
handle both kinds; windows may not overlap and can't shrink past a booking.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hy2vMnHJYP7QGsiH21zHxW
This commit is contained in:
agent 2026-10-25 20:12:46 +00:00
parent eac99b2abe
commit 636b59c706
12 changed files with 667 additions and 329 deletions

View file

@ -26,9 +26,9 @@
<p class="font-semibold">Nothing to {deleting ? 'delete' : 'create'}.</p>
{/if}
<ul class="mt-1 list-inside list-disc text-base-content/70 empty:hidden">
{#if preview.skippedOverlap}<li>{plural(preview.skippedOverlap, 'slot')} skipped — it would overlap a slot of the same service.</li>{/if}
{#if preview.skippedOverlap}<li>{plural(preview.skippedOverlap, 'slot')} skipped — it would overlap another window, or a session of the same service.</li>{/if}
{#if preview.skippedPast}<li>{plural(preview.skippedPast, 'slot')} skipped — in the past.</li>{/if}
{#if preview.kept}<li>{plural(preview.kept, 'slot')} kept — booked, or in a customer’s basket right now.</li>{/if}
{#if preview.kept}<li>{plural(preview.kept, 'slot')} kept — something in it is booked, or in a customer’s basket right now.</li>{/if}
</ul>
</div>
{/if}

View file

@ -33,7 +33,7 @@
<Modal bind:open title="Clear slots" wide>
<form method="POST" action="?/clear" use:enhance={bulk.submit} class="space-y-1">
<p class="text-sm text-base-content/70">Delete the free slots in a date range. Booked slots, and slots in a customer’s basket right now, are always kept.</p>
<p class="text-sm text-base-content/70">Delete the windows and group sessions in a date range that have nothing booked. Slots with a booking, or with a time in a customer’s basket right now, are always kept.</p>
<div class="grid gap-x-4 md:grid-cols-2">
<label class="fieldset">
@ -49,7 +49,7 @@
<label class="fieldset">
<span class="fieldset-legend">Services</span>
<select class="select w-full" name="item" bind:value={draft.item}>
<option value="">All services</option>
<option value="">All slots</option>
{#each items as item (item.id)}<option value={item.id}>{item.name}</option>{/each}
</select>
</label>

View file

@ -58,7 +58,7 @@
<label class="fieldset">
<span class="fieldset-legend">Services</span>
<select class="select w-full" name="item" bind:value={draft.item}>
<option value="">All services</option>
<option value="">All slots</option>
{#each items as item (item.id)}<option value={item.id}>{item.name}</option>{/each}
</select>
</label>

View file

@ -2,11 +2,14 @@
import { untrack } from 'svelte';
import { enhance } from '$app/forms';
import Modal from '$lib/admin/Modal.svelte';
import { WEEKDAYS, addDaysToKey, startTimesInWindows, type TimeWindow } from '$lib/slots';
import SegmentedControl from '$lib/admin/editors/SegmentedControl.svelte';
import { START_TIMES_HINTS, START_TIMES_LABELS, WEEKDAYS, addDaysToKey, startTimesInWindows, timeToMinutes, type TimeWindow } from '$lib/slots';
import type { ItemDuration, SlotKind, StartTimesMode } from '$lib/types';
import { formatDuration } from '$shared/items';
import BulkActions from './BulkActions.svelte';
import { bulkForm } from './bulkForm.svelte';
type Service = { id: string; name: string; duration: number; bufferMinutes: number; capacity: number };
type Service = { id: string; name: string; durations: ItemDuration[]; bufferMinutes: number; capacity: number };
let {
open = $bindable(false),
@ -17,14 +20,20 @@
// Kept between openings, so a second run starts from the last settings.
let draft = $state({
item: '',
kind: 'window' as SlotKind,
fromDate: '',
toDate: '',
weekdays: [1, 2, 3, 4, 5],
windows: [{ from: '09:00', to: '17:00' }] as TimeWindow[],
notes: '',
// windows
items: [] as string[],
startTimes: 'grid' as StartTimesMode,
// group sessions
item: '',
minutes: '',
bufferMinutes: '',
capacity: '',
notes: ''
capacity: ''
});
const bulk = bulkForm(
@ -33,11 +42,14 @@
);
let service = $derived(items.find((item) => item.id === draft.item));
let startTimes = $derived(
service ? startTimesInWindows(draft.windows, service.duration, Number(draft.bufferMinutes) || 0) : []
let sessionTimes = $derived(
service && draft.minutes ? startTimesInWindows(draft.windows, Number(draft.minutes), Number(draft.bufferMinutes) || 0) : []
);
let windowHours = $derived(
draft.windows.reduce((sum, w) => sum + Math.max(0, (timeToMinutes(w.to) ?? 0) - (timeToMinutes(w.from) ?? 0)), 0) / 60
);
// Each opening starts without a stale preview; the first one also fills in dates.
// Each opening starts without a stale preview; the first one also fills in dates and services.
$effect(() => {
if (!open) return;
untrack(() => {
@ -45,6 +57,9 @@
if (!draft.fromDate) {
draft.fromDate = today;
draft.toDate = addDaysToKey(today, 27);
// One-to-one services by default; group classes get sessions.
const oneToOne = items.filter((item) => item.capacity <= 1).map((item) => item.id);
draft.items = oneToOne.length ? oneToOne : items.map((item) => item.id);
}
});
});
@ -53,6 +68,7 @@
draft.item = id;
// 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.minutes = item?.durations[0] ? String(item.durations[0].minutes) : '';
draft.bufferMinutes = String(item?.bufferMinutes ?? 0);
draft.capacity = String(item?.capacity ?? 1);
}
@ -65,15 +81,15 @@
<Modal bind:open title="Generate slots" wide>
<form method="POST" action="?/generate" use:enhance={bulk.submit} class="space-y-1">
<label class="fieldset">
<span class="fieldset-legend">Service</span>
<select class="select w-full" name="item" required value={draft.item} onchange={(e) => selectService(e.currentTarget.value)}>
<option value="" disabled>Select a service…</option>
{#each items as item (item.id)}
<option value={item.id}>{item.name} ({item.duration} min)</option>
{/each}
</select>
</label>
<input type="hidden" name="kind" value={draft.kind} />
<SegmentedControl
label="Create"
class="mb-2"
options={['window', 'session']}
value={draft.kind}
formatLabel={(v) => (v === 'window' ? 'Windows' : 'Group sessions')}
onChange={(v) => (draft.kind = v as SlotKind)}
/>
<div class="grid gap-x-4 md:grid-cols-2">
<label class="fieldset">
@ -96,7 +112,7 @@
</div>
<div class="fieldset">
<span class="fieldset-legend">Opening hours ({timezone})</span>
<span class="fieldset-legend">{draft.kind === 'window' ? 'Windows each day' : 'Hours for sessions'} ({timezone})</span>
<div class="space-y-2">
{#each draft.windows as window, i}
<div class="flex items-center gap-2">
@ -112,27 +128,70 @@
<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>
<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}
{#if draft.kind === 'window'}
<div class="fieldset">
<span class="fieldset-legend">Services that can be booked</span>
<div class="grid gap-1 sm:grid-cols-2">
{#each items as item (item.id)}
<label class="label cursor-pointer gap-2 text-base-content">
<input class="checkbox checkbox-sm" type="checkbox" name="items" value={item.id} bind:group={draft.items} />
{item.name}
</label>
{/each}
</div>
</div>
<div class="fieldset">
<input type="hidden" name="startTimes" value={draft.startTimes} />
<SegmentedControl
label="Start times"
options={['grid', 'edges']}
value={draft.startTimes}
formatLabel={(v) => START_TIMES_LABELS[v as StartTimesMode]}
onChange={(v) => (draft.startTimes = v as StartTimesMode)}
/>
<span class="label whitespace-normal">{START_TIMES_HINTS[draft.startTimes]}</span>
</div>
<p class="rounded-box bg-base-200 px-4 py-2 text-sm">
{#if startTimes.length}
<span class="font-semibold">{startTimes.length} per day:</span> {startTimes.join(', ')}
{:else}
No {service.duration}-minute appointment fits in these hours.
{/if}
{draft.windows.length} window{draft.windows.length === 1 ? '' : 's'} per chosen day, {windowHours} hours in total.
</p>
{:else}
<div class="grid gap-x-4 md:grid-cols-2">
<label class="fieldset">
<span class="fieldset-legend">Service</span>
<select class="select w-full" name="item" required value={draft.item} onchange={(e) => selectService(e.currentTarget.value)}>
<option value="" disabled>Select a service…</option>
{#each items as item (item.id)}<option value={item.id}>{item.name}</option>{/each}
</select>
</label>
<label class="fieldset">
<span class="fieldset-legend">Length</span>
<select class="select w-full" name="minutes" required disabled={!service} bind:value={draft.minutes}>
{#each service?.durations ?? [] as duration (duration.minutes)}
<option value={String(duration.minutes)}>{formatDuration(duration.minutes)}</option>
{/each}
</select>
</label>
<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 session. Defaults to the service’s own.</span>
</label>
<label class="fieldset">
<span class="fieldset-legend">Places per session</span>
<input class="input w-32" type="number" min="1" max="500" step="1" name="capacity" bind:value={draft.capacity} />
<span class="label">Defaults to the service’s own.</span>
</label>
</div>
{#if service && draft.minutes}
<p class="rounded-box bg-base-200 px-4 py-2 text-sm">
{#if sessionTimes.length}
<span class="font-semibold">{sessionTimes.length} per day:</span> {sessionTimes.join(', ')}
{:else}
No {formatDuration(Number(draft.minutes))} session fits in these hours.
{/if}
</p>
{/if}
{/if}
<label class="fieldset">

View file

@ -1,7 +1,7 @@
// Server helpers for the /slots page. Planning is pure and lives in
// $shared/slots; this file does the PocketBase reads/writes around it.
import type { SlotDetail } from '$lib/slots';
import type { SlotBooking, SlotDetail } from '$lib/slots';
import type { createPocketBase } from '$lib/pb';
type Pb = ReturnType<typeof createPocketBase>;
@ -10,21 +10,23 @@ type Pb = ReturnType<typeof createPocketBase>;
const WRITE_CONCURRENCY = 5;
/**
* Slots with their places and blocking, from the shop hook — the same
* Slots with what's booked in them, 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.
* Either slots starting in `[from, to)` (optionally those offering one
* service) or one slot by id; `bookings` are all booked/held times starting
* in that range (for one slot: around its start).
*/
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', {
return pb.send<{ slots: SlotDetail[]; bookings: SlotBooking[] }>('/api/shop/slots', {
query: { detail: 1, ...query },
requestKey: null
});
return slots;
}
/** One slot's details, or null if it doesn't exist (any other failure throws). */
/** One slot's details and its own bookings, 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;
const { slots, bookings } = await loadSlotDetails(pb, { id });
return slots[0] ? { ...slots[0], bookings: bookings.filter((b) => b.slot === id) } : null;
}
export const seatsTaken = (slot: Pick<SlotDetail, 'booked' | 'held'>) => slot.booked + slot.held;