@composable-os/pack-bookings 0.1.0-beta.10 → 0.1.0-beta.13

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.
@@ -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)
@@ -53,6 +62,21 @@ export function refusalMessage(result, zone, locale = "en-AU") {
53
62
  }
54
63
  return result.message;
55
64
  }
65
+ /** The digits of a currency's minor unit (2 for AUD, 0 for JPY). */
66
+ function currencyDigits(currency) {
67
+ try {
68
+ return (new Intl.NumberFormat("en", { style: "currency", currency }).resolvedOptions()
69
+ .maximumFractionDigits ?? 2);
70
+ }
71
+ catch {
72
+ return 2;
73
+ }
74
+ }
75
+ /** An amount the host gave in the minor unit, as a buyer reads it: `25.00 AUD`. Display only. */
76
+ export function formatAmount(minor, currency) {
77
+ const digits = currencyDigits(currency);
78
+ return `${(minor / 10 ** digits).toFixed(digits)} ${currency}`;
79
+ }
56
80
  /** The member's live booking of a class, if any (to show Cancel instead of Book). */
57
81
  export function bookingFor(classId, bookings) {
58
82
  return bookings?.find((b) => b.classId === classId && b.state === "booked");
@@ -66,14 +90,25 @@ export const BOOKINGS_HOOKS_CALL = {
66
90
  useTimetable: ["x.bookings.timetable"],
67
91
  useClass: ["x.bookings.classGet"],
68
92
  useMyBookings: ["x.bookings.mine"],
93
+ useFamily: ["me.family"],
69
94
  useBook: ["x.bookings.book"],
70
95
  useCancel: ["x.bookings.cancel"],
96
+ useLeaveWaitlist: ["x.bookings.leaveWaitlist"],
71
97
  useDay: ["x.bookings.day"],
72
98
  useClassList: ["x.bookings.classList"],
73
99
  useBookFor: ["x.bookings.bookFor"],
74
100
  useCancelFor: ["x.bookings.cancelFor"],
75
101
  useReturnUse: ["x.bookings.returnUse"],
76
102
  useFindPeople: ["x.bookings.findPeople"],
103
+ // Paying for one class (a drop-in): a purchase, so web only (native blocks are noPurchase).
104
+ useBuy: ["x.bookings.buy", "order.checkout"],
105
+ useRoster: ["x.bookings.roster"],
106
+ useMarkAttended: ["x.bookings.markAttended"],
107
+ useUndoAttended: ["x.bookings.undoAttended"],
108
+ useMarkNoShow: ["x.bookings.markNoShow"],
109
+ useMarkAll: ["x.bookings.markAll"],
110
+ useMove: ["x.bookings.move"],
111
+ useCancelClass: ["x.bookings.cancelClass"],
77
112
  };
78
113
  /**
79
114
  * Hooks for a React or React Native app:
@@ -94,11 +129,31 @@ function allBookingsHooks(react, api) {
94
129
  useClass(input) {
95
130
  return useQuery(api.query("x.bookings.classGet", input));
96
131
  },
97
- /** The signed-in member's own bookings. */
132
+ /** The signed-in member's own bookings, or (with personId) a child's linked to them. */
98
133
  useMyBookings(input = {}) {
99
134
  return useQuery(api.query("x.bookings.mine", input));
100
135
  },
101
- /** Books the member into a class; the host answers booked or why not. */
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
+ */
140
+ useFamily() {
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
+ });
155
+ },
156
+ /** Books the member into a class; the host answers booked, waiting (full) or why not. */
102
157
  useBook() {
103
158
  return useMutation(api.mutation("x.bookings.book"));
104
159
  },
@@ -106,6 +161,10 @@ function allBookingsHooks(react, api) {
106
161
  useCancel() {
107
162
  return useMutation(api.mutation("x.bookings.cancel"));
108
163
  },
164
+ /** Takes the member off a class's waitlist (a booking that is waiting). */
165
+ useLeaveWaitlist() {
166
+ return useMutation(api.mutation("x.bookings.leaveWaitlist"));
167
+ },
109
168
  /** Staff: one local date's classes. */
110
169
  useDay(input) {
111
170
  return useQuery(api.query("x.bookings.day", input));
@@ -126,6 +185,18 @@ function allBookingsHooks(react, api) {
126
185
  useReturnUse() {
127
186
  return useMutation(api.mutation("x.bookings.returnUse"));
128
187
  },
188
+ /**
189
+ * Pay for one class (a drop-in, web only): `start` has the host hold the place and start the
190
+ * order; `checkout` opens the provider's page for it.
191
+ */
192
+ useBuy() {
193
+ return {
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 }),
197
+ checkout: useMutation(api.mutation("order.checkout")),
198
+ };
199
+ },
129
200
  /** Staff: People on file matching a name or email (at least two letters). */
130
201
  useFindPeople(input) {
131
202
  return useQuery({
@@ -133,5 +204,33 @@ function allBookingsHooks(react, api) {
133
204
  enabled: input.query.trim().length >= 2,
134
205
  });
135
206
  },
207
+ /** A coach or staff: a class's list with names and marks (nothing of what anyone paid). */
208
+ useRoster(input) {
209
+ return useQuery(api.query("x.bookings.roster", input));
210
+ },
211
+ /** A coach or staff: tick a name; the host says if someone ticked it first, and who. */
212
+ useMarkAttended() {
213
+ return useMutation(api.mutation("x.bookings.markAttended"));
214
+ },
215
+ /** A coach or staff: untick a name ticked by mistake. */
216
+ useUndoAttended() {
217
+ return useMutation(api.mutation("x.bookings.undoAttended"));
218
+ },
219
+ /** A coach or staff: mark a name as not come, once the class has started. */
220
+ useMarkNoShow() {
221
+ return useMutation(api.mutation("x.bookings.markNoShow"));
222
+ },
223
+ /** A coach or staff: tick everyone still booked on a class. */
224
+ useMarkAll() {
225
+ return useMutation(api.mutation("x.bookings.markAll"));
226
+ },
227
+ /** Staff: move a booking to another class with its use (refused whole when it cannot). */
228
+ useMove() {
229
+ return useMutation(api.mutation("x.bookings.move"));
230
+ },
231
+ /** A person on the staff: cancel a class and its bookings (an AI's call waits for one). */
232
+ useCancelClass() {
233
+ return useMutation(api.mutation("x.bookings.cancelClass"));
234
+ },
136
235
  };
137
236
  }
@@ -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 };