@proveanything/smartlinks 2.1.47 → 2.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.
Files changed (131) hide show
  1. package/README.md +754 -754
  2. package/dist/README.md +573 -573
  3. package/dist/adminCardsView.js +55 -55
  4. package/dist/api/dpp.d.ts +6 -0
  5. package/dist/api/dpp.js +12 -0
  6. package/dist/api/shop.d.ts +5 -1
  7. package/dist/api/shop.js +7 -7
  8. package/dist/api/subscriptions.d.ts +2 -1
  9. package/dist/api/subscriptions.js +4 -12
  10. package/dist/apiVersion.d.ts +9 -0
  11. package/dist/apiVersion.js +15 -0
  12. package/dist/baseline.css +194 -194
  13. package/dist/docs/API_SUMMARY.md +821 -811
  14. package/dist/docs/admin-cards.md +189 -189
  15. package/dist/docs/agent-tools.md +156 -156
  16. package/dist/docs/ai-tools-and-skills.md +180 -180
  17. package/dist/docs/ai.md +1721 -1721
  18. package/dist/docs/analytics.md +39 -39
  19. package/dist/docs/api-versions.md +76 -0
  20. package/dist/docs/app-data-storage.md +277 -277
  21. package/dist/docs/app-docs.md +73 -73
  22. package/dist/docs/app-manifest.md +852 -852
  23. package/dist/docs/app-records-pattern.md +388 -388
  24. package/dist/docs/appConfig.md +550 -550
  25. package/dist/docs/assistant-apps.md +172 -172
  26. package/dist/docs/assistant-context.md +119 -119
  27. package/dist/docs/bookings.md +442 -442
  28. package/dist/docs/comms.md +686 -686
  29. package/dist/docs/connectors.md +340 -340
  30. package/dist/docs/css-baseline.md +133 -133
  31. package/dist/docs/customer-guide.md +70 -70
  32. package/dist/docs/deploying-apps.md +340 -340
  33. package/dist/docs/dpp.md +115 -99
  34. package/dist/docs/entitlements.md +90 -90
  35. package/dist/docs/files.md +69 -69
  36. package/dist/docs/host-dependency-contract.md +210 -210
  37. package/dist/docs/i18n.md +9 -9
  38. package/dist/docs/integrations.md +322 -322
  39. package/dist/docs/interactions.md +545 -545
  40. package/dist/docs/item-context.md +181 -181
  41. package/dist/docs/knowledge.md +102 -102
  42. package/dist/docs/lots.md +111 -111
  43. package/dist/docs/loyalty.md +352 -352
  44. package/dist/docs/members.md +183 -183
  45. package/dist/docs/memberships.md +149 -149
  46. package/dist/docs/mobile-admin-container.md +693 -693
  47. package/dist/docs/operations.md +281 -281
  48. package/dist/docs/overview.md +485 -485
  49. package/dist/docs/proof-comms-triggers.md +142 -142
  50. package/dist/docs/proof-ownership-transfer.md +155 -155
  51. package/dist/docs/proof-product-data-scoping.md +462 -462
  52. package/dist/docs/proof-share-grants.md +305 -305
  53. package/dist/docs/realtime.md +138 -138
  54. package/dist/docs/records-admin-pattern.md +304 -304
  55. package/dist/docs/server-functions.md +1 -1
  56. package/dist/docs/service-accounts.md +64 -64
  57. package/dist/docs/standard-query.md +187 -187
  58. package/dist/docs/tag-codes.md +97 -97
  59. package/dist/docs/theme-tokens.md +252 -252
  60. package/dist/docs/wallet-passes.md +107 -107
  61. package/dist/docs/websites.md +123 -123
  62. package/dist/http.d.ts +14 -0
  63. package/dist/http.js +65 -3
  64. package/dist/index.d.ts +2 -1
  65. package/dist/index.js +3 -1
  66. package/dist/openapi.yaml +6 -3
  67. package/dist/theme-boot.js +82 -82
  68. package/dist/theme.css +75 -75
  69. package/dist/types/dpp.d.ts +14 -5
  70. package/dist/utils/index.d.ts +1 -0
  71. package/dist/utils/index.js +1 -0
  72. package/dist/utils/money.d.ts +15 -0
  73. package/dist/utils/money.js +72 -0
  74. package/dist/version.d.ts +1 -0
  75. package/dist/version.js +2 -0
  76. package/docs/API_SUMMARY.md +821 -811
  77. package/docs/admin-cards.md +189 -189
  78. package/docs/agent-tools.md +156 -156
  79. package/docs/ai-tools-and-skills.md +180 -180
  80. package/docs/ai.md +1721 -1721
  81. package/docs/analytics.md +39 -39
  82. package/docs/api-versions.md +76 -0
  83. package/docs/app-data-storage.md +277 -277
  84. package/docs/app-docs.md +73 -73
  85. package/docs/app-manifest.md +852 -852
  86. package/docs/app-records-pattern.md +388 -388
  87. package/docs/appConfig.md +550 -550
  88. package/docs/assistant-apps.md +172 -172
  89. package/docs/assistant-context.md +119 -119
  90. package/docs/bookings.md +442 -442
  91. package/docs/comms.md +686 -686
  92. package/docs/connectors.md +340 -340
  93. package/docs/css-baseline.md +133 -133
  94. package/docs/customer-guide.md +70 -70
  95. package/docs/deploying-apps.md +340 -340
  96. package/docs/dpp.md +115 -99
  97. package/docs/entitlements.md +90 -90
  98. package/docs/files.md +69 -69
  99. package/docs/host-dependency-contract.md +210 -210
  100. package/docs/i18n.md +9 -9
  101. package/docs/integrations.md +322 -322
  102. package/docs/interactions.md +545 -545
  103. package/docs/item-context.md +181 -181
  104. package/docs/knowledge.md +102 -102
  105. package/docs/lots.md +111 -111
  106. package/docs/loyalty.md +352 -352
  107. package/docs/members.md +183 -183
  108. package/docs/memberships.md +149 -149
  109. package/docs/mobile-admin-container.md +693 -693
  110. package/docs/operations.md +281 -281
  111. package/docs/overview.md +485 -485
  112. package/docs/proof-comms-triggers.md +142 -142
  113. package/docs/proof-ownership-transfer.md +155 -155
  114. package/docs/proof-product-data-scoping.md +462 -462
  115. package/docs/proof-share-grants.md +305 -305
  116. package/docs/realtime.md +138 -138
  117. package/docs/server-functions.md +1 -1
  118. package/docs/service-accounts.md +64 -64
  119. package/docs/standard-query.md +187 -187
  120. package/docs/tag-codes.md +97 -97
  121. package/docs/theme-lookbook.v1.json +120 -120
  122. package/docs/theme-tokens.md +252 -252
  123. package/docs/wallet-passes.md +107 -107
  124. package/docs/websites.md +123 -123
  125. package/openapi.yaml +6 -3
  126. package/package.json +87 -87
  127. package/scripts/headless-check.mjs +87 -87
  128. package/scripts/lib/bare-imports.mjs +57 -57
  129. package/scripts/lib/keyed-app-data.mjs +21 -21
  130. package/scripts/publish.mjs +272 -272
  131. package/scripts/register-release.mjs +91 -91
package/docs/bookings.md CHANGED
@@ -1,442 +1,442 @@
1
- # Bookings: slots, bookings and allocations
2
-
3
- Generic booking objects for **any** booking app: event tickets, classes, tours, table reservations,
4
- one-to-one appointments, room and equipment hire. The platform gives you the objects and the
5
- guarantees. Your app owns the rest: schedule, policy, pricing, payment and UI.
6
-
7
- ```ts
8
- import { bookings, utils } from '@proveanything/smartlinks'
9
- ```
10
-
11
- ---
12
-
13
- ## What the platform guarantees (and what it doesn't)
14
-
15
- | The platform | Your app |
16
- |---|---|
17
- | **No double-booking** of a person / room / table, however many requests arrive at once | The schedule (opening hours) and its UI |
18
- | **No overselling** a slot's capacity | Booking policy: notice, durations, who may book |
19
- | **Atomic** writes: a booking with several parts (a stylist *and* a chair) succeeds whole or not at all | Pricing, ticket types, payment or credits, refunds |
20
- | **Idempotent** creates (`idempotencyKey`): a retried request never books twice | Queues, waiting rooms, throttling for high-demand sales |
21
- | **Holds** that expire and give the place back | Confirmation / reminder content (flows on booking events) |
22
- | Who may book publicly, by **per-app rules** (like records / cases) | |
23
-
24
- ---
25
-
26
- ## The three objects
27
-
28
- - **Slot** — a time window with a **capacity**: an event performance, a class, a tour at 2pm. Seats
29
- are counted (`booked`, `remaining`).
30
- - **Booking** — the customer's reservation and its lifecycle. This is what "my bookings" lists.
31
- - **Allocation** — what a booking actually claims: seats on a slot (`mode: 'capacity'`) or
32
- exclusive time on a resource (`mode: 'exclusive'`). Most apps never touch these directly: they're
33
- returned on `booking.allocations`.
34
-
35
- **Resources are not a new object.** Staff, rooms, tables and equipment are whatever you already use —
36
- app records, contacts, products — referenced by type + id: `{ resourceType: 'record', resourceId: 'staff-sam' }`.
37
-
38
- ### Booking statuses
39
-
40
- | Status | Holds a place? | Meaning |
41
- |---|---|---|
42
- | `held` | yes, until `holdExpiresAt` | Claimed while the customer pays / confirms; expires automatically |
43
- | `pending` | yes | Claimed, waiting for the business to approve |
44
- | `confirmed` | yes | Booked |
45
- | `completed` / `no_show` | yes (the time was used) | Outcome after the event (admin) |
46
- | `cancelled` | no | `cancelReason`: `admin`, `owner`, `hold_expired` or your own |
47
- | `waitlisted` | no | Waiting; confirming it claims the place if one is free |
48
-
49
- ---
50
-
51
- ## Events and classes — slots
52
-
53
- ```ts
54
- // Admin (Studio, or your app's server function): publish the sessions
55
- const slot = await bookings.slots.create(collectionId, appId, {
56
- startsAt: '2026-11-14T19:30:00Z', endsAt: '2026-11-14T21:00:00Z',
57
- capacity: 200,
58
- perContactLimit: 4, // max 4 seats per person on this slot, enforced atomically
59
- data: { title: 'Autumn concert' },
60
- })
61
-
62
- // A class that also needs its instructor free: the slot blocks the instructor's time too
63
- await bookings.slots.create(collectionId, appId, {
64
- startsAt, endsAt, capacity: 12,
65
- resourceType: 'record', resourceId: 'instructor-ana', blockResource: true,
66
- })
67
-
68
- // A series, all-or-nothing (max 500)
69
- await bookings.slots.createMany(collectionId, appId, sessions, { seriesId })
70
-
71
- // Public: open slots with places left
72
- const { data } = await bookings.slots.list(collectionId, appId, { from, to, available: true })
73
- data[0].remaining
74
- ```
75
-
76
- Book seats:
77
-
78
- ```ts
79
- const booking = await bookings.create(collectionId, appId, { slotId: slot.id, quantity: 2, idempotencyKey })
80
- ```
81
-
82
- Recurring events are **expanded into concrete slots** (`seriesId`); there's no recurrence rule
83
- stored on a slot.
84
-
85
- ---
86
-
87
- ## Appointments — exclusive time on a resource
88
-
89
- ```ts
90
- await bookings.create(collectionId, appId, {
91
- resourceType: 'record', resourceId: 'staff-sam',
92
- startsAt: '2026-11-03T10:00:00Z', endsAt: '2026-11-03T10:30:00Z',
93
- bufferAfter: 15, // admin: blocks 10:30–10:45 too (clean-up); the booking still shows 10:00–10:30
94
- }, true)
95
- ```
96
-
97
- Several resources at once — all-or-nothing:
98
-
99
- ```ts
100
- await bookings.create(collectionId, appId, {
101
- startsAt, endsAt,
102
- allocations: [
103
- { resourceType: 'record', resourceId: 'stylist-jo' },
104
- { resourceType: 'record', resourceId: 'chair-2' },
105
- ],
106
- }, true)
107
- ```
108
-
109
- A pool with no fixed times (3 kayaks, bookable any time) is three resources; your app picks a free one.
110
-
111
- ### Blocked time
112
-
113
- ```ts
114
- await bookings.blocks.create(collectionId, appId, { resourceType: 'record', resourceId: 'staff-sam', startsAt, endsAt, data: { reason: 'training' } })
115
- ```
116
-
117
- Planned time off usually belongs in your schedule (an override); blocks are for ad-hoc blocked time.
118
-
119
- ---
120
-
121
- ## Schedules and free times
122
-
123
- **Your app owns the schedule.** Store it however you like — usually an app record. For the time maths,
124
- the SDK ships a pure helper:
125
-
126
- ```ts
127
- const schedule = {
128
- timezone: 'Europe/London',
129
- weekly: { mon: [{ start: '09:00', end: '12:30' }, { start: '13:30', end: '17:00' }], tue: [{ start: '09:00', end: '17:00' }] },
130
- overrides: [{ from: '2026-12-24', to: '2026-12-28', hours: [] }, { date: '2026-11-07', hours: [{ start: '10:00', end: '13:00' }] }],
131
- }
132
-
133
- const fb = await bookings.freebusy(collectionId, appId, { resources: ['record:staff-sam'], from, to }, true)
134
- const times = utils.freeTimes({
135
- hours: schedule, busy: fb.resources[0].busy,
136
- from, to, duration: 30, step: 30, bufferAfter: 15, minNotice: 240,
137
- })
138
- // → [{ start, end }, …] in UTC; render in the booker's timezone
139
- ```
140
-
141
- - Times are **local wall-clock** in `timezone`, so "9 to 5" stays 9 to 5 across daylight-saving changes.
142
- - A missing day is closed; hours never cross midnight (split them); `end` may be `'24:00'`.
143
- - `overrides` win over `weekly` (`hours: []` = closed).
144
- - Also exported: `utils.withinSchedule`, `utils.openIntervals`, `utils.subtractIntervals`, `utils.validateSchedule`.
145
-
146
- `freebusy` returns busy intervals only (bookings, blocks, slots that block a resource) — never who
147
- booked. It's collection-wide: a resource is busy whichever app booked it.
148
-
149
- ---
150
-
151
- ## Who can book — per-app rules
152
-
153
- Exactly like records, cases and threads:
154
-
155
- - **Admin calls are unconditional** — Studio users, and **your app's own server functions** (they call
156
- the admin routes with your app's authority). Pass `admin = true`.
157
- - **Public calls** (`admin = false`, the default for `create`) follow your app's config in
158
- `sites/{collectionId}/apps/{appId}` (`POST /admin/collection/:cId/apps/:appId`). With no config,
159
- **public booking is refused** — only admins (and your functions) can book.
160
-
161
- Pick a mode per app:
162
-
163
- | Mode | Config | Suits |
164
- |---|---|---|
165
- | **Public booking** | `publicCreate.bookings` allows anonymous and/or signed-in callers | "Book an appointment with me", simple events — no server code |
166
- | **Admin-only booking** | no `publicCreate.bookings` | Extra validation, queues, payment-first, high demand: book through your server function |
167
-
168
- ### `publicCreate.bookings`
169
-
170
- ```json
171
- {
172
- "publicCreate": {
173
- "bookings": {
174
- "anonymous": {
175
- "allow": true,
176
- "enforce": { "status": "held" },
177
- "limits": { "maxQuantity": 4, "holdMinutes": 15 },
178
- "rules": { "targets": ["slots"], "minNoticeMinutes": 60, "horizonDays": 90 },
179
- "edit": { "editToken": true }
180
- },
181
- "authenticated": {
182
- "allow": true,
183
- "enforce": { "status": "confirmed", "visibility": "owner" },
184
- "rules": {
185
- "targets": ["slots", "resources"],
186
- "schedule": true,
187
- "resourceTypes": ["record"],
188
- "durations": [30, 60],
189
- "minNoticeMinutes": 240,
190
- "bufferAfter": 15
191
- }
192
- }
193
- }
194
- }
195
- }
196
- ```
197
-
198
- | Field | Meaning |
199
- |---|---|
200
- | `enforce.status` | `confirmed` (instant), `pending` (business approves), `held` (pay / confirm next). Default `pending`. The caller can't override it. |
201
- | `enforce.visibility` | Default: `owner` for signed-in, `admin` for anonymous |
202
- | `limits.maxQuantity` | Max seats / claims per booking |
203
- | `limits.holdMinutes` | Hold length for public `held` bookings (default 15, max 60) |
204
- | `rules.targets` | `slots` (only your published slots) and/or `resources` (free-form time). Default `['slots']` |
205
- | `rules.schedule` | **Required for `resources`.** The platform checks the requested time against the resource's schedule: an app record of **your app** with `recordType: 'availability'`, `parentType` / `parentId` = the resource, `data` in the standard shape above |
206
- | `rules.resourceTypes` | Which resource types the public may book |
207
- | `rules.durations` | Allowed lengths in minutes |
208
- | `rules.minNoticeMinutes`, `rules.horizonDays` | How soon / how far ahead |
209
- | `rules.bufferBefore`, `rules.bufferAfter` | Applied to every public booking (the caller can't set buffers) |
210
- | `edit.editToken` | Anonymous only: the create response carries `editToken` **once** — store it so the guest can manage the booking. `edit.windowMinutes` limits how long it works |
211
-
212
- Public callers can't set `contactId` (signed-in callers get their own contact; guests none), the
213
- admin zone, buffers, `customId` or `sourceSystem`.
214
-
215
- ### `ownerActions.bookings` — what the booker may do afterwards
216
-
217
- ```json
218
- {
219
- "ownerActions": {
220
- "bookings": {
221
- "cancel": { "allow": true, "cutoffMinutes": 1440 },
222
- "move": { "allow": true, "cutoffMinutes": 1440 },
223
- "confirm": { "allow": true }
224
- }
225
- }
226
- }
227
- ```
228
-
229
- Default: nothing. `cutoffMinutes` = not within that time of the start (`CUTOFF_PASSED`). `move`
230
- re-checks the create rules for the new time. `confirm` turns the booker's own **held** booking into
231
- `confirmed` — only for apps that take no payment; `pending` and `waitlisted` are the business's to
232
- confirm.
233
-
234
- ```ts
235
- // Signed-in booker
236
- await bookings.cancel(collectionId, appId, bookingId)
237
- const mine = await bookings.mine(collectionId, appId, { from: new Date() })
238
-
239
- // Guest, with the token from create
240
- const b = await bookings.get(collectionId, appId, bookingId, false, { editToken })
241
- await bookings.move(collectionId, appId, bookingId, { slotId: other.id }, false, { editToken })
242
- ```
243
-
244
- ### `publicRead.bookings.freebusy`
245
-
246
- `{ "publicRead": { "bookings": { "freebusy": true } } }` lets public pages call `bookings.freebusy`.
247
- Off by default — most apps serve free times through their own function.
248
-
249
- ---
250
-
251
- ## In a server function — `ctx.sl.bookings`
252
-
253
- When public booking is off, your app books through its own server function. `ctx.sl.bookings` is
254
- the same API, admin-side, for **your app's** bookings only (`sl:bookings:read` / `sl:bookings:write`):
255
-
256
- ```ts
257
- // manifest: { name: 'book', trigger: { type: 'http' }, visibility: 'public', authority: 'collection',
258
- // capabilities: ['sl:bookings:write', 'sl:contacts:write'] }
259
- export async function book(ctx, req) {
260
- const me = await ctx.sl.contacts.forCaller() // null when signed out
261
- // … your rules: hours (utils.withinSchedule), a queue token, a member level, a price …
262
- const b = await ctx.sl.bookings.create({
263
- resourceType: 'record', resourceId: req.body.staffId,
264
- startsAt: req.body.startsAt, endsAt: req.body.endsAt, bufferAfter: 10,
265
- contactId: me?.contactId, status: 'confirmed', idempotencyKey: req.body.requestId,
266
- })
267
- return Response.json({ id: b.id, startsAt: b.startsAt })
268
- }
269
- ```
270
-
271
- `create`, `get`, `query`, `mine` (the caller's own), `update`, `confirm`, `cancel`, `move`,
272
- `extendHold`, `complete`, `noShow`, `freebusy`, `slots.*`, `blocks.*`. Errors carry `code`,
273
- `status` and `details` exactly as over HTTP. The local test harness
274
- (`@proveanything/smartlinks/testing`) runs bookings in memory with the same rules.
275
-
276
- ---
277
-
278
- ## Payments and high-demand sales
279
-
280
- The platform takes no payments. It gives you what a payment or credits flow needs:
281
-
282
- 1. Create the booking **`held`** (claims the place; `holdMinutes`).
283
- 2. Take payment. Slow step? `bookings.extendHold(…, minutes)` (admin; capped at 24h from creation).
284
- 3. **Confirm after payment** — from your function or payment webhook handler. Confirm is idempotent, so
285
- a repeated webhook is harmless. A run-out hold fails with `HOLD_EXPIRED`.
286
- 4. If it's never confirmed, the hold expires within a minute: the booking becomes `cancelled`
287
- (`cancelReason: 'hold_expired'`) and the place goes back. Void the payment intent on the
288
- `booking.hold_expired` event.
289
-
290
- Keep payment references, amounts, credits and refunds in the booking's `admin` / `owner` zones.
291
-
292
- **High demand** (queues, lotteries, bot checks): turn public booking **off** so the only way in is
293
- your server function; it checks the queue token / per-person limit / throttle, then creates a short
294
- `held` booking. The platform guarantees nothing is oversold however many requests arrive.
295
-
296
- ---
297
-
298
- ## Errors
299
-
300
- Booking calls fail with HTTP 409 and an `errorCode` you can show or act on:
301
-
302
- | Code | When |
303
- |---|---|
304
- | `SLOT_FULL` | Not enough places left (`details.remaining`) |
305
- | `SLOT_CLOSED` | The slot isn't open |
306
- | `RESOURCE_TAKEN` | The time isn't free on that resource (`details` names it) |
307
- | `PER_CONTACT_LIMIT` / `QUANTITY_LIMIT` | Per-person / per-booking limit |
308
- | `HOLD_EXPIRED` | The hold ran out before confirm / move |
309
- | `INVALID_STATE` | e.g. confirming a cancelled booking |
310
- | `SLOT_OVERBOOKED` / `SLOT_HAS_BOOKINGS` | Lowering capacity below what's booked / changing a booked slot's time or deleting it |
311
- | `TOO_SOON` / `TOO_FAR_AHEAD` / `IN_THE_PAST` / `DURATION_NOT_ALLOWED` | Public time rules |
312
- | `OUTSIDE_SCHEDULE` / `NO_SCHEDULE` / `SCHEDULE_INVALID` | Public schedule check |
313
- | `CUTOFF_PASSED` | Owner action inside its cutoff |
314
-
315
- `403 FORBIDDEN` when the app's rules don't allow the call; `404` for unknown ids.
316
-
317
- ---
318
-
319
- ## Events
320
-
321
- Recorded after each write commits, for flows and app functions (`trigger: { type: 'event', eventTypes: [...] }`):
322
-
323
- `booking.created`, `booking.confirmed`, `booking.cancelled`, `booking.moved`, `booking.completed`,
324
- `booking.no_show`, `booking.hold_expired`, `slot.full`, `slot.place_freed` (for waitlist promotion).
325
- Context: `{ appId, status, slotIds }` (bookings) / `{ appId, remaining }` (slots).
326
-
327
- Reminders before a booking (`booking.starts_in`) are coming; until then, send a reminder template to
328
- tomorrow's bookers with `bookings.message` (below) from a cron function.
329
-
330
- ---
331
-
332
- ## Confirmations and messages
333
-
334
- Nothing is emailed by default: your app decides what goes out, in **flows on booking events**, with
335
- **templates the business can reword in Studio** (Comms → Templates). The platform does the sending,
336
- the hydration and the consent.
337
-
338
- ### Flows on booking events
339
-
340
- A site, or an app on behalf of a business, sets them up through the cloud blueprint
341
- (`cloud/flows.json` in a site; `POST /admin/collection/:cId/blueprint/apply` with
342
- `{ appId, blueprint: { templates, flows } }` from an app's admin screen). Additive: re-applying never
343
- removes what is there, and a template edited in Studio keeps its edits unless the blueprint sends new
344
- wording.
345
-
346
- ```json
347
- {
348
- "templates": {
349
- "booking-confirmed": { "name": "You're booked", "email": { "subject": "{{ booking.serviceName }} on {{ booking.when.date }}", "body": "<p>Hi {{ contact.firstName }}, see you at {{ booking.when.time }}.</p>" }, "sms": { "body": "Booked: {{ booking.when.shortDate }} {{ booking.when.time }}" } },
350
- "booking-requested": { "name": "Request received", "email": { "subject": "We got your request", "body": "<p>We'll confirm shortly.</p>" } },
351
- "staff-new-booking": { "name": "You've been booked", "email": { "subject": "New appointment", "body": "<p>{{ booking.customer.name }}, {{ booking.when.dateTime }}</p>" } }
352
- },
353
- "flows": [
354
- { "when": { "event": "booking.confirmed" }, "do": [{ "send": "booking-confirmed", "to": "contact" }, { "send": "staff-new-booking", "to": "resource" }, { "send": "team-new-booking", "to": "team" }] },
355
- { "when": { "event": "booking.created", "status": "pending" }, "do": [{ "send": "booking-requested", "to": "contact" }] }
356
- ]
357
- }
358
- ```
359
-
360
- - `when.event`: `booking.created` / `confirmed` / `cancelled` / `moved` / `completed` / `no_show` /
361
- `hold_expired`, `slot.full` / `slot.place_freed`; `when.status` narrows to the booking's status at the
362
- time (`"pending"` for "request received").
363
- - `to`: `contact` — the booker, by their preferred channel (`channel` to force one; a template needs an
364
- `sms` part to go by text); `team` — the collection's team addresses; an **address** (`"hello@bloom.com"`,
365
- or a list of up to 10) — a particular inbox, written out: never built from what someone sent
366
- (`{{ … }}` is refused), at most 200 a day per template; `resource` — the booked
367
- person or room: a resource that is a contact, or a record with `data.email` / `data.contactId`.
368
- A **public** resource record (a staff profile the booking page lists) keeps its address on a
369
- **private child record** instead (`visibility: 'admin'`, `parentType: 'record'`, `parentId` = the
370
- resource) — the platform looks there too, and the address never ships with the public listing.
371
- - Any step can also take `replyTo` — an address, or `"contact"` (replies go to the person the event is
372
- about: a team notification answered straight to the booker) — and `cc` / `bcc` (written-out addresses,
373
- same rules as `to`, counted in the same daily cap; over it the person still gets their email, without the
374
- copies). A site's `cloud/email.json` (`{ "fromName", "replyTo" }`) sets the name the business's emails come
375
- from and their default reply-to; the From address is the business's verified domain.
376
- - `attach` on a step adds files (at most 5, 10 MB in all): `"calendar"` — the booking as an `.ics` invite
377
- that drops straight into the person's calendar (a cancelled booking's invite cancels it there);
378
- `{ "asset": "<assetId>" }` — a file in the collection's assets (terms, a price list);
379
- `{ "function": "<name>" }` — your app's own function makes it (a receipt PDF: server-functions.md,
380
- "Making an email attachment"). E.g. `{ "send": "booking-confirmed", "to": "contact", "attach": ["calendar"] }`.
381
- One that can't be made is left out; the email still goes.
382
- - Who gets what, in templates: `{{ booking }}` is the booking **ready-formatted**: its fields and
383
- `data`, plus `serviceName`, `staffName`, `customer`, `durationMinutes`, `when` (`date`,
384
- `shortDate`, `time`, `endTime`, `dateTime`, `timezone` — in the business's timezone) and, when the
385
- recipient's timezone is known and differs, `whenFor` (the same in theirs); `editToken` only when the
386
- recipient is the booker (a guest's manage link). `{{ contact }}` is the recipient; `{{ appConfig }}`
387
- the app's public config (a booking page's address, the business name) for sends to the contact.
388
-
389
- ### Who is reachable
390
-
391
- A **guest** who books with an email or phone gets a **contact** (found by that identity or made,
392
- filling only empty fields: a form's details never overwrite a known contact), and so does a customer
393
- **staff book in by hand** (`customer: { name, email, phone }` on the body or in `data`). Admin
394
- creates may pass `contactId` instead. A booking with no contact gets no messages: show the booker why
395
- an email is worth giving.
396
-
397
- ### Messaging everyone booked — `bookings.message`
398
-
399
- No list to build, no segment to save: send a template to everyone on a slot, or everyone booked in a
400
- window (optionally with one resource), each hydrated with **their own** booking.
401
-
402
- ```ts
403
- // Everyone on tomorrow's 10:00 class
404
- await SL.bookings.message(collectionId, appId, { templateId, slotId })
405
- // Everyone booked with Sam next week, by email even if they prefer texts
406
- await SL.bookings.message(collectionId, appId, { templateId, channel: 'email', from, to, resourceType: 'record', resourceId: samId })
407
- // Everyone on the class, each with their own calendar invite
408
- await SL.bookings.message(collectionId, appId, { templateId, slotId, attach: ['calendar'] })
409
- // → { matched, sent, skipped: { noContact, duplicate, failed }, failures: [{ bookingId, reason }] }
410
- ```
411
-
412
- `attach` takes the same files as `comms.sendTransactional` (comms.md, "Attachments"); `'calendar'` is
413
- each person's own booking.
414
-
415
- Statuses default to held, pending and confirmed; at most 500 recipients per call (narrow the window
416
- for more). One message per contact on a slot, one per booking in a window. Also
417
- `ctx.sl.bookings.message(input)` in a server function (`sl:bookings:write`) — a cron function that
418
- messages tomorrow's bookers is a reminder system in five lines.
419
-
420
- ---
421
-
422
- ## API reference
423
-
424
- All take `(collectionId, appId, …)`; `admin` is the last parameter where both exist.
425
-
426
- | Call | Admin | Public |
427
- |---|---|---|
428
- | `bookings.create(input, admin?)` | any status, any field | per `publicCreate.bookings` |
429
- | `bookings.list(params?)` | ✓ | — (use `mine`) |
430
- | `bookings.mine(params?)` | — | signed-in booker |
431
- | `bookings.get(id, admin?, { editToken }?)` | ✓ | booker / token |
432
- | `bookings.update(id, input)` | ✓ | — |
433
- | `bookings.confirm / cancel / move(…, admin?, { editToken }?)` | ✓ | per `ownerActions.bookings` |
434
- | `bookings.extendHold / complete / noShow` | ✓ | — |
435
- | `bookings.freebusy(q, admin?)` | ✓ | if `publicRead.bookings.freebusy` |
436
- | `bookings.message(input)` | ✓ | — |
437
- | `bookings.slots.create / createMany / update / remove` | ✓ | — |
438
- | `bookings.slots.list / get(…, admin?)` | ✓ | open, public slots |
439
- | `bookings.blocks.create / list / remove` | ✓ | — |
440
- | `utils.freeTimes / withinSchedule / openIntervals / subtractIntervals / validateSchedule` | pure helpers | |
441
-
442
- REST: `/{admin|public}/collection/:cId/app/:appId/bookings[/slots|/blocks|/freebusy|/mine|/:id[/confirm|/cancel|/move|/extend-hold|/complete|/no-show]]`.
1
+ # Bookings: slots, bookings and allocations
2
+
3
+ Generic booking objects for **any** booking app: event tickets, classes, tours, table reservations,
4
+ one-to-one appointments, room and equipment hire. The platform gives you the objects and the
5
+ guarantees. Your app owns the rest: schedule, policy, pricing, payment and UI.
6
+
7
+ ```ts
8
+ import { bookings, utils } from '@proveanything/smartlinks'
9
+ ```
10
+
11
+ ---
12
+
13
+ ## What the platform guarantees (and what it doesn't)
14
+
15
+ | The platform | Your app |
16
+ |---|---|
17
+ | **No double-booking** of a person / room / table, however many requests arrive at once | The schedule (opening hours) and its UI |
18
+ | **No overselling** a slot's capacity | Booking policy: notice, durations, who may book |
19
+ | **Atomic** writes: a booking with several parts (a stylist *and* a chair) succeeds whole or not at all | Pricing, ticket types, payment or credits, refunds |
20
+ | **Idempotent** creates (`idempotencyKey`): a retried request never books twice | Queues, waiting rooms, throttling for high-demand sales |
21
+ | **Holds** that expire and give the place back | Confirmation / reminder content (flows on booking events) |
22
+ | Who may book publicly, by **per-app rules** (like records / cases) | |
23
+
24
+ ---
25
+
26
+ ## The three objects
27
+
28
+ - **Slot** — a time window with a **capacity**: an event performance, a class, a tour at 2pm. Seats
29
+ are counted (`booked`, `remaining`).
30
+ - **Booking** — the customer's reservation and its lifecycle. This is what "my bookings" lists.
31
+ - **Allocation** — what a booking actually claims: seats on a slot (`mode: 'capacity'`) or
32
+ exclusive time on a resource (`mode: 'exclusive'`). Most apps never touch these directly: they're
33
+ returned on `booking.allocations`.
34
+
35
+ **Resources are not a new object.** Staff, rooms, tables and equipment are whatever you already use —
36
+ app records, contacts, products — referenced by type + id: `{ resourceType: 'record', resourceId: 'staff-sam' }`.
37
+
38
+ ### Booking statuses
39
+
40
+ | Status | Holds a place? | Meaning |
41
+ |---|---|---|
42
+ | `held` | yes, until `holdExpiresAt` | Claimed while the customer pays / confirms; expires automatically |
43
+ | `pending` | yes | Claimed, waiting for the business to approve |
44
+ | `confirmed` | yes | Booked |
45
+ | `completed` / `no_show` | yes (the time was used) | Outcome after the event (admin) |
46
+ | `cancelled` | no | `cancelReason`: `admin`, `owner`, `hold_expired` or your own |
47
+ | `waitlisted` | no | Waiting; confirming it claims the place if one is free |
48
+
49
+ ---
50
+
51
+ ## Events and classes — slots
52
+
53
+ ```ts
54
+ // Admin (Studio, or your app's server function): publish the sessions
55
+ const slot = await bookings.slots.create(collectionId, appId, {
56
+ startsAt: '2026-11-14T19:30:00Z', endsAt: '2026-11-14T21:00:00Z',
57
+ capacity: 200,
58
+ perContactLimit: 4, // max 4 seats per person on this slot, enforced atomically
59
+ data: { title: 'Autumn concert' },
60
+ })
61
+
62
+ // A class that also needs its instructor free: the slot blocks the instructor's time too
63
+ await bookings.slots.create(collectionId, appId, {
64
+ startsAt, endsAt, capacity: 12,
65
+ resourceType: 'record', resourceId: 'instructor-ana', blockResource: true,
66
+ })
67
+
68
+ // A series, all-or-nothing (max 500)
69
+ await bookings.slots.createMany(collectionId, appId, sessions, { seriesId })
70
+
71
+ // Public: open slots with places left
72
+ const { data } = await bookings.slots.list(collectionId, appId, { from, to, available: true })
73
+ data[0].remaining
74
+ ```
75
+
76
+ Book seats:
77
+
78
+ ```ts
79
+ const booking = await bookings.create(collectionId, appId, { slotId: slot.id, quantity: 2, idempotencyKey })
80
+ ```
81
+
82
+ Recurring events are **expanded into concrete slots** (`seriesId`); there's no recurrence rule
83
+ stored on a slot.
84
+
85
+ ---
86
+
87
+ ## Appointments — exclusive time on a resource
88
+
89
+ ```ts
90
+ await bookings.create(collectionId, appId, {
91
+ resourceType: 'record', resourceId: 'staff-sam',
92
+ startsAt: '2026-11-03T10:00:00Z', endsAt: '2026-11-03T10:30:00Z',
93
+ bufferAfter: 15, // admin: blocks 10:30–10:45 too (clean-up); the booking still shows 10:00–10:30
94
+ }, true)
95
+ ```
96
+
97
+ Several resources at once — all-or-nothing:
98
+
99
+ ```ts
100
+ await bookings.create(collectionId, appId, {
101
+ startsAt, endsAt,
102
+ allocations: [
103
+ { resourceType: 'record', resourceId: 'stylist-jo' },
104
+ { resourceType: 'record', resourceId: 'chair-2' },
105
+ ],
106
+ }, true)
107
+ ```
108
+
109
+ A pool with no fixed times (3 kayaks, bookable any time) is three resources; your app picks a free one.
110
+
111
+ ### Blocked time
112
+
113
+ ```ts
114
+ await bookings.blocks.create(collectionId, appId, { resourceType: 'record', resourceId: 'staff-sam', startsAt, endsAt, data: { reason: 'training' } })
115
+ ```
116
+
117
+ Planned time off usually belongs in your schedule (an override); blocks are for ad-hoc blocked time.
118
+
119
+ ---
120
+
121
+ ## Schedules and free times
122
+
123
+ **Your app owns the schedule.** Store it however you like — usually an app record. For the time maths,
124
+ the SDK ships a pure helper:
125
+
126
+ ```ts
127
+ const schedule = {
128
+ timezone: 'Europe/London',
129
+ weekly: { mon: [{ start: '09:00', end: '12:30' }, { start: '13:30', end: '17:00' }], tue: [{ start: '09:00', end: '17:00' }] },
130
+ overrides: [{ from: '2026-12-24', to: '2026-12-28', hours: [] }, { date: '2026-11-07', hours: [{ start: '10:00', end: '13:00' }] }],
131
+ }
132
+
133
+ const fb = await bookings.freebusy(collectionId, appId, { resources: ['record:staff-sam'], from, to }, true)
134
+ const times = utils.freeTimes({
135
+ hours: schedule, busy: fb.resources[0].busy,
136
+ from, to, duration: 30, step: 30, bufferAfter: 15, minNotice: 240,
137
+ })
138
+ // → [{ start, end }, …] in UTC; render in the booker's timezone
139
+ ```
140
+
141
+ - Times are **local wall-clock** in `timezone`, so "9 to 5" stays 9 to 5 across daylight-saving changes.
142
+ - A missing day is closed; hours never cross midnight (split them); `end` may be `'24:00'`.
143
+ - `overrides` win over `weekly` (`hours: []` = closed).
144
+ - Also exported: `utils.withinSchedule`, `utils.openIntervals`, `utils.subtractIntervals`, `utils.validateSchedule`.
145
+
146
+ `freebusy` returns busy intervals only (bookings, blocks, slots that block a resource) — never who
147
+ booked. It's collection-wide: a resource is busy whichever app booked it.
148
+
149
+ ---
150
+
151
+ ## Who can book — per-app rules
152
+
153
+ Exactly like records, cases and threads:
154
+
155
+ - **Admin calls are unconditional** — Studio users, and **your app's own server functions** (they call
156
+ the admin routes with your app's authority). Pass `admin = true`.
157
+ - **Public calls** (`admin = false`, the default for `create`) follow your app's config in
158
+ `sites/{collectionId}/apps/{appId}` (`POST /admin/collection/:cId/apps/:appId`). With no config,
159
+ **public booking is refused** — only admins (and your functions) can book.
160
+
161
+ Pick a mode per app:
162
+
163
+ | Mode | Config | Suits |
164
+ |---|---|---|
165
+ | **Public booking** | `publicCreate.bookings` allows anonymous and/or signed-in callers | "Book an appointment with me", simple events — no server code |
166
+ | **Admin-only booking** | no `publicCreate.bookings` | Extra validation, queues, payment-first, high demand: book through your server function |
167
+
168
+ ### `publicCreate.bookings`
169
+
170
+ ```json
171
+ {
172
+ "publicCreate": {
173
+ "bookings": {
174
+ "anonymous": {
175
+ "allow": true,
176
+ "enforce": { "status": "held" },
177
+ "limits": { "maxQuantity": 4, "holdMinutes": 15 },
178
+ "rules": { "targets": ["slots"], "minNoticeMinutes": 60, "horizonDays": 90 },
179
+ "edit": { "editToken": true }
180
+ },
181
+ "authenticated": {
182
+ "allow": true,
183
+ "enforce": { "status": "confirmed", "visibility": "owner" },
184
+ "rules": {
185
+ "targets": ["slots", "resources"],
186
+ "schedule": true,
187
+ "resourceTypes": ["record"],
188
+ "durations": [30, 60],
189
+ "minNoticeMinutes": 240,
190
+ "bufferAfter": 15
191
+ }
192
+ }
193
+ }
194
+ }
195
+ }
196
+ ```
197
+
198
+ | Field | Meaning |
199
+ |---|---|
200
+ | `enforce.status` | `confirmed` (instant), `pending` (business approves), `held` (pay / confirm next). Default `pending`. The caller can't override it. |
201
+ | `enforce.visibility` | Default: `owner` for signed-in, `admin` for anonymous |
202
+ | `limits.maxQuantity` | Max seats / claims per booking |
203
+ | `limits.holdMinutes` | Hold length for public `held` bookings (default 15, max 60) |
204
+ | `rules.targets` | `slots` (only your published slots) and/or `resources` (free-form time). Default `['slots']` |
205
+ | `rules.schedule` | **Required for `resources`.** The platform checks the requested time against the resource's schedule: an app record of **your app** with `recordType: 'availability'`, `parentType` / `parentId` = the resource, `data` in the standard shape above |
206
+ | `rules.resourceTypes` | Which resource types the public may book |
207
+ | `rules.durations` | Allowed lengths in minutes |
208
+ | `rules.minNoticeMinutes`, `rules.horizonDays` | How soon / how far ahead |
209
+ | `rules.bufferBefore`, `rules.bufferAfter` | Applied to every public booking (the caller can't set buffers) |
210
+ | `edit.editToken` | Anonymous only: the create response carries `editToken` **once** — store it so the guest can manage the booking. `edit.windowMinutes` limits how long it works |
211
+
212
+ Public callers can't set `contactId` (signed-in callers get their own contact; guests none), the
213
+ admin zone, buffers, `customId` or `sourceSystem`.
214
+
215
+ ### `ownerActions.bookings` — what the booker may do afterwards
216
+
217
+ ```json
218
+ {
219
+ "ownerActions": {
220
+ "bookings": {
221
+ "cancel": { "allow": true, "cutoffMinutes": 1440 },
222
+ "move": { "allow": true, "cutoffMinutes": 1440 },
223
+ "confirm": { "allow": true }
224
+ }
225
+ }
226
+ }
227
+ ```
228
+
229
+ Default: nothing. `cutoffMinutes` = not within that time of the start (`CUTOFF_PASSED`). `move`
230
+ re-checks the create rules for the new time. `confirm` turns the booker's own **held** booking into
231
+ `confirmed` — only for apps that take no payment; `pending` and `waitlisted` are the business's to
232
+ confirm.
233
+
234
+ ```ts
235
+ // Signed-in booker
236
+ await bookings.cancel(collectionId, appId, bookingId)
237
+ const mine = await bookings.mine(collectionId, appId, { from: new Date() })
238
+
239
+ // Guest, with the token from create
240
+ const b = await bookings.get(collectionId, appId, bookingId, false, { editToken })
241
+ await bookings.move(collectionId, appId, bookingId, { slotId: other.id }, false, { editToken })
242
+ ```
243
+
244
+ ### `publicRead.bookings.freebusy`
245
+
246
+ `{ "publicRead": { "bookings": { "freebusy": true } } }` lets public pages call `bookings.freebusy`.
247
+ Off by default — most apps serve free times through their own function.
248
+
249
+ ---
250
+
251
+ ## In a server function — `ctx.sl.bookings`
252
+
253
+ When public booking is off, your app books through its own server function. `ctx.sl.bookings` is
254
+ the same API, admin-side, for **your app's** bookings only (`sl:bookings:read` / `sl:bookings:write`):
255
+
256
+ ```ts
257
+ // manifest: { name: 'book', trigger: { type: 'http' }, visibility: 'public', authority: 'collection',
258
+ // capabilities: ['sl:bookings:write', 'sl:contacts:write'] }
259
+ export async function book(ctx, req) {
260
+ const me = await ctx.sl.contacts.forCaller() // null when signed out
261
+ // … your rules: hours (utils.withinSchedule), a queue token, a member level, a price …
262
+ const b = await ctx.sl.bookings.create({
263
+ resourceType: 'record', resourceId: req.body.staffId,
264
+ startsAt: req.body.startsAt, endsAt: req.body.endsAt, bufferAfter: 10,
265
+ contactId: me?.contactId, status: 'confirmed', idempotencyKey: req.body.requestId,
266
+ })
267
+ return Response.json({ id: b.id, startsAt: b.startsAt })
268
+ }
269
+ ```
270
+
271
+ `create`, `get`, `query`, `mine` (the caller's own), `update`, `confirm`, `cancel`, `move`,
272
+ `extendHold`, `complete`, `noShow`, `freebusy`, `slots.*`, `blocks.*`. Errors carry `code`,
273
+ `status` and `details` exactly as over HTTP. The local test harness
274
+ (`@proveanything/smartlinks/testing`) runs bookings in memory with the same rules.
275
+
276
+ ---
277
+
278
+ ## Payments and high-demand sales
279
+
280
+ The platform takes no payments. It gives you what a payment or credits flow needs:
281
+
282
+ 1. Create the booking **`held`** (claims the place; `holdMinutes`).
283
+ 2. Take payment. Slow step? `bookings.extendHold(…, minutes)` (admin; capped at 24h from creation).
284
+ 3. **Confirm after payment** — from your function or payment webhook handler. Confirm is idempotent, so
285
+ a repeated webhook is harmless. A run-out hold fails with `HOLD_EXPIRED`.
286
+ 4. If it's never confirmed, the hold expires within a minute: the booking becomes `cancelled`
287
+ (`cancelReason: 'hold_expired'`) and the place goes back. Void the payment intent on the
288
+ `booking.hold_expired` event.
289
+
290
+ Keep payment references, amounts, credits and refunds in the booking's `admin` / `owner` zones.
291
+
292
+ **High demand** (queues, lotteries, bot checks): turn public booking **off** so the only way in is
293
+ your server function; it checks the queue token / per-person limit / throttle, then creates a short
294
+ `held` booking. The platform guarantees nothing is oversold however many requests arrive.
295
+
296
+ ---
297
+
298
+ ## Errors
299
+
300
+ Booking calls fail with HTTP 409 and an `errorCode` you can show or act on:
301
+
302
+ | Code | When |
303
+ |---|---|
304
+ | `SLOT_FULL` | Not enough places left (`details.remaining`) |
305
+ | `SLOT_CLOSED` | The slot isn't open |
306
+ | `RESOURCE_TAKEN` | The time isn't free on that resource (`details` names it) |
307
+ | `PER_CONTACT_LIMIT` / `QUANTITY_LIMIT` | Per-person / per-booking limit |
308
+ | `HOLD_EXPIRED` | The hold ran out before confirm / move |
309
+ | `INVALID_STATE` | e.g. confirming a cancelled booking |
310
+ | `SLOT_OVERBOOKED` / `SLOT_HAS_BOOKINGS` | Lowering capacity below what's booked / changing a booked slot's time or deleting it |
311
+ | `TOO_SOON` / `TOO_FAR_AHEAD` / `IN_THE_PAST` / `DURATION_NOT_ALLOWED` | Public time rules |
312
+ | `OUTSIDE_SCHEDULE` / `NO_SCHEDULE` / `SCHEDULE_INVALID` | Public schedule check |
313
+ | `CUTOFF_PASSED` | Owner action inside its cutoff |
314
+
315
+ `403 FORBIDDEN` when the app's rules don't allow the call; `404` for unknown ids.
316
+
317
+ ---
318
+
319
+ ## Events
320
+
321
+ Recorded after each write commits, for flows and app functions (`trigger: { type: 'event', eventTypes: [...] }`):
322
+
323
+ `booking.created`, `booking.confirmed`, `booking.cancelled`, `booking.moved`, `booking.completed`,
324
+ `booking.no_show`, `booking.hold_expired`, `slot.full`, `slot.place_freed` (for waitlist promotion).
325
+ Context: `{ appId, status, slotIds }` (bookings) / `{ appId, remaining }` (slots).
326
+
327
+ Reminders before a booking (`booking.starts_in`) are coming; until then, send a reminder template to
328
+ tomorrow's bookers with `bookings.message` (below) from a cron function.
329
+
330
+ ---
331
+
332
+ ## Confirmations and messages
333
+
334
+ Nothing is emailed by default: your app decides what goes out, in **flows on booking events**, with
335
+ **templates the business can reword in Studio** (Comms → Templates). The platform does the sending,
336
+ the hydration and the consent.
337
+
338
+ ### Flows on booking events
339
+
340
+ A site, or an app on behalf of a business, sets them up through the cloud blueprint
341
+ (`cloud/flows.json` in a site; `POST /admin/collection/:cId/blueprint/apply` with
342
+ `{ appId, blueprint: { templates, flows } }` from an app's admin screen). Additive: re-applying never
343
+ removes what is there, and a template edited in Studio keeps its edits unless the blueprint sends new
344
+ wording.
345
+
346
+ ```json
347
+ {
348
+ "templates": {
349
+ "booking-confirmed": { "name": "You're booked", "email": { "subject": "{{ booking.serviceName }} on {{ booking.when.date }}", "body": "<p>Hi {{ contact.firstName }}, see you at {{ booking.when.time }}.</p>" }, "sms": { "body": "Booked: {{ booking.when.shortDate }} {{ booking.when.time }}" } },
350
+ "booking-requested": { "name": "Request received", "email": { "subject": "We got your request", "body": "<p>We'll confirm shortly.</p>" } },
351
+ "staff-new-booking": { "name": "You've been booked", "email": { "subject": "New appointment", "body": "<p>{{ booking.customer.name }}, {{ booking.when.dateTime }}</p>" } }
352
+ },
353
+ "flows": [
354
+ { "when": { "event": "booking.confirmed" }, "do": [{ "send": "booking-confirmed", "to": "contact" }, { "send": "staff-new-booking", "to": "resource" }, { "send": "team-new-booking", "to": "team" }] },
355
+ { "when": { "event": "booking.created", "status": "pending" }, "do": [{ "send": "booking-requested", "to": "contact" }] }
356
+ ]
357
+ }
358
+ ```
359
+
360
+ - `when.event`: `booking.created` / `confirmed` / `cancelled` / `moved` / `completed` / `no_show` /
361
+ `hold_expired`, `slot.full` / `slot.place_freed`; `when.status` narrows to the booking's status at the
362
+ time (`"pending"` for "request received").
363
+ - `to`: `contact` — the booker, by their preferred channel (`channel` to force one; a template needs an
364
+ `sms` part to go by text); `team` — the collection's team addresses; an **address** (`"hello@bloom.com"`,
365
+ or a list of up to 10) — a particular inbox, written out: never built from what someone sent
366
+ (`{{ … }}` is refused), at most 200 a day per template; `resource` — the booked
367
+ person or room: a resource that is a contact, or a record with `data.email` / `data.contactId`.
368
+ A **public** resource record (a staff profile the booking page lists) keeps its address on a
369
+ **private child record** instead (`visibility: 'admin'`, `parentType: 'record'`, `parentId` = the
370
+ resource) — the platform looks there too, and the address never ships with the public listing.
371
+ - Any step can also take `replyTo` — an address, or `"contact"` (replies go to the person the event is
372
+ about: a team notification answered straight to the booker) — and `cc` / `bcc` (written-out addresses,
373
+ same rules as `to`, counted in the same daily cap; over it the person still gets their email, without the
374
+ copies). A site's `cloud/email.json` (`{ "fromName", "replyTo" }`) sets the name the business's emails come
375
+ from and their default reply-to; the From address is the business's verified domain.
376
+ - `attach` on a step adds files (at most 5, 10 MB in all): `"calendar"` — the booking as an `.ics` invite
377
+ that drops straight into the person's calendar (a cancelled booking's invite cancels it there);
378
+ `{ "asset": "<assetId>" }` — a file in the collection's assets (terms, a price list);
379
+ `{ "function": "<name>" }` — your app's own function makes it (a receipt PDF: server-functions.md,
380
+ "Making an email attachment"). E.g. `{ "send": "booking-confirmed", "to": "contact", "attach": ["calendar"] }`.
381
+ One that can't be made is left out; the email still goes.
382
+ - Who gets what, in templates: `{{ booking }}` is the booking **ready-formatted**: its fields and
383
+ `data`, plus `serviceName`, `staffName`, `customer`, `durationMinutes`, `when` (`date`,
384
+ `shortDate`, `time`, `endTime`, `dateTime`, `timezone` — in the business's timezone) and, when the
385
+ recipient's timezone is known and differs, `whenFor` (the same in theirs); `editToken` only when the
386
+ recipient is the booker (a guest's manage link). `{{ contact }}` is the recipient; `{{ appConfig }}`
387
+ the app's public config (a booking page's address, the business name) for sends to the contact.
388
+
389
+ ### Who is reachable
390
+
391
+ A **guest** who books with an email or phone gets a **contact** (found by that identity or made,
392
+ filling only empty fields: a form's details never overwrite a known contact), and so does a customer
393
+ **staff book in by hand** (`customer: { name, email, phone }` on the body or in `data`). Admin
394
+ creates may pass `contactId` instead. A booking with no contact gets no messages: show the booker why
395
+ an email is worth giving.
396
+
397
+ ### Messaging everyone booked — `bookings.message`
398
+
399
+ No list to build, no segment to save: send a template to everyone on a slot, or everyone booked in a
400
+ window (optionally with one resource), each hydrated with **their own** booking.
401
+
402
+ ```ts
403
+ // Everyone on tomorrow's 10:00 class
404
+ await SL.bookings.message(collectionId, appId, { templateId, slotId })
405
+ // Everyone booked with Sam next week, by email even if they prefer texts
406
+ await SL.bookings.message(collectionId, appId, { templateId, channel: 'email', from, to, resourceType: 'record', resourceId: samId })
407
+ // Everyone on the class, each with their own calendar invite
408
+ await SL.bookings.message(collectionId, appId, { templateId, slotId, attach: ['calendar'] })
409
+ // → { matched, sent, skipped: { noContact, duplicate, failed }, failures: [{ bookingId, reason }] }
410
+ ```
411
+
412
+ `attach` takes the same files as `comms.sendTransactional` (comms.md, "Attachments"); `'calendar'` is
413
+ each person's own booking.
414
+
415
+ Statuses default to held, pending and confirmed; at most 500 recipients per call (narrow the window
416
+ for more). One message per contact on a slot, one per booking in a window. Also
417
+ `ctx.sl.bookings.message(input)` in a server function (`sl:bookings:write`) — a cron function that
418
+ messages tomorrow's bookers is a reminder system in five lines.
419
+
420
+ ---
421
+
422
+ ## API reference
423
+
424
+ All take `(collectionId, appId, …)`; `admin` is the last parameter where both exist.
425
+
426
+ | Call | Admin | Public |
427
+ |---|---|---|
428
+ | `bookings.create(input, admin?)` | any status, any field | per `publicCreate.bookings` |
429
+ | `bookings.list(params?)` | ✓ | — (use `mine`) |
430
+ | `bookings.mine(params?)` | — | signed-in booker |
431
+ | `bookings.get(id, admin?, { editToken }?)` | ✓ | booker / token |
432
+ | `bookings.update(id, input)` | ✓ | — |
433
+ | `bookings.confirm / cancel / move(…, admin?, { editToken }?)` | ✓ | per `ownerActions.bookings` |
434
+ | `bookings.extendHold / complete / noShow` | ✓ | — |
435
+ | `bookings.freebusy(q, admin?)` | ✓ | if `publicRead.bookings.freebusy` |
436
+ | `bookings.message(input)` | ✓ | — |
437
+ | `bookings.slots.create / createMany / update / remove` | ✓ | — |
438
+ | `bookings.slots.list / get(…, admin?)` | ✓ | open, public slots |
439
+ | `bookings.blocks.create / list / remove` | ✓ | — |
440
+ | `utils.freeTimes / withinSchedule / openIntervals / subtractIntervals / validateSchedule` | pure helpers | |
441
+
442
+ REST: `/{admin|public}/collection/:cId/app/:appId/bookings[/slots|/blocks|/freebusy|/mine|/:id[/confirm|/cancel|/move|/extend-hold|/complete|/no-show]]`.