@composable-os/pack-memberships 0.0.0-stage → 0.1.0-beta.10

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,559 @@
1
+ // Memberships (stage 32b): what a gym sells its members. A membership plan is paid every month
2
+ // (unlimited classes, or N a week or a month); a pass type is bought once (a class pack of N that
3
+ // expires, a drop-in as a pass of one, an intro offer once per person). What a member holds, a
4
+ // membership or a pass, is an allowance a booking spends (stage 31's Bookings pack).
5
+ //
6
+ // Everything that is a rule is the host's, declared here as behaviours (ADR 0404):
7
+ // - a plan is `sellable` with `recurring` (ADR 0485): the provider charges it every month, and the
8
+ // host's subscription to it grants a membership to its holder (`recurring.grants`, ADR 0510),
9
+ // made and moved by the host in each verified event's batch, its state and its end following the
10
+ // subscription (on trial, active, past due for at most 30 days, paused, ended). One free trial
11
+ // per person per plan object is the host's (ADR 0489);
12
+ // - a pass type is `sellable` (ADR 0435): a paid order line grants a pass with the type's uses and
13
+ // an expiry the host writes (`grants.valid`, ADR 0511); a full refund voids it, uses already
14
+ // spent stay spent; an intro offer is `onePerPerson` (ADR 0512): one per Person across every
15
+ // intro type, held from the order's start, refused to a guest who only typed an address;
16
+ // - a membership and a pass hold an `allowance` (ADRs 0493, 0494): uses per week, month or in
17
+ // all, the class types, weekdays and times of day they pay for, spent by a booking of their own
18
+ // Person, refused past the limit by the database for every actor;
19
+ // - every term of a membership or a pass is read-only: only the host (and this pack's own staff
20
+ // tools, which a person calls) writes it, never a caller of the generic record operations.
21
+ // The tools in tools.ts compose the host's operations and hold no rule of their own.
22
+ import { defineExtension } from "@composable-os/sdk";
23
+ import { buy, givePass, holdings, mine, passTypes, plans, voidPass } from "./tools.js";
24
+ /** Weekdays as the host's local time numbers them (ISO: 1 is Monday), for an off-peak limit. */
25
+ const WEEKDAYS = [
26
+ { value: "1", label: "Monday" },
27
+ { value: "2", label: "Tuesday" },
28
+ { value: "3", label: "Wednesday" },
29
+ { value: "4", label: "Thursday" },
30
+ { value: "5", label: "Friday" },
31
+ { value: "6", label: "Saturday" },
32
+ { value: "7", label: "Sunday" },
33
+ ];
34
+ const ON_SALE = [
35
+ { value: "draft", label: "Draft" },
36
+ { value: "on_sale", label: "On sale" },
37
+ { value: "retired", label: "No longer sold" },
38
+ ];
39
+ /**
40
+ * What a plan or a pass type allows, copied onto the membership or the pass it grants: class
41
+ * types (empty: every one), weekdays (empty: every day) and a time of day, in minutes after local
42
+ * midnight (empty: no bound).
43
+ */
44
+ const limits = (readOnly) => ({
45
+ classTypes: {
46
+ type: "relation",
47
+ label: "Class types",
48
+ to: "programs.classtype",
49
+ many: true,
50
+ description: "Empty: every class type.",
51
+ ...(readOnly ? { readOnly: true } : {}),
52
+ },
53
+ weekdays: {
54
+ type: "multiSelect",
55
+ label: "Days",
56
+ options: WEEKDAYS,
57
+ description: "Empty: every day.",
58
+ ...(readOnly ? { readOnly: true } : {}),
59
+ },
60
+ fromMinute: {
61
+ type: "number",
62
+ label: "Classes from (minutes after midnight)",
63
+ min: 0,
64
+ max: 1440,
65
+ step: 1,
66
+ description: "A class must start at or after this time of day: 540 is 09:00. Empty: any.",
67
+ ...(readOnly ? { readOnly: true } : {}),
68
+ },
69
+ untilMinute: {
70
+ type: "number",
71
+ label: "Classes before (minutes after midnight)",
72
+ min: 0,
73
+ max: 1440,
74
+ step: 1,
75
+ description: "A class must start before this time of day: 960 is 16:00. Empty: any.",
76
+ ...(readOnly ? { readOnly: true } : {}),
77
+ },
78
+ });
79
+ /**
80
+ * What a holder pays for: stage 31's bookings (`bookings.booking`, whose `paidBy` lists this pack),
81
+ * by the class type, the local weekday and the local minute the host copies onto the booking.
82
+ */
83
+ const SPENDS_ON = {
84
+ object: "bookings.booking",
85
+ when: [
86
+ { field: "classType", in: "classTypes" },
87
+ { field: "weekday", in: "weekdays" },
88
+ { field: "minute", atLeast: "fromMinute" },
89
+ { field: "minute", below: "untilMinute" },
90
+ ],
91
+ };
92
+ const RUNNING = ["trialing", "active", "past_due", "unpaid", "paused"];
93
+ /** A membership follows its subscription: the host moves it from any running state to another. */
94
+ const followMoves = [
95
+ ...RUNNING.map((to) => ({
96
+ from: RUNNING.filter((s) => s !== to),
97
+ to,
98
+ by: ["host"],
99
+ })),
100
+ { from: [...RUNNING], to: "ended", by: ["host"], final: true },
101
+ ];
102
+ export default defineExtension({
103
+ id: "memberships",
104
+ version: "0.1.0",
105
+ contractVersion: 1,
106
+ label: "Memberships",
107
+ description: "Membership plans paid every month and class passes bought once, with what each allows " +
108
+ "and what a member has left.",
109
+ // A plan and a pass name the class types they pay for: Programs' (stage 31 extends them).
110
+ requires: ["programs"],
111
+ permissions: {
112
+ read: ["person", "programs.classtype"],
113
+ operations: [
114
+ "public.list",
115
+ "public.get",
116
+ "my.list",
117
+ "my.get",
118
+ "record.create",
119
+ "record.update",
120
+ "record.get",
121
+ "record.list",
122
+ "allowance.get",
123
+ "allowance.list",
124
+ "order.start",
125
+ ],
126
+ modules: ["ops"],
127
+ },
128
+ objects: {
129
+ plan: {
130
+ label: "Membership plan",
131
+ plural: "Membership plans",
132
+ icon: "repeat",
133
+ titleField: "title",
134
+ description: "A membership paid every month: unlimited classes, or a number a week or a month, of " +
135
+ "the class types and times it allows. One free trial per person per plan.",
136
+ navigation: "main",
137
+ navigationOrder: 30,
138
+ fields: {
139
+ title: { type: "text", label: "Title", required: true },
140
+ summary: {
141
+ type: "text",
142
+ label: "What it gives, in a line",
143
+ description: 'Shown with the plan: "3 classes a week, off-peak".',
144
+ },
145
+ description: { type: "text", label: "Description" },
146
+ price: { type: "currency", label: "Price per month", required: true },
147
+ uses: {
148
+ type: "number",
149
+ label: "Classes per period",
150
+ min: 1,
151
+ max: 1000,
152
+ step: 1,
153
+ description: "Empty: unlimited.",
154
+ },
155
+ per: {
156
+ type: "select",
157
+ label: "Per",
158
+ required: true,
159
+ default: "week",
160
+ options: [
161
+ { value: "week", label: "A week" },
162
+ { value: "month", label: "A month" },
163
+ ],
164
+ },
165
+ ...limits(false),
166
+ trialDays: {
167
+ type: "number",
168
+ label: "Free trial (days)",
169
+ min: 0,
170
+ max: 90,
171
+ step: 1,
172
+ description: "Empty or 0: charged from the start. A person gets one trial on any plan.",
173
+ },
174
+ status: { type: "select", label: "Status", default: "draft", options: ON_SALE },
175
+ },
176
+ public: {
177
+ read: [
178
+ "title",
179
+ "summary",
180
+ "description",
181
+ "price",
182
+ "uses",
183
+ "per",
184
+ "classTypes",
185
+ "weekdays",
186
+ "fromMinute",
187
+ "untilMinute",
188
+ "trialDays",
189
+ ],
190
+ when: { field: "status", in: ["on_sale"] },
191
+ },
192
+ sellable: {
193
+ price: "price",
194
+ // Gym dues (the provider's "Fitness Centers - Dues and Membership Fees"): taxed only once
195
+ // a human admin turns tax on (ADR 0479). Whether the provider needs the gym's address as
196
+ // the place is an open check of the test-mode runbook.
197
+ tax: { code: "txcd_50021001" },
198
+ recurring: {
199
+ interval: "month",
200
+ trialDays: "trialDays",
201
+ // What a subscription to it gives its holder, kept in step by the host (ADR 0510).
202
+ grants: {
203
+ object: "membership",
204
+ set: {
205
+ person: "subscription.holder",
206
+ payer: "subscription.payer",
207
+ plan: "item",
208
+ subscription: "subscription",
209
+ title: "item.title",
210
+ uses: "item.uses",
211
+ per: "item.per",
212
+ classTypes: "item.classTypes",
213
+ weekdays: "item.weekdays",
214
+ fromMinute: "item.fromMinute",
215
+ untilMinute: "item.untilMinute",
216
+ },
217
+ from: "from",
218
+ until: "until",
219
+ states: {
220
+ trialing: "trialing",
221
+ active: "active",
222
+ past_due: "past_due",
223
+ unpaid: "unpaid",
224
+ paused: "paused",
225
+ ended: "ended",
226
+ },
227
+ },
228
+ },
229
+ },
230
+ },
231
+ membership: {
232
+ label: "Membership",
233
+ plural: "Memberships",
234
+ icon: "id-card",
235
+ titleField: "title",
236
+ description: "A member's membership: granted by the host when their subscription to a plan starts, " +
237
+ "and moved with it (on trial, active, past due, paused, ended). What it allows is the " +
238
+ "plan's as it was at the start.",
239
+ navigation: "main",
240
+ navigationOrder: 31,
241
+ fields: {
242
+ title: { type: "text", label: "Plan", readOnly: true },
243
+ person: { type: "relation", label: "Member", to: "person", readOnly: true },
244
+ payer: {
245
+ type: "relation",
246
+ label: "Paid by",
247
+ to: "person",
248
+ readOnly: true,
249
+ description: "Who pays: the member, or someone paying for them (a parent).",
250
+ },
251
+ plan: { type: "relation", label: "Plan", to: "memberships.plan", readOnly: true },
252
+ subscription: { type: "text", label: "Subscription", readOnly: true },
253
+ uses: { type: "number", label: "Classes per period", readOnly: true },
254
+ per: {
255
+ type: "select",
256
+ label: "Per",
257
+ readOnly: true,
258
+ options: [
259
+ { value: "week", label: "A week" },
260
+ { value: "month", label: "A month" },
261
+ ],
262
+ },
263
+ ...limits(true),
264
+ from: { type: "datetime", label: "From", readOnly: true },
265
+ until: {
266
+ type: "datetime",
267
+ label: "Until",
268
+ readOnly: true,
269
+ description: "When it stops: set when it is ending, has ended, or a payment is failing.",
270
+ },
271
+ status: {
272
+ type: "select",
273
+ label: "Status",
274
+ track: "timeline",
275
+ options: [
276
+ { value: "trialing", label: "On trial" },
277
+ { value: "active", label: "Active" },
278
+ { value: "past_due", label: "Past due" },
279
+ { value: "unpaid", label: "Unpaid" },
280
+ { value: "paused", label: "Paused" },
281
+ { value: "ended", label: "Ended" },
282
+ ],
283
+ },
284
+ },
285
+ states: { field: "status", initial: "active", moves: followMoves },
286
+ customer: {
287
+ field: "person",
288
+ read: [
289
+ "title",
290
+ "plan",
291
+ "uses",
292
+ "per",
293
+ "classTypes",
294
+ "weekdays",
295
+ "fromMinute",
296
+ "untilMinute",
297
+ "from",
298
+ "until",
299
+ "status",
300
+ ],
301
+ write: [],
302
+ },
303
+ allowance: {
304
+ holds: {
305
+ person: "person",
306
+ uses: "uses",
307
+ per: { field: "per" },
308
+ from: "from",
309
+ until: "until",
310
+ // Past due keeps everything while the provider retries (decision 4), up to `until`.
311
+ live: ["trialing", "active", "past_due"],
312
+ // An unlimited plan: empty uses is unlimited here, and only here.
313
+ emptyIsUnlimited: true,
314
+ spendsOn: SPENDS_ON,
315
+ },
316
+ },
317
+ timelineLinks: ["person"],
318
+ indexes: [["person"], ["plan"], ["subscription"], ["status"]],
319
+ },
320
+ passtype: {
321
+ label: "Pass type",
322
+ plural: "Pass types",
323
+ icon: "ticket",
324
+ titleField: "title",
325
+ description: "A pass bought once: a class pack of N that expires, a drop-in (a pass of one), or an " +
326
+ "intro offer, one per person.",
327
+ navigation: "main",
328
+ navigationOrder: 32,
329
+ fields: {
330
+ title: { type: "text", label: "Title", required: true },
331
+ summary: { type: "text", label: "What it gives, in a line" },
332
+ description: { type: "text", label: "Description" },
333
+ price: { type: "currency", label: "Price", required: true },
334
+ uses: {
335
+ type: "number",
336
+ label: "Classes",
337
+ required: true,
338
+ min: 1,
339
+ max: 1000,
340
+ step: 1,
341
+ },
342
+ validDays: {
343
+ type: "number",
344
+ label: "Valid for (days)",
345
+ min: 1,
346
+ max: 3650,
347
+ step: 1,
348
+ description: "From the day it is bought. Empty: it does not expire.",
349
+ },
350
+ ...limits(false),
351
+ onePerPerson: {
352
+ type: "boolean",
353
+ label: "Intro offer: one per person",
354
+ description: "A person buys one of these once, across every pass type marked so, signed in.",
355
+ },
356
+ status: { type: "select", label: "Status", default: "draft", options: ON_SALE },
357
+ },
358
+ public: {
359
+ read: [
360
+ "title",
361
+ "summary",
362
+ "description",
363
+ "price",
364
+ "uses",
365
+ "validDays",
366
+ "classTypes",
367
+ "weekdays",
368
+ "fromMinute",
369
+ "untilMinute",
370
+ "onePerPerson",
371
+ ],
372
+ when: { field: "status", in: ["on_sale"] },
373
+ },
374
+ sellable: {
375
+ price: "price",
376
+ tax: { code: "txcd_50021001" },
377
+ grants: {
378
+ object: "pass",
379
+ set: {
380
+ person: "order.person",
381
+ passtype: "item",
382
+ title: "line.title",
383
+ order: "order",
384
+ uses: "item.uses",
385
+ classTypes: "item.classTypes",
386
+ weekdays: "item.weekdays",
387
+ fromMinute: "item.fromMinute",
388
+ untilMinute: "item.untilMinute",
389
+ },
390
+ void: "void",
391
+ valid: { from: "from", until: "until", days: "item.validDays" },
392
+ },
393
+ onePerPerson: "onePerPerson",
394
+ },
395
+ },
396
+ pass: {
397
+ label: "Pass",
398
+ plural: "Passes",
399
+ icon: "ticket-check",
400
+ titleField: "title",
401
+ description: "A member's pass: granted by a paid order (or given by staff), with its classes and its " +
402
+ "expiry. Voided when its order is refunded in full; classes already taken stay taken.",
403
+ navigation: "main",
404
+ navigationOrder: 33,
405
+ fields: {
406
+ title: { type: "text", label: "Pass", readOnly: true },
407
+ person: { type: "relation", label: "Member", to: "person", readOnly: true },
408
+ passtype: {
409
+ type: "relation",
410
+ label: "Pass type",
411
+ to: "memberships.passtype",
412
+ readOnly: true,
413
+ },
414
+ order: { type: "text", label: "Order", readOnly: true },
415
+ uses: { type: "number", label: "Classes", readOnly: true },
416
+ ...limits(true),
417
+ from: { type: "datetime", label: "From", readOnly: true },
418
+ until: { type: "datetime", label: "Expires", readOnly: true },
419
+ source: {
420
+ type: "select",
421
+ label: "Where it came from",
422
+ readOnly: true,
423
+ default: "purchase",
424
+ options: [
425
+ { value: "purchase", label: "Bought" },
426
+ { value: "staff", label: "Given by staff" },
427
+ ],
428
+ },
429
+ reason: { type: "text", label: "Why it was given or voided", readOnly: true },
430
+ status: {
431
+ type: "select",
432
+ label: "Status",
433
+ track: "timeline",
434
+ options: [
435
+ { value: "active", label: "Active" },
436
+ { value: "void", label: "Void" },
437
+ ],
438
+ },
439
+ },
440
+ states: {
441
+ field: "status",
442
+ initial: "active",
443
+ moves: [
444
+ // A full refund of its order (the host), or a person on the staff with this pack's
445
+ // permission (`voidPass`, a person only); never a customer.
446
+ { from: "active", to: "void", by: ["host"], tools: ["voidPass"], final: true },
447
+ ],
448
+ },
449
+ customer: {
450
+ field: "person",
451
+ read: [
452
+ "title",
453
+ "passtype",
454
+ "uses",
455
+ "classTypes",
456
+ "weekdays",
457
+ "fromMinute",
458
+ "untilMinute",
459
+ "from",
460
+ "until",
461
+ "source",
462
+ "status",
463
+ ],
464
+ write: [],
465
+ },
466
+ allowance: {
467
+ holds: {
468
+ person: "person",
469
+ uses: "uses",
470
+ per: "total",
471
+ from: "from",
472
+ until: "until",
473
+ live: ["active"],
474
+ spendsOn: SPENDS_ON,
475
+ },
476
+ },
477
+ timelineLinks: ["person"],
478
+ indexes: [["person"], ["passtype"], ["order"], ["status"]],
479
+ },
480
+ },
481
+ views: {
482
+ activeMembers: {
483
+ label: "Active members",
484
+ object: "memberships.membership",
485
+ filter: { field: "status", op: "eq", value: "active" },
486
+ sort: { field: "from", direction: "desc" },
487
+ columns: ["person", "title", "uses", "per", "from", "until"],
488
+ },
489
+ onTrial: {
490
+ label: "On trial",
491
+ object: "memberships.membership",
492
+ filter: { field: "status", op: "eq", value: "trialing" },
493
+ sort: { field: "from", direction: "desc" },
494
+ columns: ["person", "title", "from", "until"],
495
+ },
496
+ pastDue: {
497
+ label: "Past due",
498
+ object: "memberships.membership",
499
+ filter: { field: "status", op: "eq", value: "past_due" },
500
+ sort: { field: "until", direction: "asc" },
501
+ columns: ["person", "title", "payer", "until"],
502
+ },
503
+ endingThisMonth: {
504
+ label: "Ending this month",
505
+ object: "memberships.membership",
506
+ filter: {
507
+ and: [
508
+ { field: "until", op: "gte", value: { $relative: "now" } },
509
+ { field: "until", op: "lt", value: { $relative: "startOfNextMonth" } },
510
+ ],
511
+ },
512
+ sort: { field: "until", direction: "asc" },
513
+ columns: ["person", "title", "status", "until"],
514
+ },
515
+ passesExpiringThisMonth: {
516
+ label: "Passes expiring this month",
517
+ object: "memberships.pass",
518
+ filter: {
519
+ and: [
520
+ { field: "status", op: "eq", value: "active" },
521
+ { field: "until", op: "gte", value: { $relative: "now" } },
522
+ { field: "until", op: "lt", value: { $relative: "startOfNextMonth" } },
523
+ ],
524
+ },
525
+ sort: { field: "until", direction: "asc" },
526
+ columns: ["person", "title", "uses", "until"],
527
+ },
528
+ },
529
+ recordTabs: {
530
+ personMemberships: {
531
+ label: "Memberships",
532
+ on: "person",
533
+ view: {
534
+ type: "table",
535
+ object: "memberships.membership",
536
+ relation: "person",
537
+ columns: ["title", "status", "uses", "per", "from", "until"],
538
+ sort: { field: "from", direction: "desc" },
539
+ },
540
+ order: 37,
541
+ },
542
+ personPasses: {
543
+ label: "Passes",
544
+ on: "person",
545
+ view: {
546
+ type: "table",
547
+ object: "memberships.pass",
548
+ relation: "person",
549
+ columns: ["title", "status", "uses", "source", "from", "until"],
550
+ sort: { field: "from", direction: "desc" },
551
+ },
552
+ order: 38,
553
+ },
554
+ },
555
+ // Giving or voiding a pass by hand: a person on the staff, with this permission, which admins
556
+ // and members hold and a human admin may grant to anyone else (ADR 0449).
557
+ grantable: ["memberships:passes"],
558
+ tools: { plans, passTypes, buy, mine, holdings, givePass, voidPass },
559
+ });