@composable-os/sdk 0.1.0-beta.11 → 0.1.0-beta.14

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 (104) hide show
  1. package/dist/acknowledge.d.ts +38 -0
  2. package/dist/acknowledge.js +30 -0
  3. package/dist/agreements.d.ts +80 -0
  4. package/dist/agreements.js +109 -0
  5. package/dist/allowance.d.ts +47 -5
  6. package/dist/allowance.js +59 -4
  7. package/dist/automation-check.d.ts +6 -1
  8. package/dist/automation-check.js +93 -8
  9. package/dist/automation.d.ts +62 -4
  10. package/dist/automation.js +32 -9
  11. package/dist/behaviour.js +25 -6
  12. package/dist/calendar-entry.d.ts +97 -0
  13. package/dist/calendar-entry.js +180 -0
  14. package/dist/capacity.js +1 -2
  15. package/dist/checklists.d.ts +41 -0
  16. package/dist/checklists.js +75 -0
  17. package/dist/contract.d.ts +495 -26
  18. package/dist/contract.js +21 -3
  19. package/dist/discount.d.ts +135 -0
  20. package/dist/discount.js +281 -0
  21. package/dist/document.d.ts +2 -1
  22. package/dist/document.js +2 -1
  23. package/dist/field-types.d.ts +1 -1
  24. package/dist/field-types.js +5 -0
  25. package/dist/formula/compile.d.ts +15 -0
  26. package/dist/formula/compile.js +9 -0
  27. package/dist/index.d.ts +17 -0
  28. package/dist/index.js +17 -0
  29. package/dist/intake.d.ts +134 -0
  30. package/dist/intake.js +239 -0
  31. package/dist/journey-signals.d.ts +89 -0
  32. package/dist/journey-signals.js +151 -0
  33. package/dist/manifest.d.ts +944 -56
  34. package/dist/manifest.js +1036 -60
  35. package/dist/mine.d.ts +45 -0
  36. package/dist/mine.js +119 -0
  37. package/dist/occupies.d.ts +64 -0
  38. package/dist/occupies.js +122 -0
  39. package/dist/questions.d.ts +179 -0
  40. package/dist/questions.js +514 -0
  41. package/dist/recurring.d.ts +33 -4
  42. package/dist/recurring.js +50 -3
  43. package/dist/restricted-fields.d.ts +15 -0
  44. package/dist/restricted-fields.js +97 -0
  45. package/dist/rich-text.d.ts +22 -0
  46. package/dist/rich-text.js +91 -0
  47. package/dist/routine-mail.d.ts +400 -0
  48. package/dist/routine-mail.js +955 -0
  49. package/dist/runtime.d.ts +5 -11
  50. package/dist/runtime.js +21 -11
  51. package/dist/screens.d.ts +3 -0
  52. package/dist/screens.js +74 -0
  53. package/dist/sellable.d.ts +153 -2
  54. package/dist/sellable.js +188 -0
  55. package/dist/slug.d.ts +2 -0
  56. package/dist/slug.js +6 -2
  57. package/dist/staffed.d.ts +178 -0
  58. package/dist/staffed.js +287 -0
  59. package/dist/states.d.ts +17 -1
  60. package/dist/states.js +48 -20
  61. package/dist/stock.d.ts +129 -0
  62. package/dist/stock.js +205 -0
  63. package/dist/team.d.ts +28 -0
  64. package/dist/team.js +52 -0
  65. package/dist/testing.d.ts +22 -1
  66. package/dist/testing.js +47 -5
  67. package/dist/worked-on.d.ts +42 -0
  68. package/dist/worked-on.js +79 -0
  69. package/package.json +9 -1
  70. package/src/acknowledge.ts +100 -0
  71. package/src/agreements.ts +185 -0
  72. package/src/allowance.ts +98 -8
  73. package/src/automation-check.ts +120 -9
  74. package/src/automation.ts +78 -10
  75. package/src/behaviour.ts +26 -6
  76. package/src/calendar-entry.ts +258 -0
  77. package/src/capacity.ts +1 -2
  78. package/src/checklists.ts +118 -0
  79. package/src/contract.ts +489 -27
  80. package/src/discount.ts +408 -0
  81. package/src/document.ts +4 -2
  82. package/src/field-types.ts +5 -0
  83. package/src/formula/compile.ts +10 -0
  84. package/src/index.ts +17 -0
  85. package/src/intake.ts +380 -0
  86. package/src/journey-signals.ts +215 -0
  87. package/src/manifest.ts +1236 -65
  88. package/src/mine.ts +161 -0
  89. package/src/occupies.ts +192 -0
  90. package/src/questions.ts +681 -0
  91. package/src/recurring.ts +90 -9
  92. package/src/restricted-fields.ts +129 -0
  93. package/src/rich-text.ts +112 -0
  94. package/src/routine-mail.ts +1272 -0
  95. package/src/runtime.ts +25 -12
  96. package/src/screens.ts +91 -0
  97. package/src/sellable.ts +344 -0
  98. package/src/slug.ts +7 -2
  99. package/src/staffed.ts +474 -0
  100. package/src/states.ts +68 -26
  101. package/src/stock.ts +332 -0
  102. package/src/team.ts +83 -0
  103. package/src/testing.ts +70 -6
  104. package/src/worked-on.ts +134 -0
package/src/staffed.ts ADDED
@@ -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
+ }
package/src/states.ts CHANGED
@@ -75,6 +75,11 @@ export interface TimedMove {
75
75
  * relation) is in one of these states: a class whose attendance was taken has someone ticked.
76
76
  */
77
77
  ifAnyIn?: readonly string[];
78
+ /**
79
+ * Only while each of these fields of the record has a value (stage 49, ADR 0764): an appointment
80
+ * whose coach left is never marked attended for a session nobody gave.
81
+ */
82
+ ifSet?: readonly string[];
78
83
  }
79
84
 
80
85
  /**
@@ -144,6 +149,12 @@ export interface StatesSpec {
144
149
  * one, staff included (no booking into a class that was cancelled or is over).
145
150
  */
146
151
  closed?: string | readonly string[];
152
+ /**
153
+ * Fields every create must give a value (stage 49, ADR 0764), whoever makes it, the host
154
+ * included: an appointment is made for a client. Unlike `required`, the field may be emptied
155
+ * later (erasing the Person keeps the record for the business's books, ADR 0320).
156
+ */
157
+ requires?: readonly string[];
147
158
  };
148
159
  /**
149
160
  * Moves a caller asks for by name, resolved by the host (ADR 0503): writing the name (`cancel`)
@@ -158,7 +169,10 @@ export interface StatesSpec {
158
169
  * state named in `when`, the host moves every live record of this object pointing at it, and in
159
170
  * one of `from`, to the state `when` gives, in the same batch, as the host (a class cancelled
160
171
  * moves its bookings to `cancelled_by_us`: their places released, their uses given back). Each
161
- * target needs a `host` move from each of `from`.
172
+ * target needs a `host` move from each of `from`. The parent is another of this extension's
173
+ * objects, or one of an extension this one `requires`, named in full (`ticketing.event`); a
174
+ * parent others draw places from as a pool is followed only by an object that draws none
175
+ * (ADR 0841).
162
176
  */
163
177
  follows?: {
164
178
  relation: string;
@@ -225,6 +239,7 @@ export const statesManifestSchema = z.object({
225
239
  window: moveWindow.optional(),
226
240
  staffOnly: staffOnlyPaths.optional(),
227
241
  closed: staffOnlyPaths.optional(),
242
+ requires: z.array(fieldName).min(1).max(20).optional(),
228
243
  })
229
244
  .strict()
230
245
  .optional(),
@@ -251,6 +266,7 @@ export const statesManifestSchema = z.object({
251
266
  at: fieldPath,
252
267
  minutesAfter: z.number().int().min(0).max(10_080).optional(),
253
268
  ifAnyIn: z.array(stateValue).min(1).max(20).optional(),
269
+ ifSet: z.array(fieldName).min(1).max(5).optional(),
254
270
  })
255
271
  .strict(),
256
272
  )
@@ -409,40 +425,63 @@ export function checkObjectStates(
409
425
  for (const p of staffOnlyPathsOf(states.create.closed)) {
410
426
  checkPath(p, `${sp}.create.closed`, "boolean");
411
427
  }
428
+ for (const required of states.create.requires ?? []) {
429
+ if (!fieldOf(required)) {
430
+ contract(`${sp}.create.requires`, `"${required}" is not a field of ${name}`);
431
+ }
432
+ }
412
433
  }
413
434
  if (states.follows) {
414
435
  const fp = `${sp}.follows`;
415
436
  const f = states.follows;
416
437
  const rel = fieldOf(f.relation);
417
- const target =
418
- rel?.type === "relation" && !rel.many && rel.to?.startsWith(`${m.id}.`)
419
- ? m.objects[rel.to.slice(m.id.length + 1)]
420
- : undefined;
421
- if (!target?.states) {
438
+ const to = rel?.type === "relation" && !rel.many ? rel.to : undefined;
439
+ const own = to?.startsWith(`${m.id}.`) ? to.slice(m.id.length + 1) : undefined;
440
+ const target = own !== undefined && Object.hasOwn(m.objects, own) ? m.objects[own] : undefined;
441
+ // A parent in another extension this one requires (ADR 0841): its states are the host's to
442
+ // check, at install and on every upgrade of either extension (core's checkManifest).
443
+ const otherExt = to && own === undefined && to.includes(".") ? to.split(".")[0] : undefined;
444
+ const required = otherExt !== undefined && (m.requires ?? []).includes(otherExt);
445
+ if (!target?.states && !required) {
422
446
  contract(
423
447
  `${fp}.relation`,
424
- `"${f.relation}" must be a relation (one record) of ${name} to another of this extension's objects that declares states`,
448
+ `"${f.relation}" must be a relation (one record) of ${name} to another of this extension's objects that declares states, or to an object of an extension this one requires`,
425
449
  );
426
450
  } else {
427
- // A parent whose places other records draw on as a pool: its counter counts claims that
428
- // are not its own, which the cascade's check at commit cannot read (ADR 0550).
429
- const pooled = Object.values(m.objects).some((other) => {
430
- const poolField = other.capacity?.pool;
431
- const relTo = poolField ? other.fields[poolField]?.to : undefined;
432
- return relTo === rel?.to || relTo === rel?.to?.slice(m.id.length + 1);
433
- });
434
- if (pooled) {
435
- contract(
436
- `${fp}.relation`,
437
- `${rel?.to ?? f.relation} is a pool other records draw places from: a cascade cannot follow it`,
451
+ // A parent whose places other records draw on as a pool counts claims that are not its
452
+ // own, which the cascade's check at commit cannot read (ADR 0550). A child that draws on
453
+ // it (a member of the pool, or claiming places on the parent or on a member) is refused;
454
+ // one that draws nothing moves no places and may follow it (ADR 0841). A pool is always in
455
+ // the parent's own extension, so a parent in another extension is never drawn on from here.
456
+ if (target && own !== undefined) {
457
+ const localOf = (ref: string | undefined) =>
458
+ ref?.startsWith(`${m.id}.`) ? ref.slice(m.id.length + 1) : ref;
459
+ const poolOf = (other: ObjectManifest | undefined) => {
460
+ const poolField = other?.capacity?.pool;
461
+ return poolField ? localOf(other?.fields[poolField]?.to) : undefined;
462
+ };
463
+ const members = new Set(
464
+ Object.entries(m.objects)
465
+ .filter(([, other]) => poolOf(other) === own)
466
+ .map(([key]) => key),
438
467
  );
468
+ const claimsOn = object.claims ? localOf(object.fields[object.claims.on]?.to) : undefined;
469
+ const draws =
470
+ members.has(name) ||
471
+ (claimsOn !== undefined && (claimsOn === own || members.has(claimsOn)));
472
+ if (members.size && draws) {
473
+ contract(
474
+ `${fp}.relation`,
475
+ `${to} is a pool other records draw places from, and ${name} draws places from it: a cascade cannot follow it`,
476
+ );
477
+ }
439
478
  }
440
- const parentStates = new Set(
441
- (target.fields[target.states.field]?.options ?? []).map((o) => o.value),
442
- );
443
- for (const [when, to] of Object.entries(f.when)) {
444
- if (!parentStates.has(when)) {
445
- contract(`${fp}.when`, `"${when}" is not a state of ${rel?.to ?? f.relation}`);
479
+ const parentStates = target?.states
480
+ ? new Set((target.fields[target.states.field]?.options ?? []).map((o) => o.value))
481
+ : null;
482
+ for (const [when, into] of Object.entries(f.when)) {
483
+ if (parentStates && !parentStates.has(when)) {
484
+ contract(`${fp}.when`, `"${when}" is not a state of ${to ?? f.relation}`);
446
485
  }
447
486
  for (const from of f.from) {
448
487
  if (!isState(from))
@@ -450,10 +489,10 @@ export function checkObjectStates(
450
489
  else if (
451
490
  !states.moves.some(
452
491
  (move) =>
453
- move.to === to && move.by.includes("host") && moveOrigins(move).includes(from),
492
+ move.to === into && move.by.includes("host") && moveOrigins(move).includes(from),
454
493
  )
455
494
  ) {
456
- contract(`${fp}.when`, `${name} needs a host move from "${from}" to "${to}"`);
495
+ contract(`${fp}.when`, `${name} needs a host move from "${from}" to "${into}"`);
457
496
  }
458
497
  }
459
498
  }
@@ -485,6 +524,9 @@ export function checkObjectStates(
485
524
  `"${rule.at}" must be a datetime field of ${name}, or <relation>.<field> on another of this extension's objects`,
486
525
  );
487
526
  }
527
+ for (const f of rule.ifSet ?? []) {
528
+ if (!fieldOf(f)) contract(`${tp}.ifSet`, `"${f}" is not a field of ${name}`);
529
+ }
488
530
  if (rule.ifAnyIn) {
489
531
  if (parts.length !== 2) {
490
532
  contract(`${tp}.ifAnyIn`, "ifAnyIn needs at one relation away: the records it compares");