diff --git a/README.md b/README.md index ca4e419..06468aa 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,7 @@ If schema import fails because of a PocketBase version mismatch, create these co Commerce collections (see the Stripe section above for context): - `items` — `name` text, `description` text, `price` number, `image` relation→media, `type` select `item|item_with_time`, `stock` number (nullable), `durations` json (services: `[{ minutes, price }]`, the lengths it can be booked for; `price` is then kept at the lowest of them), `bufferMinutes` number (nullable, get-ready minutes after each appointment), `capacity` number (nullable = 1, default places for new group sessions), `active` bool -- `slots` — `kind` select `window|session`, `items` relation→items (multiple: the services bookable in a window; a session's one service), `start` date, `end` date, `startTimes` select `grid|edges` (windows), `capacity` number (sessions: places), `notes` text. Nothing about bookings is stored here — see *Slot availability* below +- `slots` — `kind` select `window|session`, `items` relation→items (multiple: the services bookable in a window; a session's one service), `start` date, `end` date, `startTimes` select `grid|edges` and `gridMinutes` number (windows), `capacity` number (sessions: places), `notes` text. Nothing about bookings is stored here — see *Slot availability* below - `baskets` — `status` select `active|completed|abandoned`, `expiresAt` date, `email` text, `customerDetails` json, `stripePaymentIntentId` text - `basket_lines` — `basket` relation→baskets, `item` relation→items, `slot` relation→slots (nullable), `quantity` number (places, for a slot line), `lineType` select `item|slot`, `start` date and `minutes` number (a slot line's booked time), `heldUntil` date (a slot line's hold) - `orders` — `basket` relation→baskets, `email` text, `customerDetails` json, `total` number, `status` select `pending|paid|refunded`, `stripePaymentIntentId` text, `customerEmailSentAt` date, `clientEmailSentAt` date, `emailError` text, `anonymisedAt` date (set by GDPR erasure/retention; `email` is then empty) @@ -138,7 +138,7 @@ All routes in `app-admin` except `/login` are guarded in `src/hooks.server.ts`. - `/pages/[slug]` — block editor with draft preview - `/calendar` — events shown by Upcoming Calendar blocks - `/items` — services and products shown in Shop blocks -- `/slots` — when the practitioner can be booked: **windows** (e.g. 09:00–12:00, with the services bookable in it and where bookings may start — every 30 min, or only at the start/end of the free time) and **group sessions** (one service at one of its lengths, fixed time, places). Month calendar, a day view with free time and bookings, and bulk tools — *Generate slots* (date range × weekdays × hours → windows, or sessions spaced by length + get-ready time), *Copy a week* (repeat into the next 1–12 weeks) and *Clear slots* (slots with nothing booked). Windows may not overlap each other, nor sessions of the same service; bulk tools preview first and skip past or overlapping slots. A window can't shrink past a booking in it. +- `/slots` — when the practitioner can be booked: **windows** (e.g. 09:00–12:00, with the services bookable in it and where bookings may start — in fixed steps (every 10–60 min, default 30), or only at the start/end of the free time) and **group sessions** (one service at one of its lengths, fixed time, places). Month calendar, a day view with free time and bookings, and bulk tools — *Generate slots* (date range × weekdays × hours → windows, or sessions spaced by length + get-ready time), *Copy a week* (repeat into the next 1–12 weeks) and *Clear slots* (slots with nothing booked). Windows may not overlap each other, nor sessions of the same service; bulk tools preview first and skip past or overlapping slots. A window can't shrink past a booking in it. - `/items` lists each service's lengths and prices (*Lengths and prices* rows); the Shop block shows a slider between them. - `/orders` — confirmed bookings and purchases, with booking-email status and resend buttons - `/customers` — customer personal data (GDPR): search, export as JSON, correct details on every order, erase, and the data-retention settings (see *Personal data (GDPR)*) @@ -155,7 +155,7 @@ The shop/checkout blocks implement a basket-based booking flow: visitors add ser - A **booking** is a paid order line or a basket line whose `heldUntil` hasn't passed (10 min after adding, 30 min once payment starts), each with its own `start` and `minutes`. It keeps the practitioner busy until its service's get-ready time (`items.bufferMinutes`) is over. A lapsed hold stops counting immediately; the cron only deletes the line. The checkout shows a countdown to the first hold running out, offers "Keep it 10 more minutes" (basket action `renew_holds`; holds are extended, never shortened) in the last 3 minutes, and says which times ran out when one does. - One practitioner works every service, so no booking may overlap another — whatever the service, whatever the slot — including get-ready time. -- A **window** offers its services at any of their lengths. A booking must start and end inside it (its get-ready time may run past the end) and fit before the next booking. `startTimes = grid`: every 30 min from the window's start. `edges`: only right at the start or the end of each free stretch — a 09:00–12:00 window offers a 1-hour massage at 09:00 or 11:00; once 09:00 is booked (+15 min get-ready) it offers 10:15 or 11:00 — so time is taken from the ends and no gaps are left. +- A **window** offers its services at any of their lengths. A booking must start and end inside it (its get-ready time may run past the end) and fit before the next booking. `startTimes = grid`: every `gridMinutes` (chosen per window: 10, 15, 20, 30, 45 or 60; default 30) from the window's start. `edges`: only right at the start or the end of each free stretch — a 09:00–12:00 window offers a 1-hour massage at 09:00 or 11:00; once 09:00 is booked (+15 min get-ready) it offers 10:15 or 11:00 — so time is taken from the ends and no gaps are left. - A **group session** has `capacity` places for one service at one length; places are counted from its bookings. With any place taken its whole time is busy (windows lose that time); it stays bookable until full, and can't be booked while another booking overlaps it. - `GET /api/shop/slots?item=&minutes=&from=&to=` lists the times a customer can book for one service and length (public; the Shop block fetches it through each app's `GET /api/slots` for the months on screen), plus `next` — the first bookable time after the range — when there are none in it. With `?detail=1` a signed-in admin gets every slot in a range with its bookings, free stretches and what blocks it (the `/slots` page). - Adding to a basket (`add_slot`: `slotId` plus `itemId`, `minutes`, `start` for a window, or `seats` for a session) and changing a group line's places re-check everything inside one transaction; PocketBase runs transactions one at a time, so concurrent customers can't both get the last place or two overlapping times. The price is the service's price for the booked length, read again at checkout. diff --git a/app-admin/src/lib/admin/slots/GenerateSlotsModal.svelte b/app-admin/src/lib/admin/slots/GenerateSlotsModal.svelte index af4bc96..775d2f5 100644 --- a/app-admin/src/lib/admin/slots/GenerateSlotsModal.svelte +++ b/app-admin/src/lib/admin/slots/GenerateSlotsModal.svelte @@ -3,7 +3,7 @@ import { enhance } from '$app/forms'; import Modal from '$lib/admin/Modal.svelte'; 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 { DEFAULT_GRID_MINUTES, GRID_STEP_OPTIONS, START_TIMES_LABELS, WEEKDAYS, startTimesHint, 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'; @@ -29,6 +29,7 @@ // windows items: [] as string[], startTimes: 'grid' as StartTimesMode, + gridMinutes: DEFAULT_GRID_MINUTES, // group sessions item: '', minutes: '', @@ -149,7 +150,15 @@ formatLabel={(v) => START_TIMES_LABELS[v as StartTimesMode]} onChange={(v) => (draft.startTimes = v as StartTimesMode)} /> - {START_TIMES_HINTS[draft.startTimes]} + {#if draft.startTimes === 'grid'} + + {/if} + {startTimesHint(draft.startTimes, draft.gridMinutes)}
{draft.windows.length} window{draft.windows.length === 1 ? '' : 's'} per chosen day, {windowHours} hours in total.
diff --git a/app-admin/src/routes/slots/+page.server.ts b/app-admin/src/routes/slots/+page.server.ts
index de46f87..6195fe8 100644
--- a/app-admin/src/routes/slots/+page.server.ts
+++ b/app-admin/src/routes/slots/+page.server.ts
@@ -5,6 +5,8 @@ import type { createPocketBase } from '$lib/pb';
import { attempt } from '$lib/server/actions';
import { loadSlotDetail, loadSlotDetails, runInBatches, seatsTaken } from '$lib/server/slots';
import {
+ DEFAULT_GRID_MINUTES,
+ GRID_STEP_OPTIONS,
MAX_BULK_SLOTS,
addDaysToKey,
dateKeysBetween,
@@ -35,6 +37,7 @@ type SlotFields = {
start: string;
end: string;
startTimes: StartTimesMode | '';
+ gridMinutes: number | null;
capacity: number | null;
notes: string;
};
@@ -74,8 +77,11 @@ function parseCapacity(data: FormData, fallback: number): number | { error: stri
return capacity;
}
-function parseStartTimes(data: FormData): StartTimesMode {
- return String(data.get('startTimes')) === 'edges' ? 'edges' : 'grid';
+/** A window's start-times mode and, for `grid`, its step (one of GRID_STEP_OPTIONS). */
+function parseStartTimes(data: FormData): { startTimes: StartTimesMode; gridMinutes: number | null } {
+ if (String(data.get('startTimes')) === 'edges') return { startTimes: 'edges', gridMinutes: null };
+ const step = Number(data.get('gridMinutes'));
+ return { startTimes: 'grid', gridMinutes: GRID_STEP_OPTIONS.includes(step) ? step : DEFAULT_GRID_MINUTES };
}
/** The services ticked in a window form, all of them active services. */
@@ -98,7 +104,7 @@ function windowFields(data: FormData, services: Service[]): SlotFields | { error
const start = zonedInputToIso(`${date}T${from}`, siteTimezone());
const end = zonedInputToIso(`${date}T${to}`, siteTimezone());
if (!start || !end) return { error: 'Invalid date or time.' };
- return { kind: 'window', items, start, end, startTimes: parseStartTimes(data), capacity: null, notes: String(data.get('notes') || '').trim() };
+ return { kind: 'window', items, start, end, ...parseStartTimes(data), capacity: null, notes: String(data.get('notes') || '').trim() };
}
/** A group session from the form: one service at one of its lengths, a start and places. */
@@ -113,7 +119,7 @@ function sessionFields(data: FormData, services: Service[]): SlotFields | { erro
const capacity = parseCapacity(data, service.capacity);
if (typeof capacity !== 'number') return capacity;
const end = new Date(instantMs(start) + minutes * 60_000).toISOString();
- return { kind: 'session', items: [service.id], start, end, startTimes: '', capacity, notes: String(data.get('notes') || '').trim() };
+ return { kind: 'session', items: [service.id], start, end, startTimes: '', gridMinutes: null, capacity, notes: String(data.get('notes') || '').trim() };
}
/**
@@ -331,12 +337,12 @@ export const actions: Actions = {
const bufferMinutes = rawBuffer === '' ? service.bufferMinutes : parseInt(rawBuffer, 10);
if (Number.isNaN(bufferMinutes) || bufferMinutes < 0) return fail(400, { message: 'Get-ready time must be 0 or more minutes.' });
const planned = planGeneratedSlots({ ...range, ...days, durationMinutes: minutes, bufferMinutes, timeZone: siteTimezone() });
- candidates = planned.map((slot) => ({ ...slot, kind: 'session', items: [service.id], startTimes: '', capacity, notes }));
+ candidates = planned.map((slot) => ({ ...slot, kind: 'session', items: [service.id], startTimes: '', gridMinutes: null, capacity, notes }));
} else {
const items = chosenServices(data, services);
if ('error' in items) return fail(400, { message: items.error });
const startTimes = parseStartTimes(data);
- candidates = planWindows({ ...range, ...days, timeZone: siteTimezone() }).map((slot) => ({ ...slot, kind: 'window', items, startTimes, capacity: null, notes }));
+ candidates = planWindows({ ...range, ...days, timeZone: siteTimezone() }).map((slot) => ({ ...slot, kind: 'window', items, ...startTimes, capacity: null, notes }));
}
if (!candidates.length) return fail(400, { message: 'Nothing fits those days and times.' });
@@ -367,7 +373,7 @@ export const actions: Actions = {
const start = shiftByDays(slot.start, 7 * week, siteTimezone());
const end = shiftByDays(slot.end, 7 * week, siteTimezone());
if (!start || !end) continue;
- candidates.push({ kind: slot.kind, items: slot.items, start, end, startTimes: slot.startTimes ?? '', capacity: slot.capacity ?? null, notes: slot.notes });
+ candidates.push({ kind: slot.kind, items: slot.items, start, end, startTimes: slot.startTimes ?? '', gridMinutes: slot.gridMinutes ?? null, capacity: slot.capacity ?? null, notes: slot.notes });
}
}
diff --git a/app-admin/src/routes/slots/+page.svelte b/app-admin/src/routes/slots/+page.svelte
index aa07d26..9325f2a 100644
--- a/app-admin/src/routes/slots/+page.svelte
+++ b/app-admin/src/routes/slots/+page.svelte
@@ -8,7 +8,7 @@
import ClearSlotsModal from '$lib/admin/slots/ClearSlotsModal.svelte';
import CopyWeekModal from '$lib/admin/slots/CopyWeekModal.svelte';
import GenerateSlotsModal from '$lib/admin/slots/GenerateSlotsModal.svelte';
- import { SLOT_STATE_LABELS, START_TIMES_HINTS, START_TIMES_LABELS, WEEKDAYS, addDaysToKey, addMonthsToKey, formatDateKey, mondayOfKey, monthGridKeys, slotState } from '$lib/slots';
+ import { DEFAULT_GRID_MINUTES, GRID_STEP_OPTIONS, SLOT_STATE_LABELS, START_TIMES_LABELS, WEEKDAYS, startTimesHint, startTimesLabel, addDaysToKey, addMonthsToKey, formatDateKey, mondayOfKey, monthGridKeys, slotState } from '$lib/slots';
import type { BulkResult, SlotState } from '$lib/slots';
import type { SlotKind, StartTimesMode } from '$lib/types';
import { formatDuration } from '$shared/items';
@@ -123,6 +123,7 @@
to: string;
items: string[];
startTimes: StartTimesMode;
+ gridMinutes: number;
// session
item: string;
minutes: string;
@@ -158,6 +159,7 @@
to: '12:00',
items: serviceFilter ? [serviceFilter] : oneToOneServices(items),
startTimes: 'grid',
+ gridMinutes: DEFAULT_GRID_MINUTES,
item: serviceFilter,
minutes: service?.durations[0] ? String(service.durations[0].minutes) : '',
start: `${date}T09:00`,
@@ -212,6 +214,7 @@
to: toZonedDatetimeInput(slot.end, data.timezone).slice(11, 16),
items: [...slot.items],
startTimes: slot.startTimes ?? 'grid',
+ gridMinutes: slot.gridMinutes ?? DEFAULT_GRID_MINUTES,
item: slot.items[0] ?? '',
minutes: String(slot.minutes ?? ''),
start,
@@ -355,7 +358,7 @@
{slot.kind === 'window' ? 'Window' : 'Group session'} · {serviceNames(slot)}{places(slot) ? ` · ${places(slot)} places` : ''}
- {#if slot.kind === 'window'}{freeLabel(slot)} · {START_TIMES_LABELS[slot.startTimes ?? 'grid']}{:else if state === 'blocked'}{describe(slot, state)}{:else}{formatDuration(slot.minutes ?? 0)}{/if}
+ {#if slot.kind === 'window'}{freeLabel(slot)} · {startTimesLabel(slot.startTimes ?? 'grid', slot.gridMinutes)}{:else if state === 'blocked'}{describe(slot, state)}{:else}{formatDuration(slot.minutes ?? 0)}{/if}
{#if slot.notes}{slot.notes}{/if}
@@ -459,7 +462,15 @@
formatLabel={(v) => START_TIMES_LABELS[v as StartTimesMode]}
onChange={(v) => (draft.startTimes = v as StartTimesMode)}
/>
- {START_TIMES_HINTS[draft.startTimes]}
+ {#if draft.startTimes === 'grid'}
+
+ {/if}
+ {startTimesHint(draft.startTimes, draft.gridMinutes)}
{:else}
{#if sessionLocked}
diff --git a/packages/shared/src/slots.ts b/packages/shared/src/slots.ts
index d02ba34..2cd4963 100644
--- a/packages/shared/src/slots.ts
+++ b/packages/shared/src/slots.ts
@@ -72,6 +72,8 @@ export type SlotDetail = PlannedSlot & {
held: number;
// window
startTimes?: StartTimesMode;
+ /** `grid` windows: minutes between start times. */
+ gridMinutes?: number;
free?: PlannedSlot[];
// session
minutes?: number;
@@ -80,14 +82,25 @@ export type SlotDetail = PlannedSlot & {
};
export const START_TIMES_LABELS: Record