@mccaskie-labs/workbench-bookings 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/README.md +71 -2
- package/dist/bookings.d.ts +160 -0
- package/dist/bookings.js +446 -0
- package/dist/cli.d.ts +10 -0
- package/dist/cli.js +125 -0
- package/dist/config.d.ts +9 -0
- package/dist/config.js +165 -0
- package/dist/errors.d.ts +25 -0
- package/dist/errors.js +10 -0
- package/dist/format.d.ts +12 -0
- package/dist/format.js +24 -0
- package/dist/ical.d.ts +13 -0
- package/dist/ical.js +67 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +20 -0
- package/dist/migration.d.ts +4 -0
- package/dist/migration.js +441 -0
- package/dist/slots.d.ts +24 -0
- package/dist/slots.js +87 -0
- package/dist/starters.d.ts +31 -0
- package/dist/starters.js +29 -0
- package/dist/styles.d.ts +34 -0
- package/dist/styles.js +97 -0
- package/dist/time.d.ts +15 -0
- package/dist/time.js +82 -0
- package/dist/types.d.ts +130 -0
- package/dist/types.js +4 -0
- package/dist/ui.d.ts +131 -0
- package/dist/ui.js +208 -0
- package/dist/words.d.ts +4 -0
- package/dist/words.js +160 -0
- package/package.json +48 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# @mccaskie-labs/workbench-bookings
|
|
2
|
+
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Bookings 0.2.0: places per booking (a service's `places`, 1 to its capacity), signed-in members (book() takes the app's member id; `mine()`, `cancelAsMember()` and `renderMyBookings` give a My bookings page), files attached to the confirmation (the `attachments` option, for a tax invoice), and `cancel_until: "0m"` for cancelling up to the start. The manifest asks `bookings.places` and `bookings.members`. 0.1.0 was never published, so its migration is replaced, not added to.
|
|
8
|
+
|
|
9
|
+
## 0.1.0
|
|
10
|
+
|
|
11
|
+
- First version (`docs/capabilities/13-bookings.md`): a bookings calendar over the app's own database. `workbench/bookings.json` (opening hours, closed days, services with their length, buffer, places and start times, notice, how far ahead, instant or owner-approved, reminders, the change cut-off, what the form asks) is checked by `checkBookingConfig` and `npx workbench-bookings generate`, with `init` (an Australian starter: weekdays 9 to 5 in Sydney) and `migration`. `openSlots` gives the free times as a pure function, across daylight saving. `createBookings` with `bookingsMigration` (service role only): `slots()`, `book()` (under the account's lock, so two people never take the last place; refused with `not_allowed` when the plan lacks `bookings`, `too_many` past three upcoming bookings an email address), the customer's `cancel()` and `move()` from a manage link whose secret is kept only as a SHA-256, the owner's `approve()`, `decline()`, `cancelByOwner()` and `list()`, a calendar feed (`feedToken()`, `feed()`, no customer names unless asked) and each booking's calendar file (`icsFor()`). Emails through workbench-email: a confirmation with the calendar file, a request received, confirmed, declined, moved and cancelled, and the owner's new booking, request, move and cancellation. `tick()` from the cron every 15 minutes sends each reminder once even when runs overlap, and declines requests nobody answered within `hold`; `cleanup()` deletes bookings 180 days after they end. Pages as plain HTML (`renderSlotPicker`, `renderBookingForm`, `renderBookingDone`, `renderBookingManage`, `renderBookingList`, with `bookingAction` and `ownerBookingAction`), `wb-booking-*` classes with `BOOKING_STYLES`, and words in Australian English under `ui.wbbooking.*`. No AI.
|
package/README.md
CHANGED
|
@@ -1,3 +1,72 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mccaskie-labs/workbench-bookings
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A bookings calendar for apps built on Workbench, a private platform: customers pick a free time on the app's booking page, give their name and email, and get a confirmation with a calendar file, reminders before it starts, and a private link to move or cancel it. The owner gets an email for each booking, a list in the app, and a feed for their own calendar. Spec: `docs/capabilities/13-bookings.md`. Published publicly so Workbench projects can install it with no token; it is not supported for other uses.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install @mccaskie-labs/workbench-bookings
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
No AI. Bookings live in the app's own database; email goes through `workbench-email` and the Gateway.
|
|
10
|
+
|
|
11
|
+
## Set up
|
|
12
|
+
|
|
13
|
+
1. `npx workbench-bookings init` writes `workbench/bookings.json`: one 30-minute appointment, weekdays 9 to 5 in Sydney, confirmed at once, a reminder the day before, changes online until 24 hours before. Edit it; the client questions in `capabilities/bookings.json` say how. Set `time_zone` from the country pack and put its public holidays in `closed`.
|
|
14
|
+
2. `npx workbench-bookings generate` checks it and writes `src/bookings.generated.ts` (`BOOKING_CONFIG`). Checks can run `npx workbench-bookings generate --check`.
|
|
15
|
+
3. `npx workbench-bookings migration` writes `bookingsMigration` and its down file. It creates functions, so Control asks for approval even on staging.
|
|
16
|
+
4. Add `"*/15 * * * *"` to `triggers.crons` in `wrangler.jsonc`, and `bookings` to a plan's features in `workbench/plans.json` if the app has plans.
|
|
17
|
+
|
|
18
|
+
## Use
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { createBookings } from '@mccaskie-labs/workbench-bookings'
|
|
22
|
+
import { BOOKING_CONFIG } from './bookings.generated'
|
|
23
|
+
|
|
24
|
+
const bookings = createBookings({
|
|
25
|
+
db: serviceClient, mailer, plans, config: BOOKING_CONFIG,
|
|
26
|
+
business: async (account) => ({ name: 'Harbour Physio', location: '1 The Corso, Manly' }),
|
|
27
|
+
owners: async (account) => [{ email: await ownerEmail(account) }],
|
|
28
|
+
links: { manage: (token) => `https://${host}/b/${token}`, bookings: () => `https://${host}/app/bookings` },
|
|
29
|
+
// Optional: hours and closed days the owner keeps in the app.
|
|
30
|
+
settings: async (account) => ({ closed: await leaveDays(account) }),
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const days = await bookings.slots(account, 'appointment') // free times, a week from today
|
|
34
|
+
const { booking, token } = await bookings.book({ account, service: 'appointment', start: days[0].slots[0].start, customer: { name, email } })
|
|
35
|
+
await bookings.cancel(token) // or move(token, start): from the customer's link
|
|
36
|
+
|
|
37
|
+
// scheduled(), every 15 minutes, and cleanup() once a day:
|
|
38
|
+
ctx.waitUntil(bookings.tick())
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
- `book()` takes the account's lock and counts what overlaps (with each service's `buffer`), so two people never get the last place; a service with `capacity` fills place by place, and with `places` one booking can take several (a member and friends). In a `shared` diary (the default) any booking blocks every other service; `per_service` gives each its own.
|
|
42
|
+
- `confirm: "owner_approves"` makes each booking a request that holds its time until the owner accepts or declines it; one nobody answers in `hold` (2 days) is declined by `tick()`.
|
|
43
|
+
- The manage `token` is the only way to the customer's booking: only its SHA-256 is stored. Put it in the link, never in a log.
|
|
44
|
+
- Emails: the customer's confirmation (with `booking.ics`), request received, confirmed, declined, moved, cancelled and reminders; the owner's new booking, request, move and cancellation (`owner_email: "none"` turns those off). A failed email never undoes a booking (`onEmailError`); each has an Idempotency-Key, so a retried reminder goes once.
|
|
45
|
+
- The owner's calendar feed: `feedToken(account)` makes a secret (a new one retires the old), and `feed(token)` answers the `.ics` for a GET route. It names the service only unless `feed.names` is true.
|
|
46
|
+
- Members: when customers sign in to book, pass the signed-in member's id from the app's own session (`bookingAction(bookings, fields, { account, member: session.userId })`, or `book({ ..., member })`), never from the form. `mine(account, member)` lists their bookings for a My bookings page (`renderMyBookings`), and `cancelAsMember(id, account, member)` cancels one of theirs until `cancel_until` (`"0m"`: up to the start). The emailed link keeps working.
|
|
47
|
+
- `attachments: async (booking) => [{ filename, mimeType, bytes }]` adds the project's files (a tax invoice from workbench-pdf) to the confirmation, after the calendar file. If it fails, the email goes without them and `onEmailError` hears `attachment`.
|
|
48
|
+
- If the app keeps customer records, record the customer when a booking is made, for example `contacts.record({ account, person: { name, email }, from: { kind: 'booking', type: 'booking', id: booking.id } })`. A booking is never consent to marketing.
|
|
49
|
+
|
|
50
|
+
## The pages
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { bookingAction, ownerBookingAction, renderBookingDone, renderBookingForm, renderBookingList, renderBookingManage, renderMyBookings, renderSlotPicker } from '@mccaskie-labs/workbench-bookings'
|
|
54
|
+
|
|
55
|
+
const tz = BOOKING_CONFIG.time_zone
|
|
56
|
+
renderSlotPicker(days, { endpoint: '/book/details', service: 'appointment', title: 'Appointment', timeZone: tz, more: '/book?from=2026-12-07' })
|
|
57
|
+
renderBookingForm({ endpoint: '/book', service, title, start, timeZone: tz, ask: BOOKING_CONFIG.ask, confirm: BOOKING_CONFIG.confirm })
|
|
58
|
+
const r = await bookingAction(bookings, fields, { account }) // after Turnstile; r.body.text and r.body.field when it didn't go through
|
|
59
|
+
renderBookingDone(r.body.booking, { title, timeZone: tz, manage: `/b/${r.body.token}`, ics: `/b/${r.body.token}.ics` })
|
|
60
|
+
renderBookingManage(booking, { endpoint: '/book', token, title, timeZone: tz, changeable: bookings.changeable(booking), move: `/b/${token}/move` })
|
|
61
|
+
// Behind the owner's sign-in:
|
|
62
|
+
renderBookingList(await bookings.list(account), { endpoint: '/app/bookings', timeZone: tz, titleOf: (s) => BOOKING_CONFIG.services[s].title })
|
|
63
|
+
await ownerBookingAction(bookings, fields, account) // approve, decline or cancel
|
|
64
|
+
// A signed-in member's own bookings (bookingAction's cancel takes the booking's id when ctx.member is set):
|
|
65
|
+
renderMyBookings(await bookings.mine(account, member), { endpoint: '/me/bookings', timeZone: tz, titleOf, changeable: bookings.changeable, book: '/book' })
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Every element carries a `wb-booking-*` class with a default style (`BOOKING_STYLES`, written by `npx workbench-ui styles`). Words are in `BOOKING_WORDS` (Australian English) under `ui.wbbooking.*`; pass the project's `t` for other languages, and `locale` for dates (default `en-AU`).
|
|
69
|
+
|
|
70
|
+
## Data
|
|
71
|
+
|
|
72
|
+
`bookings` (service, times, status, places, the signed-in member's id when there is one, the customer's name, email, optional phone and optional note, the manage secret's SHA-256, reminder state) and `booking_feeds` (each account's feed secret's SHA-256), in the app's own database, service role only. `cleanup()` deletes a booking `retention_days` (180) after it ends. Workbench never holds any of it.
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { type Words } from "./format.ts";
|
|
2
|
+
import type { Booking, BookingConfig, Customer, SlotDay, TickResult, WeekHours } from "./types.ts";
|
|
3
|
+
type DbError = {
|
|
4
|
+
message?: string;
|
|
5
|
+
code?: string;
|
|
6
|
+
details?: string;
|
|
7
|
+
} | null;
|
|
8
|
+
/** The part of a Supabase client bookings need. Any supabase-js client fits; use the service client. */
|
|
9
|
+
export interface BookingsClient {
|
|
10
|
+
rpc(fn: string, args: Record<string, unknown>): PromiseLike<{
|
|
11
|
+
data: unknown;
|
|
12
|
+
error: DbError;
|
|
13
|
+
}>;
|
|
14
|
+
}
|
|
15
|
+
/** The part of workbench-email's createMailer(...) bookings use. */
|
|
16
|
+
export interface BookingsMailer {
|
|
17
|
+
send(mail: {
|
|
18
|
+
to: string | string[];
|
|
19
|
+
subject: string;
|
|
20
|
+
text: string;
|
|
21
|
+
template: string;
|
|
22
|
+
replyTo?: string;
|
|
23
|
+
attachments?: {
|
|
24
|
+
filename: string;
|
|
25
|
+
mimeType: string;
|
|
26
|
+
bytes: Uint8Array;
|
|
27
|
+
}[];
|
|
28
|
+
idempotencyKey?: string;
|
|
29
|
+
}): Promise<unknown>;
|
|
30
|
+
}
|
|
31
|
+
/** The part of workbench-plans' createPlans(...) bookings use. */
|
|
32
|
+
export interface BookingsPlans {
|
|
33
|
+
can(account: string, feature: string): Promise<{
|
|
34
|
+
ok: boolean;
|
|
35
|
+
reason?: string;
|
|
36
|
+
}>;
|
|
37
|
+
}
|
|
38
|
+
/** What the emails and calendar files say about the business. */
|
|
39
|
+
export interface Business {
|
|
40
|
+
/** "Harbour Physio". */
|
|
41
|
+
name: string;
|
|
42
|
+
/** Where the booking happens, for the calendar file. */
|
|
43
|
+
location?: string;
|
|
44
|
+
/** Replies to the customer's emails go here. */
|
|
45
|
+
replyTo?: string;
|
|
46
|
+
}
|
|
47
|
+
/** The account's own hours and closed days, when the app keeps them in its database (settings on the owner's page). */
|
|
48
|
+
export interface AccountSettings {
|
|
49
|
+
hours?: WeekHours;
|
|
50
|
+
closed?: readonly string[];
|
|
51
|
+
}
|
|
52
|
+
export interface BookingsOptions {
|
|
53
|
+
/** The service client. */
|
|
54
|
+
db: BookingsClient;
|
|
55
|
+
/** workbench/bookings.json, or BOOKING_CONFIG from src/bookings.generated.ts. Checked here. */
|
|
56
|
+
config: unknown;
|
|
57
|
+
/** createMailer(...). Without it no email goes; the pages still work. */
|
|
58
|
+
mailer?: BookingsMailer;
|
|
59
|
+
/** createPlans(...): taking a booking needs the account's plan to have the `bookings` feature (plans.feature). */
|
|
60
|
+
plans?: BookingsPlans;
|
|
61
|
+
/** The business's name for emails and calendar files. Default: the account. */
|
|
62
|
+
business?: (account: string) => Promise<Business>;
|
|
63
|
+
/** Who gets the owner's emails. Needed for them. */
|
|
64
|
+
owners?: (account: string) => Promise<readonly {
|
|
65
|
+
email: string;
|
|
66
|
+
}[]>;
|
|
67
|
+
/** The customer's private page for a booking, from its secret (the manage link), and the owner's bookings page. */
|
|
68
|
+
links?: {
|
|
69
|
+
manage?: (token: string) => string;
|
|
70
|
+
bookings?: (account: string) => string;
|
|
71
|
+
};
|
|
72
|
+
/** The account's own hours and closed days (public holidays from the country pack, leave). */
|
|
73
|
+
settings?: (account: string) => Promise<AccountSettings | null>;
|
|
74
|
+
/** For dates and times in emails. Default en-AU. */
|
|
75
|
+
locale?: string;
|
|
76
|
+
/** The project's strings lookup, for the emails' words. */
|
|
77
|
+
t?: Words;
|
|
78
|
+
/** Files to attach to the customer's confirmation (booked or confirmed), besides the calendar file: a tax invoice from workbench-pdf. A failure here sends the email without them and is reported. */
|
|
79
|
+
attachments?: (b: Booking) => Promise<readonly {
|
|
80
|
+
filename: string;
|
|
81
|
+
mimeType: string;
|
|
82
|
+
bytes: Uint8Array;
|
|
83
|
+
}[]>;
|
|
84
|
+
/** An email that failed: the booking stands. Default: console.error with no address in it. */
|
|
85
|
+
onEmailError?: (error: unknown, what: string) => void;
|
|
86
|
+
/** How many bookings one tick handles at most. Default 200. */
|
|
87
|
+
batch?: number;
|
|
88
|
+
/** The clock, for tests. */
|
|
89
|
+
now?: () => Date;
|
|
90
|
+
}
|
|
91
|
+
export interface BookRequest {
|
|
92
|
+
account: string;
|
|
93
|
+
service: string;
|
|
94
|
+
/** A slot's start, as slots() gave it. */
|
|
95
|
+
start: string | Date;
|
|
96
|
+
customer: Customer;
|
|
97
|
+
/** How many places, 1 to the service's places. Default 1. */
|
|
98
|
+
places?: number;
|
|
99
|
+
/** The signed-in member's id from the app's own sign-in, when they're signed in. Never from the form. */
|
|
100
|
+
member?: string;
|
|
101
|
+
}
|
|
102
|
+
export interface Bookings {
|
|
103
|
+
/** The free times for a service, day by day, from `from` (default today) for `days` (default 7). */
|
|
104
|
+
slots(account: string, service: string, opts?: {
|
|
105
|
+
from?: string;
|
|
106
|
+
days?: number;
|
|
107
|
+
}): Promise<SlotDay[]>;
|
|
108
|
+
/** Books a free time. Answers the booking and the secret for its manage link (kept nowhere else: put it in the link, never in a log). */
|
|
109
|
+
book(req: BookRequest): Promise<{
|
|
110
|
+
booking: Booking;
|
|
111
|
+
token: string;
|
|
112
|
+
}>;
|
|
113
|
+
/** One booking, or null. */
|
|
114
|
+
get(id: string): Promise<Booking | null>;
|
|
115
|
+
/** The booking a manage link's secret belongs to, or null. */
|
|
116
|
+
byToken(token: string): Promise<Booking | null>;
|
|
117
|
+
/** Whether the customer can still change or cancel it online (cancel_until). */
|
|
118
|
+
changeable(b: Booking): boolean;
|
|
119
|
+
/** The customer cancels from their manage link. */
|
|
120
|
+
cancel(token: string): Promise<Booking>;
|
|
121
|
+
/** The customer moves it to another free time from their manage link. */
|
|
122
|
+
move(token: string, start: string | Date): Promise<Booking>;
|
|
123
|
+
/** The owner accepts a request (confirm: owner_approves). */
|
|
124
|
+
approve(id: string, account: string): Promise<Booking>;
|
|
125
|
+
/** The owner declines a request. */
|
|
126
|
+
decline(id: string, account: string, reason?: string): Promise<Booking>;
|
|
127
|
+
/** The owner cancels a booking. */
|
|
128
|
+
cancelByOwner(id: string, account: string, reason?: string): Promise<Booking>;
|
|
129
|
+
/** A signed-in member's bookings that haven't ended (with `past`, from 30 days back), every status, earliest first. */
|
|
130
|
+
mine(account: string, member: string, opts?: {
|
|
131
|
+
past?: boolean;
|
|
132
|
+
}): Promise<Booking[]>;
|
|
133
|
+
/** A signed-in member cancels one of their own bookings (until cancel_until). Ask the app's sign-in for a fresh code first when the project wants it. */
|
|
134
|
+
cancelAsMember(id: string, account: string, member: string): Promise<Booking>;
|
|
135
|
+
/** The account's bookings from `from` (default today) for `days` (default 14), every status unless open. */
|
|
136
|
+
list(account: string, opts?: {
|
|
137
|
+
from?: string;
|
|
138
|
+
days?: number;
|
|
139
|
+
open?: boolean;
|
|
140
|
+
}): Promise<Booking[]>;
|
|
141
|
+
/** The booking as a calendar file, for "Add to my calendar" (from its manage link's secret). */
|
|
142
|
+
icsFor(token: string): Promise<string | null>;
|
|
143
|
+
/** Makes the account a new calendar feed secret; the old feed address stops working. */
|
|
144
|
+
feedToken(account: string): Promise<string>;
|
|
145
|
+
/** The calendar feed a feed secret belongs to, or null. */
|
|
146
|
+
feed(token: string): Promise<string | null>;
|
|
147
|
+
/** The cron's scan, every 15 minutes: reminders, and requests nobody answered. Safe to run twice at once. */
|
|
148
|
+
tick(): Promise<TickResult>;
|
|
149
|
+
/** The daily clean-up: bookings retention_days after they end. */
|
|
150
|
+
cleanup(): Promise<{
|
|
151
|
+
bookings: number;
|
|
152
|
+
}>;
|
|
153
|
+
readonly config: BookingConfig;
|
|
154
|
+
}
|
|
155
|
+
/** The customer's details from the booking form, checked against what bookings.json asks. */
|
|
156
|
+
export declare function customerOf(v: unknown, ask?: BookingConfig["ask"]): Customer;
|
|
157
|
+
/** When the next reminder goes: the earliest reminder time after `after`, before the start. */
|
|
158
|
+
export declare function nextReminder(config: BookingConfig, start: Date, after: Date): Date | null;
|
|
159
|
+
export declare function createBookings(opts: BookingsOptions): Bookings;
|
|
160
|
+
export {};
|