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

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.
@@ -1,4 +1,4 @@
1
- import { type ClientHolding, type PackClient } from "@composable-os/client";
1
+ import { type ClientHolding, type PackClient, type UseMutation, type UseQuery } from "@composable-os/client";
2
2
  import type { z } from "@composable-os/sdk";
3
3
  import type { book, bookFor, buy, cancel, cancelClass, cancelFor, classGet, classList, day, family, findPeople, leaveWaitlist, markAll, markAttended, markNoShow, mine, move, returnUse, roster, timetable, undoAttended } from "../tools.ts";
4
4
  type Io<T extends {
@@ -22,6 +22,24 @@ export type BookingsOperations = {
22
22
  "x.bookings.family": {
23
23
  kind: "query";
24
24
  } & Io<typeof family>;
25
+ "me.family": {
26
+ kind: "query";
27
+ input: Record<string, never>;
28
+ output: {
29
+ self: {
30
+ personId: string;
31
+ name: string;
32
+ };
33
+ dependants: {
34
+ personId: string;
35
+ name: string;
36
+ }[];
37
+ guardians: {
38
+ personId: string;
39
+ name: string;
40
+ }[];
41
+ };
42
+ };
25
43
  "x.bookings.book": {
26
44
  kind: "mutation";
27
45
  } & Io<typeof book>;
@@ -105,6 +123,8 @@ export type BuyResult = Out<"x.bookings.buy">;
105
123
  export type FoundPeople = Out<"x.bookings.findPeople">;
106
124
  /** The client of every operation this entry calls (ADR 0453). */
107
125
  export type BookingsClient = PackClient<BookingsOperations>;
126
+ /** Today's date (YYYY-MM-DD) in `timeZone`, by this device's clock. */
127
+ export declare function todayIn(timeZone: string): string;
108
128
  /** A class's start and end in its own zone ("Tue 3 Mar, 06:00–07:00"), for display. */
109
129
  export declare function classTime(klass: {
110
130
  startsAt: string | null;
@@ -123,28 +143,7 @@ export declare function refusalMessage(result: {
123
143
  export declare function formatAmount(minor: number, currency: string): string;
124
144
  /** The member's live booking of a class, if any (to show Cancel instead of Book). */
125
145
  export declare function bookingFor(classId: string, bookings: readonly MyBooking[] | undefined): MyBooking | undefined;
126
- /** The app's `useQuery`, as far as these hooks use it. */
127
- export type UseQuery = <T>(options: {
128
- queryKey: readonly unknown[];
129
- queryFn: () => Promise<T>;
130
- enabled?: boolean;
131
- }) => {
132
- data: T | undefined;
133
- isPending: boolean;
134
- error: unknown;
135
- refetch?: () => unknown;
136
- };
137
- /** The app's `useMutation`, as far as these hooks use it. */
138
- export type UseMutation = <I, O>(options: {
139
- mutationKey: readonly unknown[];
140
- mutationFn: (input: I) => Promise<O>;
141
- }) => {
142
- mutate(input: I): void;
143
- mutateAsync(input: I): Promise<O>;
144
- data: O | undefined;
145
- isPending: boolean;
146
- error: unknown;
147
- };
146
+ export type { UseMutation, UseQuery } from "@composable-os/client";
148
147
  /**
149
148
  * The operations each hook calls (ADR 0453). A customer's client gets the member's hooks (the
150
149
  * timetable, book, cancel, mine), the public's only the timetable and a class, the staff client
@@ -154,7 +153,7 @@ export declare const BOOKINGS_HOOKS_CALL: {
154
153
  readonly useTimetable: readonly ["x.bookings.timetable"];
155
154
  readonly useClass: readonly ["x.bookings.classGet"];
156
155
  readonly useMyBookings: readonly ["x.bookings.mine"];
157
- readonly useFamily: readonly ["x.bookings.family"];
156
+ readonly useFamily: readonly ["me.family"];
158
157
  readonly useBook: readonly ["x.bookings.book"];
159
158
  readonly useCancel: readonly ["x.bookings.cancel"];
160
159
  readonly useLeaveWaitlist: readonly ["x.bookings.leaveWaitlist"];
@@ -268,7 +267,10 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
268
267
  error: unknown;
269
268
  refetch?: () => unknown;
270
269
  };
271
- /** Who the member may book for: themselves and the children staff linked to them. */
270
+ /**
271
+ * Who the member may book for: themselves and the children staff linked to them, read from
272
+ * the host's own family (`me.family`, stage 48) in the `family` tool's shape.
273
+ */
272
274
  useFamily(): {
273
275
  data: {
274
276
  people: {
@@ -295,6 +297,11 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
295
297
  holderId?: string | undefined;
296
298
  waitlist?: boolean | undefined;
297
299
  }): Promise<{
300
+ result: "booked";
301
+ bookingId: string;
302
+ classId: string;
303
+ holderId: string | null;
304
+ } | {
298
305
  result: "waiting";
299
306
  bookingId: string;
300
307
  classId: string;
@@ -313,13 +320,13 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
313
320
  name: string;
314
321
  reasons: string[];
315
322
  }[] | undefined;
316
- } | {
323
+ }>;
324
+ data: {
317
325
  result: "booked";
318
326
  bookingId: string;
319
327
  classId: string;
320
328
  holderId: string | null;
321
- }>;
322
- data: {
329
+ } | {
323
330
  result: "waiting";
324
331
  bookingId: string;
325
332
  classId: string;
@@ -338,14 +345,10 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
338
345
  name: string;
339
346
  reasons: string[];
340
347
  }[] | undefined;
341
- } | {
342
- result: "booked";
343
- bookingId: string;
344
- classId: string;
345
- holderId: string | null;
346
348
  } | undefined;
347
349
  isPending: boolean;
348
350
  error: unknown;
351
+ reset?: () => void;
349
352
  };
350
353
  /** Cancels a booking: first the host's preview (in time or late), then with confirm. */
351
354
  useCancel(): {
@@ -398,6 +401,7 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
398
401
  } | undefined;
399
402
  isPending: boolean;
400
403
  error: unknown;
404
+ reset?: () => void;
401
405
  };
402
406
  /** Takes the member off a class's waitlist (a booking that is waiting). */
403
407
  useLeaveWaitlist(): {
@@ -440,6 +444,7 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
440
444
  } | undefined;
441
445
  isPending: boolean;
442
446
  error: unknown;
447
+ reset?: () => void;
443
448
  };
444
449
  /** Staff: one local date's classes. */
445
450
  useDay(input: In<"x.bookings.day">): {
@@ -502,6 +507,7 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
502
507
  holderId: string | null;
503
508
  bookedBy: string | null;
504
509
  promotedAt: string | null;
510
+ healthNote: boolean | null;
505
511
  passedOver: string | null;
506
512
  }[];
507
513
  } | undefined;
@@ -594,6 +600,7 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
594
600
  } | undefined;
595
601
  isPending: boolean;
596
602
  error: unknown;
603
+ reset?: () => void;
597
604
  };
598
605
  /** Staff: cancel a booking, in time or late by the host's clock. */
599
606
  useCancelFor(): {
@@ -642,6 +649,7 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
642
649
  } | undefined;
643
650
  isPending: boolean;
644
651
  error: unknown;
652
+ reset?: () => void;
645
653
  };
646
654
  /** A person on the staff: give a booking's use back after the late point (destructive). */
647
655
  useReturnUse(): {
@@ -688,6 +696,7 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
688
696
  } | undefined;
689
697
  isPending: boolean;
690
698
  error: unknown;
699
+ reset?: () => void;
691
700
  };
692
701
  /**
693
702
  * Pay for one class (a drop-in, web only): `start` has the host hold the place and start the
@@ -699,11 +708,13 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
699
708
  classId: string;
700
709
  forPersonId?: string | undefined;
701
710
  email?: string | undefined;
711
+ agreements?: Record<string, unknown>[] | undefined;
702
712
  }): void;
703
713
  mutateAsync(input: {
704
714
  classId: string;
705
715
  forPersonId?: string | undefined;
706
716
  email?: string | undefined;
717
+ agreements?: Record<string, unknown>[] | undefined;
707
718
  }): Promise<{
708
719
  result: "refused";
709
720
  reason: "not_open" | "closed" | "staff_only" | "no_use" | "already_booked" | "full" | "too_late" | "not_found" | "changed" | "not_running" | "person_exists" | "possible_duplicates" | "waitlist_closed" | "no_drop_in" | "ask_desk" | "try_later";
@@ -750,6 +761,7 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
750
761
  } | undefined;
751
762
  isPending: boolean;
752
763
  error: unknown;
764
+ reset?: () => void;
753
765
  };
754
766
  checkout: {
755
767
  mutate(input: {
@@ -769,6 +781,7 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
769
781
  } | undefined;
770
782
  isPending: boolean;
771
783
  error: unknown;
784
+ reset?: () => void;
772
785
  };
773
786
  };
774
787
  /** Staff: People on file matching a name or email (at least two letters). */
@@ -816,6 +829,7 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
816
829
  markedAt: string | null;
817
830
  markedBy: string | null;
818
831
  promotedAt: string | null;
832
+ healthNote: boolean | null;
819
833
  }[];
820
834
  } | undefined;
821
835
  isPending: boolean;
@@ -867,6 +881,7 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
867
881
  } | undefined;
868
882
  isPending: boolean;
869
883
  error: unknown;
884
+ reset?: () => void;
870
885
  };
871
886
  /** A coach or staff: untick a name ticked by mistake. */
872
887
  useUndoAttended(): {
@@ -913,6 +928,7 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
913
928
  } | undefined;
914
929
  isPending: boolean;
915
930
  error: unknown;
931
+ reset?: () => void;
916
932
  };
917
933
  /** A coach or staff: mark a name as not come, once the class has started. */
918
934
  useMarkNoShow(): {
@@ -959,6 +975,7 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
959
975
  } | undefined;
960
976
  isPending: boolean;
961
977
  error: unknown;
978
+ reset?: () => void;
962
979
  };
963
980
  /** A coach or staff: tick everyone still booked on a class. */
964
981
  useMarkAll(): {
@@ -995,6 +1012,7 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
995
1012
  } | undefined;
996
1013
  isPending: boolean;
997
1014
  error: unknown;
1015
+ reset?: () => void;
998
1016
  };
999
1017
  /** Staff: move a booking to another class with its use (refused whole when it cannot). */
1000
1018
  useMove(): {
@@ -1071,6 +1089,7 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
1071
1089
  } | undefined;
1072
1090
  isPending: boolean;
1073
1091
  error: unknown;
1092
+ reset?: () => void;
1074
1093
  };
1075
1094
  /** A person on the staff: cancel a class and its bookings (an AI's call waits for one). */
1076
1095
  useCancelClass(): {
@@ -1093,12 +1112,13 @@ export declare function createBookingsHooks<C extends ClientHolding>(react: {
1093
1112
  } | undefined;
1094
1113
  isPending: boolean;
1095
1114
  error: unknown;
1115
+ reset?: () => void;
1096
1116
  };
1097
1117
  }, {
1098
1118
  readonly useTimetable: readonly ["x.bookings.timetable"];
1099
1119
  readonly useClass: readonly ["x.bookings.classGet"];
1100
1120
  readonly useMyBookings: readonly ["x.bookings.mine"];
1101
- readonly useFamily: readonly ["x.bookings.family"];
1121
+ readonly useFamily: readonly ["me.family"];
1102
1122
  readonly useBook: readonly ["x.bookings.book"];
1103
1123
  readonly useCancel: readonly ["x.bookings.cancel"];
1104
1124
  readonly useLeaveWaitlist: readonly ["x.bookings.leaveWaitlist"];
@@ -1207,7 +1227,10 @@ declare function allBookingsHooks(react: {
1207
1227
  error: unknown;
1208
1228
  refetch?: () => unknown;
1209
1229
  };
1210
- /** Who the member may book for: themselves and the children staff linked to them. */
1230
+ /**
1231
+ * Who the member may book for: themselves and the children staff linked to them, read from
1232
+ * the host's own family (`me.family`, stage 48) in the `family` tool's shape.
1233
+ */
1211
1234
  useFamily(): {
1212
1235
  data: {
1213
1236
  people: {
@@ -1234,6 +1257,11 @@ declare function allBookingsHooks(react: {
1234
1257
  holderId?: string | undefined;
1235
1258
  waitlist?: boolean | undefined;
1236
1259
  }): Promise<{
1260
+ result: "booked";
1261
+ bookingId: string;
1262
+ classId: string;
1263
+ holderId: string | null;
1264
+ } | {
1237
1265
  result: "waiting";
1238
1266
  bookingId: string;
1239
1267
  classId: string;
@@ -1252,13 +1280,13 @@ declare function allBookingsHooks(react: {
1252
1280
  name: string;
1253
1281
  reasons: string[];
1254
1282
  }[] | undefined;
1255
- } | {
1283
+ }>;
1284
+ data: {
1256
1285
  result: "booked";
1257
1286
  bookingId: string;
1258
1287
  classId: string;
1259
1288
  holderId: string | null;
1260
- }>;
1261
- data: {
1289
+ } | {
1262
1290
  result: "waiting";
1263
1291
  bookingId: string;
1264
1292
  classId: string;
@@ -1277,14 +1305,10 @@ declare function allBookingsHooks(react: {
1277
1305
  name: string;
1278
1306
  reasons: string[];
1279
1307
  }[] | undefined;
1280
- } | {
1281
- result: "booked";
1282
- bookingId: string;
1283
- classId: string;
1284
- holderId: string | null;
1285
1308
  } | undefined;
1286
1309
  isPending: boolean;
1287
1310
  error: unknown;
1311
+ reset?: () => void;
1288
1312
  };
1289
1313
  /** Cancels a booking: first the host's preview (in time or late), then with confirm. */
1290
1314
  useCancel(): {
@@ -1337,6 +1361,7 @@ declare function allBookingsHooks(react: {
1337
1361
  } | undefined;
1338
1362
  isPending: boolean;
1339
1363
  error: unknown;
1364
+ reset?: () => void;
1340
1365
  };
1341
1366
  /** Takes the member off a class's waitlist (a booking that is waiting). */
1342
1367
  useLeaveWaitlist(): {
@@ -1379,6 +1404,7 @@ declare function allBookingsHooks(react: {
1379
1404
  } | undefined;
1380
1405
  isPending: boolean;
1381
1406
  error: unknown;
1407
+ reset?: () => void;
1382
1408
  };
1383
1409
  /** Staff: one local date's classes. */
1384
1410
  useDay(input: In<"x.bookings.day">): {
@@ -1441,6 +1467,7 @@ declare function allBookingsHooks(react: {
1441
1467
  holderId: string | null;
1442
1468
  bookedBy: string | null;
1443
1469
  promotedAt: string | null;
1470
+ healthNote: boolean | null;
1444
1471
  passedOver: string | null;
1445
1472
  }[];
1446
1473
  } | undefined;
@@ -1533,6 +1560,7 @@ declare function allBookingsHooks(react: {
1533
1560
  } | undefined;
1534
1561
  isPending: boolean;
1535
1562
  error: unknown;
1563
+ reset?: () => void;
1536
1564
  };
1537
1565
  /** Staff: cancel a booking, in time or late by the host's clock. */
1538
1566
  useCancelFor(): {
@@ -1581,6 +1609,7 @@ declare function allBookingsHooks(react: {
1581
1609
  } | undefined;
1582
1610
  isPending: boolean;
1583
1611
  error: unknown;
1612
+ reset?: () => void;
1584
1613
  };
1585
1614
  /** A person on the staff: give a booking's use back after the late point (destructive). */
1586
1615
  useReturnUse(): {
@@ -1627,6 +1656,7 @@ declare function allBookingsHooks(react: {
1627
1656
  } | undefined;
1628
1657
  isPending: boolean;
1629
1658
  error: unknown;
1659
+ reset?: () => void;
1630
1660
  };
1631
1661
  /**
1632
1662
  * Pay for one class (a drop-in, web only): `start` has the host hold the place and start the
@@ -1638,11 +1668,13 @@ declare function allBookingsHooks(react: {
1638
1668
  classId: string;
1639
1669
  forPersonId?: string | undefined;
1640
1670
  email?: string | undefined;
1671
+ agreements?: Record<string, unknown>[] | undefined;
1641
1672
  }): void;
1642
1673
  mutateAsync(input: {
1643
1674
  classId: string;
1644
1675
  forPersonId?: string | undefined;
1645
1676
  email?: string | undefined;
1677
+ agreements?: Record<string, unknown>[] | undefined;
1646
1678
  }): Promise<{
1647
1679
  result: "refused";
1648
1680
  reason: "not_open" | "closed" | "staff_only" | "no_use" | "already_booked" | "full" | "too_late" | "not_found" | "changed" | "not_running" | "person_exists" | "possible_duplicates" | "waitlist_closed" | "no_drop_in" | "ask_desk" | "try_later";
@@ -1689,6 +1721,7 @@ declare function allBookingsHooks(react: {
1689
1721
  } | undefined;
1690
1722
  isPending: boolean;
1691
1723
  error: unknown;
1724
+ reset?: () => void;
1692
1725
  };
1693
1726
  checkout: {
1694
1727
  mutate(input: {
@@ -1708,6 +1741,7 @@ declare function allBookingsHooks(react: {
1708
1741
  } | undefined;
1709
1742
  isPending: boolean;
1710
1743
  error: unknown;
1744
+ reset?: () => void;
1711
1745
  };
1712
1746
  };
1713
1747
  /** Staff: People on file matching a name or email (at least two letters). */
@@ -1755,6 +1789,7 @@ declare function allBookingsHooks(react: {
1755
1789
  markedAt: string | null;
1756
1790
  markedBy: string | null;
1757
1791
  promotedAt: string | null;
1792
+ healthNote: boolean | null;
1758
1793
  }[];
1759
1794
  } | undefined;
1760
1795
  isPending: boolean;
@@ -1806,6 +1841,7 @@ declare function allBookingsHooks(react: {
1806
1841
  } | undefined;
1807
1842
  isPending: boolean;
1808
1843
  error: unknown;
1844
+ reset?: () => void;
1809
1845
  };
1810
1846
  /** A coach or staff: untick a name ticked by mistake. */
1811
1847
  useUndoAttended(): {
@@ -1852,6 +1888,7 @@ declare function allBookingsHooks(react: {
1852
1888
  } | undefined;
1853
1889
  isPending: boolean;
1854
1890
  error: unknown;
1891
+ reset?: () => void;
1855
1892
  };
1856
1893
  /** A coach or staff: mark a name as not come, once the class has started. */
1857
1894
  useMarkNoShow(): {
@@ -1898,6 +1935,7 @@ declare function allBookingsHooks(react: {
1898
1935
  } | undefined;
1899
1936
  isPending: boolean;
1900
1937
  error: unknown;
1938
+ reset?: () => void;
1901
1939
  };
1902
1940
  /** A coach or staff: tick everyone still booked on a class. */
1903
1941
  useMarkAll(): {
@@ -1934,6 +1972,7 @@ declare function allBookingsHooks(react: {
1934
1972
  } | undefined;
1935
1973
  isPending: boolean;
1936
1974
  error: unknown;
1975
+ reset?: () => void;
1937
1976
  };
1938
1977
  /** Staff: move a booking to another class with its use (refused whole when it cannot). */
1939
1978
  useMove(): {
@@ -2010,6 +2049,7 @@ declare function allBookingsHooks(react: {
2010
2049
  } | undefined;
2011
2050
  isPending: boolean;
2012
2051
  error: unknown;
2052
+ reset?: () => void;
2013
2053
  };
2014
2054
  /** A person on the staff: cancel a class and its bookings (an AI's call waits for one). */
2015
2055
  useCancelClass(): {
@@ -2032,8 +2072,8 @@ declare function allBookingsHooks(react: {
2032
2072
  } | undefined;
2033
2073
  isPending: boolean;
2034
2074
  error: unknown;
2075
+ reset?: () => void;
2035
2076
  };
2036
2077
  };
2037
2078
  /** Every hook of this entry. A block takes the ones it calls: `Pick<BookingsHooks, "useBook">`. */
2038
2079
  export type BookingsHooks = ReturnType<typeof allBookingsHooks>;
2039
- export {};
@@ -8,8 +8,17 @@
8
8
  // and it holds no rules. When booking opens, whether a cancel is late, whether a class type is
9
9
  // booked by the gym, the places left and what a booking spends are the host's: a block shows the
10
10
  // host's answer (`book`'s and `cancel`'s), never works one out.
11
- import { packHooks } from "@composable-os/client";
11
+ import { packHooks, } from "@composable-os/client";
12
12
  // --- Plain functions (display only) --------------------------------------------------------------
13
+ /** Today's date (YYYY-MM-DD) in `timeZone`, by this device's clock. */
14
+ export function todayIn(timeZone) {
15
+ return new Intl.DateTimeFormat("en-CA", {
16
+ timeZone,
17
+ year: "numeric",
18
+ month: "2-digit",
19
+ day: "2-digit",
20
+ }).format(new Date());
21
+ }
13
22
  /** A class's start and end in its own zone ("Tue 3 Mar, 06:00–07:00"), for display. */
14
23
  export function classTime(klass, locale = "en-AU") {
15
24
  if (!klass.startsAt)
@@ -81,7 +90,7 @@ export const BOOKINGS_HOOKS_CALL = {
81
90
  useTimetable: ["x.bookings.timetable"],
82
91
  useClass: ["x.bookings.classGet"],
83
92
  useMyBookings: ["x.bookings.mine"],
84
- useFamily: ["x.bookings.family"],
93
+ useFamily: ["me.family"],
85
94
  useBook: ["x.bookings.book"],
86
95
  useCancel: ["x.bookings.cancel"],
87
96
  useLeaveWaitlist: ["x.bookings.leaveWaitlist"],
@@ -124,9 +133,25 @@ function allBookingsHooks(react, api) {
124
133
  useMyBookings(input = {}) {
125
134
  return useQuery(api.query("x.bookings.mine", input));
126
135
  },
127
- /** Who the member may book for: themselves and the children staff linked to them. */
136
+ /**
137
+ * Who the member may book for: themselves and the children staff linked to them, read from
138
+ * the host's own family (`me.family`, stage 48) in the `family` tool's shape.
139
+ */
128
140
  useFamily() {
129
- return useQuery(api.query("x.bookings.family", {}));
141
+ const read = api.query("me.family", {});
142
+ return useQuery({
143
+ // Its own key under me.family's: a write that makes me.family stale refreshes it too.
144
+ queryKey: [...read.queryKey, "bookings"],
145
+ queryFn: async () => {
146
+ const found = await read.queryFn();
147
+ return {
148
+ people: [
149
+ { ...found.self, self: true },
150
+ ...found.dependants.map((d) => ({ ...d, self: false })),
151
+ ],
152
+ };
153
+ },
154
+ });
130
155
  },
131
156
  /** Books the member into a class; the host answers booked, waiting (full) or why not. */
132
157
  useBook() {
@@ -166,7 +191,9 @@ function allBookingsHooks(react, api) {
166
191
  */
167
192
  useBuy() {
168
193
  return {
169
- start: useMutation(api.mutation("x.bookings.buy")),
194
+ // A visitor's agreements (a name typed, a signature, health answers) go in this call:
195
+ // not kept in the app's cache once it is over (stage 44).
196
+ start: useMutation({ ...api.mutation("x.bookings.buy"), gcTime: 0 }),
170
197
  checkout: useMutation(api.mutation("order.checkout")),
171
198
  };
172
199
  },
@@ -0,0 +1,5 @@
1
+ import type { DashboardDeclaration } from "@composable-os/sdk";
2
+ export declare const bookingsOverview: DashboardDeclaration;
3
+ export declare const bookingsDashboards: {
4
+ bookings: DashboardDeclaration;
5
+ };
@@ -0,0 +1,157 @@
1
+ /** Classes starting in the period, not cancelled by the gym. */
2
+ const IN_PERIOD = "c.starts_at >= :dates_from AND c.starts_at < :dates_to AND c.state != 'cancelled'";
3
+ /** A class's bookings, with the class, in the period. */
4
+ const BOOKINGS = `FROM rec_x_bookings_booking b JOIN rec_x_bookings_class c ON c.id = b.class
5
+ WHERE ${IN_PERIOD}`;
6
+ /** A booking that held a place on its class: booked, came, or did not come. */
7
+ const HELD = "b.state IN ('booked', 'attended', 'no_show')";
8
+ /** A booking that ever waited: waiting now, left, moved in or passed over. */
9
+ const WAITED = "(b.state IN ('waiting', 'left') OR b.promoted_at IS NOT NULL OR b.passed_over_at IS NOT NULL)";
10
+ export const bookingsOverview = {
11
+ title: "Bookings",
12
+ description: "Attendance by class type, hour and coach; how full classes are; late cancels and no-shows " +
13
+ "by month and member; waiting-list joins and how many got in; first visits and whether " +
14
+ "they came back.",
15
+ filters: [{ type: "dateRange", id: "dates", label: "Classes from", default: "this_month" }],
16
+ widgets: [
17
+ {
18
+ id: "attended",
19
+ type: "kpi",
20
+ title: "Came",
21
+ description: "Bookings ticked as come, on classes in the dates.",
22
+ sql: `SELECT count(*) AS value ${BOOKINGS} AND b.state = 'attended'`,
23
+ mapping: { value: "value" },
24
+ options: { compare: true },
25
+ layout: { w: 3, h: 1 },
26
+ },
27
+ {
28
+ id: "no-shows",
29
+ type: "kpi",
30
+ title: "No-shows",
31
+ description: "Bookings marked as not come (by a coach, or after the class by the host).",
32
+ sql: `SELECT count(*) AS value ${BOOKINGS} AND b.state = 'no_show'`,
33
+ mapping: { value: "value" },
34
+ options: { compare: true },
35
+ layout: { w: 3, h: 1 },
36
+ },
37
+ {
38
+ id: "late-cancels",
39
+ type: "kpi",
40
+ title: "Late cancels",
41
+ description: "Bookings cancelled after the late-cancel time: each kept its use.",
42
+ sql: `SELECT count(*) AS value ${BOOKINGS} AND b.state = 'late_cancelled'`,
43
+ mapping: { value: "value" },
44
+ options: { compare: true },
45
+ layout: { w: 3, h: 1 },
46
+ },
47
+ {
48
+ id: "full",
49
+ type: "kpi",
50
+ title: "How full",
51
+ description: "Places taken over places, across the classes in the dates (booked, came or not come).",
52
+ sql: `SELECT coalesce(round(100.0 * sum(coalesce(k.taken, 0)) / nullif(sum(coalesce(k.places, 0)), 0)), 0) AS value
53
+ FROM rec_x_bookings_class c
54
+ LEFT JOIN rec_capacity k ON k.object_type = 'bookings.class' AND k.record_id = c.id
55
+ WHERE ${IN_PERIOD}`,
56
+ mapping: { value: "value" },
57
+ options: { compare: true, format: "percent" },
58
+ layout: { w: 3, h: 1 },
59
+ },
60
+ {
61
+ id: "by-type",
62
+ type: "bar",
63
+ title: "Attendance by class type",
64
+ description: "Who came, and who held a place, per class type.",
65
+ sql: `SELECT t.id AS type_id, coalesce(t.name, 'No class type') AS type,
66
+ sum(CASE WHEN b.state = 'attended' THEN 1 ELSE 0 END) AS came,
67
+ sum(CASE WHEN ${HELD} THEN 1 ELSE 0 END) AS booked
68
+ ${BOOKINGS.replace("WHERE", "LEFT JOIN rec_x_programs_classtype t ON t.id = c.class_type WHERE")}
69
+ GROUP BY t.id, t.name ORDER BY came DESC, type LIMIT 20`,
70
+ mapping: { x: "type", y: ["came", "booked"] },
71
+ layout: { w: 6, h: 2 },
72
+ },
73
+ {
74
+ id: "by-hour",
75
+ type: "bar",
76
+ title: "Attendance by time of day",
77
+ description: "Who came, by the hour the class starts in its own time zone.",
78
+ sql: `SELECT printf('%02d:00', b.minute / 60) AS hour, count(*) AS came
79
+ ${BOOKINGS} AND b.state = 'attended' AND b.minute IS NOT NULL
80
+ GROUP BY hour ORDER BY hour`,
81
+ mapping: { x: "hour", y: ["came"] },
82
+ layout: { w: 6, h: 2 },
83
+ },
84
+ {
85
+ id: "by-coach",
86
+ type: "bar",
87
+ title: "Attendance by coach",
88
+ description: "Who came, per coach of the class.",
89
+ sql: `SELECT coalesce(co.name, 'No coach') AS coach, count(*) AS came
90
+ ${BOOKINGS.replace("WHERE", "LEFT JOIN rec_x_bookings_coach co ON co.id = c.coach WHERE")} AND b.state = 'attended'
91
+ GROUP BY co.id, co.name ORDER BY came DESC, coach LIMIT 20`,
92
+ mapping: { x: "coach", y: ["came"] },
93
+ layout: { w: 6, h: 2 },
94
+ },
95
+ {
96
+ id: "misses-by-month",
97
+ type: "line",
98
+ title: "Late cancels and no-shows by month",
99
+ description: "Per month of the class.",
100
+ sql: `SELECT substr(c.starts_at, 1, 7) AS month,
101
+ sum(CASE WHEN b.state = 'late_cancelled' THEN 1 ELSE 0 END) AS late_cancels,
102
+ sum(CASE WHEN b.state = 'no_show' THEN 1 ELSE 0 END) AS no_shows
103
+ ${BOOKINGS} GROUP BY month ORDER BY month`,
104
+ mapping: { x: "month", y: ["late_cancels", "no_shows"] },
105
+ layout: { w: 6, h: 2 },
106
+ },
107
+ {
108
+ id: "waitlist",
109
+ type: "table",
110
+ title: "Waiting lists",
111
+ description: "Bookings that joined a waiting list, and how many were moved in from it, per class type.",
112
+ sql: `SELECT coalesce(t.name, 'No class type') AS type, count(*) AS joined,
113
+ sum(CASE WHEN b.promoted_at IS NOT NULL THEN 1 ELSE 0 END) AS got_in
114
+ ${BOOKINGS.replace("WHERE", "LEFT JOIN rec_x_programs_classtype t ON t.id = c.class_type WHERE")} AND ${WAITED}
115
+ GROUP BY t.id, t.name ORDER BY joined DESC, type LIMIT 20`,
116
+ mapping: { columns: ["type", "joined", "got_in"] },
117
+ layout: { w: 6, h: 2 },
118
+ },
119
+ {
120
+ id: "first-visits",
121
+ type: "table",
122
+ title: "First visits",
123
+ description: "Members whose first class they came to is in the dates, and how many classes they came " +
124
+ "to after it (0: they have not come back).",
125
+ sql: `WITH came AS (
126
+ SELECT b.person AS person, c.starts_at AS at
127
+ FROM rec_x_bookings_booking b JOIN rec_x_bookings_class c ON c.id = b.class
128
+ WHERE b.state = 'attended' AND b.person IS NOT NULL
129
+ ), firsts AS (SELECT person, min(at) AS first FROM came GROUP BY person)
130
+ SELECT p.id AS id, p.display_name AS member, f.first AS first_class,
131
+ (SELECT count(*) FROM came l WHERE l.person = f.person AND l.at > f.first) AS came_back
132
+ FROM firsts f JOIN rec_person p ON p.id = f.person
133
+ WHERE f.first >= :dates_from AND f.first < :dates_to
134
+ ORDER BY f.first, p.display_name LIMIT 100`,
135
+ mapping: { columns: ["member", "first_class", "came_back"] },
136
+ clickThrough: {
137
+ object: "person",
138
+ filter: { field: "id", op: "eq", value: { $row: "id" } },
139
+ },
140
+ layout: { w: 6, h: 2 },
141
+ },
142
+ {
143
+ id: "misses-by-member",
144
+ type: "table",
145
+ title: "Late cancels and no-shows by member",
146
+ description: "Members with a late cancel or a no-show on a class in the dates, most first.",
147
+ sql: `SELECT p.id AS id, p.display_name AS member,
148
+ sum(CASE WHEN b.state = 'late_cancelled' THEN 1 ELSE 0 END) AS late_cancels,
149
+ sum(CASE WHEN b.state = 'no_show' THEN 1 ELSE 0 END) AS no_shows
150
+ ${BOOKINGS.replace("WHERE", "JOIN rec_person p ON p.id = b.person WHERE")} AND b.state IN ('late_cancelled', 'no_show')
151
+ GROUP BY p.id, p.display_name ORDER BY late_cancels + no_shows DESC, member LIMIT 50`,
152
+ mapping: { columns: ["member", "late_cancels", "no_shows"] },
153
+ layout: { w: 12, h: 2 },
154
+ },
155
+ ],
156
+ };
157
+ export const bookingsDashboards = { bookings: bookingsOverview };