@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
@@ -0,0 +1,408 @@
1
+ // The `discount` behaviour (stage 34 step 4, ADR 0693): an object declares that its records are
2
+ // discounts (money off an order), and names the fields that say what is off, on what, for whom,
3
+ // when and how often. The host reads the record when it prices an order and enforces every rule:
4
+ // a code a buyer types, a member's price that applies by itself, one deal per order (the better
5
+ // for the buyer), and limits the database keeps under orders started at once. No caller sends an
6
+ // amount: a caller sends at most a code. Its declaration, its manifest schema and its contract
7
+ // checks live here; the host's side is packages/core/src/discounts/.
8
+ import { z } from "zod";
9
+ import type { ExtensionManifest, FieldManifest, ObjectManifest } from "./manifest.ts";
10
+ import { moveOrigins } from "./states.ts";
11
+
12
+ /**
13
+ * What a discount takes off: `percent` of each line it applies to (rounded down per line);
14
+ * `amount`, money off the order, spread over the lines it applies to in proportion; `price`, a set
15
+ * price for one of each line it applies to (a member's $15 drop-in), never above the line's own.
16
+ */
17
+ export const DISCOUNT_KINDS = ["percent", "amount", "price"] as const;
18
+ export type DiscountKind = (typeof DISCOUNT_KINDS)[number];
19
+
20
+ /**
21
+ * Who a discount is for. `anyone` includes a guest. Every other value needs a buyer who proved who
22
+ * they are (a signed-in customer, or staff buying for a Person): a guest never qualifies, the same
23
+ * answer for every address. `members`: the Person holds a live subscription (to one of `plans`
24
+ * when it names any). `new_customers`: no paid order and no subscription for any Person sharing a
25
+ * folded address with them (ADR 0692). `organisation`: the Person is currently at one of
26
+ * `organisations`. `tag`: the Person has the tag named in `tag`.
27
+ */
28
+ export const DISCOUNT_WHO = [
29
+ "anyone",
30
+ "signed_in",
31
+ "members",
32
+ "new_customers",
33
+ "organisation",
34
+ "tag",
35
+ ] as const;
36
+ export type DiscountWho = (typeof DISCOUNT_WHO)[number];
37
+
38
+ /**
39
+ * A promo code as the host keeps and compares it (ADR 0940): Unicode-normalised (NFKC), with no
40
+ * white space and no invisible characters (zero-width spaces, soft hyphens: Unicode's default
41
+ * ignorables), in upper case, so `été 26`, `ÉTÉ26` and `Été26` are one code, and non-ASCII letters
42
+ * match however they are typed. The host stores a code word folded when a record is written, and
43
+ * folds what a buyer types the same way before it looks the word up by its index. One function,
44
+ * here, for both.
45
+ */
46
+ export function foldCode(word: string): string {
47
+ return word
48
+ .normalize("NFKC")
49
+ .replace(/[\s\p{Default_Ignorable_Code_Point}]+/gu, "")
50
+ .toUpperCase()
51
+ .normalize("NFKC");
52
+ }
53
+
54
+ /** The least length of a code word, in characters after folding (ADR 0940): no `SALE`, no `A1`. */
55
+ export const CODE_MIN_LENGTH = 6;
56
+ /** The longest code word (and the longest a buyer may type). */
57
+ export const CODE_MAX_LENGTH = 64;
58
+
59
+ /**
60
+ * The letters a generated code is made of: 32, with no 0, 1, I or O, so nothing is misread and
61
+ * each character is 5 bits exactly (no bias from the random bytes).
62
+ */
63
+ export const CODE_ALPHABET = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
64
+
65
+ /**
66
+ * `count` random single-use codes (ADR 0940), each `length` characters of CODE_ALPHABET from
67
+ * `crypto.getRandomValues`: 16 by default, 80 bits, which no rate of guessing reaches. Already
68
+ * folded. For a pack's tool that makes a batch of codes; a person still approves writing them
69
+ * (ADR 0693).
70
+ */
71
+ export function randomCodes(count: number, length = 16): string[] {
72
+ if (!Number.isInteger(count) || count < 1 || count > 10_000) {
73
+ throw new Error("randomCodes makes 1 to 10,000 codes");
74
+ }
75
+ if (!Number.isInteger(length) || length < 12 || length > CODE_MAX_LENGTH) {
76
+ throw new Error(`A generated code is 12 to ${CODE_MAX_LENGTH} characters (60 bits or more)`);
77
+ }
78
+ // The Web Crypto global of every runtime the SDK runs in (Workers, browsers, Node 20+); the SDK
79
+ // is typed without DOM or Workers types, so it is named here.
80
+ const { crypto } = globalThis as unknown as {
81
+ crypto: { getRandomValues(array: Uint8Array): Uint8Array };
82
+ };
83
+ const codes = new Set<string>();
84
+ while (codes.size < count) {
85
+ const bytes = crypto.getRandomValues(new Uint8Array(length));
86
+ codes.add(Array.from(bytes, (b: number) => CODE_ALPHABET[b & 31]).join(""));
87
+ }
88
+ return [...codes];
89
+ }
90
+
91
+ /**
92
+ * Codes kept as records of another object of this extension (a promo code belongs to a
93
+ * discount): `discount` is that object's relation field (one record) to this object, `word` its
94
+ * text field holding what the buyer types (compared without case), `uses` an optional number field
95
+ * of the most orders one code may be used on (1: a single-use code), and `active` an optional
96
+ * boolean field that, off, voids the code.
97
+ */
98
+ export interface DiscountCodesSpec {
99
+ object: string;
100
+ discount: string;
101
+ word: string;
102
+ uses?: string;
103
+ active?: string;
104
+ }
105
+
106
+ /**
107
+ * Records that are discounts (ADR 0693). Every name is a field of this object.
108
+ *
109
+ * - `kind`: a select field whose options are among `percent`, `amount` and `price`; `percent` the
110
+ * number field of the percentage (0 to 100), `amount` the currency or number field of the amount
111
+ * off or the set price, in the currency's major unit.
112
+ * - `live`: the states (of this object's `states`) in which the discount applies. Entering one is
113
+ * a person's act: every move into a live state is by `["host"]` with the tools a person makes it
114
+ * through, or by staff writing a state field declared `personApproves`, and nothing is created
115
+ * live but by the host, so an assistant's request waits for a person. The host also holds any
116
+ * change to a live discount by anyone but a person for a person's approval.
117
+ * - How it applies: `automatic`, a boolean field (on: it applies by itself to whoever qualifies);
118
+ * `code`, a text field of one shared word; `codes`, codes kept as records of another object. At
119
+ * least one.
120
+ * - `who`: a select field whose options are among DISCOUNT_WHO (left out: anyone); `plans` a
121
+ * relation field to plans (`members`), `organisations` one to organisations, `tag` a text field.
122
+ * - On what: `objects`, a multi-select (or select) field whose option values are the objects it
123
+ * applies to (`bookings.booking`), and `records`, relation fields naming records it applies to.
124
+ * With neither, every line but a delivery. A line it applies to is any of either.
125
+ * - When: `from` and `until`, date or datetime fields (a date from the start of its day through
126
+ * its end, UTC).
127
+ * - Limits: `totalUses` (orders across everyone), `perPerson` (orders per Person, folded
128
+ * addresses counting as one), number fields; `minSpend`, a currency or number field of the least
129
+ * the lines it applies to must cost. A use is held from the order's start, used when it is paid,
130
+ * given back when it expires or is cancelled unpaid; a refund keeps it used.
131
+ *
132
+ * Not on an object that is sellable, has stock or a capacity: a discount is not bought.
133
+ */
134
+ export interface DiscountSpec {
135
+ kind: string;
136
+ percent?: string;
137
+ amount?: string;
138
+ live: readonly string[];
139
+ automatic?: string;
140
+ code?: string;
141
+ codes?: DiscountCodesSpec;
142
+ who?: string;
143
+ plans?: string;
144
+ organisations?: string;
145
+ tag?: string;
146
+ objects?: string;
147
+ records?: readonly string[];
148
+ from?: string;
149
+ until?: string;
150
+ totalUses?: string;
151
+ perPerson?: string;
152
+ minSpend?: string;
153
+ }
154
+
155
+ const fieldName = z.string().max(40);
156
+
157
+ export const discountManifestSchema = z
158
+ .object({
159
+ kind: fieldName,
160
+ percent: fieldName.optional(),
161
+ amount: fieldName.optional(),
162
+ live: z.array(z.string().min(1).max(100)).min(1).max(10),
163
+ automatic: fieldName.optional(),
164
+ code: fieldName.optional(),
165
+ codes: z
166
+ .object({
167
+ object: fieldName,
168
+ discount: fieldName,
169
+ word: fieldName,
170
+ uses: fieldName.optional(),
171
+ active: fieldName.optional(),
172
+ })
173
+ .strict()
174
+ .optional(),
175
+ who: fieldName.optional(),
176
+ plans: fieldName.optional(),
177
+ organisations: fieldName.optional(),
178
+ tag: fieldName.optional(),
179
+ objects: fieldName.optional(),
180
+ records: z.array(fieldName).min(1).max(5).optional(),
181
+ from: fieldName.optional(),
182
+ until: fieldName.optional(),
183
+ totalUses: fieldName.optional(),
184
+ perPerson: fieldName.optional(),
185
+ minSpend: fieldName.optional(),
186
+ })
187
+ .strict();
188
+ export type DiscountManifest = z.infer<typeof discountManifestSchema>;
189
+
190
+ const NUMBER: readonly string[] = ["number", "percent"];
191
+ const MONEY: readonly string[] = ["currency", "number"];
192
+ const WHEN: readonly string[] = ["date", "datetime"];
193
+
194
+ /** Reports problems with `objects.<name>.discount` through `contract`. */
195
+ export function checkObjectDiscount(
196
+ m: ExtensionManifest,
197
+ name: string,
198
+ object: ObjectManifest,
199
+ contract: (path: string, message: string) => void,
200
+ ): void {
201
+ const discount = object.discount;
202
+ if (!discount) return;
203
+ const path = `objects.${name}.discount`;
204
+ const fieldOf = (owner: ObjectManifest, local: string): FieldManifest | undefined =>
205
+ Object.hasOwn(owner.fields, local) ? owner.fields[local] : undefined;
206
+ const single = (field: FieldManifest | undefined, types: readonly string[]) =>
207
+ field !== undefined && types.includes(field.type) && !field.many;
208
+ const need = (
209
+ key: keyof DiscountSpec,
210
+ local: string | undefined,
211
+ types: readonly string[],
212
+ words: string,
213
+ ) => {
214
+ if (local === undefined) return;
215
+ if (!single(fieldOf(object, local), types)) {
216
+ contract(`${path}.${key}`, `"${local}" must be ${words} field of ${name}`);
217
+ }
218
+ };
219
+ /** The option values of a select field of this object, or null when it is not one. */
220
+ const optionsOf = (local: string, multi: boolean): string[] | null => {
221
+ const field = fieldOf(object, local);
222
+ const ok = field?.type === "select" || (multi && field?.type === "multiSelect");
223
+ return ok ? (field?.options ?? []).map((o) => o.value) : null;
224
+ };
225
+
226
+ // What is off.
227
+ const kinds = optionsOf(discount.kind, false);
228
+ if (!kinds) {
229
+ contract(`${path}.kind`, `"${discount.kind}" must be a select field of ${name}`);
230
+ } else {
231
+ for (const value of kinds) {
232
+ if (!(DISCOUNT_KINDS as readonly string[]).includes(value)) {
233
+ contract(
234
+ `${path}.kind`,
235
+ `"${value}" is not a kind of discount: the options of ${discount.kind} are among ${DISCOUNT_KINDS.join(", ")}`,
236
+ );
237
+ }
238
+ }
239
+ if (kinds.includes("percent") && discount.percent === undefined) {
240
+ contract(
241
+ `${path}.percent`,
242
+ "A percentage discount needs percent: the field of its percentage",
243
+ );
244
+ }
245
+ if ((kinds.includes("amount") || kinds.includes("price")) && discount.amount === undefined) {
246
+ contract(`${path}.amount`, "An amount or a set price needs amount: the field of the money");
247
+ }
248
+ }
249
+ need("percent", discount.percent, NUMBER, "a number or percent");
250
+ need("amount", discount.amount, MONEY, "a currency or number");
251
+
252
+ // How it applies.
253
+ if (
254
+ discount.automatic === undefined &&
255
+ discount.code === undefined &&
256
+ discount.codes === undefined
257
+ ) {
258
+ contract(
259
+ path,
260
+ "Say how the discount applies: automatic (a boolean field), code (a text field) or codes (records of another object)",
261
+ );
262
+ }
263
+ need("automatic", discount.automatic, ["boolean"], "a boolean");
264
+ need("code", discount.code, ["text"], "a text");
265
+ if (discount.codes) {
266
+ const codes = discount.codes;
267
+ const cp = `${path}.codes`;
268
+ const owner = Object.hasOwn(m.objects, codes.object) ? m.objects[codes.object] : undefined;
269
+ if (!owner || codes.object === name) {
270
+ contract(`${cp}.object`, `"${codes.object}" must be another object of this extension`);
271
+ } else {
272
+ const relation = fieldOf(owner, codes.discount);
273
+ if (relation?.type !== "relation" || relation.many || relation.to !== `${m.id}.${name}`) {
274
+ contract(
275
+ `${cp}.discount`,
276
+ `"${codes.discount}" must be a relation field (one record) of ${codes.object} to ${m.id}.${name}`,
277
+ );
278
+ }
279
+ if (!single(fieldOf(owner, codes.word), ["text"])) {
280
+ contract(`${cp}.word`, `"${codes.word}" must be a text field of ${codes.object}`);
281
+ }
282
+ if (codes.uses !== undefined && !single(fieldOf(owner, codes.uses), ["number"])) {
283
+ contract(`${cp}.uses`, `"${codes.uses}" must be a number field of ${codes.object}`);
284
+ }
285
+ if (codes.active !== undefined && !single(fieldOf(owner, codes.active), ["boolean"])) {
286
+ contract(`${cp}.active`, `"${codes.active}" must be a boolean field of ${codes.object}`);
287
+ }
288
+ if (owner.sellable || owner.discount) {
289
+ contract(`${cp}.object`, `${codes.object} holds codes: it is not sold and not a discount`);
290
+ }
291
+ }
292
+ }
293
+
294
+ // For whom.
295
+ if (discount.who !== undefined) {
296
+ const who = optionsOf(discount.who, false);
297
+ if (!who) {
298
+ contract(`${path}.who`, `"${discount.who}" must be a select field of ${name}`);
299
+ } else {
300
+ for (const value of who) {
301
+ if (!(DISCOUNT_WHO as readonly string[]).includes(value)) {
302
+ contract(
303
+ `${path}.who`,
304
+ `"${value}" is not who a discount is for: the options of ${discount.who} are among ${DISCOUNT_WHO.join(", ")}`,
305
+ );
306
+ }
307
+ }
308
+ if (who.includes("tag") && discount.tag === undefined) {
309
+ contract(`${path}.tag`, "A discount for a tag needs tag: the text field naming it");
310
+ }
311
+ if (who.includes("organisation") && discount.organisations === undefined) {
312
+ contract(
313
+ `${path}.organisations`,
314
+ "A discount for an organisation needs organisations: the relation field naming them",
315
+ );
316
+ }
317
+ }
318
+ }
319
+ const relation = (local: string | undefined) =>
320
+ local === undefined ? undefined : fieldOf(object, local);
321
+ if (discount.plans !== undefined) {
322
+ const field = relation(discount.plans);
323
+ if (field?.type !== "relation") {
324
+ contract(`${path}.plans`, `"${discount.plans}" must be a relation field to a plan`);
325
+ }
326
+ }
327
+ if (discount.organisations !== undefined) {
328
+ const field = relation(discount.organisations);
329
+ if (field?.type !== "relation" || field.to !== "organisation") {
330
+ contract(
331
+ `${path}.organisations`,
332
+ `"${discount.organisations}" must be a relation field to organisation`,
333
+ );
334
+ }
335
+ }
336
+ need("tag", discount.tag, ["text"], "a text");
337
+
338
+ // On what.
339
+ if (discount.objects !== undefined && !optionsOf(discount.objects, true)) {
340
+ contract(
341
+ `${path}.objects`,
342
+ `"${discount.objects}" must be a multi-select or select field of ${name} whose option values are objects`,
343
+ );
344
+ }
345
+ for (const [i, local] of (discount.records ?? []).entries()) {
346
+ if (relation(local)?.type !== "relation") {
347
+ contract(`${path}.records.${i}`, `"${local}" must be a relation field of ${name}`);
348
+ }
349
+ }
350
+
351
+ // When, and how often.
352
+ need("from", discount.from, WHEN, "a date or datetime");
353
+ need("until", discount.until, WHEN, "a date or datetime");
354
+ need("totalUses", discount.totalUses, ["number"], "a number");
355
+ need("perPerson", discount.perPerson, ["number"], "a number");
356
+ need("minSpend", discount.minSpend, MONEY, "a currency or number");
357
+
358
+ for (const key of ["sellable", "stock", "capacity"] as const) {
359
+ if (object[key] !== undefined) {
360
+ contract(`objects.${name}.${key}`, `A discount is not bought or counted: it has no ${key}`);
361
+ }
362
+ }
363
+
364
+ // Live states, entered only by a person (an AI's request waits), as stock moves are (ADR 0681).
365
+ const states = object.states;
366
+ if (!states) {
367
+ contract(`${path}.live`, `${name} must declare states: live names the states it applies in`);
368
+ return;
369
+ }
370
+ const stateField = fieldOf(object, states.field);
371
+ const values = new Set((stateField?.options ?? []).map((o) => o.value));
372
+ for (const live of discount.live) {
373
+ if (!values.has(live)) contract(`${path}.live`, `"${live}" is not a state of ${name}`);
374
+ }
375
+ const liveSet = new Set(discount.live);
376
+ if (liveSet.has(states.initial)) {
377
+ contract(
378
+ `${path}.live`,
379
+ `"${states.initial}" is where a new discount starts: a discount is made in another state and moved live by a person`,
380
+ );
381
+ }
382
+ // A person moves it live: through a tool a person makes the move with (a shipped pack, whose
383
+ // tools declare a permission), or by staff writing a state field a person approves (any
384
+ // extension: anyone else's write of it waits for a person, ADR 0507). Never a customer.
385
+ const personField = stateField?.personApproves === true;
386
+ states.moves.forEach((move, i) => {
387
+ if (!liveSet.has(move.to)) return;
388
+ // A move between two live states (active to paused and back) changes nothing a buyer gets.
389
+ if (moveOrigins(move).every((from) => liveSet.has(from))) return;
390
+ const byTool = move.by.every((mover) => mover === "host");
391
+ const byPerson = personField && move.by.every((mover) => mover !== "customer");
392
+ if (!byTool && !byPerson) {
393
+ contract(
394
+ `objects.${name}.states.moves.${i}.by`,
395
+ `Entering "${move.to}" makes a discount apply: declare it by ["host"] with the tools a person makes it through (states.moves.<n>.tools), or by ["staff"] with a state field a person approves (fields.${states.field}.personApproves)`,
396
+ );
397
+ }
398
+ });
399
+ for (const live of discount.live) {
400
+ const created = states.create?.in?.[live];
401
+ if (created?.some((mover) => mover !== "host")) {
402
+ contract(
403
+ `objects.${name}.states.create.in.${live}`,
404
+ `Creating a discount in "${live}" makes it apply: only the host may; create it in another state and move it through a tool`,
405
+ );
406
+ }
407
+ }
408
+ }
@@ -29,6 +29,8 @@
29
29
  // question, visibility/access.ts) or its `unless` key is true (a lesson marked as a free
30
30
  // preview). A `ref` to `media` may name the kind of item it takes (`media: "video"`).
31
31
 
32
+ import { MEDIA_KINDS, type MediaKind } from "./media.ts";
33
+
32
34
  /** The most a document may be, in bytes of its JSON, whatever a field declares. */
33
35
  export const DOCUMENT_MAX_BYTES = 524_288;
34
36
  /** A field that declares no `maxBytes`. */
@@ -111,7 +113,7 @@ export type DocumentHolders =
111
113
  | { keys: readonly string[]; unless?: string };
112
114
 
113
115
  /** The kinds of media item (stage 42, ADR 0475). */
114
- export type DocumentMediaKind = "image" | "video" | "file";
116
+ export type DocumentMediaKind = MediaKind;
115
117
  export interface DocumentArrayNode extends NodeBase {
116
118
  type: "array";
117
119
  of: DocumentNode;
@@ -404,7 +406,7 @@ export function checkDocumentSchema(
404
406
  }
405
407
  if (value.media !== undefined) {
406
408
  if (value.to !== "media") add(`${path}.media`, "media is for a ref to media");
407
- else if (!["image", "video", "file"].includes(String(value.media))) {
409
+ else if (!(MEDIA_KINDS as readonly string[]).includes(String(value.media))) {
408
410
  add(`${path}.media`, "media is image, video or file");
409
411
  }
410
412
  }
@@ -36,6 +36,9 @@ export const FIELD_TYPES = [
36
36
  // A video from the media library, held in Cloudflare Stream, played only with a token the host
37
37
  // signs (stage 42, ADR 0475).
38
38
  "video",
39
+ // One of the business's checklist templates, by id (stage 45 step 4b, ADR 0650): an active one
40
+ // is checked on write. Named by an object's `checklists` declaration.
41
+ "checklist",
39
42
  ] as const;
40
43
  export type FieldTypeName = (typeof FIELD_TYPES)[number];
41
44
 
@@ -54,6 +57,7 @@ export function formulaTypeOf(
54
57
  case "url":
55
58
  case "timezone":
56
59
  case "slug":
60
+ case "checklist":
57
61
  return "text";
58
62
  case "relation":
59
63
  return many ? null : "text";
@@ -104,6 +108,7 @@ export const LOSSLESS_TYPE_CHANGES: Readonly<Record<FieldTypeName, readonly Fiel
104
108
  slug: ["text", "longText"],
105
109
  image: [],
106
110
  video: [],
111
+ checklist: [],
107
112
  };
108
113
 
109
114
  export function isLosslessChange(from: FieldTypeName, to: FieldTypeName): boolean {
@@ -21,6 +21,16 @@ export type FormulaFieldRef =
21
21
  /** Another formula field, compiled on demand (cycles are reported). */
22
22
  | { expression: string };
23
23
 
24
+ /**
25
+ * The host's read-only published values a formula of a publishable object's own fields may read
26
+ * (ADR 0706): when the current version went live, and its number. Columns of the object's rec_*
27
+ * view, empty while it is not published.
28
+ */
29
+ export const PUBLISHED_FORMULA_FIELDS = {
30
+ publishedAt: { type: "datetime", sql: '"_pub_at"' },
31
+ publishedVersion: { type: "number", sql: '"_pub_version"' },
32
+ } as const satisfies Record<string, FormulaFieldRef>;
33
+
24
34
  export interface FormulaEnvironment {
25
35
  /** Fields of the same record, by name. Names are case-sensitive. */
26
36
  fields: Record<string, FormulaFieldRef>;
@@ -3,20 +3,32 @@
3
3
  // import { defineExtension, defineTool, z } from "@composable-os/sdk";
4
4
  // export default defineExtension({ id: "example", version: "1.0.0", contractVersion: 1, ... });
5
5
  export { z } from "zod";
6
+ export * from "./acknowledge.ts";
7
+ export * from "./agreements.ts";
6
8
  export * from "./allowance.ts";
7
9
  export * from "./automation.ts";
8
10
  export * from "./automation-check.ts";
11
+ export * from "./calendar-entry.ts";
9
12
  export * from "./capacity.ts";
13
+ export * from "./checklists.ts";
10
14
  export * from "./contract.ts";
15
+ export * from "./discount.ts";
11
16
  export * from "./document.ts";
12
17
  export * from "./field-types.ts";
18
+ export * from "./intake.ts";
19
+ export * from "./journey-signals.ts";
13
20
  export * from "./manifest.ts";
14
21
  export * from "./media.ts";
22
+ export * from "./mine.ts";
23
+ export * from "./occupies.ts";
15
24
  export * from "./permissions.ts";
25
+ export * from "./questions.ts";
16
26
  export * from "./recurring.ts";
17
27
  export * from "./renamed.ts";
28
+ export * from "./restricted-fields.ts";
18
29
  export * from "./rich-text.ts";
19
30
  export * from "./rich-text-fixture.ts";
31
+ export * from "./routine-mail.ts";
20
32
  export {
21
33
  type AutomationRunValue,
22
34
  AutomationStepError,
@@ -30,7 +42,12 @@ export {
30
42
  runAutomation,
31
43
  runExtension,
32
44
  } from "./runtime.ts";
45
+ export * from "./screens.ts";
33
46
  export * from "./sellable.ts";
34
47
  export * from "./slug.ts";
48
+ export * from "./staffed.ts";
35
49
  export * from "./states.ts";
50
+ export * from "./stock.ts";
51
+ export * from "./team.ts";
36
52
  export * from "./unique.ts";
53
+ export * from "./worked-on.ts";