@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,474 @@
1
+ // The `staffed` behaviour (stage 45 step 2, ADR 0621): a record that is a shift, a time someone
2
+ // works. Its declaration, its manifest schema and its contract checks live here; the host's side
3
+ // (the time claim, refused by the database, and the warnings) is packages/core/src/staffing/.
4
+ import { z } from "zod";
5
+ import type { ExtensionManifest, FieldManifest, ObjectManifest } from "./manifest.ts";
6
+
7
+ /**
8
+ * A shift (ADR 0621): a time, a kind of work, and at most one staff account working it. The host
9
+ * keeps a **time claim** for every live, covered shift and refuses, in the database, a second
10
+ * shift that overlaps one account's time, for every actor and however many write at once
11
+ * (`conflict`, `rule: "double_booked"`). It answers a write with **warnings** it does not refuse:
12
+ * the person is away, not qualified for the kind, or the shift is left with no one.
13
+ *
14
+ * - `who`: the person. `{ field: "staff" }` is a `user` field of this object (the staff account
15
+ * itself). `{ field: "coach", account: "user" }` is a relation (one record) to another of this
16
+ * extension's objects whose `user` field holds the account (a class's coach profile); a profile
17
+ * with no account covers the shift for members but claims no one's time.
18
+ * - `starts`, `ends`: datetime fields of this object, the shift's own time. Ranges are half-open:
19
+ * 06:00 to 07:00 and 07:00 to 08:00 do not clash.
20
+ * - `zone`: a timezone field (optional), the shift's local zone.
21
+ * - `kind`: a relation (one record) to what qualifications are for (a class type, a duty).
22
+ * - `live`: the states of the object's `states` in which the record is a shift. A record in any
23
+ * other state, or in the Trash, claims nothing.
24
+ * - `place` (optional): a field, or `relation.field`, shown on the roster and in the feed.
25
+ * - `pattern` (optional): the series field that is the shift's regular person (`link` is the
26
+ * series' `recurring.link` on this object, `who` the series field copied onto `who.field`).
27
+ * - `payBefore`, `payAfter` (optional): `kind.field` number paths, minutes counted for pay only
28
+ * (preparation and pack-down); they never enter the time claim.
29
+ * - `claimBefore`, `claimAfter` (optional, ADR 0760): `kind.field` number paths, minutes kept free
30
+ * before and after the shift (a service's buffer). They enter the time claim, never the shift's
31
+ * own times or pay; the kind must be another of this extension's objects, so the host follows a
32
+ * change of its minutes. Classes declare none: two back to back stay allowed.
33
+ * - `account` (optional, ADR 0760): `"required"`: a live shift whose person is a profile holding
34
+ * no account is refused, at write and by the database, so no shift with a person escapes the
35
+ * claim (an appointment). A shift with nobody on it stays allowed (uncovered). Only with
36
+ * `who.account`: a user field always is the account.
37
+ * - `private` (optional, ADR 0761): the record's title may name a client (an appointment). The
38
+ * roster shows the shift as its kind to anyone but its person and those who roster, a clash
39
+ * answer names it by its kind, and it is never offered to others. The feed never shows a title.
40
+ * - `open` (optional, ADRs 0762 and 0763): the shift is booked at free times (an appointment).
41
+ * The host answers the free start times of a kind (`staffing.free`): its providers' open hours,
42
+ * less time off, their claims with margins, taken rooms, closures and the client's own shifts,
43
+ * on the kind's step; and staff writes are warned `outside_hours` and `client_busy`.
44
+ * - `length`: `kind.field`, the minutes a shift of the kind lasts (a service's length).
45
+ * - `step` (optional): `kind.field`, the minutes between start times (15, 30 or 60; 30 when
46
+ * empty), counted from the organisation's local midnight.
47
+ * - `rooms` (optional): `kind.field`, a relation (many) of the kind to the things this object
48
+ * `occupies`, in the order they are offered; empty on a kind record, it needs none.
49
+ * - `client` (optional): a relation field (one record) of this object to a Person: one client's
50
+ * live shifts of this object do not overlap.
51
+ * - `closedBy` (optional): another of this extension's objects whose records are date ranges
52
+ * (`from`, `until`: date fields): nothing is free on their dates.
53
+ * - `customer` (optional): `true`: a create or move by a customer or the public must be on a
54
+ * free time, by every door, in the batch that writes it.
55
+ * - `evidence` (optional): what shows a shift was worked, beside its pay line (ADR 0627): another
56
+ * object of this extension whose records name the shift (`link`, a relation to this object) and
57
+ * the staff account that marked each (`by`, a user field): a class's bookings, ticked by whoever
58
+ * took the list. The host reads only that the list was taken, and by whom.
59
+ * - `timesFrom` (optional, ADR 0751): a relation (one record, required) to another of this
60
+ * extension's objects whose time this shift is: a crew member's place on a visit. The host
61
+ * copies the record's `starts`, `ends`, `zone` (when declared) and `kind` fields, which that
62
+ * object has under the same names, onto the shift at create, when it is pointed elsewhere, and
63
+ * whenever the record changes them, in the same statement (triggers the reconciler builds), so
64
+ * every place moves with its visit and the time claim is checked for each: a move that would put
65
+ * one person on two overlapping shifts fails whole, naming them. The shift's own time fields are
66
+ * then read-only, written by the host alone. Not with a series making the shift (`pattern`).
67
+ *
68
+ * Only shipped packs and the org's own extensions declare it: who works when feeds pay.
69
+ */
70
+ export interface StaffedSpec {
71
+ who: { field: string; account?: string };
72
+ starts: string;
73
+ ends: string;
74
+ zone?: string;
75
+ kind: string;
76
+ live: readonly string[];
77
+ place?: string;
78
+ pattern?: { link: string; who: string };
79
+ payBefore?: { field: string };
80
+ payAfter?: { field: string };
81
+ claimBefore?: { field: string };
82
+ claimAfter?: { field: string };
83
+ account?: "required";
84
+ private?: boolean;
85
+ open?: StaffedOpenSpec;
86
+ evidence?: { object: string; link: string; by: string };
87
+ timesFrom?: string;
88
+ }
89
+
90
+ /** A shift booked at free times (ADRs 0762 and 0763): see `StaffedSpec.open`. */
91
+ export interface StaffedOpenSpec {
92
+ length: { field: string };
93
+ step?: { field: string };
94
+ rooms?: { field: string };
95
+ client?: string;
96
+ closedBy?: { object: string; from: string; until: string };
97
+ customer?: true;
98
+ }
99
+
100
+ const fieldName = z.string().max(40);
101
+ const path = z.string().max(81);
102
+
103
+ export const staffedManifestSchema = z
104
+ .object({
105
+ who: z.object({ field: fieldName, account: fieldName.optional() }).strict(),
106
+ starts: fieldName,
107
+ ends: fieldName,
108
+ zone: fieldName.optional(),
109
+ kind: fieldName,
110
+ live: z.array(z.string().min(1).max(100)).min(1).max(20),
111
+ place: path.optional(),
112
+ pattern: z.object({ link: fieldName, who: fieldName }).strict().optional(),
113
+ payBefore: z.object({ field: path }).strict().optional(),
114
+ payAfter: z.object({ field: path }).strict().optional(),
115
+ claimBefore: z.object({ field: path }).strict().optional(),
116
+ claimAfter: z.object({ field: path }).strict().optional(),
117
+ account: z.literal("required").optional(),
118
+ private: z.boolean().optional(),
119
+ open: z
120
+ .object({
121
+ length: z.object({ field: path }).strict(),
122
+ step: z.object({ field: path }).strict().optional(),
123
+ rooms: z.object({ field: path }).strict().optional(),
124
+ client: fieldName.optional(),
125
+ closedBy: z
126
+ .object({ object: fieldName, from: fieldName, until: fieldName })
127
+ .strict()
128
+ .optional(),
129
+ customer: z.literal(true).optional(),
130
+ })
131
+ .strict()
132
+ .optional(),
133
+ evidence: z.object({ object: fieldName, link: fieldName, by: fieldName }).strict().optional(),
134
+ timesFrom: fieldName.optional(),
135
+ })
136
+ .strict();
137
+ export type StaffedManifest = z.infer<typeof staffedManifestSchema>;
138
+
139
+ /** Reports problems with `objects.<name>.staffed` through `contract`. */
140
+ export function checkObjectStaffed(
141
+ m: ExtensionManifest,
142
+ name: string,
143
+ object: ObjectManifest,
144
+ contract: (path: string, message: string) => void,
145
+ ): void {
146
+ const staffed = object.staffed;
147
+ if (!staffed) return;
148
+ const at = `objects.${name}.staffed`;
149
+ const own = (o: ObjectManifest, local: string): FieldManifest | undefined =>
150
+ Object.hasOwn(o.fields, local) ? o.fields[local] : undefined;
151
+ const single = (field: FieldManifest | undefined, types: readonly string[]) =>
152
+ field !== undefined && types.includes(field.type) && !field.many;
153
+
154
+ // The person.
155
+ const who = own(object, staffed.who.field);
156
+ if (staffed.who.account === undefined) {
157
+ if (!single(who, ["user"])) {
158
+ contract(`${at}.who.field`, `"${staffed.who.field}" must be a user field of ${name}`);
159
+ }
160
+ } else {
161
+ const target = who?.type === "relation" && !who.many ? who.to : undefined;
162
+ const local = target?.startsWith(`${m.id}.`) ? target.slice(m.id.length + 1) : undefined;
163
+ const holder = local && Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
164
+ if (!holder || local === name) {
165
+ contract(
166
+ `${at}.who.field`,
167
+ `"${staffed.who.field}" must be a relation field (one record) to another of this extension's objects`,
168
+ );
169
+ } else if (!single(own(holder, staffed.who.account), ["user"])) {
170
+ contract(
171
+ `${at}.who.account`,
172
+ `"${staffed.who.account}" must be a user field of ${target as string}`,
173
+ );
174
+ }
175
+ }
176
+ // The time and the zone.
177
+ for (const key of ["starts", "ends"] as const) {
178
+ if (!single(own(object, staffed[key]), ["datetime"])) {
179
+ contract(`${at}.${key}`, `"${staffed[key]}" must be a datetime field of ${name}`);
180
+ }
181
+ }
182
+ if (staffed.starts === staffed.ends) {
183
+ contract(`${at}.ends`, "A shift's start and end are two fields");
184
+ }
185
+ if (staffed.zone !== undefined && !single(own(object, staffed.zone), ["timezone"])) {
186
+ contract(`${at}.zone`, `"${staffed.zone}" must be a timezone field of ${name}`);
187
+ }
188
+
189
+ // The kind.
190
+ const kind = own(object, staffed.kind);
191
+ if (kind?.type !== "relation" || kind.many) {
192
+ contract(`${at}.kind`, `"${staffed.kind}" must be a relation field (one record) of ${name}`);
193
+ }
194
+
195
+ // The states in which it is a shift.
196
+ const states = object.states;
197
+ if (!states) {
198
+ contract(`${at}.live`, `${name} must declare states: a shift is live in some of them`);
199
+ } else {
200
+ const field = own(object, states.field);
201
+ const values = new Set((field?.options ?? []).map((o) => o.value));
202
+ for (const state of staffed.live) {
203
+ if (!values.has(state)) contract(`${at}.live`, `"${state}" is not a state of ${name}`);
204
+ }
205
+ if (new Set(staffed.live).size !== staffed.live.length) {
206
+ contract(`${at}.live`, "List each state once");
207
+ }
208
+ }
209
+
210
+ // Where it is.
211
+ if (staffed.place !== undefined) {
212
+ const [first, second, ...rest] = staffed.place.split(".");
213
+ const field = first === undefined ? undefined : own(object, first);
214
+ if (!field || rest.length || (second !== undefined && field.type !== "relation")) {
215
+ contract(
216
+ `${at}.place`,
217
+ `"${staffed.place}" must be a field of ${name}, or a relation of it and a field of what it names`,
218
+ );
219
+ }
220
+ }
221
+
222
+ // The pattern: a series of this extension making these records, copying its person onto them.
223
+ if (staffed.pattern) {
224
+ const series = Object.entries(m.objects).find(
225
+ ([, o]) => o.recurring?.generates === name && o.recurring.link === staffed.pattern?.link,
226
+ );
227
+ if (!series) {
228
+ contract(
229
+ `${at}.pattern.link`,
230
+ `"${staffed.pattern.link}" must be the link of a series of this extension that makes ${name}`,
231
+ );
232
+ } else if (series[1].recurring?.copy?.[staffed.who.field] !== staffed.pattern.who) {
233
+ contract(
234
+ `${at}.pattern.who`,
235
+ `${series[0]} must copy "${staffed.pattern.who}" onto "${staffed.who.field}"`,
236
+ );
237
+ }
238
+ }
239
+
240
+ // Minutes for pay only, read from the kind.
241
+ for (const key of ["payBefore", "payAfter"] as const) {
242
+ const declared = staffed[key];
243
+ if (!declared) continue;
244
+ const [via, field, ...rest] = declared.field.split(".");
245
+ if (via !== staffed.kind || !field || rest.length) {
246
+ contract(
247
+ `${at}.${key}.field`,
248
+ `"${declared.field}" must be ${staffed.kind}.<a number field of what it names>`,
249
+ );
250
+ }
251
+ }
252
+
253
+ // Minutes kept free around the shift, in its claim (ADR 0760): a number field of its kind, which
254
+ // is another of this extension's objects (the host builds triggers on its table to follow it).
255
+ const kindTarget = kind?.type === "relation" && !kind.many ? kind.to : undefined;
256
+ const kindLocal = kindTarget?.startsWith(`${m.id}.`)
257
+ ? kindTarget.slice(m.id.length + 1)
258
+ : undefined;
259
+ const kindObject =
260
+ kindLocal && kindLocal !== name && Object.hasOwn(m.objects, kindLocal)
261
+ ? m.objects[kindLocal]
262
+ : undefined;
263
+ for (const key of ["claimBefore", "claimAfter"] as const) {
264
+ const declared = staffed[key];
265
+ if (!declared) continue;
266
+ const [via, field, ...rest] = declared.field.split(".");
267
+ if (via !== staffed.kind || !field || rest.length) {
268
+ contract(
269
+ `${at}.${key}.field`,
270
+ `"${declared.field}" must be ${staffed.kind}.<a number field of what it names>`,
271
+ );
272
+ } else if (!kindObject) {
273
+ contract(
274
+ `${at}.${key}.field`,
275
+ `"${staffed.kind}" must name another of this extension's objects for minutes in the claim`,
276
+ );
277
+ } else if (!single(own(kindObject, field), ["number"])) {
278
+ contract(
279
+ `${at}.${key}.field`,
280
+ `"${field}" must be a number field of ${kindTarget as string}`,
281
+ );
282
+ }
283
+ }
284
+
285
+ // An account required: only a profile can hold none.
286
+ if (staffed.account === "required" && staffed.who.account === undefined) {
287
+ contract(
288
+ `${at}.account`,
289
+ `"required" needs who.account: "${staffed.who.field}" is a user field, always an account`,
290
+ );
291
+ }
292
+
293
+ // Booked at free times (ADRs 0762 and 0763): the kind's length, step and rooms, the client, the
294
+ // closures.
295
+ if (staffed.open) {
296
+ const open = staffed.open;
297
+ // A field of the kind (null once a problem with the path is reported).
298
+ const kindField = (key: "length" | "step" | "rooms"): FieldManifest | undefined | null => {
299
+ const declared = open[key];
300
+ if (!declared) return null;
301
+ const [via, field, ...rest] = declared.field.split(".");
302
+ if (via !== staffed.kind || !field || rest.length) {
303
+ contract(
304
+ `${at}.open.${key}.field`,
305
+ `"${declared.field}" must be ${staffed.kind}.<a field of what it names>`,
306
+ );
307
+ return null;
308
+ }
309
+ if (!kindObject) {
310
+ contract(
311
+ `${at}.open.${key}.field`,
312
+ `"${staffed.kind}" must name another of this extension's objects for free times`,
313
+ );
314
+ return null;
315
+ }
316
+ return own(kindObject, field);
317
+ };
318
+ for (const key of ["length", "step"] as const) {
319
+ const field = kindField(key);
320
+ if (field !== null && !single(field, ["number"])) {
321
+ contract(
322
+ `${at}.open.${key}.field`,
323
+ `"${open[key]?.field}" must be a number field of ${kindTarget as string}`,
324
+ );
325
+ }
326
+ }
327
+ const rooms = kindField("rooms");
328
+ if (rooms !== null) {
329
+ const thingField = object.occupies ? own(object, object.occupies.thing) : undefined;
330
+ if (!thingField) {
331
+ contract(`${at}.open.rooms`, `${name} must declare occupies for the rooms it is booked in`);
332
+ } else if (rooms?.type !== "relation" || !rooms.many || rooms.to !== thingField.to) {
333
+ contract(
334
+ `${at}.open.rooms.field`,
335
+ `"${open.rooms?.field}" must be a relation field (many) of ${kindTarget as string} to ${thingField.to as string}`,
336
+ );
337
+ }
338
+ }
339
+ if (open.client !== undefined) {
340
+ const client = own(object, open.client);
341
+ if (client?.type !== "relation" || client.many || client.to !== "person") {
342
+ contract(
343
+ `${at}.open.client`,
344
+ `"${open.client}" must be a relation field (one record) of ${name} to person`,
345
+ );
346
+ }
347
+ }
348
+ if (open.closedBy) {
349
+ const c = open.closedBy;
350
+ const closure =
351
+ c.object !== name && Object.hasOwn(m.objects, c.object) ? m.objects[c.object] : undefined;
352
+ if (!closure) {
353
+ contract(
354
+ `${at}.open.closedBy.object`,
355
+ `"${c.object}" must be another object of this extension`,
356
+ );
357
+ } else {
358
+ for (const key of ["from", "until"] as const) {
359
+ if (!single(own(closure, c[key]), ["date"])) {
360
+ contract(
361
+ `${at}.open.closedBy.${key}`,
362
+ `"${c[key]}" must be a date field of ${c.object}`,
363
+ );
364
+ }
365
+ }
366
+ }
367
+ }
368
+ }
369
+
370
+ // The evidence a shift was worked: another object of this extension naming it, and who marked.
371
+ if (staffed.evidence) {
372
+ const { object: other, link, by } = staffed.evidence;
373
+ const evidence =
374
+ other !== name && Object.hasOwn(m.objects, other) ? m.objects[other] : undefined;
375
+ const linkField = evidence ? own(evidence, link) : undefined;
376
+ if (!evidence) {
377
+ contract(`${at}.evidence.object`, `"${other}" must be another object of this extension`);
378
+ } else if (
379
+ linkField?.type !== "relation" ||
380
+ linkField.many ||
381
+ linkField.to !== `${m.id}.${name}`
382
+ ) {
383
+ contract(
384
+ `${at}.evidence.link`,
385
+ `"${link}" must be a relation field (one record) of ${other} to ${m.id}.${name}`,
386
+ );
387
+ } else if (!single(own(evidence, by), ["user"])) {
388
+ contract(`${at}.evidence.by`, `"${by}" must be a user field of ${other}`);
389
+ }
390
+ }
391
+
392
+ // Times that follow another record (ADR 0751): copied by the host from what a relation names.
393
+ if (staffed.timesFrom !== undefined) checkTimesFrom(m, name, object, staffed, contract);
394
+ }
395
+
396
+ /**
397
+ * `staffed.timesFrom` (ADR 0751): a required relation (one record) to another of this extension's
398
+ * objects that has the shift's time fields under the same names and types (its kind a relation to
399
+ * the same object); the shift's own copies are read-only, and no series makes the shift.
400
+ */
401
+ function checkTimesFrom(
402
+ m: ExtensionManifest,
403
+ name: string,
404
+ object: ObjectManifest,
405
+ staffed: StaffedManifest,
406
+ contract: (path: string, message: string) => void,
407
+ ): void {
408
+ const at = `objects.${name}.staffed.timesFrom`;
409
+ const local = staffed.timesFrom as string;
410
+ const own = (o: ObjectManifest, f: string): FieldManifest | undefined =>
411
+ Object.hasOwn(o.fields, f) ? o.fields[f] : undefined;
412
+ const link = own(object, local);
413
+ const target = link?.type === "relation" && !link.many ? link.to : undefined;
414
+ const parentName = target?.startsWith(`${m.id}.`) ? target.slice(m.id.length + 1) : undefined;
415
+ const parent =
416
+ parentName && parentName !== name && Object.hasOwn(m.objects, parentName)
417
+ ? m.objects[parentName]
418
+ : undefined;
419
+ if (!parent) {
420
+ contract(
421
+ at,
422
+ `"${local}" must be a relation field (one record) of ${name} to another of this extension's objects`,
423
+ );
424
+ return;
425
+ }
426
+ if (!link?.required) {
427
+ contract(at, `"${local}" must be required: a shift whose times follow a record names one`);
428
+ }
429
+ if (local === staffed.who.field || local === staffed.kind) {
430
+ contract(at, `"${local}" is the shift's person or kind, not the record its times follow`);
431
+ }
432
+ const followed: [string, string][] = [
433
+ [staffed.starts, "datetime"],
434
+ [staffed.ends, "datetime"],
435
+ ...(staffed.zone !== undefined ? ([[staffed.zone, "timezone"]] as [string, string][]) : []),
436
+ [staffed.kind, "relation"],
437
+ ];
438
+ for (const [field, type] of followed) {
439
+ const mine = own(object, field);
440
+ const theirs = own(parent, field);
441
+ if (
442
+ !theirs ||
443
+ theirs.type !== type ||
444
+ theirs.many ||
445
+ (type === "relation" && theirs.to !== mine?.to)
446
+ ) {
447
+ contract(
448
+ at,
449
+ `${target as string} must have "${field}" as ${name} has it (${type === "relation" ? `a relation to ${mine?.to ?? "the same object"}` : `a ${type} field`}): the host copies it from there`,
450
+ );
451
+ }
452
+ if (mine && !mine.readOnly) {
453
+ contract(
454
+ `objects.${name}.fields.${field}.readOnly`,
455
+ `"${field}" must be readOnly: the host copies it from ${local}, nobody edits it`,
456
+ );
457
+ }
458
+ }
459
+ // One step only (the review of PR #419): the host reads the shifts that follow a record before it
460
+ // moves, not theirs in turn, so a record whose own times follow another is not followed.
461
+ if (parent.staffed?.timesFrom !== undefined) {
462
+ contract(
463
+ at,
464
+ `${target as string}'s own times follow another record: a shift's times follow a record whose times are its own`,
465
+ );
466
+ }
467
+ const series = Object.values(m.objects).some((o) => o.recurring?.generates === name);
468
+ if (staffed.pattern || series) {
469
+ contract(
470
+ at,
471
+ `${name} is made by a series, which sets its times: its times cannot follow ${local}`,
472
+ );
473
+ }
474
+ }