@composable-os/pack-memberships 0.0.0-stage → 0.1.0-beta.9
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/LICENSE +109 -0
- package/dist/blocks/memberships-mine/mine.d.ts +6 -0
- package/dist/blocks/memberships-mine/mine.js +48 -0
- package/dist/blocks/memberships-native-mine/mine-screen.d.ts +4 -0
- package/dist/blocks/memberships-native-mine/mine-screen.js +12 -0
- package/dist/blocks/memberships-passes/passes.d.ts +10 -0
- package/dist/blocks/memberships-passes/passes.js +43 -0
- package/dist/blocks/memberships-plans/plans.d.ts +15 -0
- package/dist/blocks/memberships-plans/plans.js +106 -0
- package/dist/blocks/memberships-staff-holdings/holdings.d.ts +10 -0
- package/dist/blocks/memberships-staff-holdings/holdings.js +35 -0
- package/dist/client/index.d.ts +808 -0
- package/dist/client/index.js +212 -0
- package/dist/index.d.ts +894 -0
- package/dist/index.js +559 -0
- package/dist/tools.d.ts +134 -0
- package/dist/tools.js +346 -0
- package/package.json +36 -4
- package/README.md +0 -3
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// The Memberships pack's headless entry (stage 32b; ADRs 0418 and 0453): functions and hooks over
|
|
2
|
+
// the typed client, no markup. A client app imports it as `@composable-os/pack-memberships/client`
|
|
3
|
+
// and hands it its generated client; the memberships blocks render what it returns.
|
|
4
|
+
//
|
|
5
|
+
// Nothing here decides a price, a trial, whether an intro offer may be bought, or how many classes
|
|
6
|
+
// are left: the host does (`sellable`, the one-trial rule, `onePerPerson`, `allowance.get`). The
|
|
7
|
+
// billing actions (subscribe, cancel, resume, the provider's portal, buying a pass) are the web's;
|
|
8
|
+
// a native block reads only (the block lint).
|
|
9
|
+
import { packHooks } from "@composable-os/client";
|
|
10
|
+
export const PLAN = "memberships.plan";
|
|
11
|
+
export const MEMBERSHIP = "memberships.membership";
|
|
12
|
+
export const PASS = "memberships.pass";
|
|
13
|
+
// --- Display only ---------------------------------------------------------------------------------
|
|
14
|
+
/** A price as the host gave it, in its currency (`99.00 AUD`). Display only: never worked out. */
|
|
15
|
+
export function formatPrice(price, currency) {
|
|
16
|
+
if (price === null || price === undefined)
|
|
17
|
+
return "";
|
|
18
|
+
try {
|
|
19
|
+
return currency
|
|
20
|
+
? new Intl.NumberFormat(undefined, { style: "currency", currency }).format(price)
|
|
21
|
+
: String(price);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return `${price}${currency ? ` ${currency}` : ""}`;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** A date as a reader sees it (`3 Dec 2026`), from the host's ISO timestamp. Display only. */
|
|
28
|
+
export function day(iso) {
|
|
29
|
+
if (!iso)
|
|
30
|
+
return "";
|
|
31
|
+
const at = new Date(iso);
|
|
32
|
+
return Number.isNaN(at.getTime())
|
|
33
|
+
? ""
|
|
34
|
+
: at.toLocaleDateString(undefined, { day: "numeric", month: "short", year: "numeric" });
|
|
35
|
+
}
|
|
36
|
+
const clock = (minute) => `${String(Math.floor(minute / 60)).padStart(2, "0")}:${String(minute % 60).padStart(2, "0")}`;
|
|
37
|
+
const DAY_NAMES = ["", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
|
|
38
|
+
/** What a plan or pass allows, in words: `3 classes a week · Mon, Wed · 09:00 to 16:00`. */
|
|
39
|
+
export function allowsWords(item) {
|
|
40
|
+
const per = item.per === "month" ? " a month" : item.per === "week" ? " a week" : "";
|
|
41
|
+
const count = item.uses === null
|
|
42
|
+
? "Unlimited classes"
|
|
43
|
+
: `${item.uses} ${item.uses === 1 ? "class" : "classes"}${per}`;
|
|
44
|
+
const days = item.weekdays.length
|
|
45
|
+
? item.weekdays.map((d) => DAY_NAMES[Number(d)] ?? d).join(", ")
|
|
46
|
+
: "";
|
|
47
|
+
const times = item.fromMinute !== null || item.untilMinute !== null
|
|
48
|
+
? `${item.fromMinute !== null ? `from ${clock(item.fromMinute)}` : ""}${item.fromMinute !== null && item.untilMinute !== null ? " " : ""}${item.untilMinute !== null ? `before ${clock(item.untilMinute)}` : ""}`
|
|
49
|
+
: "";
|
|
50
|
+
return [count, days, times].filter(Boolean).join(" · ");
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* What is left of a holding, in words, from the host's count: `2 of 3 left this week`. Display
|
|
54
|
+
* only: whether it can pay for a class is the host's, at booking.
|
|
55
|
+
*/
|
|
56
|
+
export function leftWords(h) {
|
|
57
|
+
if (!h.live) {
|
|
58
|
+
if (h.status === "void")
|
|
59
|
+
return "Void";
|
|
60
|
+
if (h.until && Date.parse(h.until) <= Date.now())
|
|
61
|
+
return `Ended ${day(h.until)}`;
|
|
62
|
+
if (h.status === "paused")
|
|
63
|
+
return "Paused";
|
|
64
|
+
if (h.status === "unpaid" || h.status === "ended")
|
|
65
|
+
return "Not active";
|
|
66
|
+
if (h.period && h.period.left === 0)
|
|
67
|
+
return "None left";
|
|
68
|
+
return "Not active";
|
|
69
|
+
}
|
|
70
|
+
const p = h.period;
|
|
71
|
+
if (!p || p.left === null)
|
|
72
|
+
return "Unlimited";
|
|
73
|
+
const when = h.per === "week" ? " this week" : h.per === "month" ? " this month" : "";
|
|
74
|
+
return h.uses !== null ? `${p.left} of ${h.uses} left${when}` : `${p.left} left${when}`;
|
|
75
|
+
}
|
|
76
|
+
/** What a membership's subscription means to its payer, in words. Display only. */
|
|
77
|
+
export function subscriptionStatus(sub) {
|
|
78
|
+
const ends = sub.cancelAtPeriodEnd ? sub.currentPeriodEnd : sub.cancelAt;
|
|
79
|
+
switch (sub.state) {
|
|
80
|
+
case "trialing":
|
|
81
|
+
return ends
|
|
82
|
+
? `Free, ending on ${day(ends)}: nothing will be charged`
|
|
83
|
+
: `Free until ${day(sub.trialEnd ?? sub.currentPeriodEnd)}, then charged each ${sub.interval}`;
|
|
84
|
+
case "active":
|
|
85
|
+
return ends
|
|
86
|
+
? `Active until ${day(ends)}, then it ends`
|
|
87
|
+
: `Active: next charge on ${day(sub.currentPeriodEnd)}`;
|
|
88
|
+
case "past_due":
|
|
89
|
+
return sub.actionUrl
|
|
90
|
+
? "A payment needs you to confirm it"
|
|
91
|
+
: "A payment failed: the payment provider will try again";
|
|
92
|
+
case "unpaid":
|
|
93
|
+
return "Unpaid: update your card to keep booking";
|
|
94
|
+
case "paused":
|
|
95
|
+
return "Paused";
|
|
96
|
+
case "incomplete":
|
|
97
|
+
return "Waiting for its first payment";
|
|
98
|
+
case "expired":
|
|
99
|
+
return "Never started: its first payment did not go through";
|
|
100
|
+
case "ended":
|
|
101
|
+
return `Ended${sub.endedAt ? ` on ${day(sub.endedAt)}` : ""}`;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** Whether a subscription is still the payer's to cancel, resume or pay (not ended). */
|
|
105
|
+
export const isLive = (sub) => sub.state !== "ended" && sub.state !== "expired";
|
|
106
|
+
/** What a member is told when the host refuses a pass (its `rule` and `reason`). Display only. */
|
|
107
|
+
export function refusalWords(details) {
|
|
108
|
+
if (details?.rule === "once_per_person") {
|
|
109
|
+
switch (details.reason) {
|
|
110
|
+
case "sign_in":
|
|
111
|
+
return "This offer is one per person: sign in to buy it.";
|
|
112
|
+
case "used":
|
|
113
|
+
return "This offer is one per person, and you have had yours.";
|
|
114
|
+
case "held":
|
|
115
|
+
return "This offer is one per person, and an order for yours is waiting to be paid.";
|
|
116
|
+
default:
|
|
117
|
+
return "This offer is one per person.";
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (details?.rule === "sale_window" || details?.rule === "not_for_sale") {
|
|
121
|
+
return "This is not on sale just now.";
|
|
122
|
+
}
|
|
123
|
+
return "We could not start your order.";
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The operations each hook calls (ADR 0453). The public's client gets the plans, the pass types,
|
|
127
|
+
* buying and subscribing; a customer's client also what they hold and their subscriptions; the
|
|
128
|
+
* staff client a person's holdings and giving or voiding a pass.
|
|
129
|
+
*/
|
|
130
|
+
export const MEMBERSHIPS_HOOKS_CALL = {
|
|
131
|
+
usePlans: ["x.memberships.plans"],
|
|
132
|
+
usePassTypes: ["x.memberships.passTypes"],
|
|
133
|
+
useBuyPass: ["x.memberships.buy", "order.checkout"],
|
|
134
|
+
useMine: ["x.memberships.mine"],
|
|
135
|
+
useHoldings: ["x.memberships.holdings"],
|
|
136
|
+
useGivePass: ["x.memberships.givePass"],
|
|
137
|
+
useVoidPass: ["x.memberships.voidPass"],
|
|
138
|
+
// A membership's billing (stage 32a's flows). Every action is the web's: a native block may
|
|
139
|
+
// read the subscription only (the block lint).
|
|
140
|
+
useSubscribe: ["subscription.start"],
|
|
141
|
+
useMySubscriptions: ["subscription.list"],
|
|
142
|
+
useCancelSubscription: ["subscription.cancel"],
|
|
143
|
+
useResumeSubscription: ["subscription.resume"],
|
|
144
|
+
useBillingPortal: ["subscription.portal"],
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* Hooks for a React or React Native app:
|
|
148
|
+
* `const memberships = createMembershipsHooks({ useQuery, useMutation }, api)`. The hooks a client
|
|
149
|
+
* cannot back are not there, and calling one does not compile.
|
|
150
|
+
*/
|
|
151
|
+
export function createMembershipsHooks(react, client) {
|
|
152
|
+
return packHooks(client, MEMBERSHIPS_HOOKS_CALL, (api) => allMembershipsHooks(react, api));
|
|
153
|
+
}
|
|
154
|
+
function allMembershipsHooks(react, api) {
|
|
155
|
+
const { useQuery, useMutation } = react;
|
|
156
|
+
return {
|
|
157
|
+
/** The membership plans on sale, with the host's prices and trial days. */
|
|
158
|
+
usePlans() {
|
|
159
|
+
return useQuery(api.query("x.memberships.plans", {}));
|
|
160
|
+
},
|
|
161
|
+
/** The passes on sale, with the host's prices. */
|
|
162
|
+
usePassTypes() {
|
|
163
|
+
return useQuery(api.query("x.memberships.passTypes", {}));
|
|
164
|
+
},
|
|
165
|
+
/** Start an order for a pass; then `checkout` it on the provider's page (web only). */
|
|
166
|
+
useBuyPass() {
|
|
167
|
+
return {
|
|
168
|
+
start: useMutation(api.mutation("x.memberships.buy")),
|
|
169
|
+
checkout: useMutation(api.mutation("order.checkout")),
|
|
170
|
+
};
|
|
171
|
+
},
|
|
172
|
+
/** The signed-in member's memberships and passes, with the host's count of what is left. */
|
|
173
|
+
useMine() {
|
|
174
|
+
return useQuery(api.query("x.memberships.mine", {}));
|
|
175
|
+
},
|
|
176
|
+
/** Staff: one person's memberships and passes. */
|
|
177
|
+
useHoldings(personId) {
|
|
178
|
+
return useQuery(api.query("x.memberships.holdings", { personId }));
|
|
179
|
+
},
|
|
180
|
+
/** Staff (a person): give a pass by hand, with a reason. */
|
|
181
|
+
useGivePass() {
|
|
182
|
+
return useMutation(api.mutation("x.memberships.givePass"));
|
|
183
|
+
},
|
|
184
|
+
/** Staff (a person): void a pass given by hand, with a reason. */
|
|
185
|
+
useVoidPass() {
|
|
186
|
+
return useMutation(api.mutation("x.memberships.voidPass"));
|
|
187
|
+
},
|
|
188
|
+
/** Start a membership: the host opens the provider's page (web only); the trial is the host's. */
|
|
189
|
+
useSubscribe() {
|
|
190
|
+
return useMutation(api.mutation("subscription.start"));
|
|
191
|
+
},
|
|
192
|
+
/** The signed-in payer's own membership subscriptions, newest first. */
|
|
193
|
+
useMySubscriptions() {
|
|
194
|
+
return useQuery(api.query("subscription.list", {
|
|
195
|
+
filter: { field: "planObject", op: "eq", value: PLAN },
|
|
196
|
+
limit: 20,
|
|
197
|
+
}));
|
|
198
|
+
},
|
|
199
|
+
/** End their own membership when its period ends (web only). */
|
|
200
|
+
useCancelSubscription() {
|
|
201
|
+
return useMutation(api.mutation("subscription.cancel"));
|
|
202
|
+
},
|
|
203
|
+
/** Undo that, before the period ends (web only). */
|
|
204
|
+
useResumeSubscription() {
|
|
205
|
+
return useMutation(api.mutation("subscription.resume"));
|
|
206
|
+
},
|
|
207
|
+
/** The provider's portal for their card and past invoices (web only, never in a frame). */
|
|
208
|
+
useBillingPortal() {
|
|
209
|
+
return useMutation(api.mutation("subscription.portal"));
|
|
210
|
+
},
|
|
211
|
+
};
|
|
212
|
+
}
|