@composable-os/cli 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.
Files changed (173) hide show
  1. package/dist/{accounts-X4CKM3HL.js → accounts-GOCNY6WI.js} +17 -14
  2. package/dist/app-RPRZMLU5.js +43 -0
  3. package/dist/app-upload-YJVEKZC6.js +198 -0
  4. package/dist/{blocks-YWDHEBOK.js → blocks-DE4RRWPJ.js} +207 -25
  5. package/dist/{chunk-VY64DMQO.js → chunk-2B6YUYZV.js} +23576 -11674
  6. package/dist/{run-RT2HVWMK.js → chunk-2CCHB37H.js} +9 -146
  7. package/dist/{chunk-DU6OIFOU.js → chunk-3OGDWKJ3.js} +530 -434
  8. package/dist/chunk-5D7BDF4Y.js +141 -0
  9. package/dist/{chunk-WO6D3XP3.js → chunk-6CM3WZG5.js} +13 -2470
  10. package/dist/chunk-CIBJGB5H.js +962 -0
  11. package/dist/chunk-EPDTPGZJ.js +330 -0
  12. package/dist/{chunk-LLULPZAJ.js → chunk-HAYF4G5V.js} +66 -23
  13. package/dist/chunk-HCG6T53D.js +61904 -0
  14. package/dist/chunk-JX4IFWNT.js +330 -0
  15. package/dist/{chunk-A4NTSYWV.js → chunk-KFHKNXGH.js} +786 -386
  16. package/dist/chunk-KMFMD52Z.js +13276 -0
  17. package/dist/{chunk-E4WCYTCT.js → chunk-LX2TTLVY.js} +174 -55
  18. package/dist/{chunk-GYTEW66E.js → chunk-O3R4FS2J.js} +1 -1
  19. package/dist/{chunk-LJFLENFC.js → chunk-Q6Y45DY6.js} +25 -2
  20. package/dist/{chunk-Y7JZFSTQ.js → chunk-QJZGYJVV.js} +823 -44
  21. package/dist/{chunk-6VXHPKGG.js → chunk-QQ3YSL5F.js} +172 -16
  22. package/dist/chunk-RGKJKC5T.js +876 -0
  23. package/dist/{chunk-IFG4FRW5.js → chunk-RSEWEFAV.js} +12 -2
  24. package/dist/{chunk-2ABSVMGB.js → chunk-SMJZIK2O.js} +1 -1
  25. package/dist/{chunk-CP6BXI6F.js → chunk-U72NEGCT.js} +8818 -3829
  26. package/dist/{chunk-WC5LHBBA.js → chunk-Z4IKF23A.js} +11 -7
  27. package/dist/chunk-Z7OVQ6BC.js +713 -0
  28. package/dist/{chunk-6UWRT4VM.js → chunk-ZJSFX4RD.js} +175 -3
  29. package/dist/{cli-2DFENR62.js → cli-OKQHHSOE.js} +17 -23
  30. package/dist/{cli-YIOVSKOD.js → cli-VYSD2S4Z.js} +231 -82
  31. package/dist/cli.js +5 -5
  32. package/dist/{client-repo-PRU3VCQQ.js → client-repo-NNUCHQ2Y.js} +7 -5
  33. package/dist/create.js +5 -5
  34. package/dist/{fields-2NC7LSYE.js → fields-GTHE6TTS.js} +27 -3
  35. package/dist/gateway-DPPEQZT4.js +25 -0
  36. package/dist/grants-6JY4XZO7.js +27 -0
  37. package/dist/invoke-MRONRZ5Q.js +33 -0
  38. package/dist/moves-UAUCMWL5.js +36 -0
  39. package/dist/{order-PUWD65BC.js → order-RKLGT74L.js} +2 -2
  40. package/dist/{person-WIQQ7ITU.js → person-YOZTFV4Q.js} +11 -11
  41. package/dist/plan-on-sale-H4F26YIN.js +26 -0
  42. package/dist/platform/migrations/0610_waitlists.sql +54 -0
  43. package/dist/platform/migrations/0630_subscription_changes.sql +122 -0
  44. package/dist/platform/migrations/0650_person_links.sql +90 -0
  45. package/dist/platform/migrations/0670_unmoved_guard.sql +52 -0
  46. package/dist/platform/migrations/0690_sellable_refund_due.sql +98 -0
  47. package/dist/platform/migrations/0730_practitioners.sql +79 -0
  48. package/dist/platform/migrations/0750_routine_mail.sql +98 -0
  49. package/dist/platform/migrations/0770_clear_fixed_role_grants.sql +64 -0
  50. package/dist/platform/migrations/0810_recurrence_exceptions.sql +66 -0
  51. package/dist/platform/migrations/0811_staff_time.sql +109 -0
  52. package/dist/platform/migrations/0812_staff_offers.sql +112 -0
  53. package/dist/platform/migrations/0813_pay_periods.sql +241 -0
  54. package/dist/platform/migrations/0814_staff_feed.sql +47 -0
  55. package/dist/platform/migrations/0815_checklists.sql +237 -0
  56. package/dist/platform/migrations/0816_checklist_schedules.sql +121 -0
  57. package/dist/platform/migrations/0817_checklist_day_window.sql +35 -0
  58. package/dist/platform/migrations/0850_agreements.sql +271 -0
  59. package/dist/platform/migrations/0851_agreement_acceptances.sql +387 -0
  60. package/dist/platform/migrations/0853_agreement_enforcement.sql +58 -0
  61. package/dist/platform/migrations/0854_agreement_copy.sql +149 -0
  62. package/dist/platform/migrations/0855_agreement_retention.sql +279 -0
  63. package/dist/platform/migrations/0856_subscription_cooling_off.sql +159 -0
  64. package/dist/platform/migrations/0857_agreement_copy_guardian.sql +337 -0
  65. package/dist/platform/migrations/0858_agreement_desk.sql +175 -0
  66. package/dist/platform/migrations/0910_routine_messages.sql +99 -0
  67. package/dist/platform/migrations/0911_texts.sql +60 -0
  68. package/dist/platform/migrations/0913_contact_consent.sql +93 -0
  69. package/dist/platform/migrations/0916_contact_mailbox_index.sql +29 -0
  70. package/dist/platform/migrations/0930_stock_movements.sql +95 -0
  71. package/dist/platform/migrations/0931_stock_low.sql +25 -0
  72. package/dist/platform/migrations/0932_order_delivery.sql +78 -0
  73. package/dist/platform/migrations/0950_order_adjustments.sql +111 -0
  74. package/dist/platform/migrations/0951_order_price_guards.sql +33 -0
  75. package/dist/platform/migrations/0952_order_paid_another_way.sql +194 -0
  76. package/dist/platform/migrations/0953_discount_uses.sql +183 -0
  77. package/dist/platform/migrations/0955_folded_addresses.sql +92 -0
  78. package/dist/platform/migrations/0970_published_text.sql +59 -0
  79. package/dist/platform/migrations/0971_publish_acks.sql +68 -0
  80. package/dist/platform/migrations/0990_customer_email_change.sql +65 -0
  81. package/dist/platform/migrations/1010_intake_links.sql +148 -0
  82. package/dist/platform/migrations/1030_invoices.sql +747 -0
  83. package/dist/platform/migrations/1031_invoice_orders.sql +327 -0
  84. package/dist/platform/migrations/1050_inbox_conversations.sql +83 -0
  85. package/dist/platform/migrations/1090_staff_hours.sql +43 -0
  86. package/dist/platform/migrations/1091_staff_time_margins.sql +12 -0
  87. package/dist/platform/migrations/1092_client_guard.sql +26 -0
  88. package/dist/platform/migrations/1095_thing_time.sql +70 -0
  89. package/dist/platform/migrations/1130_devices.sql +162 -0
  90. package/dist/platform/migrations/1135_person_codes.sql +88 -0
  91. package/dist/platform/migrations/1150_finance_periods.sql +316 -0
  92. package/dist/platform/migrations/1151_payouts.sql +202 -0
  93. package/dist/platform/migrations/1153_finance_money_dates.sql +18 -0
  94. package/dist/platform/migrations/1170_journeys.sql +318 -0
  95. package/dist/platform/migrations/1171_person_birthday.sql +52 -0
  96. package/dist/platform/migrations/1175_person_signals.sql +29 -0
  97. package/dist/platform/migrations/1190_restricted_values.sql +25 -0
  98. package/dist/platform/migrations/1230_note_levels.sql +113 -0
  99. package/dist/platform/migrations/1231_person_carers.sql +33 -0
  100. package/dist/platform/migrations/1232_note_kinds.sql +122 -0
  101. package/dist/platform/migrations/1270_time_entries.sql +145 -0
  102. package/dist/platform/migrations/1271_leave.sql +249 -0
  103. package/dist/platform/migrations/1273_time_entry_reopened.sql +21 -0
  104. package/dist/platform/migrations/1330_accounting.sql +260 -0
  105. package/dist/platform/migrations/1350_customer_feed.sql +24 -0
  106. package/dist/platform/migrations/1390_locations.sql +77 -0
  107. package/dist/platform/migrations/1410_outside_copy.sql +213 -0
  108. package/dist/platform/migrations/1430_inbox_activities.sql +231 -0
  109. package/dist/platform/migrations/1450_wrong_guesses.sql +23 -0
  110. package/dist/platform/sdk/acknowledge.ts +100 -0
  111. package/dist/platform/sdk/agreements.ts +185 -0
  112. package/dist/platform/sdk/allowance.ts +98 -8
  113. package/dist/platform/sdk/automation-check.ts +120 -9
  114. package/dist/platform/sdk/automation.ts +78 -10
  115. package/dist/platform/sdk/behaviour.ts +26 -6
  116. package/dist/platform/sdk/calendar-entry.ts +258 -0
  117. package/dist/platform/sdk/capacity.ts +103 -2
  118. package/dist/platform/sdk/checklists.ts +118 -0
  119. package/dist/platform/sdk/contract.ts +514 -27
  120. package/dist/platform/sdk/discount.ts +408 -0
  121. package/dist/platform/sdk/document.ts +4 -2
  122. package/dist/platform/sdk/field-types.ts +5 -0
  123. package/dist/platform/sdk/formula/compile.ts +10 -0
  124. package/dist/platform/sdk/index.ts +17 -0
  125. package/dist/platform/sdk/intake.ts +380 -0
  126. package/dist/platform/sdk/journey-signals.ts +215 -0
  127. package/dist/platform/sdk/manifest.ts +1313 -66
  128. package/dist/platform/sdk/mine.ts +161 -0
  129. package/dist/platform/sdk/occupies.ts +192 -0
  130. package/dist/platform/sdk/questions.ts +681 -0
  131. package/dist/platform/sdk/recurring.ts +90 -9
  132. package/dist/platform/sdk/restricted-fields.ts +129 -0
  133. package/dist/platform/sdk/rich-text.ts +112 -0
  134. package/dist/platform/sdk/routine-mail.ts +1272 -0
  135. package/dist/platform/sdk/runtime.ts +25 -12
  136. package/dist/platform/sdk/screens.ts +91 -0
  137. package/dist/platform/sdk/sellable.ts +595 -1
  138. package/dist/platform/sdk/slug.ts +7 -2
  139. package/dist/platform/sdk/staffed.ts +474 -0
  140. package/dist/platform/sdk/states.ts +220 -64
  141. package/dist/platform/sdk/stock.ts +332 -0
  142. package/dist/platform/sdk/team.ts +83 -0
  143. package/dist/platform/sdk/testing.ts +70 -6
  144. package/dist/platform/sdk/worked-on.ts +134 -0
  145. package/dist/{products-2H5DR232.js → products-GCQLSZ3U.js} +2 -2
  146. package/dist/run-7ID64RA6.js +65 -0
  147. package/dist/run-FH7OGGTV.js +131 -0
  148. package/dist/run-OYZIAP6M.js +3363 -0
  149. package/dist/{run-Y7T4TVOL.js → run-QOFRRSZV.js} +41 -60
  150. package/dist/{run-PZUCSDDL.js → run-TJAEWR5U.js} +21 -51
  151. package/dist/{src-XTOD7EXW.js → src-52VERUPK.js} +312 -4
  152. package/dist/src-PHRELWN5.js +40 -0
  153. package/dist/visibility-SZCINPP4.js +58 -0
  154. package/package.json +2 -2
  155. package/dist/chunk-6GDEDOV7.js +0 -157
  156. package/dist/chunk-6UPUF4ML.js +0 -29998
  157. package/dist/chunk-7CQGMVDO.js +0 -63
  158. package/dist/chunk-GYSGVT6R.js +0 -284
  159. package/dist/chunk-JJHHNV46.js +0 -227
  160. package/dist/chunk-K6T42YCH.js +0 -704
  161. package/dist/chunk-M7A6FR2Z.js +0 -2586
  162. package/dist/chunk-O2UIZHND.js +0 -249
  163. package/dist/chunk-Q4N55M2Z.js +0 -552
  164. package/dist/chunk-QGGJ2G6Z.js +0 -349
  165. package/dist/chunk-R4W5XAUD.js +0 -837
  166. package/dist/chunk-TQFGGSA4.js +0 -748
  167. package/dist/chunk-YJJHF65I.js +0 -759
  168. package/dist/chunk-Z3RMMB6S.js +0 -668
  169. package/dist/grants-QXMKYMGB.js +0 -32
  170. package/dist/invoke-I3NNNXVL.js +0 -39
  171. package/dist/plan-on-sale-NB774CQL.js +0 -26
  172. package/dist/src-GYS6NBQB.js +0 -44
  173. package/dist/visibility-DLGHU4YN.js +0 -33
@@ -6,7 +6,7 @@
6
6
  import { z } from "zod";
7
7
  import { customerWritable, spendsOnList } from "./allowance.ts";
8
8
  import type { ExtensionManifest, FieldManifest, ObjectManifest } from "./manifest.ts";
9
- import { moveOrigins } from "./states.ts";
9
+ import { moveOrigins, resolveFieldPath } from "./states.ts";
10
10
 
11
11
  /**
12
12
  * Where a granted record's field gets its value, when the host creates it:
@@ -107,6 +107,164 @@ export interface SellableSpec {
107
107
  * "sign_in"`). Such a line holds one, and an order at most one such line. Not on a plan.
108
108
  */
109
109
  onePerPerson?: string;
110
+ /**
111
+ * A paid line moves a record that already exists (stage 31a step 8, ADR 0560), see MovesSpec.
112
+ * A record of such an object is bought once, when it is made: `order.startHeld` makes it in
113
+ * `held` and starts its order in one batch. It is never put on `order.start`. Not with
114
+ * `grants`, `deposit`, `recurring`, `onePerPerson`, `maxPerOrder` or `saleWindow`.
115
+ */
116
+ moves?: MovesSpec;
117
+ /**
118
+ * What kind of sale this is in the finance reports, and when its money is earned (stage 52,
119
+ * ADR 0792), see FinanceSpec. Left out: category `other`, earned `at_sale`.
120
+ */
121
+ finance?: FinanceSpec;
122
+ /**
123
+ * The records are goods (stage 37, ADR 0682): things handed over or sent (a variant of an item
124
+ * in a shop), see GoodsSpec. An order with goods needs a delivery (`delivery`), and each goods
125
+ * line must be sold in the order's channel. Not with `deposit`, `recurring`, `moves` or
126
+ * `delivery`.
127
+ */
128
+ goods?: GoodsSpec;
129
+ /**
130
+ * The records are ways of getting goods to the buyer (stage 37, ADR 0682): a delivery option,
131
+ * see DeliverySpec. Never a line of `lines`: the buyer chooses one in `order.start`'s and
132
+ * `order.quote`'s `delivery`, and the host prices it (ADR 0685). Not with `goods`, `deposit`,
133
+ * `recurring`, `moves`, `onePerPerson` or `maxPerOrder` (one per order), nor beside `stock`.
134
+ */
135
+ delivery?: DeliverySpec;
136
+ }
137
+
138
+ /** Where goods are sold (stage 37 decision 3, ADR 0682): a select field holds one of these. */
139
+ export const SOLD_IN = ["online", "in_person", "both"] as const;
140
+ export type SoldIn = (typeof SOLD_IN)[number];
141
+
142
+ /** How a delivery option gets goods to the buyer (ADR 0682). */
143
+ export const DELIVERY_KINDS = ["post", "collect", "here"] as const;
144
+ export type DeliveryKind = (typeof DELIVERY_KINDS)[number];
145
+
146
+ /**
147
+ * Goods (ADR 0682). Every order with goods has a channel the host works out, never the caller's:
148
+ * a `post` or `collect` delivery is `online`, a `here` delivery ("take it now") is `in_person`.
149
+ *
150
+ * - `soldIn`: where a record is sold: a select field whose values are among `online`,
151
+ * `in_person` and `both`, of this object or through up to two relations (`item.whereSold`: a
152
+ * variant is sold where its item is). Left out or empty: both. A line not sold in the order's
153
+ * channel is refused (`rule: "not_sold_here"`), on every path.
154
+ */
155
+ export interface GoodsSpec {
156
+ soldIn?: string;
157
+ }
158
+
159
+ /**
160
+ * A delivery option (ADR 0682, priced by ADR 0685). A record is one way of getting an order's
161
+ * goods to the buyer; its object is sellable, so it has a price (which may be 0), a tax code and
162
+ * what a paid line grants (a shipment to pack, for `post` and `collect`; nothing is granted for a
163
+ * `here` record).
164
+ *
165
+ * - `kind`: a select field whose values are among `post` (an address is needed, in one of the
166
+ * option's countries), `collect` (at a place the record says; no address) and `here` (take it
167
+ * now, in person; no address).
168
+ * - `freeOver`: a currency or number field: when the goods on the order cost at least this
169
+ * (before the delivery), the delivery costs nothing. Empty: never free.
170
+ * - `countries`: a text field: the two-letter country codes (ISO 3166-1) a `post` record posts
171
+ * to, separated by commas or spaces; or `*`, everywhere no other post option of the object
172
+ * names. Empty: it posts nowhere yet.
173
+ * - `active`: a boolean field: a record with it off cannot be chosen.
174
+ */
175
+ export interface DeliverySpec {
176
+ kind: string;
177
+ freeOver?: string;
178
+ countries?: string;
179
+ active?: string;
180
+ }
181
+
182
+ /** How a sale's money is earned (ADR 0792), as the Unearned report reads it. */
183
+ export const EARNED = ["at_sale", "over_period", "per_use", "at_date"] as const;
184
+ export type Earned = (typeof EARNED)[number];
185
+
186
+ /**
187
+ * What kind of sale a sellable object is in the finance reports (stage 52, ADR 0792). It only
188
+ * labels the object's own sales: it prices nothing, grants nothing and moves no money. Which way
189
+ * the books treat a sale is the business's accountant's call; this says what the reports show.
190
+ *
191
+ * - `category`: the kind of sale its lines are counted under in Sales, Tax, Refunds and the book
192
+ * entries' `sales:<key>` and `refunds:<key>` headings. `key` is stable (lower case, digits and
193
+ * `_`); `label` is what the reports call it until an admin renames it. Two objects with the same
194
+ * key are one category, so this extension gives them one label.
195
+ * - `earned`: when the money is earned, for the Unearned report (cash is counted when it moved,
196
+ * whatever this says):
197
+ * - `at_sale`: when it is paid (a course, goods);
198
+ * - `over_period`: day by day over the order's period, from its start to its end (a plan's
199
+ * paid period); only on a plan (`recurring`);
200
+ * - `per_use`: as the uses of what a paid line grants are spent, the rest when it expires (a
201
+ * class pass); only when `grants` grants an object that holds uses (`allowance.holds`);
202
+ * - `at_date`: on the date in `at` (an event's start).
203
+ * - `at`: with `at_date`, a date or datetime field of this object, or `<relation>.<field>` through
204
+ * up to two relations (`event.startsAt`, `class.startsAt`).
205
+ *
206
+ * A line whose object issues stored value or a voucher (stage 34) is held for customers, not a
207
+ * sale, whatever it declares.
208
+ */
209
+ export interface FinanceSpec {
210
+ category?: { key: string; label: string };
211
+ earned?: Earned;
212
+ at?: string;
213
+ }
214
+
215
+ /** What `sellable.finance` comes to when it, or a part of it, is left out (ADR 0792). */
216
+ export const FINANCE_DEFAULTS = {
217
+ category: { key: "other", label: "Other" },
218
+ earned: "at_sale",
219
+ } as const satisfies Required<Omit<FinanceSpec, "at">>;
220
+
221
+ /** A sellable object's finance declaration with its defaults filled (ADR 0792). */
222
+ export function sellableFinance(finance: FinanceSpec | undefined): {
223
+ category: { key: string; label: string };
224
+ earned: Earned;
225
+ at?: string;
226
+ } {
227
+ return {
228
+ category: finance?.category ?? FINANCE_DEFAULTS.category,
229
+ earned: finance?.earned ?? FINANCE_DEFAULTS.earned,
230
+ ...(finance?.at !== undefined ? { at: finance.at } : {}),
231
+ };
232
+ }
233
+
234
+ /**
235
+ * What an order does to the record it was started for (ADR 0560): a drop-in booking held while
236
+ * its order is unpaid. The host makes every move, as the host, in the batch that moves the order.
237
+ *
238
+ * - `held`: the state `order.startHeld` makes the record in (`states.create.in` lets the host,
239
+ * and only the host, create it there). A capacity claimant holds its place in it.
240
+ * - `paid`: the order is paid: the record moves `held` -> `paid`.
241
+ * - `expired`: the order expired or was cancelled unpaid: `held` -> `expired`, a final state.
242
+ * A record that leaves `held` any other way (a class the gym cancels) cancels its unpaid order.
243
+ * - `refund`: states that give the money back. A record whose order is paid entering one of them
244
+ * queues the order's refund for a person with `orders:refund` to approve (never an AI).
245
+ * - `price`: where the price of one is read when the record is made: a number field through up
246
+ * to two relations (`class.classType.dropInPrice`), else the extension's number setting. The
247
+ * host writes it into the read-only `price` field, and the order's line is priced from it.
248
+ * - `title`: the order line's title, a text field through up to two relations (`class.title`).
249
+ * - `order`: a read-only text field the host writes the order's id into.
250
+ * - `guest`: a buyer who is not signed in is matched to no Person before payment: their record is
251
+ * held with its customer field empty (so it is no one's, blocks no one's own, and tells the
252
+ * buyer nothing about who has that address), and is given its Person when the payment is
253
+ * verified, by the rule a guest's order uses. The customer field must not be `required`; the
254
+ * host keeps it set in every state but `held`. Without it, a guest cannot buy the object.
255
+ * - `holdCap`: a number setting of the extension: the percent of the claimed record's places that
256
+ * unpaid guest holds may take at once (at least one). Over it a guest is told to try again soon.
257
+ */
258
+ export interface MovesSpec {
259
+ held: string;
260
+ paid: string;
261
+ expired: string;
262
+ refund?: readonly string[];
263
+ price?: { field?: string; setting?: string };
264
+ title?: string;
265
+ order?: string;
266
+ guest?: boolean;
267
+ holdCap?: { setting: string };
110
268
  }
111
269
 
112
270
  /**
@@ -141,8 +299,31 @@ export interface RecurringSpec {
141
299
  * `sellable.recurring`, and its `grants` is what the subscription gives while it runs.
142
300
  */
143
301
  grants?: RecurringGrantSpec;
302
+ /**
303
+ * Whether staff may pause a subscription to the plan (stage 32b, decision 10, ADR 0531), and
304
+ * for how long: `minDays` and `maxDays` name number fields of the plan holding the shortest and
305
+ * longest pause the company allows (empty: 1 day, and 366 days, the most the host asks the
306
+ * provider for). Left out, a subscription to the plan is never paused. Only staff pause, with
307
+ * `subscriptions:manage`, as a person; while paused nothing is charged, granted or spent.
308
+ */
309
+ pause?: { minDays?: string; maxDays?: string };
310
+ /**
311
+ * A cooling-off period (stage 44 step 7, decision 11, ADR 0647): `setting` names a number setting
312
+ * of this extension holding whole days (0: off), which a person changes (`personApproves`),
313
+ * between 0 and `MAX_COOLING_OFF_DAYS`. A new member (a subscription started here without a
314
+ * continuation offer) may end their subscription within that many days of the provider-confirmed
315
+ * start, counted in the business's time zone to the end of the last day, for a full refund of
316
+ * what they paid; each refund waits for a person with `orders:refund` (ADR 0429). Once per
317
+ * holder. The days in force when a subscription started are kept with it: a setting shortened
318
+ * later never shortens that member's window. What any law requires is the business's to
319
+ * confirm; the platform only holds the number.
320
+ */
321
+ coolingOff?: { setting: string };
144
322
  }
145
323
 
324
+ /** The longest cooling-off period the host keeps (days); a longer setting reads as this. */
325
+ export const MAX_COOLING_OFF_DAYS = 90;
326
+
146
327
  /** Where a field of what a subscription grants gets its value (see RecurringSpec.grants). */
147
328
  export type RecurringGrantSource =
148
329
  | "item"
@@ -256,6 +437,23 @@ export const sellableManifestSchema = z
256
437
  .strict()
257
438
  .optional(),
258
439
  onePerPerson: fieldName.optional(),
440
+ moves: z
441
+ .object({
442
+ held: z.string().min(1).max(100),
443
+ paid: z.string().min(1).max(100),
444
+ expired: z.string().min(1).max(100),
445
+ refund: z.array(z.string().min(1).max(100)).max(20).optional(),
446
+ price: z
447
+ .object({ field: z.string().max(130).optional(), setting: fieldName.optional() })
448
+ .strict()
449
+ .optional(),
450
+ title: z.string().max(130).optional(),
451
+ order: fieldName.optional(),
452
+ guest: z.boolean().optional(),
453
+ holdCap: z.object({ setting: fieldName }).strict().optional(),
454
+ })
455
+ .strict()
456
+ .optional(),
259
457
  tax: z
260
458
  .object({
261
459
  code: z.string().regex(TAX_CODE_PATTERN, "A product tax code: txcd_ and eight digits"),
@@ -277,6 +475,11 @@ export const sellableManifestSchema = z
277
475
  .object({
278
476
  interval: z.enum(["week", "month", "year"]),
279
477
  trialDays: fieldName.optional(),
478
+ pause: z
479
+ .object({ minDays: fieldName.optional(), maxDays: fieldName.optional() })
480
+ .strict()
481
+ .optional(),
482
+ coolingOff: z.object({ setting: fieldName }).strict().optional(),
280
483
  grants: z
281
484
  .object({
282
485
  object: z.string().max(40),
@@ -299,6 +502,33 @@ export const sellableManifestSchema = z
299
502
  })
300
503
  .strict()
301
504
  .optional(),
505
+ finance: z
506
+ .object({
507
+ category: z
508
+ .object({
509
+ key: z.string().regex(/^[a-z][a-z0-9_]{0,39}$/, "Lower case letters, digits and _"),
510
+ label: z.string().min(1).max(60),
511
+ })
512
+ .strict()
513
+ .optional(),
514
+ earned: z.enum(EARNED).optional(),
515
+ at: z.string().max(130).optional(),
516
+ })
517
+ .strict()
518
+ .optional(),
519
+ goods: z
520
+ .object({ soldIn: z.string().max(130).optional() })
521
+ .strict()
522
+ .optional(),
523
+ delivery: z
524
+ .object({
525
+ kind: fieldName,
526
+ freeOver: fieldName.optional(),
527
+ countries: fieldName.optional(),
528
+ active: fieldName.optional(),
529
+ })
530
+ .strict()
531
+ .optional(),
302
532
  })
303
533
  .strict();
304
534
  export type SellableManifest = z.infer<typeof sellableManifestSchema>;
@@ -366,6 +596,40 @@ export function checkObjectSellable(
366
596
  if (trial !== undefined && !single(trial, new Set(["number"]))) {
367
597
  contract(`${path}.recurring.trialDays`, `"${trial}" must be a number field of ${name}`);
368
598
  }
599
+ for (const key of ["minDays", "maxDays"] as const) {
600
+ const days = sellable.recurring.pause?.[key];
601
+ if (days !== undefined && !single(days, new Set(["number"]))) {
602
+ contract(`${path}.recurring.pause.${key}`, `"${days}" must be a number field of ${name}`);
603
+ }
604
+ }
605
+ const coolingOff = sellable.recurring.coolingOff;
606
+ if (coolingOff) {
607
+ const settings = m.settings ?? {};
608
+ const setting = Object.hasOwn(settings, coolingOff.setting)
609
+ ? settings[coolingOff.setting]
610
+ : undefined;
611
+ const at = `${path}.recurring.coolingOff.setting`;
612
+ if (setting?.type !== "number") {
613
+ contract(at, `"${coolingOff.setting}" must be a number setting of ${m.id}`);
614
+ } else if (setting.personApproves !== true) {
615
+ // Refunds follow from it: a person's to change, never an AI's (decision 11).
616
+ contract(
617
+ at,
618
+ `"${coolingOff.setting}" must be a setting a person approves (personApproves): the cooling-off days are never an AI's to change`,
619
+ );
620
+ } else if (
621
+ setting.min === undefined ||
622
+ setting.min < 0 ||
623
+ setting.max === undefined ||
624
+ setting.max > MAX_COOLING_OFF_DAYS ||
625
+ (typeof setting.default === "number" && setting.default !== 0)
626
+ ) {
627
+ contract(
628
+ at,
629
+ `"${coolingOff.setting}" must be bounded from 0 to at most ${MAX_COOLING_OFF_DAYS} days, and off (0 or no default) until a person sets it`,
630
+ );
631
+ }
632
+ }
369
633
  if (sellable.onePerPerson !== undefined) {
370
634
  contract(
371
635
  `${path}.onePerPerson`,
@@ -414,6 +678,11 @@ export function checkObjectSellable(
414
678
  }
415
679
  }
416
680
  if (sellable.grants) checkGrants(m, name, object, sellable.grants, `${path}.grants`, contract);
681
+ if (sellable.moves) checkMoves(m, name, object, sellable.moves, `${path}.moves`, contract);
682
+ if (sellable.goods || sellable.delivery) checkGoods(m, name, object, path, contract);
683
+ if (sellable.finance) {
684
+ checkFinance(m, name, object, sellable.finance, `${path}.finance`, contract);
685
+ }
417
686
  const place = sellable.tax?.place;
418
687
  if (place) {
419
688
  let holder: ObjectManifest | undefined = object;
@@ -451,6 +720,331 @@ export function checkObjectSellable(
451
720
  }
452
721
  }
453
722
 
723
+ /** A select field's option values (empty when it is not a select field). */
724
+ function selectValues(field: FieldManifest | undefined): string[] | null {
725
+ if (!field || field.type !== "select" || field.many) return null;
726
+ return (field.options ?? []).map((option) =>
727
+ typeof option === "string" ? option : (option as { value: string }).value,
728
+ );
729
+ }
730
+
731
+ /**
732
+ * Checks `sellable.goods` and `sellable.delivery` (ADR 0682): where goods are sold is a select of
733
+ * `online`, `in_person` and `both`; a delivery option's kind a select of `post`, `collect` and
734
+ * `here`, with its other fields of the right types; and neither is bought by deposit, by
735
+ * subscription or by moving a record. A delivery is one per order and has no stock.
736
+ */
737
+ function checkGoods(
738
+ m: ExtensionManifest,
739
+ name: string,
740
+ object: ObjectManifest,
741
+ path: string,
742
+ contract: (path: string, message: string) => void,
743
+ ): void {
744
+ const sellable = object.sellable as SellableManifest;
745
+ const fieldOf = (local: string): FieldManifest | undefined =>
746
+ Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
747
+ if (sellable.goods && sellable.delivery) {
748
+ contract(`${path}.delivery`, "A record is goods or a delivery option, not both");
749
+ }
750
+ const what = sellable.goods ? "goods" : "a delivery option";
751
+ for (const key of ["deposit", "recurring", "moves"] as const) {
752
+ if (sellable[key] !== undefined) {
753
+ contract(`${path}.${key}`, `${name} is ${what}, sold whole on an order: it has no ${key}`);
754
+ }
755
+ }
756
+ const soldIn = sellable.goods?.soldIn;
757
+ if (soldIn !== undefined) {
758
+ const values = selectValues(resolveFieldPath(m, object, soldIn));
759
+ if (!values || values.some((v) => !(SOLD_IN as readonly string[]).includes(v))) {
760
+ contract(
761
+ `${path}.goods.soldIn`,
762
+ `"${soldIn}" must be a select field (of ${name}, or through its relations) whose values are among ${SOLD_IN.join(", ")}`,
763
+ );
764
+ }
765
+ }
766
+ const delivery = sellable.delivery;
767
+ if (!delivery) return;
768
+ for (const key of ["onePerPerson", "maxPerOrder"] as const) {
769
+ if (sellable[key] !== undefined) {
770
+ contract(`${path}.${key}`, `A delivery option is one per order: it has no ${key}`);
771
+ }
772
+ }
773
+ if (object.stock) {
774
+ contract(`objects.${name}.stock`, "A delivery option is not counted on a shelf: no stock");
775
+ }
776
+ const kinds = selectValues(fieldOf(delivery.kind));
777
+ if (!kinds?.length || kinds.some((v) => !(DELIVERY_KINDS as readonly string[]).includes(v))) {
778
+ contract(
779
+ `${path}.delivery.kind`,
780
+ `"${delivery.kind}" must be a select field of ${name} whose values are among ${DELIVERY_KINDS.join(", ")}`,
781
+ );
782
+ }
783
+ if (delivery.freeOver !== undefined) {
784
+ const field = fieldOf(delivery.freeOver);
785
+ if (!field || !NUMERIC.has(field.type) || field.many) {
786
+ contract(
787
+ `${path}.delivery.freeOver`,
788
+ `"${delivery.freeOver}" must be a currency or number field of ${name}`,
789
+ );
790
+ }
791
+ }
792
+ if (delivery.countries !== undefined) {
793
+ const field = fieldOf(delivery.countries);
794
+ if (!field || !TEXT.has(field.type) || field.many) {
795
+ contract(
796
+ `${path}.delivery.countries`,
797
+ `"${delivery.countries}" must be a text field of ${name}`,
798
+ );
799
+ }
800
+ }
801
+ if (delivery.active !== undefined) {
802
+ const field = fieldOf(delivery.active);
803
+ if (!field || field.type !== "boolean" || field.many) {
804
+ contract(
805
+ `${path}.delivery.active`,
806
+ `"${delivery.active}" must be a boolean field of ${name}`,
807
+ );
808
+ }
809
+ }
810
+ }
811
+
812
+ /**
813
+ * Checks `sellable.finance` (ADR 0792): each way of earning names what the host reads to work it
814
+ * out, and one category key has one label across this extension.
815
+ */
816
+ function checkFinance(
817
+ m: ExtensionManifest,
818
+ name: string,
819
+ object: ObjectManifest,
820
+ finance: NonNullable<SellableManifest["finance"]>,
821
+ path: string,
822
+ contract: (path: string, message: string) => void,
823
+ ): void {
824
+ const sellable = object.sellable as SellableManifest;
825
+ const earned = finance.earned ?? FINANCE_DEFAULTS.earned;
826
+ if (earned === "at_date") {
827
+ if (finance.at === undefined) {
828
+ contract(`${path}.at`, "at_date names its date: at, a date or datetime field");
829
+ } else {
830
+ const at = resolveFieldPath(m, object, finance.at);
831
+ if (!at || at.many || !DATES.has(at.type)) {
832
+ contract(
833
+ `${path}.at`,
834
+ `"${finance.at}" must be a date or datetime field of ${name}, or <relation>.<field> through up to two relations, every field on the way declared by this extension`,
835
+ );
836
+ }
837
+ }
838
+ } else if (finance.at !== undefined) {
839
+ contract(`${path}.at`, "at is the date of at_date: declare earned: at_date, or remove at");
840
+ }
841
+ if (earned === "over_period" && !sellable.recurring) {
842
+ contract(
843
+ `${path}.earned`,
844
+ "over_period is earned over a paid period: only a plan (recurring) has one",
845
+ );
846
+ }
847
+ if (earned === "per_use") {
848
+ const grants = sellable.grants;
849
+ const local = grants?.object.startsWith(`${m.id}.`)
850
+ ? grants.object.slice(m.id.length + 1)
851
+ : grants?.object;
852
+ const granted =
853
+ local !== undefined && Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
854
+ if (!granted?.allowance || !("holds" in granted.allowance)) {
855
+ contract(
856
+ `${path}.earned`,
857
+ "per_use is earned as uses are spent: grants must grant an object that holds uses (allowance.holds)",
858
+ );
859
+ }
860
+ }
861
+ const category = finance.category;
862
+ if (category) {
863
+ for (const [other, o] of Object.entries(m.objects)) {
864
+ if (other === name) continue;
865
+ const theirs = o.sellable?.finance?.category;
866
+ if (theirs?.key === category.key && theirs.label !== category.label) {
867
+ contract(
868
+ `${path}.category.label`,
869
+ `${other} names category "${category.key}" "${theirs.label}": one category has one label`,
870
+ );
871
+ break;
872
+ }
873
+ }
874
+ }
875
+ }
876
+
877
+ /**
878
+ * Checks `sellable.moves` (ADR 0560): each of these is a way the order's batch could otherwise fail
879
+ * (a payment the host could not apply), or a record someone could hold with no order.
880
+ */
881
+ function checkMoves(
882
+ m: ExtensionManifest,
883
+ name: string,
884
+ object: ObjectManifest,
885
+ moves: NonNullable<SellableManifest["moves"]>,
886
+ path: string,
887
+ contract: (path: string, message: string) => void,
888
+ ): void {
889
+ const sellable = object.sellable as SellableManifest;
890
+ for (const key of [
891
+ "grants",
892
+ "deposit",
893
+ "recurring",
894
+ "onePerPerson",
895
+ "maxPerOrder",
896
+ "saleWindow",
897
+ ] as const) {
898
+ if (sellable[key] !== undefined) {
899
+ contract(
900
+ `objects.${name}.sellable.${key}`,
901
+ `A record bought with moves is one record on one order: it has no ${key}`,
902
+ );
903
+ }
904
+ }
905
+ const field = (local: string) =>
906
+ Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
907
+ const price = field(sellable.price);
908
+ if (price && !price.readOnly) {
909
+ contract(
910
+ `objects.${name}.fields.${sellable.price}`,
911
+ `"${sellable.price}" holds what the host priced it at: make it readOnly`,
912
+ );
913
+ }
914
+ if (moves.order !== undefined) {
915
+ const order = field(moves.order);
916
+ if (order?.type !== "text" || order.many || !order.readOnly) {
917
+ contract(`${path}.order`, `"${moves.order}" must be a readOnly text field of ${name}`);
918
+ }
919
+ }
920
+ if (moves.price) {
921
+ if (moves.price.field === undefined && moves.price.setting === undefined) {
922
+ contract(`${path}.price`, "price names a field, a setting or both");
923
+ }
924
+ if (moves.price.field !== undefined) {
925
+ const at = resolveFieldPath(m, object, moves.price.field);
926
+ if (!at || at.many || (at.type !== "number" && at.type !== "currency")) {
927
+ contract(
928
+ `${path}.price.field`,
929
+ `"${moves.price.field}" must be a number field of ${name}, or <relation>.<field> through up to two relations, every field on the way declared by this extension`,
930
+ );
931
+ }
932
+ }
933
+ if (moves.price.setting !== undefined) {
934
+ const settings = m.settings ?? {};
935
+ const setting = Object.hasOwn(settings, moves.price.setting)
936
+ ? settings[moves.price.setting]
937
+ : undefined;
938
+ if (setting?.type !== "number") {
939
+ contract(
940
+ `${path}.price.setting`,
941
+ `"${moves.price.setting}" must be a number setting of ${m.id}`,
942
+ );
943
+ }
944
+ }
945
+ }
946
+ if (moves.title !== undefined) {
947
+ const at = resolveFieldPath(m, object, moves.title);
948
+ if (!at || at.many || at.type !== "text") {
949
+ contract(
950
+ `${path}.title`,
951
+ `"${moves.title}" must be a text field of ${name}, or through up to two relations`,
952
+ );
953
+ }
954
+ }
955
+ if (!object.customer) contract(path, `${name} must declare customer: whose record it is`);
956
+ if (moves.guest && object.customer) {
957
+ const owner = field(object.customer.field);
958
+ if (owner?.required) {
959
+ contract(
960
+ `objects.${name}.fields.${object.customer.field}`,
961
+ `A guest's ${name} is held with no ${object.customer.field} until it is paid: it cannot be required (the host keeps it set outside "${moves.held}")`,
962
+ );
963
+ }
964
+ }
965
+ if (moves.holdCap) {
966
+ const settings = m.settings ?? {};
967
+ const setting = Object.hasOwn(settings, moves.holdCap.setting)
968
+ ? settings[moves.holdCap.setting]
969
+ : undefined;
970
+ if (setting?.type !== "number") {
971
+ contract(
972
+ `${path}.holdCap.setting`,
973
+ `"${moves.holdCap.setting}" must be a number setting of ${m.id}`,
974
+ );
975
+ }
976
+ if (!moves.guest) contract(`${path}.holdCap`, "holdCap limits guests' holds: declare guest");
977
+ if (!object.claims) contract(`${path}.holdCap`, `${name} must claim places (claims)`);
978
+ }
979
+ const states = object.states;
980
+ if (!states) {
981
+ contract(path, `${name} must declare states: the order moves it`);
982
+ return;
983
+ }
984
+ const values = new Set((object.fields[states.field]?.options ?? []).map((o) => o.value));
985
+ const finals = new Set(states.moves.filter((mv) => mv.final).map((mv) => mv.to));
986
+ for (const key of ["held", "paid", "expired"] as const) {
987
+ if (!values.has(moves[key])) {
988
+ contract(`${path}.${key}`, `"${moves[key]}" is not a state of ${name}`);
989
+ }
990
+ }
991
+ if (new Set([moves.held, moves.paid, moves.expired]).size !== 3) {
992
+ contract(path, "held, paid and expired are three different states");
993
+ }
994
+ const creators = states.create?.in?.[moves.held] ?? [];
995
+ if (!creators.includes("host")) {
996
+ contract(
997
+ `${path}.held`,
998
+ `states.create.in must let the host create ${name} in "${moves.held}"`,
999
+ );
1000
+ }
1001
+ if (creators.some((by) => by !== "host")) {
1002
+ contract(
1003
+ `${path}.held`,
1004
+ `Only the host creates ${name} in "${moves.held}": anyone else would hold it with no order`,
1005
+ );
1006
+ }
1007
+ const hostMove = (to: string) =>
1008
+ states.moves.some(
1009
+ (mv) => mv.to === to && moveOrigins(mv).includes(moves.held) && mv.by.includes("host"),
1010
+ );
1011
+ for (const key of ["paid", "expired"] as const) {
1012
+ if (!hostMove(moves[key])) {
1013
+ contract(
1014
+ `${path}.${key}`,
1015
+ `${name} needs a move from "${moves.held}" to "${moves[key]}" the host may make`,
1016
+ );
1017
+ }
1018
+ }
1019
+ if (!finals.has(moves.expired)) {
1020
+ contract(`${path}.expired`, `"${moves.expired}" must be a final state of ${name}`);
1021
+ }
1022
+ for (const mv of states.moves) {
1023
+ if (!moveOrigins(mv).includes(moves.held)) continue;
1024
+ if (mv.by.some((by) => by !== "host")) {
1025
+ contract(
1026
+ `${path}.held`,
1027
+ `Only the host moves ${name} out of "${moves.held}" (to "${mv.to}"): its order says where it goes`,
1028
+ );
1029
+ }
1030
+ }
1031
+ for (const [i, state] of (moves.refund ?? []).entries()) {
1032
+ if (!values.has(state) || state === moves.held || state === moves.expired) {
1033
+ contract(
1034
+ `${path}.refund.${i}`,
1035
+ `"${state}" must be a state of ${name} other than held and expired`,
1036
+ );
1037
+ }
1038
+ }
1039
+ const allowance = object.allowance;
1040
+ if (allowance && "spends" in allowance && !allowance.spends.waived) {
1041
+ contract(
1042
+ path,
1043
+ `${name} spends uses: declare allowance.spends.waived, which the host sets on a record its order pays for`,
1044
+ );
1045
+ }
1046
+ }
1047
+
454
1048
  function checkGrants(
455
1049
  m: ExtensionManifest,
456
1050
  name: string,
@@ -15,11 +15,16 @@ export const SLUG_FROM_TITLE_MAX = 80;
15
15
  * a hyphen, at most SLUG_FROM_TITLE_MAX characters. Empty when nothing is left.
16
16
  */
17
17
  export function slugify(title: string): string {
18
- const plain = title
18
+ return hyphenate(title, SLUG_FROM_TITLE_MAX);
19
+ }
20
+
21
+ /** Text as words of lowercase letters and digits joined by hyphens, accents dropped, at most `max`. */
22
+ export function hyphenate(text: string, max: number): string {
23
+ const plain = text
19
24
  .normalize("NFKD")
20
25
  .replace(/[̀-ͯ]/g, "")
21
26
  .toLowerCase()
22
27
  .replace(/[^a-z0-9]+/g, "-")
23
28
  .replace(/^-+|-+$/g, "");
24
- return plain.slice(0, SLUG_FROM_TITLE_MAX).replace(/-+$/g, "");
29
+ return plain.slice(0, max).replace(/-+$/g, "");
25
30
  }