@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
@@ -0,0 +1,79 @@
1
+ // The `workedOn` declaration (stage 58 step 1, ADR 0850): a record time can be recorded against (a
2
+ // job). Its manifest schema and contract checks live here; the host's side (the time entries,
3
+ // refused by the database when one person's overlap) is packages/core/src/staffing/time.ts.
4
+ import { z } from "zod";
5
+ const fieldName = z.string().max(40);
6
+ export const workedOnManifestSchema = z
7
+ .object({
8
+ live: z.array(z.string().min(1).max(100)).min(1).max(20),
9
+ title: fieldName,
10
+ kind: fieldName.optional(),
11
+ place: z.string().max(81).optional(),
12
+ readers: z.string().max(80),
13
+ partOf: fieldName.optional(),
14
+ })
15
+ .strict();
16
+ /** Reports problems with `objects.<name>.workedOn` through `contract`. */
17
+ export function checkObjectWorkedOn(m, name, object, contract) {
18
+ const declared = object.workedOn;
19
+ if (!declared)
20
+ return;
21
+ const at = `objects.${name}.workedOn`;
22
+ const own = (o, local) => Object.hasOwn(o.fields, local) ? o.fields[local] : undefined;
23
+ // The states in which time may be recorded against it.
24
+ const states = object.states;
25
+ if (!states) {
26
+ contract(`${at}.live`, `${name} must declare states: time is recorded in some of them`);
27
+ }
28
+ else {
29
+ const field = own(object, states.field);
30
+ const values = new Set((field?.options ?? []).map((o) => o.value));
31
+ for (const state of declared.live) {
32
+ if (!values.has(state))
33
+ contract(`${at}.live`, `"${state}" is not a state of ${name}`);
34
+ }
35
+ if (new Set(declared.live).size !== declared.live.length) {
36
+ contract(`${at}.live`, "List each state once");
37
+ }
38
+ }
39
+ // What it is called, of what kind, and where.
40
+ const title = own(object, declared.title);
41
+ if (!title || title.many) {
42
+ contract(`${at}.title`, `"${declared.title}" must be a field (one value) of ${name}`);
43
+ }
44
+ if (declared.kind !== undefined) {
45
+ const kind = own(object, declared.kind);
46
+ if (kind?.type !== "relation" || kind.many) {
47
+ contract(`${at}.kind`, `"${declared.kind}" must be a relation field (one record) of ${name}`);
48
+ }
49
+ }
50
+ if (declared.place !== undefined) {
51
+ const [first, second, ...rest] = declared.place.split(".");
52
+ const field = first === undefined ? undefined : own(object, first);
53
+ if (!field || rest.length || (second !== undefined && field.type !== "relation")) {
54
+ contract(`${at}.place`, `"${declared.place}" must be a field of ${name}, or a relation of it and a field of what it names`);
55
+ }
56
+ }
57
+ // Who reads its time: a permission this extension's tools declare.
58
+ const permissions = new Set(Object.values(m.tools).flatMap((t) => t.permission === undefined
59
+ ? []
60
+ : typeof t.permission === "string"
61
+ ? [t.permission]
62
+ : t.permission));
63
+ if (!declared.readers.startsWith(`${m.id}:`) || !permissions.has(declared.readers)) {
64
+ contract(`${at}.readers`, `"${declared.readers}" must be a permission of this extension's tools (${m.id}:<word>)`);
65
+ }
66
+ // What a staffed visit's time also counts to.
67
+ if (declared.partOf !== undefined) {
68
+ if (!object.staffed) {
69
+ contract(`${at}.partOf`, `Only a shift (an object that declares staffed) is part of a job`);
70
+ }
71
+ const via = own(object, declared.partOf);
72
+ const target = via?.type === "relation" && !via.many ? via.to : undefined;
73
+ const local = target?.startsWith(`${m.id}.`) ? target.slice(m.id.length + 1) : undefined;
74
+ const job = local && Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
75
+ if (!job || local === name || !job.workedOn || job.workedOn.partOf !== undefined) {
76
+ contract(`${at}.partOf`, `"${declared.partOf}" must be a relation field (one record) to another of this extension's objects that declares workedOn`);
77
+ }
78
+ }
79
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@composable-os/sdk",
3
- "version": "0.1.0-beta.11",
3
+ "version": "0.1.0-beta.14",
4
4
  "license": "FSL-1.1-ALv2",
5
5
  "type": "module",
6
6
  "repository": {
@@ -21,6 +21,14 @@
21
21
  "types": "./dist/formula/index.d.ts",
22
22
  "default": "./dist/formula/index.js"
23
23
  },
24
+ "./questions": {
25
+ "types": "./dist/questions.d.ts",
26
+ "default": "./dist/questions.js"
27
+ },
28
+ "./rich-text": {
29
+ "types": "./dist/rich-text.d.ts",
30
+ "default": "./dist/rich-text.js"
31
+ },
24
32
  "./testing": {
25
33
  "types": "./dist/testing.d.ts",
26
34
  "default": "./dist/testing.js"
@@ -0,0 +1,100 @@
1
+ // Read and understood (stage 38, ADR 0702): a publishable object may ask the staff who must read
2
+ // a record to confirm each version that matters, with one tap.
3
+ //
4
+ // publishable: {
5
+ // content: ["title", "body"],
6
+ // acknowledge: { who: "mustRead", manage: "knowledge:manage" },
7
+ // }
8
+ //
9
+ // `who` names a `document` field of the object's own, outside the content, whose schema is
10
+ // MUST_READ_SCHEMA: `{ everyone: true }`, or `{ everyone: false, roles: [...] }` where each entry
11
+ // is a staff role (`admin`, `member`, `viewer`), a pack role (`rostering.team`) or a pack
12
+ // permission an admin grants (`bookings:desk`). The host reads it on the day, against each
13
+ // account's role, pack role and grants. `manage` names a restricted, grantable permission of the
14
+ // extension's tools: who holds it (and admins) reads who has confirmed, and changes who must read
15
+ // once a record has been published.
16
+ import type { DocumentSchema } from "./document.ts";
17
+
18
+ /** What an entry of `roles` may be: a staff role, `<pack>.<role>` or `<pack>:<permission>`. */
19
+ export const MUST_READ_ENTRY_RE =
20
+ /^(?:admin|member|viewer|[a-z][a-z0-9]{1,29}[.:][a-zA-Z0-9]{1,40})$/;
21
+
22
+ /** The most entries one "must read" lists. */
23
+ export const MUST_READ_MAX = 50;
24
+
25
+ /** The one schema of an acknowledged object's "must read" field. */
26
+ export const MUST_READ_SCHEMA: DocumentSchema = {
27
+ root: {
28
+ type: "object",
29
+ fields: {
30
+ everyone: { type: "boolean", description: "Every staff account must read it" },
31
+ roles: {
32
+ type: "array",
33
+ max: MUST_READ_MAX,
34
+ optional: true,
35
+ description: "Staff roles, pack roles and granted pack permissions that must read it",
36
+ of: {
37
+ type: "text",
38
+ min: 1,
39
+ max: 71,
40
+ chars: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:",
41
+ },
42
+ },
43
+ },
44
+ },
45
+ };
46
+
47
+ // --- The pack's words and what else the host guards (stage 38 step 5, ADR 0706) -----------------
48
+ //
49
+ // acknowledge: {
50
+ // who: "mustRead",
51
+ // manage: "knowledge:manage",
52
+ // managed: ["reviewEvery"],
53
+ // owner: "owner",
54
+ // notices: {
55
+ // asked: { title: "Please read: {record}" },
56
+ // reminder: { title: "Still to read: {record}", afterDays: 7 },
57
+ // outstanding: { title: "Not yet confirmed: {record}" },
58
+ // welcome: { title: "Procedures to read" },
59
+ // },
60
+ // }
61
+ //
62
+ // `managed` names more fields of the object's own, outside the content, that the host guards as
63
+ // it guards who must read: a person's to change, never an AI actor's, and once the record has
64
+ // been published, only an admin's or a holder of `manage`. `owner` names a `user` field: who
65
+ // looks after the record, told each week who has not confirmed it. `notices` are the pack's
66
+ // words; the host sends them, to the people only it can work out (who must read, who has not), and
67
+ // `{record}` stands for the record's published title. Each notice is optional: left out, none.
68
+
69
+ /** One notice's words: `{record}` in either stands for the record's title. */
70
+ export interface AcknowledgeNotice {
71
+ title: string;
72
+ body?: string;
73
+ }
74
+
75
+ /** The notices the host sends for an acknowledged object (ADR 0706). */
76
+ export interface AcknowledgeNotices {
77
+ /** A version that asks everyone goes live: to everyone who must read it, once per version. */
78
+ asked?: AcknowledgeNotice;
79
+ /** Still to read `afterDays` after it became theirs to read: to that person, once per version. */
80
+ reminder?: AcknowledgeNotice & { afterDays: number };
81
+ /** Each week while anyone has not confirmed: to the record's `owner`. */
82
+ outstanding?: AcknowledgeNotice;
83
+ /** A new staff account with records to read: to that person, once, listing them. */
84
+ welcome?: AcknowledgeNotice;
85
+ }
86
+
87
+ /** `publishable.acknowledge`: see the comment above and ADRs 0702 and 0706. */
88
+ export interface AcknowledgeSpec {
89
+ who: string;
90
+ manage: string;
91
+ managed?: readonly string[];
92
+ owner?: string;
93
+ notices?: AcknowledgeNotices;
94
+ }
95
+
96
+ /** The longest a notice's title and body may be. */
97
+ export const ACKNOWLEDGE_NOTICE_TITLE_MAX = 120;
98
+ export const ACKNOWLEDGE_NOTICE_BODY_MAX = 600;
99
+ /** The longest a reminder may wait, in days. */
100
+ export const ACKNOWLEDGE_REMINDER_MAX_DAYS = 60;
@@ -0,0 +1,185 @@
1
+ // The `agreements` behaviour (stage 44, ADR 0641): an object marks an agreement point, a place a
2
+ // business may need a person to have agreed to its documents first (buying a membership, making a
3
+ // booking). The pack names the point; the business attaches its own documents to it in the admin.
4
+ // Nothing is needed anywhere until a person publishes a document and attaches it. Its declaration,
5
+ // its manifest schema and its contract checks live here; the host's side is
6
+ // packages/core/src/agreements/.
7
+ import { z } from "zod";
8
+ import type { ExtensionManifest, ObjectManifest } from "./manifest.ts";
9
+ import { moveOrigins } from "./states.ts";
10
+
11
+ /** Kinds of agreement document a business writes (`care` is the platform's own, stage 43). */
12
+ export const AGREEMENT_KINDS = ["terms", "waiver", "health", "guardian", "other"] as const;
13
+ export type AgreementKind = (typeof AGREEMENT_KINDS)[number];
14
+
15
+ /** A point's name: a lowercase word, shared by objects that mean the same place. */
16
+ export const AGREEMENT_POINT_RE = /^[a-z][a-z0-9_]{1,39}$/;
17
+
18
+ /**
19
+ * An agreement point (ADR 0641).
20
+ *
21
+ * - `point`: the point's name. Objects may share one (a membership plan and a pass type are both
22
+ * "buying a membership or a pass").
23
+ * - `label`: what the admin shows when attaching documents.
24
+ * - `suggests`: kinds of document the admin offers first. A suggestion, never a requirement.
25
+ * - `enter` and `person`: for a record a caller makes (a booking), the states whose entry is checked
26
+ * and the relation to the Person it is for. Left out, the point is checked where the object is
27
+ * bought (a `sellable` object).
28
+ * - `keeps`: with `enter`, for an object without `claims`, the states a record keeps its entry in
29
+ * (a ticket valid or used at the door), as `claims` says which states hold a place: a record
30
+ * created in one is asked, and a move between two of them is the same entry and is not (staff
31
+ * undoing a check-in). Left out, only `enter`'s states count.
32
+ * - `importMovesOver`: with `enter`, a record the record import makes (a person's door:
33
+ * `cos courses import`) is someone moving over from another system, and is not asked (stage 44,
34
+ * issue #281). A later import that gives it to another Person or makes it count again is asked.
35
+ * Left out, an import is asked as any caller's write.
36
+ * - `inlineStep`: the pack's own screens (its blocks that buy or book this object) show what is
37
+ * due and let a person agree on the way (stage 44 step 5). Left out, nothing on the pack's
38
+ * screens can agree, so a document attached there refuses every purchase or booking until the
39
+ * person agrees elsewhere (My agreements, the desk, paper): the admin is warned when attaching.
40
+ *
41
+ * In stage 44 step 1 the declaration is inert: it is checked and listed, and documents can be
42
+ * attached to it; the host asks it from step 3.
43
+ */
44
+ export interface AgreementsSpec {
45
+ point: string;
46
+ label: string;
47
+ suggests?: readonly AgreementKind[];
48
+ enter?: readonly string[];
49
+ person?: string;
50
+ keeps?: readonly string[];
51
+ importMovesOver?: boolean;
52
+ inlineStep?: boolean;
53
+ }
54
+
55
+ export const agreementsManifestSchema = z.object({
56
+ point: z.string().max(40),
57
+ label: z.string().min(1).max(80),
58
+ suggests: z.array(z.enum(AGREEMENT_KINDS)).max(AGREEMENT_KINDS.length).optional(),
59
+ enter: z.array(z.string().max(40)).min(1).max(20).optional(),
60
+ person: z.string().max(40).optional(),
61
+ keeps: z.array(z.string().max(40)).min(1).max(20).optional(),
62
+ importMovesOver: z.boolean().optional(),
63
+ inlineStep: z.boolean().optional(),
64
+ });
65
+ export type AgreementsManifest = z.infer<typeof agreementsManifestSchema>;
66
+
67
+ /** The states an object's `states` declaration names. */
68
+ function stateNames(object: ObjectManifest): Set<string> {
69
+ const states = object.states;
70
+ if (!states) return new Set();
71
+ return new Set([
72
+ states.initial,
73
+ ...states.moves.flatMap((move) => [...moveOrigins(move), move.to]),
74
+ ]);
75
+ }
76
+
77
+ /** The contract checks of an object's `agreements` declaration. */
78
+ export function checkObjectAgreements(
79
+ _m: ExtensionManifest,
80
+ name: string,
81
+ object: ObjectManifest,
82
+ contract: (path: string, message: string) => void,
83
+ ): void {
84
+ const declared = object.agreements;
85
+ if (!declared) return;
86
+ const path = `objects.${name}.agreements`;
87
+ if (!AGREEMENT_POINT_RE.test(declared.point)) {
88
+ contract(`${path}.point`, "point must be a lowercase word (letters, digits, _), 2 to 40 long");
89
+ }
90
+ if (declared.suggests && new Set(declared.suggests).size !== declared.suggests.length) {
91
+ contract(`${path}.suggests`, "suggests lists each kind once");
92
+ }
93
+ if (!declared.enter) {
94
+ if (declared.person !== undefined) {
95
+ contract(`${path}.person`, "person is for a point checked when a record enters a state");
96
+ }
97
+ if (declared.keeps !== undefined) {
98
+ contract(`${path}.keeps`, "keeps is for a point checked when a record enters a state");
99
+ }
100
+ if (declared.importMovesOver !== undefined) {
101
+ contract(
102
+ `${path}.importMovesOver`,
103
+ "importMovesOver is for a point checked when a record enters a state",
104
+ );
105
+ }
106
+ if (!object.sellable) {
107
+ contract(
108
+ path,
109
+ `${name} is not sellable: a point on it needs enter (the states whose entry is checked) and person`,
110
+ );
111
+ }
112
+ return;
113
+ }
114
+ const states = stateNames(object);
115
+ if (!object.states) {
116
+ contract(`${path}.enter`, `enter names states, and ${name} declares none`);
117
+ } else {
118
+ for (const [i, state] of declared.enter.entries()) {
119
+ if (!states.has(state))
120
+ contract(`${path}.enter.${i}`, `"${state}" is not a state of ${name}`);
121
+ }
122
+ for (const [i, state] of (declared.keeps ?? []).entries()) {
123
+ if (!states.has(state))
124
+ contract(`${path}.keeps.${i}`, `"${state}" is not a state of ${name}`);
125
+ }
126
+ }
127
+ if (declared.keeps && object.claims) {
128
+ contract(`${path}.keeps`, `${name} holds places (claims): its claims say which states keep it`);
129
+ }
130
+ const person = declared.person;
131
+ const field =
132
+ person !== undefined && Object.hasOwn(object.fields, person)
133
+ ? object.fields[person]
134
+ : undefined;
135
+ if (field?.type !== "relation" || field.many || field.to !== "person") {
136
+ contract(`${path}.person`, `person must be a relation field (one record) to person`);
137
+ }
138
+ }
139
+
140
+ /**
141
+ * A health-note list (stage 44, ADR 0643): records of this object make a list a person on the
142
+ * staff reads (a class's bookings), and the host may mark, beside each Person on it, a yes on
143
+ * their current health form, nothing else. `list` is the relation to the list's own record (the
144
+ * class), `person` the relation to the Person. The host reads the People itself; pack code names
145
+ * only the list's record (`agreement.listHealthNotes`), inside a tool a person called.
146
+ */
147
+ export interface HealthNotesSpec {
148
+ list: string;
149
+ person: string;
150
+ }
151
+
152
+ export const healthNotesManifestSchema = z.object({
153
+ list: z.string().max(40),
154
+ person: z.string().max(40),
155
+ });
156
+ export type HealthNotesManifest = z.infer<typeof healthNotesManifestSchema>;
157
+
158
+ /** The contract checks of an object's `healthNotes` declaration. */
159
+ export function checkObjectHealthNotes(
160
+ m: ExtensionManifest,
161
+ name: string,
162
+ object: ObjectManifest,
163
+ contract: (path: string, message: string) => void,
164
+ ): void {
165
+ const declared = object.healthNotes;
166
+ if (!declared) return;
167
+ const path = `objects.${name}.healthNotes`;
168
+ const one = (field: string) =>
169
+ Object.hasOwn(object.fields, field) ? object.fields[field] : undefined;
170
+ const list = one(declared.list);
171
+ const self = `${m.id}.${name}`;
172
+ if (
173
+ list?.type !== "relation" ||
174
+ list.many ||
175
+ list.to === "person" ||
176
+ list.to === self ||
177
+ list.to === name
178
+ ) {
179
+ contract(`${path}.list`, "list must be a relation field (one record) to another object");
180
+ }
181
+ const person = one(declared.person);
182
+ if (person?.type !== "relation" || person.many || person.to !== "person") {
183
+ contract(`${path}.person`, "person must be a relation field (one record) to person");
184
+ }
185
+ }
package/src/allowance.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  // packages/core/src/allowance/.
6
6
  import { z } from "zod";
7
7
  import type { ExtensionManifest, FieldManifest, ObjectManifest } from "./manifest.ts";
8
+ import { moveOrigins } from "./states.ts";
8
9
 
9
10
  /** What a holder's uses are counted over. Weeks start on Monday; both in the holder's zone. */
10
11
  export const ALLOWANCE_PERIODS = ["total", "week", "month"] as const;
@@ -29,12 +30,19 @@ export const ALLOWANCE_VALUES = ["used", "left"] as const;
29
30
  * 16:00, `below: 960`). An empty holder field sets no bound.
30
31
  *
31
32
  * A spender with no value for `field` meets no condition that sets a limit.
33
+ *
34
+ * - `value` (stage 49, ADR 0764), in place of `field`: what every record of the spender is, a fixed
35
+ * word compared with `in` ("sessions": an appointment, never a class). No caller sets it.
36
+ * - `empty` (stage 49, ADR 0764): `"none"` makes an empty holder field for `in` allow nothing
37
+ * instead of anything: a pass that names no services covers no appointment. Left out, `"any"`.
32
38
  */
33
39
  export interface AllowanceCondition {
34
- field: string;
40
+ field?: string;
41
+ value?: string;
35
42
  in?: string | readonly string[];
36
43
  atLeast?: string | number;
37
44
  below?: string | number;
45
+ empty?: "any" | "none";
38
46
  }
39
47
 
40
48
  /** What a holder may be spent on: an object that declares `allowance.spends`, and conditions. */
@@ -92,7 +100,8 @@ export interface AllowanceHolderSpec {
92
100
  * be live then. Left out: the time of the spend.
93
101
  * - `copy`: read-only fields of its own the host fills, at create and at each spend, from one
94
102
  * relation away (`{ classType: "class.classType" }`), so a holder's `when` can read them and no
95
- * caller can set them.
103
+ * caller can set them. `<relation>.id` is the related record's own id (`{ serviceId:
104
+ * "service.id" }`, ADR 0764): an appointment's service, which its kind and length follow.
96
105
  * - `local`: read-only number fields of its own the host fills from `at` in `zone` (a timezone
97
106
  * field of its own or one relation away; the organisation's when left out or empty):
98
107
  * `weekday` (1 Monday to 7 Sunday) and `minute` (minutes since local midnight).
@@ -100,6 +109,12 @@ export interface AllowanceHolderSpec {
100
109
  * A record created into a spend state with it set takes the use of the record it names: in the
101
110
  * same batch the host moves that record to `state` (a return state) and spends from the same
102
111
  * holder, so a move never fails for want of a use the member already had.
112
+ * - `fixed` (ADR 0764): fields of its own that change only by such a move, never by an update,
113
+ * for every caller but the host (an appointment's times and service): a session a late cancel
114
+ * used never comes back by moving its time first.
115
+ * - `anchor` (ADR 0764): `{ field, from }`, a read-only datetime field the host sets at create:
116
+ * the moved record's, or else its own `from` (its start). A window may count from it, so a
117
+ * moved record keeps the late point of the one it replaced. It is fixed too.
103
118
  * - `paidBy`: holders of other extensions that may pay for it, by object (`memberships.pass`) or
104
119
  * by extension (`memberships`). Left out, only this extension's holders. A holder naming a
105
120
  * spender of another extension that does not list it is refused at install.
@@ -108,6 +123,12 @@ export interface AllowanceHolderSpec {
108
123
  * its order). Only the host and this extension's own code set it, never a customer, and it
109
124
  * cannot change while the record is in a spend state.
110
125
  *
126
+ * - `released` (stage 44 step 7, ADR 0647): a state of `returnIn` the host moves a record into
127
+ * when the holder it spent from is ended early by its payer's cooling off and the record's time
128
+ * is still ahead (a class not yet started): its use is returned and its place freed, as any move
129
+ * into a return state does. Each state a record holds a use in (`spendIn`) needs a move into it
130
+ * by the host. Left out, such records are kept.
131
+ *
111
132
  * Every field a holder's condition reads must be one the host fills (`copy` or `local`), so no
112
133
  * caller (a customer, staff, an AI) sets what pays for it.
113
134
  */
@@ -119,9 +140,15 @@ export interface AllowanceSpenderSpec {
119
140
  at?: string;
120
141
  copy?: Readonly<Record<string, string>>;
121
142
  local?: { weekday?: string; minute?: string; zone?: string };
122
- movedFrom?: { field: string; state: string };
143
+ movedFrom?: {
144
+ field: string;
145
+ state: string;
146
+ fixed?: readonly string[];
147
+ anchor?: { field: string; from: string };
148
+ };
123
149
  paidBy?: readonly string[];
124
150
  waived?: string;
151
+ released?: string;
125
152
  }
126
153
 
127
154
  /** The allowance behaviour: an object holds uses or spends them, never both. */
@@ -133,10 +160,12 @@ const stateValue = z.string().min(1).max(100);
133
160
 
134
161
  const conditionSchema = z
135
162
  .object({
136
- field: fieldName,
163
+ field: fieldName.optional(),
164
+ value: z.string().min(1).max(100).optional(),
137
165
  in: z.union([fieldName, z.array(z.string().max(200)).min(1).max(100)]).optional(),
138
166
  atLeast: z.union([fieldName, z.number()]).optional(),
139
167
  below: z.union([fieldName, z.number()]).optional(),
168
+ empty: z.enum(["any", "none"]).optional(),
140
169
  })
141
170
  .strict();
142
171
 
@@ -180,9 +209,18 @@ export const allowanceManifestSchema = z.union([
180
209
  })
181
210
  .strict()
182
211
  .optional(),
183
- movedFrom: z.object({ field: fieldName, state: stateValue }).strict().optional(),
212
+ movedFrom: z
213
+ .object({
214
+ field: fieldName,
215
+ state: stateValue,
216
+ fixed: z.array(fieldName).min(1).max(10).optional(),
217
+ anchor: z.object({ field: fieldName, from: fieldName }).strict().optional(),
218
+ })
219
+ .strict()
220
+ .optional(),
184
221
  paidBy: z.array(path).min(1).max(20).optional(),
185
222
  waived: fieldName.optional(),
223
+ released: stateValue.optional(),
186
224
  })
187
225
  .strict(),
188
226
  })
@@ -364,10 +402,22 @@ export function checkObjectAllowance(
364
402
  if (ops.length !== 1) {
365
403
  contract(cp, "A condition compares with exactly one of in, atLeast and below");
366
404
  }
367
- const field = fieldOf(spender, clause.field);
368
- if (!field || field.type === "formula") {
405
+ if ((clause.field === undefined) === (clause.value === undefined)) {
406
+ contract(cp, "A condition reads exactly one of field and value");
407
+ }
408
+ if (clause.value !== undefined && clause.in === undefined) {
409
+ contract(`${cp}.value`, "A fixed value is compared with in");
410
+ }
411
+ if (clause.empty !== undefined && clause.in === undefined) {
412
+ contract(`${cp}.empty`, "empty says what an empty in allows: it needs in");
413
+ }
414
+ // A fixed word (`value`) is what every record of the spender is: only the holder's is read.
415
+ const field = clause.field === undefined ? null : fieldOf(spender, clause.field);
416
+ if (clause.field === undefined) {
417
+ // Nothing of the spender to check.
418
+ } else if (!field || field.type === "formula") {
369
419
  contract(`${cp}.field`, `"${clause.field}" must be a field of ${spenderName}`);
370
- } else if (!filledBy.has(clause.field)) {
420
+ } else if (!filledBy.has(clause.field as string)) {
371
421
  contract(
372
422
  `${cp}.field`,
373
423
  `"${clause.field}" must be a field ${spenderName}'s host fills (its copy or local), so no caller sets what pays for it`,
@@ -432,6 +482,8 @@ export function checkObjectAllowance(
432
482
  }
433
483
  const other = ownObject(relation.to);
434
484
  if (!other) return null; // Another extension's object or a core one: the host checks.
485
+ // The related record's own id (ADR 0764): what it is, which only the relation sets.
486
+ if (target === "id") return { type: "text" } as FieldManifest;
435
487
  const field = fieldOf(other[1], target);
436
488
  if (!field) contract(p, `"${target}" is not a field of ${other[0]}`);
437
489
  return field;
@@ -495,6 +547,27 @@ export function checkObjectAllowance(
495
547
  }
496
548
  notCustomerWritten(s.waived, `${sp}.waived`, "whether it spends a use");
497
549
  }
550
+ if (s.released !== undefined) {
551
+ if (!s.returnIn.includes(s.released)) {
552
+ contract(
553
+ `${sp}.released`,
554
+ `"${s.released}" must be one of returnIn: a released record gives its use back`,
555
+ );
556
+ } else if (object.states) {
557
+ for (const from of s.spendIn) {
558
+ const ok = object.states.moves.some(
559
+ (move) =>
560
+ move.to === s.released && moveOrigins(move).includes(from) && move.by.includes("host"),
561
+ );
562
+ if (!ok) {
563
+ contract(
564
+ `${sp}.released`,
565
+ `A move from "${from}" into "${s.released}" by the host must be declared: the host releases it`,
566
+ );
567
+ }
568
+ }
569
+ }
570
+ }
498
571
  if (s.movedFrom) {
499
572
  const rel = fieldOf(object, s.movedFrom.field);
500
573
  if (rel?.type !== "relation" || rel.many || (rel.to !== `${m.id}.${name}` && rel.to !== name)) {
@@ -503,6 +576,23 @@ export function checkObjectAllowance(
503
576
  `"${s.movedFrom.field}" must be a relation field (one record) to ${m.id}.${name}`,
504
577
  );
505
578
  }
579
+ for (const f of s.movedFrom.fixed ?? []) {
580
+ if (!fieldOf(object, f))
581
+ contract(`${sp}.movedFrom.fixed`, `"${f}" is not a field of ${name}`);
582
+ }
583
+ if (s.movedFrom.anchor) {
584
+ const { field, from } = s.movedFrom.anchor;
585
+ const a = fieldOf(object, field);
586
+ if (a?.type !== "datetime" || a.many || !a.readOnly) {
587
+ contract(
588
+ `${sp}.movedFrom.anchor.field`,
589
+ `"${field}" must be a read-only datetime field of ${name}: the host sets it`,
590
+ );
591
+ }
592
+ if (fieldOf(object, from)?.type !== "datetime") {
593
+ contract(`${sp}.movedFrom.anchor.from`, `"${from}" must be a datetime field of ${name}`);
594
+ }
595
+ }
506
596
  if (!s.returnIn.includes(s.movedFrom.state)) {
507
597
  contract(
508
598
  `${sp}.movedFrom.state`,