@composable-os/pack-bookings 0.0.0-stage → 0.1.0-beta.11

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/dist/index.js ADDED
@@ -0,0 +1,974 @@
1
+ // Bookings (stage 31a, build steps 3 to 5 and 7: the spine, spending, the waitlist, and
2
+ // attendance, moves and cancelling a class; ADRs 0504, 0520 and 0553): a gym's or studio's
3
+ // timetable and the
4
+ // bookings members make on it. Staff set a weekly timetable once (series), the host makes each
5
+ // week's classes from it, and members book and cancel them on the web and in the app.
6
+ //
7
+ // Everything that is a rule is the host's, declared here as behaviours (ADR 0404):
8
+ // - a series makes its classes ahead in local time across clock changes (`recurring`, ADR 0500),
9
+ // never less far ahead than the longest booking window a class type sets (`horizonAtLeast`),
10
+ // never on a closure's dates, and never over a class staff changed by hand;
11
+ // - a class has places (`capacity`), and a booking holds one by its state (`claims`, ADR 0501),
12
+ // refused by the database past the places, for every actor;
13
+ // - one live booking per Person per class (`unique`, ADR 0502);
14
+ // - when a customer may book (from the class type's "opens" days before, else the
15
+ // organisation's; until its "closes" minutes before) and cancel in time (until its "late from"
16
+ // minutes before; late until the start; then not at all), and which class types only staff
17
+ // book, are windows and staff-only rules on the booking's states (ADR 0503), worked out from
18
+ // the class type's own value or the organisation's setting (ADR 0505). A member's "cancel" is
19
+ // a requested move the host resolves to `cancelled` or `late_cancelled` by its clock;
20
+ // - a booking entering `booked` spends a use of its Person's membership or pass and an in-time
21
+ // cancel gives it back (`allowance`, ADRs 0493 and 0494), unless staff booked it paid at the
22
+ // desk (`waived`, ADR 0506). The holders are the Memberships pack's (stage 32b);
23
+ // - a member joins a full class's waitlist, spending nothing; a place given back before the
24
+ // class type's "waitlist stops" time goes to the waitlist in the same batch, and the host
25
+ // moves the first waiter in, spending their use then, or passes them over when nothing they
26
+ // hold can pay (`claims.waitlist`, ADR 0520; step 5);
27
+ // - a visitor, or a member with nothing to spend, pays for one class on the web (a drop-in,
28
+ // decision 8): the host makes the booking held while its order is unpaid, priced from the
29
+ // class type's drop-in price or the organisation's, booked when the payment is verified and
30
+ // released when the order expires; a paid drop-in cancelled in time or by the gym owes the
31
+ // money back, which a person approves (`sellable.moves`, ADR 0560; step 8);
32
+ // - members read the timetable and how many places are left, never who is booked (decision 13);
33
+ // - a coach ticks names from an hour before the class (a window that binds staff, ADR 0551);
34
+ // of two ticks at once one is made (ADR 0437); 30 minutes after a class ends, the host makes
35
+ // its still-booked bookings no-shows, only if someone on it was ticked (`states.after`,
36
+ // ADR 0550). A no-show keeps its use;
37
+ // - staff move a booking to another class by making the new one (`movedFrom`): the old one is
38
+ // moved, its place given back and its use spent again on the new one, in one batch, refused
39
+ // whole when the new class is full or the use cannot be spent (ADRs 0493 and 0551);
40
+ // - staff cancel a class (a person's act, or a person approves an AI's request, ADR 0552), and
41
+ // every booked, waiting and held booking of it is cancelled in the same batch (ADR 0507);
42
+ // - the coach role (`roles`, ADR 0436): the day's classes, their lists, and ticking names.
43
+ // The tools in tools.ts compose the host's operations and add no rules of their own.
44
+ import { defineExtension } from "@composable-os/sdk";
45
+ import { book, bookFor, buy, cancel, cancelClass, cancelFor, classGet, classList, day, family, findPeople, leaveWaitlist, markAll, markAttended, markNoShow, mine, move, returnUse, roster, timetable, undoAttended, } from "./tools.js";
46
+ /** Weekday values a series' `weekdays` uses (ADR 0500). */
47
+ const WEEKDAYS = [
48
+ { value: "mon", label: "Monday" },
49
+ { value: "tue", label: "Tuesday" },
50
+ { value: "wed", label: "Wednesday" },
51
+ { value: "thu", label: "Thursday" },
52
+ { value: "fri", label: "Friday" },
53
+ { value: "sat", label: "Saturday" },
54
+ { value: "sun", label: "Sunday" },
55
+ ];
56
+ /** A booking's states (spec 31, "Booking states"). Only some are reachable at this step. */
57
+ const BOOKING_STATES = [
58
+ { value: "booked", label: "Booked" },
59
+ { value: "pending", label: "Held while paying" },
60
+ { value: "waiting", label: "Waiting" },
61
+ { value: "left", label: "Left the waitlist" },
62
+ { value: "cancelled", label: "Cancelled" },
63
+ { value: "late_cancelled", label: "Cancelled late" },
64
+ { value: "excused", label: "Cancelled, class given back" },
65
+ { value: "attended", label: "Attended" },
66
+ { value: "no_show", label: "No-show" },
67
+ { value: "cancelled_by_us", label: "Cancelled by the gym" },
68
+ { value: "moved", label: "Moved" },
69
+ { value: "expired", label: "Expired" },
70
+ ];
71
+ /** What a class the gym cancels cancels with it: every booking holding a place or a use. */
72
+ const FOLLOW_CANCEL = ["booked", "waiting", "pending", "attended", "no_show", "late_cancelled"];
73
+ /** A booking that holds or takes a place, and so counts as the Person's one for its class. */
74
+ const ACTIVE = ["pending", "booked", "waiting", "attended", "no_show"];
75
+ /** When a customer may book, and when a cancel is late, from the class type or the org. */
76
+ const opens = {
77
+ at: "class.startsAt",
78
+ daysBefore: { field: "class.classType.opensDays", setting: "opensDays" },
79
+ zone: "class.timezone",
80
+ };
81
+ const closes = {
82
+ at: "class.startsAt",
83
+ minutesBefore: { field: "class.classType.closesMinutes", setting: "closesMinutes" },
84
+ };
85
+ const lateFrom = {
86
+ at: "class.startsAt",
87
+ minutesBefore: { field: "class.classType.lateMinutes", setting: "lateMinutes" },
88
+ };
89
+ /** A class's list opens for ticking an hour before it starts (guess: people arrive early). */
90
+ const listOpens = { at: "class.startsAt", minutesBefore: 60 };
91
+ // A booking rule is a person's to change (spec 31: "Change the booking rules: a person only"):
92
+ // set by the assistant, an MCP client, an automation or extension code, it waits for a person's
93
+ // approval (`personApproves`, ADR 0507).
94
+ const minutes = (label, description) => ({
95
+ type: "number",
96
+ label,
97
+ description,
98
+ min: 0,
99
+ max: 10_080,
100
+ step: 1,
101
+ personApproves: true,
102
+ });
103
+ export default defineExtension({
104
+ id: "bookings",
105
+ version: "0.1.0",
106
+ contractVersion: 1,
107
+ label: "Bookings",
108
+ description: "A timetable of classes made from weekly series, rooms and coaches, and the bookings members " +
109
+ "make and cancel within the gym's rules. Class types are Programs'.",
110
+ // The class type is Programs' (spec 31): the host installs Bookings only beside Programs.
111
+ requires: ["programs"],
112
+ permissions: {
113
+ // person.get / person.create: a booking's person on a class list, and a walk-in staff book.
114
+ // programs.classtype: a class type's name and booking rules on staff's day view.
115
+ read: ["person", "programs.classtype"],
116
+ write: ["person"],
117
+ // record.*, my.* and state.resolve are not listed: they are granted by object (Bookings'
118
+ // own, and the read and write lists above), and a listed one would reach every object.
119
+ // duplicate.check: a walk-in is checked against the People on file before one is made.
120
+ // waitlist.position: where a waiter stands in the queue (a member: their own only).
121
+ // me.family: the member's own id, and the children staff linked to them (ADR 0540).
122
+ // order.startHeld: a drop-in, the booking made with its order by the host (ADR 0560).
123
+ operations: [
124
+ "order.startHeld",
125
+ "public.list",
126
+ "public.get",
127
+ "capacity.get",
128
+ "duplicate.check",
129
+ "waitlist.position",
130
+ "me.family",
131
+ ],
132
+ modules: ["ops"],
133
+ },
134
+ // Bookings' fields on Programs' class type: how it shows, and its booking rules when they
135
+ // differ from the organisation's (Settings → Extensions → Bookings). Empty: the organisation's.
136
+ fields: {
137
+ "programs.classtype": {
138
+ colour: {
139
+ type: "text",
140
+ label: "Colour",
141
+ description: "How its classes show on the timetable, e.g. #2f6fed.",
142
+ },
143
+ opensDays: {
144
+ type: "number",
145
+ label: "Booking opens (days before)",
146
+ description: "Members may book this many days before a class, at the same time of day. Empty: the " +
147
+ "organisation's setting.",
148
+ min: 0,
149
+ max: 365,
150
+ step: 1,
151
+ personApproves: true,
152
+ },
153
+ closesMinutes: minutes("Booking closes (minutes before)", "Members may book until this many minutes before a class starts (0: until it starts). " +
154
+ "Empty: the organisation's setting."),
155
+ lateMinutes: minutes("Late cancel from (minutes before)", "A cancel this close to the start or closer is late and keeps the class it used. Empty: " +
156
+ "the organisation's setting."),
157
+ promotionMinutes: minutes("Waitlist stops (minutes before)", "Nobody is moved in from the waitlist after this: a place freed later goes to whoever " +
158
+ "books first. Empty: the organisation's setting."),
159
+ reminderMinutes: minutes("Reminder (minutes before)", "When members are reminded of a class. Empty: the organisation's setting. (Reminders " +
160
+ "come in a later release.)"),
161
+ dropInPrice: {
162
+ type: "number",
163
+ label: "Drop-in price",
164
+ description: "What one class costs someone paying for it on the website, in the organisation's " +
165
+ "currency. Empty: the organisation's setting; empty there too, no drop-ins.",
166
+ min: 0,
167
+ max: 100_000,
168
+ step: 0.01,
169
+ personApproves: true,
170
+ },
171
+ staffOnly: {
172
+ type: "boolean",
173
+ label: "Booked by staff only",
174
+ description: "Members cannot book its classes themselves; staff book them in.",
175
+ personApproves: true,
176
+ },
177
+ },
178
+ },
179
+ objects: {
180
+ room: {
181
+ label: "Room",
182
+ plural: "Rooms",
183
+ icon: "door-open",
184
+ titleField: "name",
185
+ description: "Where classes are held.",
186
+ navigation: "settings",
187
+ fields: {
188
+ name: { type: "text", label: "Name", required: true },
189
+ places: {
190
+ type: "number",
191
+ label: "Places",
192
+ description: "The most the room holds: a guide for staff, not a limit on a class.",
193
+ min: 0,
194
+ step: 1,
195
+ },
196
+ notes: { type: "longText", label: "Notes" },
197
+ },
198
+ public: { read: ["name"] },
199
+ },
200
+ coach: {
201
+ label: "Coach",
202
+ plural: "Coaches",
203
+ icon: "user-round",
204
+ titleField: "name",
205
+ description: "Who teaches classes. Name, photo and bio are shown on the timetable.",
206
+ navigation: "settings",
207
+ fields: {
208
+ name: { type: "text", label: "Name", required: true },
209
+ photo: { type: "url", label: "Photo" },
210
+ bio: { type: "longText", label: "Short bio" },
211
+ user: {
212
+ type: "user",
213
+ label: "Staff account",
214
+ description: "Their staff account, if they have one (a visiting coach may not).",
215
+ },
216
+ active: { type: "boolean", label: "Active", default: true },
217
+ },
218
+ public: { read: ["name", "photo", "bio"] },
219
+ },
220
+ series: {
221
+ label: "Series",
222
+ plural: "Series",
223
+ icon: "calendar-sync",
224
+ titleField: "title",
225
+ titleDefault: { from: ["classType"] },
226
+ description: "One line of the timetable: a class type on chosen weekdays at a local time, from a " +
227
+ "date. The host makes its classes ahead.",
228
+ navigation: "main",
229
+ navigationOrder: 31,
230
+ fields: {
231
+ title: { type: "text", label: "Title" },
232
+ classType: {
233
+ type: "relation",
234
+ label: "Class type",
235
+ to: "programs.classtype",
236
+ required: true,
237
+ },
238
+ weekdays: { type: "multiSelect", label: "Weekdays", options: WEEKDAYS, required: true },
239
+ time: {
240
+ type: "text",
241
+ label: "Starts at (local time)",
242
+ description: "HH:MM in the series' time zone, e.g. 06:00.",
243
+ required: true,
244
+ },
245
+ length: {
246
+ type: "number",
247
+ label: "Length (minutes)",
248
+ required: true,
249
+ min: 5,
250
+ max: 1440,
251
+ step: 1,
252
+ },
253
+ room: { type: "relation", label: "Room", to: "bookings.room" },
254
+ coach: { type: "relation", label: "Coach", to: "bookings.coach" },
255
+ places: { type: "number", label: "Places", required: true, min: 0, step: 1 },
256
+ firstDate: { type: "date", label: "First date", required: true },
257
+ lastDate: { type: "date", label: "Last date", description: "Empty: ongoing." },
258
+ timezone: { type: "timezone", label: "Time zone" },
259
+ state: {
260
+ type: "select",
261
+ label: "State",
262
+ track: "timeline",
263
+ options: [
264
+ { value: "active", label: "Active" },
265
+ { value: "ended", label: "Ended" },
266
+ ],
267
+ },
268
+ },
269
+ states: {
270
+ field: "state",
271
+ initial: "active",
272
+ moves: [{ from: "active", to: "ended", by: ["staff"], final: true }],
273
+ },
274
+ recurring: {
275
+ generates: "class",
276
+ link: "series",
277
+ rule: {
278
+ weekdays: "weekdays",
279
+ time: "time",
280
+ length: "length",
281
+ from: "firstDate",
282
+ until: "lastDate",
283
+ zone: "timezone",
284
+ },
285
+ into: { starts: "startsAt", ends: "endsAt", zone: "timezone", changed: "changed" },
286
+ copy: {
287
+ title: "title",
288
+ classType: "classType",
289
+ room: "room",
290
+ coach: "coach",
291
+ places: "places",
292
+ },
293
+ cancel: "cancelled",
294
+ ended: { field: "state", in: ["ended"] },
295
+ closures: { object: "closure", from: "fromDate", until: "untilDate" },
296
+ // The organisation's "opens" setting is at most 56 days; a class 56 days out must exist
297
+ // when its booking opens, so one day more (as horizonAtLeast adds for a class type's).
298
+ horizonDays: 57,
299
+ // Never less than the longest window a class type opens booking (ADR 0500, 0505).
300
+ horizonAtLeast: { object: "programs.classtype", field: "opensDays" },
301
+ },
302
+ indexes: [["state"], ["classType"]],
303
+ },
304
+ class: {
305
+ label: "Class",
306
+ plural: "Classes",
307
+ icon: "calendar-clock",
308
+ titleField: "title",
309
+ titleDefault: { from: ["classType"] },
310
+ description: "One dated class: made by a series, or a one-off. Members book its places; its record " +
311
+ "page is its list.",
312
+ navigation: "main",
313
+ navigationOrder: 30,
314
+ fields: {
315
+ title: { type: "text", label: "Title" },
316
+ series: {
317
+ type: "relation",
318
+ label: "Series",
319
+ to: "bookings.series",
320
+ readOnly: true,
321
+ description: "The series that made it. Empty for a one-off class.",
322
+ },
323
+ classType: {
324
+ type: "relation",
325
+ label: "Class type",
326
+ to: "programs.classtype",
327
+ required: true,
328
+ },
329
+ startsAt: { type: "datetime", label: "Starts", required: true },
330
+ // Required (step 7): no-shows are written after it, and a class is cancelled only before.
331
+ endsAt: { type: "datetime", label: "Ends", required: true },
332
+ // Left empty, the organisation's zone (ADR 0460): a class always has one.
333
+ timezone: { type: "timezone", label: "Time zone", required: true },
334
+ room: { type: "relation", label: "Room", to: "bookings.room" },
335
+ coach: { type: "relation", label: "Coach", to: "bookings.coach" },
336
+ // Required: a class is never unlimited by leaving its places empty.
337
+ places: { type: "number", label: "Places", required: true, min: 0, step: 1 },
338
+ notes: { type: "longText", label: "Notes for members" },
339
+ state: {
340
+ type: "select",
341
+ label: "State",
342
+ track: "timeline",
343
+ options: [
344
+ { value: "scheduled", label: "Scheduled" },
345
+ { value: "cancelled", label: "Cancelled" },
346
+ { value: "done", label: "Done" },
347
+ ],
348
+ },
349
+ changed: {
350
+ type: "boolean",
351
+ label: "Changed from its series",
352
+ readOnly: true,
353
+ description: "Edited by hand: its series no longer changes it.",
354
+ },
355
+ cancelledReason: {
356
+ type: "longText",
357
+ label: "Why it was cancelled",
358
+ readOnly: true,
359
+ description: "The reason staff gave when they cancelled the class.",
360
+ },
361
+ // A class that is not scheduled cannot be booked by a member (the booking's create rule).
362
+ closed: {
363
+ type: "formula",
364
+ label: "Closed to members",
365
+ expression: 'coalesce(state, "scheduled") != "scheduled"',
366
+ },
367
+ },
368
+ states: {
369
+ field: "state",
370
+ initial: "scheduled",
371
+ moves: [
372
+ // The host cancels a class (a closure, a series ended, each confirmed by a person, ADR
373
+ // 0500), and so does cancelClass: a person's act, or an AI's request a person approves
374
+ // (ADR 0552). No other staff write cancels one. Its bookings follow in the same batch.
375
+ // Only until the class ends (binding the tool's staff write; the host is not bound): a
376
+ // class that happened is not cancelled afterwards, so no-shows never sit on one.
377
+ {
378
+ from: "scheduled",
379
+ to: "cancelled",
380
+ by: ["host"],
381
+ tools: ["cancelClass"],
382
+ window: { until: "endsAt" },
383
+ bindsStaff: true,
384
+ final: true,
385
+ },
386
+ { from: "scheduled", to: "done", by: ["staff", "host"], final: true },
387
+ ],
388
+ },
389
+ // Members see how many places are left (decision 13), never who has them.
390
+ capacity: { places: "places", show: "exact" },
391
+ public: {
392
+ read: [
393
+ "title",
394
+ "classType",
395
+ "startsAt",
396
+ "endsAt",
397
+ "timezone",
398
+ "room",
399
+ "coach",
400
+ "notes",
401
+ "state",
402
+ ],
403
+ when: { field: "state", in: ["scheduled"] },
404
+ },
405
+ // A booking's member reads their class whatever its state (a cancelled one says so).
406
+ customerVia: {
407
+ from: "bookings.booking",
408
+ field: "class",
409
+ read: [
410
+ "title",
411
+ "classType",
412
+ "startsAt",
413
+ "endsAt",
414
+ "timezone",
415
+ "room",
416
+ "coach",
417
+ "notes",
418
+ "state",
419
+ ],
420
+ },
421
+ indexes: [
422
+ ["startsAt"],
423
+ ["state", "startsAt"],
424
+ ["series"],
425
+ ["classType", "startsAt"],
426
+ ["endsAt"],
427
+ ],
428
+ },
429
+ closure: {
430
+ label: "Closure",
431
+ plural: "Closures",
432
+ icon: "calendar-x",
433
+ titleField: "name",
434
+ description: "Dates the gym is closed (holidays): no classes are made on them, and classes already " +
435
+ "made are cancelled (a person confirms when any have bookings).",
436
+ navigation: "settings",
437
+ fields: {
438
+ name: { type: "text", label: "Reason", required: true },
439
+ fromDate: { type: "date", label: "From", required: true },
440
+ untilDate: { type: "date", label: "Until", required: true },
441
+ },
442
+ },
443
+ booking: {
444
+ label: "Booking",
445
+ plural: "Bookings",
446
+ icon: "calendar-check",
447
+ titleField: "title",
448
+ titleDefault: { from: ["person"] },
449
+ description: "One person's place in one class.",
450
+ navigation: "main",
451
+ navigationOrder: 32,
452
+ fields: {
453
+ title: { type: "text", label: "Title" },
454
+ class: { type: "relation", label: "Class", to: "bookings.class", required: true },
455
+ // Set on every booking but a guest's drop-in held while unpaid, which is nobody's until the
456
+ // payment is verified (`sellable.moves.guest`: the host keeps it set in every other state).
457
+ person: { type: "relation", label: "Person", to: "person" },
458
+ state: { type: "select", label: "State", track: "timeline", options: BOOKING_STATES },
459
+ holder: {
460
+ type: "text",
461
+ label: "Paid from",
462
+ description: "The membership or pass the booking spent a use of (its id). Left empty, the host " +
463
+ "chooses the one whose use would lapse first.",
464
+ },
465
+ classType: {
466
+ type: "relation",
467
+ label: "Class type",
468
+ to: "programs.classtype",
469
+ readOnly: true,
470
+ description: "The class's type, copied by the host: what a membership's limits read.",
471
+ },
472
+ weekday: {
473
+ type: "number",
474
+ label: "Weekday",
475
+ readOnly: true,
476
+ description: "The class's local weekday (1 Monday to 7 Sunday), set by the host.",
477
+ },
478
+ minute: {
479
+ type: "number",
480
+ label: "Local minute",
481
+ readOnly: true,
482
+ description: "The class's local start, in minutes after midnight, set by the host.",
483
+ },
484
+ paidOtherwise: {
485
+ type: "boolean",
486
+ label: "Paid another way",
487
+ readOnly: true,
488
+ description: "Paid at the desk (a walk-in staff booked) or as a drop-in on the website: it " +
489
+ "spends no use. Set when it is made.",
490
+ },
491
+ // A drop-in (ADR 0560): what it cost and its order, written by the host at the buy.
492
+ dropInPrice: {
493
+ type: "number",
494
+ label: "Drop-in paid",
495
+ readOnly: true,
496
+ description: "What the drop-in cost, set by the host when it was bought.",
497
+ },
498
+ dropInOrder: {
499
+ type: "text",
500
+ label: "Drop-in order",
501
+ readOnly: true,
502
+ description: "The order that pays for this drop-in, set by the host.",
503
+ },
504
+ bookedBy: {
505
+ type: "user",
506
+ label: "Booked by",
507
+ readOnly: true,
508
+ description: "The staff account that booked it. Empty when the member booked.",
509
+ },
510
+ madeBy: {
511
+ type: "relation",
512
+ label: "Booked by (member)",
513
+ to: "person",
514
+ readOnly: true,
515
+ description: "The member who made it, set by the host: the person themselves, or the parent or " +
516
+ "guardian who booked for them. Empty when staff booked. Nobody edits it.",
517
+ },
518
+ movedFrom: {
519
+ type: "relation",
520
+ label: "Moved from",
521
+ to: "bookings.booking",
522
+ readOnly: true,
523
+ description: "The booking this one replaced when staff moved it to this class.",
524
+ },
525
+ note: { type: "longText", label: "Staff note" },
526
+ // Who ticked the name (or marked a no-show), and when (step 7): the coach who was first.
527
+ markedAt: {
528
+ type: "datetime",
529
+ label: "Attendance marked",
530
+ readOnly: true,
531
+ description: "When a coach ticked them as come, or marked them as not come.",
532
+ },
533
+ markedBy: {
534
+ type: "user",
535
+ label: "Marked by",
536
+ readOnly: true,
537
+ description: "The staff account that marked their attendance.",
538
+ },
539
+ markedByName: {
540
+ type: "text",
541
+ label: "Marked by (name)",
542
+ readOnly: true,
543
+ description: "That staff member's name as it was then.",
544
+ },
545
+ // The waitlist's record of what the host did (ADR 0520), for the class list and messages.
546
+ promotedAt: {
547
+ type: "datetime",
548
+ label: "Moved in from the waitlist",
549
+ readOnly: true,
550
+ description: "When the host moved them in from the waitlist.",
551
+ },
552
+ passedOverAt: {
553
+ type: "datetime",
554
+ label: "Passed over",
555
+ readOnly: true,
556
+ description: "When a place was free but nothing they hold could pay for it: they stay waiting.",
557
+ },
558
+ passedOverReason: {
559
+ type: "text",
560
+ label: "Why passed over",
561
+ readOnly: true,
562
+ description: "The host's reason (no_uses_left, ended, not_live, not_covered…).",
563
+ },
564
+ },
565
+ states: {
566
+ field: "state",
567
+ initial: "booked",
568
+ create: {
569
+ // A member or staff join a full class's waitlist (ADR 0520: only while it is full,
570
+ // before the cut-off). Held while paying (step 8) is the host's alone.
571
+ in: { waiting: ["staff", "customer", "host"], pending: ["host"] },
572
+ window: { from: opens, until: closes },
573
+ // A class type booked by staff only (staff may book it).
574
+ staffOnly: "class.classType.staffOnly",
575
+ // A class that is not scheduled (cancelled or done): nobody books it, staff included.
576
+ closed: "class.closed",
577
+ },
578
+ // A class the gym cancels (cancelClass, a closure, a series changed, ended or trashed)
579
+ // cancels every booking that holds a place or a use in the same batch: booked, ticked,
580
+ // marked not come and cancelled late included, each use given back once (ADRs 0507, 0553).
581
+ follows: {
582
+ relation: "class",
583
+ when: { cancelled: "cancelled_by_us" },
584
+ from: FOLLOW_CANCEL,
585
+ },
586
+ // A member asks to cancel; the host picks in time or late by its clock (ADR 0503).
587
+ requests: { cancel: ["cancelled", "late_cancelled"] },
588
+ moves: [
589
+ { from: "pending", to: "booked", by: ["host"] },
590
+ { from: "pending", to: "expired", by: ["host"], final: true },
591
+ // The host promotes (taking the waitlist's hold); a member waiting on a class with a
592
+ // free place (after the cut-off, say) takes it themselves, inside the booking window
593
+ // and the staff-only rule, never a place held for the queue (ADR 0520).
594
+ {
595
+ from: "waiting",
596
+ to: "booked",
597
+ by: ["host", "customer"],
598
+ window: { from: opens, until: closes },
599
+ staffOnly: "class.classType.staffOnly",
600
+ },
601
+ { from: "waiting", to: "left", by: ["staff", "customer"], final: true },
602
+ {
603
+ from: "booked",
604
+ to: "cancelled",
605
+ by: ["staff", "customer"],
606
+ final: true,
607
+ window: { until: lateFrom },
608
+ // In time for everyone, staff included (ADR 0507): giving a use back later is a
609
+ // person's own act, the move below.
610
+ bindsStaff: true,
611
+ },
612
+ // Not final: a class the gym cancels afterwards gives the kept use back (ADR 0553).
613
+ {
614
+ from: "booked",
615
+ to: "late_cancelled",
616
+ by: ["staff", "customer"],
617
+ window: { from: lateFrom, until: "class.startsAt" },
618
+ },
619
+ // A person on the staff gives the use back whatever the time, with a reason, through
620
+ // returnUse alone (ADR 0513's `tools`: humanOnly, with its permission).
621
+ { from: "booked", to: "excused", by: ["host"], tools: ["returnUse"], final: true },
622
+ // A coach ticks a name from an hour before the class, and any time after it (a late
623
+ // correction), never days ahead (a window that binds staff, ADR 0551). Unticked, it is
624
+ // booked again; a no-show ticked is attended. Each keeps the use.
625
+ {
626
+ from: "booked",
627
+ to: "attended",
628
+ by: ["staff"],
629
+ window: { from: listOpens },
630
+ bindsStaff: true,
631
+ },
632
+ { from: "attended", to: "booked", by: ["staff"] },
633
+ // Marked by a coach once the class has started; by the host after it (states.after).
634
+ {
635
+ from: "booked",
636
+ to: "no_show",
637
+ by: ["staff", "host"],
638
+ window: { from: "class.startsAt" },
639
+ bindsStaff: true,
640
+ },
641
+ { from: "no_show", to: "attended", by: ["staff"] },
642
+ { from: FOLLOW_CANCEL, to: "cancelled_by_us", by: ["host"], final: true },
643
+ // A move to another class: made only by making the new booking (`movedFrom`), in its
644
+ // batch (ADR 0551), and only before the class starts; after it, giving the use back is a
645
+ // person's act (returnUse).
646
+ {
647
+ from: "booked",
648
+ to: "moved",
649
+ by: ["staff"],
650
+ final: true,
651
+ window: { until: "class.startsAt" },
652
+ bindsStaff: true,
653
+ },
654
+ ],
655
+ // 30 minutes after a class ends, a booking still booked is a no-show, only when someone
656
+ // on the class was marked (its list was taken): a forgotten list makes nobody a no-show
657
+ // (spec 31, decision 11; ADR 0550).
658
+ after: [
659
+ {
660
+ from: "booked",
661
+ to: "no_show",
662
+ at: "class.endsAt",
663
+ minutesAfter: 30,
664
+ ifAnyIn: ["attended", "no_show"],
665
+ },
666
+ ],
667
+ },
668
+ // A place on the class while held, booked, attended or a no-show (ADR 0501).
669
+ // Its waitlist (ADR 0520): a place given back before the class type's "waitlist stops"
670
+ // time (else the organisation's) goes to the first waiter, whom the host moves in.
671
+ claims: {
672
+ on: "class",
673
+ held: ["pending"],
674
+ confirmed: ["booked", "attended", "no_show"],
675
+ waitlist: {
676
+ state: "waiting",
677
+ promote: "booked",
678
+ until: {
679
+ at: "class.startsAt",
680
+ minutesBefore: {
681
+ field: "class.classType.promotionMinutes",
682
+ setting: "promotionMinutes",
683
+ },
684
+ },
685
+ promotedAt: "promotedAt",
686
+ passedOverAt: "passedOverAt",
687
+ passedOverReason: "passedOverReason",
688
+ },
689
+ },
690
+ unique: [{ fields: ["person", "class"], states: ACTIVE }],
691
+ // A drop-in (decision 8, ADR 0560): x.bookings.buy has the host make the booking held
692
+ // (`pending`, a place claimed) with its order, priced from the class type's drop-in price,
693
+ // else the organisation's. A verified payment books it; the order expiring or cancelled
694
+ // releases it (`expired`). A paid one cancelled in time, given back by a person, or
695
+ // cancelled by the gym owes its money back, which a person with orders:refund approves
696
+ // (decision 15). A late cancel and a no-show keep it (no fee, no refund).
697
+ sellable: {
698
+ price: "dropInPrice",
699
+ // Stripe's "Fee for Personal Training/Fitness Classes" (in-person group classes); a human
700
+ // admin may override it in Settings -> Orders.
701
+ tax: { code: "txcd_50021003" },
702
+ moves: {
703
+ held: "pending",
704
+ paid: "booked",
705
+ expired: "expired",
706
+ refund: ["cancelled", "excused", "cancelled_by_us"],
707
+ price: { field: "class.classType.dropInPrice", setting: "dropInPrice" },
708
+ title: "class.title",
709
+ order: "dropInOrder",
710
+ // A visitor is matched to no Person until they have paid (ADR 0560): their hold is
711
+ // nobody's, blocks nobody's own booking and says nothing about who has the address.
712
+ guest: true,
713
+ // How much of a class unpaid guest holds may take at once.
714
+ holdCap: { setting: "dropInHoldPercent" },
715
+ },
716
+ },
717
+ // A spender of the Memberships pack's holders (stage 32b's ids, spec 32): booked spends,
718
+ // an in-time cancel, a class the gym cancels and a move give back; a late cancel and a
719
+ // no-show keep it. The period is the class's, and its type and local time are what a
720
+ // membership's limits read (ADRs 0493 and 0494).
721
+ allowance: {
722
+ spends: {
723
+ person: "person",
724
+ holder: "holder",
725
+ spendIn: ["booked"],
726
+ returnIn: ["cancelled", "excused", "cancelled_by_us", "moved"],
727
+ at: "class.startsAt",
728
+ copy: { classType: "class.classType" },
729
+ local: { weekday: "weekday", minute: "minute", zone: "class.timezone" },
730
+ movedFrom: { field: "movedFrom", state: "moved" },
731
+ paidBy: ["memberships.membership", "memberships.pass"],
732
+ waived: "paidOtherwise",
733
+ },
734
+ },
735
+ // A member reads and changes only their own bookings, and a parent or guardian staff linked
736
+ // to a child the child's too (ADR 0540); never anyone else's (decision 13).
737
+ customer: {
738
+ field: "person",
739
+ read: [
740
+ "person",
741
+ "class",
742
+ "state",
743
+ "holder",
744
+ "classType",
745
+ "promotedAt",
746
+ "passedOverAt",
747
+ "passedOverReason",
748
+ "paidOtherwise",
749
+ "dropInPrice",
750
+ "dropInOrder",
751
+ ],
752
+ write: ["class", "state", "holder"],
753
+ // Which class and which holder are chosen when it is made, never after (ADR 0507).
754
+ createOnly: ["class", "holder"],
755
+ // A guardian books, cancels and joins waitlists for a live dependant, under the same
756
+ // windows and rules, and reads their bookings (ADR 0540). The use is the child's: the
757
+ // allowance spends the booking's Person's holders only.
758
+ guardians: { write: true, madeBy: "madeBy" },
759
+ },
760
+ timelineLinks: ["person"],
761
+ indexes: [["class", "state"], ["person", "state"], ["state"]],
762
+ },
763
+ },
764
+ views: {
765
+ todaysClasses: {
766
+ label: "Today's classes",
767
+ object: "bookings.class",
768
+ filter: {
769
+ and: [
770
+ { field: "startsAt", op: "gte", value: { $relative: "today" } },
771
+ { field: "startsAt", op: "lt", value: { $relative: "tomorrow" } },
772
+ ],
773
+ },
774
+ sort: { field: "startsAt", direction: "asc" },
775
+ columns: ["title", "startsAt", "coach", "room", "places", "taken", "available", "state"],
776
+ },
777
+ thisWeeksClasses: {
778
+ label: "This week's classes",
779
+ object: "bookings.class",
780
+ filter: {
781
+ and: [
782
+ { field: "startsAt", op: "gte", value: { $relative: "startOfWeek" } },
783
+ { field: "startsAt", op: "lt", value: { $relative: "startOfNextWeek" } },
784
+ ],
785
+ },
786
+ sort: { field: "startsAt", direction: "asc" },
787
+ columns: ["title", "startsAt", "coach", "room", "taken", "available", "state"],
788
+ },
789
+ activeSeries: {
790
+ label: "Timetable",
791
+ object: "bookings.series",
792
+ filter: { field: "state", op: "eq", value: "active" },
793
+ sort: { field: "time", direction: "asc" },
794
+ columns: ["title", "weekdays", "time", "length", "coach", "room", "places", "firstDate"],
795
+ },
796
+ waitlists: {
797
+ label: "Waiting",
798
+ object: "bookings.booking",
799
+ filter: { field: "state", op: "eq", value: "waiting" },
800
+ sort: { field: "createdAt", direction: "asc" },
801
+ columns: ["title", "class", "person", "createdAt", "passedOverAt", "passedOverReason"],
802
+ },
803
+ lateCancels: {
804
+ label: "Late cancels",
805
+ object: "bookings.booking",
806
+ filter: { field: "state", op: "eq", value: "late_cancelled" },
807
+ sort: { field: "updatedAt", direction: "desc" },
808
+ columns: ["title", "class", "person", "updatedAt"],
809
+ },
810
+ },
811
+ recordTabs: {
812
+ // A class's list: who is booked, on its record page.
813
+ classBookings: {
814
+ label: "Bookings",
815
+ on: "bookings.class",
816
+ view: {
817
+ type: "table",
818
+ object: "bookings.booking",
819
+ relation: "class",
820
+ columns: [
821
+ "person",
822
+ "state",
823
+ "holder",
824
+ "paidOtherwise",
825
+ "dropInPrice",
826
+ "bookedBy",
827
+ "promotedAt",
828
+ "passedOverReason",
829
+ "markedAt",
830
+ "markedByName",
831
+ "createdAt",
832
+ ],
833
+ sort: { field: "createdAt", direction: "asc" },
834
+ },
835
+ order: 10,
836
+ },
837
+ seriesClasses: {
838
+ label: "Classes",
839
+ on: "bookings.series",
840
+ view: {
841
+ type: "table",
842
+ object: "bookings.class",
843
+ relation: "series",
844
+ columns: ["startsAt", "coach", "room", "places", "taken", "state", "changed"],
845
+ sort: { field: "startsAt", direction: "asc" },
846
+ },
847
+ order: 10,
848
+ },
849
+ personBookings: {
850
+ label: "Bookings",
851
+ on: "person",
852
+ view: {
853
+ type: "table",
854
+ object: "bookings.booking",
855
+ relation: "person",
856
+ columns: ["class", "state", "holder", "createdAt"],
857
+ sort: { field: "createdAt", direction: "desc" },
858
+ },
859
+ order: 35,
860
+ },
861
+ },
862
+ // The organisation's booking rules, which a class type's own values override. Changed by a
863
+ // person with the extensions permission (Settings → Extensions → Bookings).
864
+ settings: {
865
+ opensDays: {
866
+ type: "number",
867
+ label: "Booking opens (days before)",
868
+ description: "Members may book a class this many days before it, at the same time of day. At most " +
869
+ "56, the timetable's horizon; a class type may open further ahead.",
870
+ default: 7,
871
+ min: 0,
872
+ max: 56,
873
+ personApproves: true,
874
+ },
875
+ closesMinutes: {
876
+ type: "number",
877
+ label: "Booking closes (minutes before)",
878
+ description: "Members may book until this many minutes before a class (0: until it starts).",
879
+ default: 0,
880
+ min: 0,
881
+ max: 10_080,
882
+ personApproves: true,
883
+ },
884
+ lateMinutes: {
885
+ type: "number",
886
+ label: "Late cancel from (minutes before)",
887
+ description: "A cancel this close to the start is late: it keeps the class it used.",
888
+ default: 60,
889
+ min: 0,
890
+ max: 10_080,
891
+ personApproves: true,
892
+ },
893
+ promotionMinutes: {
894
+ type: "number",
895
+ label: "Waitlist stops (minutes before)",
896
+ description: "Nobody is moved in from the waitlist after this: a place freed later goes to whoever " +
897
+ "books first.",
898
+ default: 120,
899
+ min: 0,
900
+ max: 10_080,
901
+ personApproves: true,
902
+ },
903
+ dropInPrice: {
904
+ type: "number",
905
+ label: "Drop-in price",
906
+ description: "What one class costs someone paying for it on the website, in the organisation's " +
907
+ "currency, unless its class type sets its own. Empty: no drop-ins.",
908
+ min: 0,
909
+ max: 100_000,
910
+ personApproves: true,
911
+ },
912
+ dropInHoldPercent: {
913
+ type: "number",
914
+ label: "Places held for unpaid drop-ins (percent)",
915
+ description: "At most this share of a class's places (at least one) may be held at once for visitors " +
916
+ "who have not paid yet. More are told to try again shortly.",
917
+ default: 50,
918
+ min: 0,
919
+ max: 100,
920
+ personApproves: true,
921
+ },
922
+ reminderMinutes: {
923
+ type: "number",
924
+ label: "Reminder (minutes before)",
925
+ description: "When members are reminded of a class (a later release).",
926
+ default: 180,
927
+ min: 0,
928
+ max: 10_080,
929
+ personApproves: true,
930
+ },
931
+ },
932
+ // The coach role (spec 31, decision 10; ADR 0436): the day's classes and their lists, and ticking
933
+ // names. Nothing else: no money (no holders, no desk payments, no orders), no people beyond the
934
+ // names on a list, no bookings made or cancelled, no settings.
935
+ roles: {
936
+ coach: {
937
+ label: "Coach",
938
+ description: "Sees the day's classes and each class's list, and ticks who came (or marks who did " +
939
+ "not). Nothing else: no money, no other people, no booking or cancelling, no settings.",
940
+ permissions: ["bookings:coach"],
941
+ read: ["bookings.class"],
942
+ // Today's classes and lists only (spec 31: "read today's classes and their lists"): a class
943
+ // starting today in its own zone, or running now (past midnight until it ends). Another
944
+ // day's corrections are the desk's.
945
+ today: {
946
+ "bookings.class": { at: "startsAt", until: "endsAt", zone: "timezone" },
947
+ "bookings.booking": { at: "class.startsAt", until: "class.endsAt", zone: "class.timezone" },
948
+ },
949
+ },
950
+ },
951
+ tools: {
952
+ timetable,
953
+ classGet,
954
+ book,
955
+ buy,
956
+ cancel,
957
+ leaveWaitlist,
958
+ mine,
959
+ findPeople,
960
+ bookFor,
961
+ cancelFor,
962
+ returnUse,
963
+ day,
964
+ classList,
965
+ roster,
966
+ markAttended,
967
+ undoAttended,
968
+ markNoShow,
969
+ markAll,
970
+ move,
971
+ cancelClass,
972
+ family,
973
+ },
974
+ });