@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.
@@ -0,0 +1,209 @@
1
+ // The Bookings pack's headless entry (stage 31a step 3; ADR 0418): functions and hooks over the
2
+ // typed client, no markup. A client app imports it as `@composable-os/pack-bookings/client` and
3
+ // hands it its generated client; the bookings blocks (web and native) render what it returns.
4
+ // Web and native share this logic, not their screens.
5
+ //
6
+ // As every pack's entry: the operations are typed from the pack's own tool schemas, it takes the
7
+ // client and the app's `useQuery` and `useMutation` as arguments (no React or TanStack import),
8
+ // and it holds no rules. When booking opens, whether a cancel is late, whether a class type is
9
+ // booked by the gym, the places left and what a booking spends are the host's: a block shows the
10
+ // host's answer (`book`'s and `cancel`'s), never works one out.
11
+ import { packHooks } from "@composable-os/client";
12
+ // --- Plain functions (display only) --------------------------------------------------------------
13
+ /** A class's start and end in its own zone ("Tue 3 Mar, 06:00–07:00"), for display. */
14
+ export function classTime(klass, locale = "en-AU") {
15
+ if (!klass.startsAt)
16
+ return "";
17
+ const zone = klass.timeZone ?? undefined;
18
+ const day = new Intl.DateTimeFormat(locale, {
19
+ weekday: "short",
20
+ day: "numeric",
21
+ month: "short",
22
+ ...(zone ? { timeZone: zone } : {}),
23
+ }).format(new Date(klass.startsAt));
24
+ const time = (iso) => new Intl.DateTimeFormat(locale, {
25
+ hour: "2-digit",
26
+ minute: "2-digit",
27
+ hourCycle: "h23",
28
+ ...(zone ? { timeZone: zone } : {}),
29
+ }).format(new Date(iso));
30
+ return `${day}, ${time(klass.startsAt)}${klass.endsAt ? `–${time(klass.endsAt)}` : ""}`;
31
+ }
32
+ /** What a class's places say ("5 places left", "Full"), from the host's numbers. */
33
+ export function placesLabel(places) {
34
+ if (places.status === "sold_out")
35
+ return "Full";
36
+ if (places.available === undefined || places.available === null)
37
+ return "Places available";
38
+ return places.available === 1 ? "1 place left" : `${places.available} places left`;
39
+ }
40
+ /** The message to show for a refusal the host gave (with when booking opens, if it said). */
41
+ export function refusalMessage(result, zone, locale = "en-AU") {
42
+ if (result.reason === "not_open" && result.opensAt) {
43
+ const when = new Intl.DateTimeFormat(locale, {
44
+ weekday: "short",
45
+ day: "numeric",
46
+ month: "short",
47
+ hour: "2-digit",
48
+ minute: "2-digit",
49
+ hourCycle: "h23",
50
+ ...(zone ? { timeZone: zone } : {}),
51
+ }).format(new Date(result.opensAt));
52
+ return `Booking opens ${when}.`;
53
+ }
54
+ return result.message;
55
+ }
56
+ /** The digits of a currency's minor unit (2 for AUD, 0 for JPY). */
57
+ function currencyDigits(currency) {
58
+ try {
59
+ return (new Intl.NumberFormat("en", { style: "currency", currency }).resolvedOptions()
60
+ .maximumFractionDigits ?? 2);
61
+ }
62
+ catch {
63
+ return 2;
64
+ }
65
+ }
66
+ /** An amount the host gave in the minor unit, as a buyer reads it: `25.00 AUD`. Display only. */
67
+ export function formatAmount(minor, currency) {
68
+ const digits = currencyDigits(currency);
69
+ return `${(minor / 10 ** digits).toFixed(digits)} ${currency}`;
70
+ }
71
+ /** The member's live booking of a class, if any (to show Cancel instead of Book). */
72
+ export function bookingFor(classId, bookings) {
73
+ return bookings?.find((b) => b.classId === classId && b.state === "booked");
74
+ }
75
+ /**
76
+ * The operations each hook calls (ADR 0453). A customer's client gets the member's hooks (the
77
+ * timetable, book, cancel, mine), the public's only the timetable and a class, the staff client
78
+ * all of them.
79
+ */
80
+ export const BOOKINGS_HOOKS_CALL = {
81
+ useTimetable: ["x.bookings.timetable"],
82
+ useClass: ["x.bookings.classGet"],
83
+ useMyBookings: ["x.bookings.mine"],
84
+ useFamily: ["x.bookings.family"],
85
+ useBook: ["x.bookings.book"],
86
+ useCancel: ["x.bookings.cancel"],
87
+ useLeaveWaitlist: ["x.bookings.leaveWaitlist"],
88
+ useDay: ["x.bookings.day"],
89
+ useClassList: ["x.bookings.classList"],
90
+ useBookFor: ["x.bookings.bookFor"],
91
+ useCancelFor: ["x.bookings.cancelFor"],
92
+ useReturnUse: ["x.bookings.returnUse"],
93
+ useFindPeople: ["x.bookings.findPeople"],
94
+ // Paying for one class (a drop-in): a purchase, so web only (native blocks are noPurchase).
95
+ useBuy: ["x.bookings.buy", "order.checkout"],
96
+ useRoster: ["x.bookings.roster"],
97
+ useMarkAttended: ["x.bookings.markAttended"],
98
+ useUndoAttended: ["x.bookings.undoAttended"],
99
+ useMarkNoShow: ["x.bookings.markNoShow"],
100
+ useMarkAll: ["x.bookings.markAll"],
101
+ useMove: ["x.bookings.move"],
102
+ useCancelClass: ["x.bookings.cancelClass"],
103
+ };
104
+ /**
105
+ * Hooks for a React or React Native app:
106
+ * `const bookings = createBookingsHooks({ useQuery, useMutation }, api)`. `api` is any generated
107
+ * client: the customer's or the staff one. The hooks it cannot back are not there.
108
+ */
109
+ export function createBookingsHooks(react, client) {
110
+ return packHooks(client, BOOKINGS_HOOKS_CALL, (api) => allBookingsHooks(react, api));
111
+ }
112
+ function allBookingsHooks(react, api) {
113
+ const { useQuery, useMutation } = react;
114
+ return {
115
+ /** The timetable between two times (default: the next 7 days). */
116
+ useTimetable(input = {}) {
117
+ return useQuery(api.query("x.bookings.timetable", input));
118
+ },
119
+ /** One scheduled class. */
120
+ useClass(input) {
121
+ return useQuery(api.query("x.bookings.classGet", input));
122
+ },
123
+ /** The signed-in member's own bookings, or (with personId) a child's linked to them. */
124
+ useMyBookings(input = {}) {
125
+ return useQuery(api.query("x.bookings.mine", input));
126
+ },
127
+ /** Who the member may book for: themselves and the children staff linked to them. */
128
+ useFamily() {
129
+ return useQuery(api.query("x.bookings.family", {}));
130
+ },
131
+ /** Books the member into a class; the host answers booked, waiting (full) or why not. */
132
+ useBook() {
133
+ return useMutation(api.mutation("x.bookings.book"));
134
+ },
135
+ /** Cancels a booking: first the host's preview (in time or late), then with confirm. */
136
+ useCancel() {
137
+ return useMutation(api.mutation("x.bookings.cancel"));
138
+ },
139
+ /** Takes the member off a class's waitlist (a booking that is waiting). */
140
+ useLeaveWaitlist() {
141
+ return useMutation(api.mutation("x.bookings.leaveWaitlist"));
142
+ },
143
+ /** Staff: one local date's classes. */
144
+ useDay(input) {
145
+ return useQuery(api.query("x.bookings.day", input));
146
+ },
147
+ /** Staff: a class's list. */
148
+ useClassList(input) {
149
+ return useQuery(api.query("x.bookings.classList", input));
150
+ },
151
+ /** Staff: book anyone, a walk-in included. */
152
+ useBookFor() {
153
+ return useMutation(api.mutation("x.bookings.bookFor"));
154
+ },
155
+ /** Staff: cancel a booking, in time or late by the host's clock. */
156
+ useCancelFor() {
157
+ return useMutation(api.mutation("x.bookings.cancelFor"));
158
+ },
159
+ /** A person on the staff: give a booking's use back after the late point (destructive). */
160
+ useReturnUse() {
161
+ return useMutation(api.mutation("x.bookings.returnUse"));
162
+ },
163
+ /**
164
+ * Pay for one class (a drop-in, web only): `start` has the host hold the place and start the
165
+ * order; `checkout` opens the provider's page for it.
166
+ */
167
+ useBuy() {
168
+ return {
169
+ start: useMutation(api.mutation("x.bookings.buy")),
170
+ checkout: useMutation(api.mutation("order.checkout")),
171
+ };
172
+ },
173
+ /** Staff: People on file matching a name or email (at least two letters). */
174
+ useFindPeople(input) {
175
+ return useQuery({
176
+ ...api.query("x.bookings.findPeople", input),
177
+ enabled: input.query.trim().length >= 2,
178
+ });
179
+ },
180
+ /** A coach or staff: a class's list with names and marks (nothing of what anyone paid). */
181
+ useRoster(input) {
182
+ return useQuery(api.query("x.bookings.roster", input));
183
+ },
184
+ /** A coach or staff: tick a name; the host says if someone ticked it first, and who. */
185
+ useMarkAttended() {
186
+ return useMutation(api.mutation("x.bookings.markAttended"));
187
+ },
188
+ /** A coach or staff: untick a name ticked by mistake. */
189
+ useUndoAttended() {
190
+ return useMutation(api.mutation("x.bookings.undoAttended"));
191
+ },
192
+ /** A coach or staff: mark a name as not come, once the class has started. */
193
+ useMarkNoShow() {
194
+ return useMutation(api.mutation("x.bookings.markNoShow"));
195
+ },
196
+ /** A coach or staff: tick everyone still booked on a class. */
197
+ useMarkAll() {
198
+ return useMutation(api.mutation("x.bookings.markAll"));
199
+ },
200
+ /** Staff: move a booking to another class with its use (refused whole when it cannot). */
201
+ useMove() {
202
+ return useMutation(api.mutation("x.bookings.move"));
203
+ },
204
+ /** A person on the staff: cancel a class and its bookings (an AI's call waits for one). */
205
+ useCancelClass() {
206
+ return useMutation(api.mutation("x.bookings.cancelClass"));
207
+ },
208
+ };
209
+ }