@composable-os/sdk 0.1.0-beta.11 → 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 (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
@@ -0,0 +1,178 @@
1
+ import { z } from "zod";
2
+ import type { ExtensionManifest, ObjectManifest } from "./manifest.ts";
3
+ /**
4
+ * A shift (ADR 0621): a time, a kind of work, and at most one staff account working it. The host
5
+ * keeps a **time claim** for every live, covered shift and refuses, in the database, a second
6
+ * shift that overlaps one account's time, for every actor and however many write at once
7
+ * (`conflict`, `rule: "double_booked"`). It answers a write with **warnings** it does not refuse:
8
+ * the person is away, not qualified for the kind, or the shift is left with no one.
9
+ *
10
+ * - `who`: the person. `{ field: "staff" }` is a `user` field of this object (the staff account
11
+ * itself). `{ field: "coach", account: "user" }` is a relation (one record) to another of this
12
+ * extension's objects whose `user` field holds the account (a class's coach profile); a profile
13
+ * with no account covers the shift for members but claims no one's time.
14
+ * - `starts`, `ends`: datetime fields of this object, the shift's own time. Ranges are half-open:
15
+ * 06:00 to 07:00 and 07:00 to 08:00 do not clash.
16
+ * - `zone`: a timezone field (optional), the shift's local zone.
17
+ * - `kind`: a relation (one record) to what qualifications are for (a class type, a duty).
18
+ * - `live`: the states of the object's `states` in which the record is a shift. A record in any
19
+ * other state, or in the Trash, claims nothing.
20
+ * - `place` (optional): a field, or `relation.field`, shown on the roster and in the feed.
21
+ * - `pattern` (optional): the series field that is the shift's regular person (`link` is the
22
+ * series' `recurring.link` on this object, `who` the series field copied onto `who.field`).
23
+ * - `payBefore`, `payAfter` (optional): `kind.field` number paths, minutes counted for pay only
24
+ * (preparation and pack-down); they never enter the time claim.
25
+ * - `claimBefore`, `claimAfter` (optional, ADR 0760): `kind.field` number paths, minutes kept free
26
+ * before and after the shift (a service's buffer). They enter the time claim, never the shift's
27
+ * own times or pay; the kind must be another of this extension's objects, so the host follows a
28
+ * change of its minutes. Classes declare none: two back to back stay allowed.
29
+ * - `account` (optional, ADR 0760): `"required"`: a live shift whose person is a profile holding
30
+ * no account is refused, at write and by the database, so no shift with a person escapes the
31
+ * claim (an appointment). A shift with nobody on it stays allowed (uncovered). Only with
32
+ * `who.account`: a user field always is the account.
33
+ * - `private` (optional, ADR 0761): the record's title may name a client (an appointment). The
34
+ * roster shows the shift as its kind to anyone but its person and those who roster, a clash
35
+ * answer names it by its kind, and it is never offered to others. The feed never shows a title.
36
+ * - `open` (optional, ADRs 0762 and 0763): the shift is booked at free times (an appointment).
37
+ * The host answers the free start times of a kind (`staffing.free`): its providers' open hours,
38
+ * less time off, their claims with margins, taken rooms, closures and the client's own shifts,
39
+ * on the kind's step; and staff writes are warned `outside_hours` and `client_busy`.
40
+ * - `length`: `kind.field`, the minutes a shift of the kind lasts (a service's length).
41
+ * - `step` (optional): `kind.field`, the minutes between start times (15, 30 or 60; 30 when
42
+ * empty), counted from the organisation's local midnight.
43
+ * - `rooms` (optional): `kind.field`, a relation (many) of the kind to the things this object
44
+ * `occupies`, in the order they are offered; empty on a kind record, it needs none.
45
+ * - `client` (optional): a relation field (one record) of this object to a Person: one client's
46
+ * live shifts of this object do not overlap.
47
+ * - `closedBy` (optional): another of this extension's objects whose records are date ranges
48
+ * (`from`, `until`: date fields): nothing is free on their dates.
49
+ * - `customer` (optional): `true`: a create or move by a customer or the public must be on a
50
+ * free time, by every door, in the batch that writes it.
51
+ * - `evidence` (optional): what shows a shift was worked, beside its pay line (ADR 0627): another
52
+ * object of this extension whose records name the shift (`link`, a relation to this object) and
53
+ * the staff account that marked each (`by`, a user field): a class's bookings, ticked by whoever
54
+ * took the list. The host reads only that the list was taken, and by whom.
55
+ * - `timesFrom` (optional, ADR 0751): a relation (one record, required) to another of this
56
+ * extension's objects whose time this shift is: a crew member's place on a visit. The host
57
+ * copies the record's `starts`, `ends`, `zone` (when declared) and `kind` fields, which that
58
+ * object has under the same names, onto the shift at create, when it is pointed elsewhere, and
59
+ * whenever the record changes them, in the same statement (triggers the reconciler builds), so
60
+ * every place moves with its visit and the time claim is checked for each: a move that would put
61
+ * one person on two overlapping shifts fails whole, naming them. The shift's own time fields are
62
+ * then read-only, written by the host alone. Not with a series making the shift (`pattern`).
63
+ *
64
+ * Only shipped packs and the org's own extensions declare it: who works when feeds pay.
65
+ */
66
+ export interface StaffedSpec {
67
+ who: {
68
+ field: string;
69
+ account?: string;
70
+ };
71
+ starts: string;
72
+ ends: string;
73
+ zone?: string;
74
+ kind: string;
75
+ live: readonly string[];
76
+ place?: string;
77
+ pattern?: {
78
+ link: string;
79
+ who: string;
80
+ };
81
+ payBefore?: {
82
+ field: string;
83
+ };
84
+ payAfter?: {
85
+ field: string;
86
+ };
87
+ claimBefore?: {
88
+ field: string;
89
+ };
90
+ claimAfter?: {
91
+ field: string;
92
+ };
93
+ account?: "required";
94
+ private?: boolean;
95
+ open?: StaffedOpenSpec;
96
+ evidence?: {
97
+ object: string;
98
+ link: string;
99
+ by: string;
100
+ };
101
+ timesFrom?: string;
102
+ }
103
+ /** A shift booked at free times (ADRs 0762 and 0763): see `StaffedSpec.open`. */
104
+ export interface StaffedOpenSpec {
105
+ length: {
106
+ field: string;
107
+ };
108
+ step?: {
109
+ field: string;
110
+ };
111
+ rooms?: {
112
+ field: string;
113
+ };
114
+ client?: string;
115
+ closedBy?: {
116
+ object: string;
117
+ from: string;
118
+ until: string;
119
+ };
120
+ customer?: true;
121
+ }
122
+ export declare const staffedManifestSchema: z.ZodObject<{
123
+ who: z.ZodObject<{
124
+ field: z.ZodString;
125
+ account: z.ZodOptional<z.ZodString>;
126
+ }, z.core.$strict>;
127
+ starts: z.ZodString;
128
+ ends: z.ZodString;
129
+ zone: z.ZodOptional<z.ZodString>;
130
+ kind: z.ZodString;
131
+ live: z.ZodArray<z.ZodString>;
132
+ place: z.ZodOptional<z.ZodString>;
133
+ pattern: z.ZodOptional<z.ZodObject<{
134
+ link: z.ZodString;
135
+ who: z.ZodString;
136
+ }, z.core.$strict>>;
137
+ payBefore: z.ZodOptional<z.ZodObject<{
138
+ field: z.ZodString;
139
+ }, z.core.$strict>>;
140
+ payAfter: z.ZodOptional<z.ZodObject<{
141
+ field: z.ZodString;
142
+ }, z.core.$strict>>;
143
+ claimBefore: z.ZodOptional<z.ZodObject<{
144
+ field: z.ZodString;
145
+ }, z.core.$strict>>;
146
+ claimAfter: z.ZodOptional<z.ZodObject<{
147
+ field: z.ZodString;
148
+ }, z.core.$strict>>;
149
+ account: z.ZodOptional<z.ZodLiteral<"required">>;
150
+ private: z.ZodOptional<z.ZodBoolean>;
151
+ open: z.ZodOptional<z.ZodObject<{
152
+ length: z.ZodObject<{
153
+ field: z.ZodString;
154
+ }, z.core.$strict>;
155
+ step: z.ZodOptional<z.ZodObject<{
156
+ field: z.ZodString;
157
+ }, z.core.$strict>>;
158
+ rooms: z.ZodOptional<z.ZodObject<{
159
+ field: z.ZodString;
160
+ }, z.core.$strict>>;
161
+ client: z.ZodOptional<z.ZodString>;
162
+ closedBy: z.ZodOptional<z.ZodObject<{
163
+ object: z.ZodString;
164
+ from: z.ZodString;
165
+ until: z.ZodString;
166
+ }, z.core.$strict>>;
167
+ customer: z.ZodOptional<z.ZodLiteral<true>>;
168
+ }, z.core.$strict>>;
169
+ evidence: z.ZodOptional<z.ZodObject<{
170
+ object: z.ZodString;
171
+ link: z.ZodString;
172
+ by: z.ZodString;
173
+ }, z.core.$strict>>;
174
+ timesFrom: z.ZodOptional<z.ZodString>;
175
+ }, z.core.$strict>;
176
+ export type StaffedManifest = z.infer<typeof staffedManifestSchema>;
177
+ /** Reports problems with `objects.<name>.staffed` through `contract`. */
178
+ export declare function checkObjectStaffed(m: ExtensionManifest, name: string, object: ObjectManifest, contract: (path: string, message: string) => void): void;
@@ -0,0 +1,287 @@
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
+ const fieldName = z.string().max(40);
6
+ const path = z.string().max(81);
7
+ export const staffedManifestSchema = z
8
+ .object({
9
+ who: z.object({ field: fieldName, account: fieldName.optional() }).strict(),
10
+ starts: fieldName,
11
+ ends: fieldName,
12
+ zone: fieldName.optional(),
13
+ kind: fieldName,
14
+ live: z.array(z.string().min(1).max(100)).min(1).max(20),
15
+ place: path.optional(),
16
+ pattern: z.object({ link: fieldName, who: fieldName }).strict().optional(),
17
+ payBefore: z.object({ field: path }).strict().optional(),
18
+ payAfter: z.object({ field: path }).strict().optional(),
19
+ claimBefore: z.object({ field: path }).strict().optional(),
20
+ claimAfter: z.object({ field: path }).strict().optional(),
21
+ account: z.literal("required").optional(),
22
+ private: z.boolean().optional(),
23
+ open: z
24
+ .object({
25
+ length: z.object({ field: path }).strict(),
26
+ step: z.object({ field: path }).strict().optional(),
27
+ rooms: z.object({ field: path }).strict().optional(),
28
+ client: fieldName.optional(),
29
+ closedBy: z
30
+ .object({ object: fieldName, from: fieldName, until: fieldName })
31
+ .strict()
32
+ .optional(),
33
+ customer: z.literal(true).optional(),
34
+ })
35
+ .strict()
36
+ .optional(),
37
+ evidence: z.object({ object: fieldName, link: fieldName, by: fieldName }).strict().optional(),
38
+ timesFrom: fieldName.optional(),
39
+ })
40
+ .strict();
41
+ /** Reports problems with `objects.<name>.staffed` through `contract`. */
42
+ export function checkObjectStaffed(m, name, object, contract) {
43
+ const staffed = object.staffed;
44
+ if (!staffed)
45
+ return;
46
+ const at = `objects.${name}.staffed`;
47
+ const own = (o, local) => Object.hasOwn(o.fields, local) ? o.fields[local] : undefined;
48
+ const single = (field, types) => field !== undefined && types.includes(field.type) && !field.many;
49
+ // The person.
50
+ const who = own(object, staffed.who.field);
51
+ if (staffed.who.account === undefined) {
52
+ if (!single(who, ["user"])) {
53
+ contract(`${at}.who.field`, `"${staffed.who.field}" must be a user field of ${name}`);
54
+ }
55
+ }
56
+ else {
57
+ const target = who?.type === "relation" && !who.many ? who.to : undefined;
58
+ const local = target?.startsWith(`${m.id}.`) ? target.slice(m.id.length + 1) : undefined;
59
+ const holder = local && Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
60
+ if (!holder || local === name) {
61
+ contract(`${at}.who.field`, `"${staffed.who.field}" must be a relation field (one record) to another of this extension's objects`);
62
+ }
63
+ else if (!single(own(holder, staffed.who.account), ["user"])) {
64
+ contract(`${at}.who.account`, `"${staffed.who.account}" must be a user field of ${target}`);
65
+ }
66
+ }
67
+ // The time and the zone.
68
+ for (const key of ["starts", "ends"]) {
69
+ if (!single(own(object, staffed[key]), ["datetime"])) {
70
+ contract(`${at}.${key}`, `"${staffed[key]}" must be a datetime field of ${name}`);
71
+ }
72
+ }
73
+ if (staffed.starts === staffed.ends) {
74
+ contract(`${at}.ends`, "A shift's start and end are two fields");
75
+ }
76
+ if (staffed.zone !== undefined && !single(own(object, staffed.zone), ["timezone"])) {
77
+ contract(`${at}.zone`, `"${staffed.zone}" must be a timezone field of ${name}`);
78
+ }
79
+ // The kind.
80
+ const kind = own(object, staffed.kind);
81
+ if (kind?.type !== "relation" || kind.many) {
82
+ contract(`${at}.kind`, `"${staffed.kind}" must be a relation field (one record) of ${name}`);
83
+ }
84
+ // The states in which it is a shift.
85
+ const states = object.states;
86
+ if (!states) {
87
+ contract(`${at}.live`, `${name} must declare states: a shift is live in some of them`);
88
+ }
89
+ else {
90
+ const field = own(object, states.field);
91
+ const values = new Set((field?.options ?? []).map((o) => o.value));
92
+ for (const state of staffed.live) {
93
+ if (!values.has(state))
94
+ contract(`${at}.live`, `"${state}" is not a state of ${name}`);
95
+ }
96
+ if (new Set(staffed.live).size !== staffed.live.length) {
97
+ contract(`${at}.live`, "List each state once");
98
+ }
99
+ }
100
+ // Where it is.
101
+ if (staffed.place !== undefined) {
102
+ const [first, second, ...rest] = staffed.place.split(".");
103
+ const field = first === undefined ? undefined : own(object, first);
104
+ if (!field || rest.length || (second !== undefined && field.type !== "relation")) {
105
+ contract(`${at}.place`, `"${staffed.place}" must be a field of ${name}, or a relation of it and a field of what it names`);
106
+ }
107
+ }
108
+ // The pattern: a series of this extension making these records, copying its person onto them.
109
+ if (staffed.pattern) {
110
+ const series = Object.entries(m.objects).find(([, o]) => o.recurring?.generates === name && o.recurring.link === staffed.pattern?.link);
111
+ if (!series) {
112
+ contract(`${at}.pattern.link`, `"${staffed.pattern.link}" must be the link of a series of this extension that makes ${name}`);
113
+ }
114
+ else if (series[1].recurring?.copy?.[staffed.who.field] !== staffed.pattern.who) {
115
+ contract(`${at}.pattern.who`, `${series[0]} must copy "${staffed.pattern.who}" onto "${staffed.who.field}"`);
116
+ }
117
+ }
118
+ // Minutes for pay only, read from the kind.
119
+ for (const key of ["payBefore", "payAfter"]) {
120
+ const declared = staffed[key];
121
+ if (!declared)
122
+ continue;
123
+ const [via, field, ...rest] = declared.field.split(".");
124
+ if (via !== staffed.kind || !field || rest.length) {
125
+ contract(`${at}.${key}.field`, `"${declared.field}" must be ${staffed.kind}.<a number field of what it names>`);
126
+ }
127
+ }
128
+ // Minutes kept free around the shift, in its claim (ADR 0760): a number field of its kind, which
129
+ // is another of this extension's objects (the host builds triggers on its table to follow it).
130
+ const kindTarget = kind?.type === "relation" && !kind.many ? kind.to : undefined;
131
+ const kindLocal = kindTarget?.startsWith(`${m.id}.`)
132
+ ? kindTarget.slice(m.id.length + 1)
133
+ : undefined;
134
+ const kindObject = kindLocal && kindLocal !== name && Object.hasOwn(m.objects, kindLocal)
135
+ ? m.objects[kindLocal]
136
+ : undefined;
137
+ for (const key of ["claimBefore", "claimAfter"]) {
138
+ const declared = staffed[key];
139
+ if (!declared)
140
+ continue;
141
+ const [via, field, ...rest] = declared.field.split(".");
142
+ if (via !== staffed.kind || !field || rest.length) {
143
+ contract(`${at}.${key}.field`, `"${declared.field}" must be ${staffed.kind}.<a number field of what it names>`);
144
+ }
145
+ else if (!kindObject) {
146
+ contract(`${at}.${key}.field`, `"${staffed.kind}" must name another of this extension's objects for minutes in the claim`);
147
+ }
148
+ else if (!single(own(kindObject, field), ["number"])) {
149
+ contract(`${at}.${key}.field`, `"${field}" must be a number field of ${kindTarget}`);
150
+ }
151
+ }
152
+ // An account required: only a profile can hold none.
153
+ if (staffed.account === "required" && staffed.who.account === undefined) {
154
+ contract(`${at}.account`, `"required" needs who.account: "${staffed.who.field}" is a user field, always an account`);
155
+ }
156
+ // Booked at free times (ADRs 0762 and 0763): the kind's length, step and rooms, the client, the
157
+ // closures.
158
+ if (staffed.open) {
159
+ const open = staffed.open;
160
+ // A field of the kind (null once a problem with the path is reported).
161
+ const kindField = (key) => {
162
+ const declared = open[key];
163
+ if (!declared)
164
+ return null;
165
+ const [via, field, ...rest] = declared.field.split(".");
166
+ if (via !== staffed.kind || !field || rest.length) {
167
+ contract(`${at}.open.${key}.field`, `"${declared.field}" must be ${staffed.kind}.<a field of what it names>`);
168
+ return null;
169
+ }
170
+ if (!kindObject) {
171
+ contract(`${at}.open.${key}.field`, `"${staffed.kind}" must name another of this extension's objects for free times`);
172
+ return null;
173
+ }
174
+ return own(kindObject, field);
175
+ };
176
+ for (const key of ["length", "step"]) {
177
+ const field = kindField(key);
178
+ if (field !== null && !single(field, ["number"])) {
179
+ contract(`${at}.open.${key}.field`, `"${open[key]?.field}" must be a number field of ${kindTarget}`);
180
+ }
181
+ }
182
+ const rooms = kindField("rooms");
183
+ if (rooms !== null) {
184
+ const thingField = object.occupies ? own(object, object.occupies.thing) : undefined;
185
+ if (!thingField) {
186
+ contract(`${at}.open.rooms`, `${name} must declare occupies for the rooms it is booked in`);
187
+ }
188
+ else if (rooms?.type !== "relation" || !rooms.many || rooms.to !== thingField.to) {
189
+ contract(`${at}.open.rooms.field`, `"${open.rooms?.field}" must be a relation field (many) of ${kindTarget} to ${thingField.to}`);
190
+ }
191
+ }
192
+ if (open.client !== undefined) {
193
+ const client = own(object, open.client);
194
+ if (client?.type !== "relation" || client.many || client.to !== "person") {
195
+ contract(`${at}.open.client`, `"${open.client}" must be a relation field (one record) of ${name} to person`);
196
+ }
197
+ }
198
+ if (open.closedBy) {
199
+ const c = open.closedBy;
200
+ const closure = c.object !== name && Object.hasOwn(m.objects, c.object) ? m.objects[c.object] : undefined;
201
+ if (!closure) {
202
+ contract(`${at}.open.closedBy.object`, `"${c.object}" must be another object of this extension`);
203
+ }
204
+ else {
205
+ for (const key of ["from", "until"]) {
206
+ if (!single(own(closure, c[key]), ["date"])) {
207
+ contract(`${at}.open.closedBy.${key}`, `"${c[key]}" must be a date field of ${c.object}`);
208
+ }
209
+ }
210
+ }
211
+ }
212
+ }
213
+ // The evidence a shift was worked: another object of this extension naming it, and who marked.
214
+ if (staffed.evidence) {
215
+ const { object: other, link, by } = staffed.evidence;
216
+ const evidence = other !== name && Object.hasOwn(m.objects, other) ? m.objects[other] : undefined;
217
+ const linkField = evidence ? own(evidence, link) : undefined;
218
+ if (!evidence) {
219
+ contract(`${at}.evidence.object`, `"${other}" must be another object of this extension`);
220
+ }
221
+ else if (linkField?.type !== "relation" ||
222
+ linkField.many ||
223
+ linkField.to !== `${m.id}.${name}`) {
224
+ contract(`${at}.evidence.link`, `"${link}" must be a relation field (one record) of ${other} to ${m.id}.${name}`);
225
+ }
226
+ else if (!single(own(evidence, by), ["user"])) {
227
+ contract(`${at}.evidence.by`, `"${by}" must be a user field of ${other}`);
228
+ }
229
+ }
230
+ // Times that follow another record (ADR 0751): copied by the host from what a relation names.
231
+ if (staffed.timesFrom !== undefined)
232
+ checkTimesFrom(m, name, object, staffed, contract);
233
+ }
234
+ /**
235
+ * `staffed.timesFrom` (ADR 0751): a required relation (one record) to another of this extension's
236
+ * objects that has the shift's time fields under the same names and types (its kind a relation to
237
+ * the same object); the shift's own copies are read-only, and no series makes the shift.
238
+ */
239
+ function checkTimesFrom(m, name, object, staffed, contract) {
240
+ const at = `objects.${name}.staffed.timesFrom`;
241
+ const local = staffed.timesFrom;
242
+ const own = (o, f) => Object.hasOwn(o.fields, f) ? o.fields[f] : undefined;
243
+ const link = own(object, local);
244
+ const target = link?.type === "relation" && !link.many ? link.to : undefined;
245
+ const parentName = target?.startsWith(`${m.id}.`) ? target.slice(m.id.length + 1) : undefined;
246
+ const parent = parentName && parentName !== name && Object.hasOwn(m.objects, parentName)
247
+ ? m.objects[parentName]
248
+ : undefined;
249
+ if (!parent) {
250
+ contract(at, `"${local}" must be a relation field (one record) of ${name} to another of this extension's objects`);
251
+ return;
252
+ }
253
+ if (!link?.required) {
254
+ contract(at, `"${local}" must be required: a shift whose times follow a record names one`);
255
+ }
256
+ if (local === staffed.who.field || local === staffed.kind) {
257
+ contract(at, `"${local}" is the shift's person or kind, not the record its times follow`);
258
+ }
259
+ const followed = [
260
+ [staffed.starts, "datetime"],
261
+ [staffed.ends, "datetime"],
262
+ ...(staffed.zone !== undefined ? [[staffed.zone, "timezone"]] : []),
263
+ [staffed.kind, "relation"],
264
+ ];
265
+ for (const [field, type] of followed) {
266
+ const mine = own(object, field);
267
+ const theirs = own(parent, field);
268
+ if (!theirs ||
269
+ theirs.type !== type ||
270
+ theirs.many ||
271
+ (type === "relation" && theirs.to !== mine?.to)) {
272
+ contract(at, `${target} 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`);
273
+ }
274
+ if (mine && !mine.readOnly) {
275
+ contract(`objects.${name}.fields.${field}.readOnly`, `"${field}" must be readOnly: the host copies it from ${local}, nobody edits it`);
276
+ }
277
+ }
278
+ // One step only (the review of PR #419): the host reads the shifts that follow a record before it
279
+ // moves, not theirs in turn, so a record whose own times follow another is not followed.
280
+ if (parent.staffed?.timesFrom !== undefined) {
281
+ contract(at, `${target}'s own times follow another record: a shift's times follow a record whose times are its own`);
282
+ }
283
+ const series = Object.values(m.objects).some((o) => o.recurring?.generates === name);
284
+ if (staffed.pattern || series) {
285
+ contract(at, `${name} is made by a series, which sets its times: its times cannot follow ${local}`);
286
+ }
287
+ }
package/dist/states.d.ts CHANGED
@@ -68,6 +68,11 @@ export interface TimedMove {
68
68
  * relation) is in one of these states: a class whose attendance was taken has someone ticked.
69
69
  */
70
70
  ifAnyIn?: readonly string[];
71
+ /**
72
+ * Only while each of these fields of the record has a value (stage 49, ADR 0764): an appointment
73
+ * whose coach left is never marked attended for a session nobody gave.
74
+ */
75
+ ifSet?: readonly string[];
71
76
  }
72
77
  /**
73
78
  * A number a window is worked out from (ADR 0505): a number; a number field named as a window's
@@ -136,6 +141,12 @@ export interface StatesSpec {
136
141
  * one, staff included (no booking into a class that was cancelled or is over).
137
142
  */
138
143
  closed?: string | readonly string[];
144
+ /**
145
+ * Fields every create must give a value (stage 49, ADR 0764), whoever makes it, the host
146
+ * included: an appointment is made for a client. Unlike `required`, the field may be emptied
147
+ * later (erasing the Person keeps the record for the business's books, ADR 0320).
148
+ */
149
+ requires?: readonly string[];
139
150
  };
140
151
  /**
141
152
  * Moves a caller asks for by name, resolved by the host (ADR 0503): writing the name (`cancel`)
@@ -150,7 +161,10 @@ export interface StatesSpec {
150
161
  * state named in `when`, the host moves every live record of this object pointing at it, and in
151
162
  * one of `from`, to the state `when` gives, in the same batch, as the host (a class cancelled
152
163
  * moves its bookings to `cancelled_by_us`: their places released, their uses given back). Each
153
- * target needs a `host` move from each of `from`.
164
+ * target needs a `host` move from each of `from`. The parent is another of this extension's
165
+ * objects, or one of an extension this one `requires`, named in full (`ticketing.event`); a
166
+ * parent others draw places from as a pool is followed only by an object that draws none
167
+ * (ADR 0841).
154
168
  */
155
169
  follows?: {
156
170
  relation: string;
@@ -249,6 +263,7 @@ export declare const statesManifestSchema: z.ZodObject<{
249
263
  }, z.core.$strict>>;
250
264
  staffOnly: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>>;
251
265
  closed: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>>;
266
+ requires: z.ZodOptional<z.ZodArray<z.ZodString>>;
252
267
  }, z.core.$strict>>;
253
268
  requests: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodArray<z.ZodString>>>;
254
269
  follows: z.ZodOptional<z.ZodObject<{
@@ -262,6 +277,7 @@ export declare const statesManifestSchema: z.ZodObject<{
262
277
  at: z.ZodString;
263
278
  minutesAfter: z.ZodOptional<z.ZodNumber>;
264
279
  ifAnyIn: z.ZodOptional<z.ZodArray<z.ZodString>>;
280
+ ifSet: z.ZodOptional<z.ZodArray<z.ZodString>>;
265
281
  }, z.core.$strict>>>;
266
282
  }, z.core.$strip>;
267
283
  export type StatesManifest = z.infer<typeof statesManifestSchema>;
package/dist/states.js CHANGED
@@ -62,6 +62,7 @@ export const statesManifestSchema = z.object({
62
62
  window: moveWindow.optional(),
63
63
  staffOnly: staffOnlyPaths.optional(),
64
64
  closed: staffOnlyPaths.optional(),
65
+ requires: z.array(fieldName).min(1).max(20).optional(),
65
66
  })
66
67
  .strict()
67
68
  .optional(),
@@ -87,6 +88,7 @@ export const statesManifestSchema = z.object({
87
88
  at: fieldPath,
88
89
  minutesAfter: z.number().int().min(0).max(10_080).optional(),
89
90
  ifAnyIn: z.array(stateValue).min(1).max(20).optional(),
91
+ ifSet: z.array(fieldName).min(1).max(5).optional(),
90
92
  })
91
93
  .strict())
92
94
  .min(1)
@@ -215,38 +217,60 @@ export function checkObjectStates(m, name, object, contract) {
215
217
  for (const p of staffOnlyPathsOf(states.create.closed)) {
216
218
  checkPath(p, `${sp}.create.closed`, "boolean");
217
219
  }
220
+ for (const required of states.create.requires ?? []) {
221
+ if (!fieldOf(required)) {
222
+ contract(`${sp}.create.requires`, `"${required}" is not a field of ${name}`);
223
+ }
224
+ }
218
225
  }
219
226
  if (states.follows) {
220
227
  const fp = `${sp}.follows`;
221
228
  const f = states.follows;
222
229
  const rel = fieldOf(f.relation);
223
- const target = rel?.type === "relation" && !rel.many && rel.to?.startsWith(`${m.id}.`)
224
- ? m.objects[rel.to.slice(m.id.length + 1)]
225
- : undefined;
226
- if (!target?.states) {
227
- contract(`${fp}.relation`, `"${f.relation}" must be a relation (one record) of ${name} to another of this extension's objects that declares states`);
230
+ const to = rel?.type === "relation" && !rel.many ? rel.to : undefined;
231
+ const own = to?.startsWith(`${m.id}.`) ? to.slice(m.id.length + 1) : undefined;
232
+ const target = own !== undefined && Object.hasOwn(m.objects, own) ? m.objects[own] : undefined;
233
+ // A parent in another extension this one requires (ADR 0841): its states are the host's to
234
+ // check, at install and on every upgrade of either extension (core's checkManifest).
235
+ const otherExt = to && own === undefined && to.includes(".") ? to.split(".")[0] : undefined;
236
+ const required = otherExt !== undefined && (m.requires ?? []).includes(otherExt);
237
+ if (!target?.states && !required) {
238
+ contract(`${fp}.relation`, `"${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`);
228
239
  }
229
240
  else {
230
- // A parent whose places other records draw on as a pool: its counter counts claims that
231
- // are not its own, which the cascade's check at commit cannot read (ADR 0550).
232
- const pooled = Object.values(m.objects).some((other) => {
233
- const poolField = other.capacity?.pool;
234
- const relTo = poolField ? other.fields[poolField]?.to : undefined;
235
- return relTo === rel?.to || relTo === rel?.to?.slice(m.id.length + 1);
236
- });
237
- if (pooled) {
238
- contract(`${fp}.relation`, `${rel?.to ?? f.relation} is a pool other records draw places from: a cascade cannot follow it`);
241
+ // A parent whose places other records draw on as a pool counts claims that are not its
242
+ // own, which the cascade's check at commit cannot read (ADR 0550). A child that draws on
243
+ // it (a member of the pool, or claiming places on the parent or on a member) is refused;
244
+ // one that draws nothing moves no places and may follow it (ADR 0841). A pool is always in
245
+ // the parent's own extension, so a parent in another extension is never drawn on from here.
246
+ if (target && own !== undefined) {
247
+ const localOf = (ref) => ref?.startsWith(`${m.id}.`) ? ref.slice(m.id.length + 1) : ref;
248
+ const poolOf = (other) => {
249
+ const poolField = other?.capacity?.pool;
250
+ return poolField ? localOf(other?.fields[poolField]?.to) : undefined;
251
+ };
252
+ const members = new Set(Object.entries(m.objects)
253
+ .filter(([, other]) => poolOf(other) === own)
254
+ .map(([key]) => key));
255
+ const claimsOn = object.claims ? localOf(object.fields[object.claims.on]?.to) : undefined;
256
+ const draws = members.has(name) ||
257
+ (claimsOn !== undefined && (claimsOn === own || members.has(claimsOn)));
258
+ if (members.size && draws) {
259
+ contract(`${fp}.relation`, `${to} is a pool other records draw places from, and ${name} draws places from it: a cascade cannot follow it`);
260
+ }
239
261
  }
240
- const parentStates = new Set((target.fields[target.states.field]?.options ?? []).map((o) => o.value));
241
- for (const [when, to] of Object.entries(f.when)) {
242
- if (!parentStates.has(when)) {
243
- contract(`${fp}.when`, `"${when}" is not a state of ${rel?.to ?? f.relation}`);
262
+ const parentStates = target?.states
263
+ ? new Set((target.fields[target.states.field]?.options ?? []).map((o) => o.value))
264
+ : null;
265
+ for (const [when, into] of Object.entries(f.when)) {
266
+ if (parentStates && !parentStates.has(when)) {
267
+ contract(`${fp}.when`, `"${when}" is not a state of ${to ?? f.relation}`);
244
268
  }
245
269
  for (const from of f.from) {
246
270
  if (!isState(from))
247
271
  contract(`${fp}.from`, `"${from}" is not an option of ${states.field}`);
248
- else if (!states.moves.some((move) => move.to === to && move.by.includes("host") && moveOrigins(move).includes(from))) {
249
- contract(`${fp}.when`, `${name} needs a host move from "${from}" to "${to}"`);
272
+ else if (!states.moves.some((move) => move.to === into && move.by.includes("host") && moveOrigins(move).includes(from))) {
273
+ contract(`${fp}.when`, `${name} needs a host move from "${from}" to "${into}"`);
250
274
  }
251
275
  }
252
276
  }
@@ -272,6 +296,10 @@ export function checkObjectStates(m, name, object, contract) {
272
296
  if (at?.type !== "datetime" || at.many || (parts.length === 2 && !ownTarget)) {
273
297
  contract(`${tp}.at`, `"${rule.at}" must be a datetime field of ${name}, or <relation>.<field> on another of this extension's objects`);
274
298
  }
299
+ for (const f of rule.ifSet ?? []) {
300
+ if (!fieldOf(f))
301
+ contract(`${tp}.ifSet`, `"${f}" is not a field of ${name}`);
302
+ }
275
303
  if (rule.ifAnyIn) {
276
304
  if (parts.length !== 2) {
277
305
  contract(`${tp}.ifAnyIn`, "ifAnyIn needs at one relation away: the records it compares");