@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,134 @@
1
+ import { z } from "zod";
2
+ import type { ExtensionManifest, FieldManifest, ObjectManifest } from "./manifest.ts";
3
+ /**
4
+ * How sure the host is whose a submission is (ADR 0721): `account`, a signed-in customer;
5
+ * `link`, a personal link; `staff`, entered by staff; `address`, an address a visitor typed,
6
+ * unconfirmed. An anonymous submission has none.
7
+ */
8
+ export declare const INTAKE_CERTAINTIES: readonly ["account", "link", "staff", "address"];
9
+ export type IntakeCertainty = (typeof INTAKE_CERTAINTIES)[number];
10
+ /** Who may answer a form: anyone, signed-in customers, a personal link's holder, or staff. */
11
+ export declare const INTAKE_ANSWERED_BY: readonly ["public", "customers", "links", "staff"];
12
+ export type IntakeAnsweredBy = (typeof INTAKE_ANSWERED_BY)[number];
13
+ /** Which way a submission came in. */
14
+ export declare const INTAKE_VIAS: readonly ["site", "app", "page", "link", "staff"];
15
+ export type IntakeVia = (typeof INTAKE_VIAS)[number];
16
+ /** What the host writes in a destination's `source` field. */
17
+ export declare const INTAKE_SOURCE = "website";
18
+ /**
19
+ * What is answered (ADR 0720). Every name is a field of this object.
20
+ *
21
+ * - `questions`: a document field with `QUESTIONS_SCHEMA`, one of `publishable.content`: answers
22
+ * are checked against the published version, and the host checks the questions on every write
23
+ * (`checkQuestions`, with the form's anonymous flag and its destination's fields).
24
+ * - `answeredBy`: a select whose options are among `INTAKE_ANSWERED_BY`. It, `anonymous` and
25
+ * `destination` are in `publishable.content` too: the host reads them from the version answered.
26
+ * - `openIn`: the states of the object's `states` in which it takes answers.
27
+ * - `anonymous` (a boolean field), `destination` (a text or select field holding an object's
28
+ * registry name), `opensAt` and `closesAt` (datetime fields), `dailyCap` (a number field: at
29
+ * most so many in 24 hours) and `notify` (a user field: who is told; left empty, the admins)
30
+ * are optional.
31
+ * - `hourlyCap` (optional): a number setting of this extension, one a person approves
32
+ * (`personApproves`): at most so many submissions an hour across every form of the deployment.
33
+ * Left out, or the setting empty, the platform's 200.
34
+ *
35
+ * A form object with `publishable.slug` is also found by its slug (`intake.open { slug }`): a link
36
+ * or a QR code names the form by its address, never by its id.
37
+ */
38
+ export interface IntakeFormSpec {
39
+ role: "form";
40
+ questions: string;
41
+ answeredBy: string;
42
+ openIn: readonly string[];
43
+ anonymous?: string;
44
+ destination?: string;
45
+ opensAt?: string;
46
+ closesAt?: string;
47
+ dailyCap?: string;
48
+ notify?: string;
49
+ hourlyCap?: string;
50
+ }
51
+ /**
52
+ * One set of answers to one version of a form (ADR 0720). The host makes it in the call's batch;
53
+ * no customer or public declaration may make it readable outside the org.
54
+ *
55
+ * - `form`: a relation (one record) to another of this extension's objects that declares
56
+ * `intake` with the role `form`. One submission object per form object.
57
+ * - `version`: a number field: the form's published version answered.
58
+ * - `answers`: a document field with `ANSWERS_SCHEMA`.
59
+ * - `person`: a relation to `person`: the submitter's (none on an anonymous form).
60
+ * - `certainty`: a select with exactly the options `INTAKE_CERTAINTIES`.
61
+ * - `spam`: a state of the object's `states` the host may move to from its initial state: what a
62
+ * submission the host keeps as spam is created in.
63
+ * - `via` (a select among `INTAKE_VIAS`) and `rating` (a number field: the first rating answer,
64
+ * for dashboards) are optional.
65
+ * - `marketing` (optional, a boolean field): set by the host when the person ticked a `marketing`
66
+ * consent line of the version answered ("keep me updated", decision 10), so a list can find
67
+ * them. The words they saw are the version's.
68
+ * - `about` and `aboutObject` (optional, together, text fields): set by the host when the answers
69
+ * came by a personal link about a record (a booking, ADR 0723): the record's id and its object's
70
+ * registry name. A field, not a relation: the record may be any installed pack's (Bookings'),
71
+ * and a pack that is not installed cannot be pointed to.
72
+ */
73
+ export interface IntakeSubmissionSpec {
74
+ role: "submission";
75
+ form: string;
76
+ version: string;
77
+ answers: string;
78
+ person: string;
79
+ certainty: string;
80
+ spam: string;
81
+ via?: string;
82
+ rating?: string;
83
+ marketing?: string;
84
+ about?: string;
85
+ aboutObject?: string;
86
+ }
87
+ /**
88
+ * A record a form may also make (ADR 0720): its `person` (a relation to `person`), the `fields`
89
+ * a form's questions may fill, and `source` (optional: a text or select field the host sets to
90
+ * `website`). Its owner comes from assignment rules, as for any create.
91
+ */
92
+ export interface IntakeDestinationSpec {
93
+ role: "destination";
94
+ person: string;
95
+ fields: readonly string[];
96
+ source?: string;
97
+ }
98
+ export type IntakeSpec = IntakeFormSpec | IntakeSubmissionSpec | IntakeDestinationSpec;
99
+ export declare const intakeManifestSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
100
+ role: z.ZodLiteral<"form">;
101
+ questions: z.ZodString;
102
+ answeredBy: z.ZodString;
103
+ openIn: z.ZodArray<z.ZodString>;
104
+ anonymous: z.ZodOptional<z.ZodString>;
105
+ destination: z.ZodOptional<z.ZodString>;
106
+ opensAt: z.ZodOptional<z.ZodString>;
107
+ closesAt: z.ZodOptional<z.ZodString>;
108
+ dailyCap: z.ZodOptional<z.ZodString>;
109
+ notify: z.ZodOptional<z.ZodString>;
110
+ hourlyCap: z.ZodOptional<z.ZodString>;
111
+ }, z.core.$strict>, z.ZodObject<{
112
+ role: z.ZodLiteral<"submission">;
113
+ form: z.ZodString;
114
+ version: z.ZodString;
115
+ answers: z.ZodString;
116
+ person: z.ZodString;
117
+ certainty: z.ZodString;
118
+ spam: z.ZodString;
119
+ via: z.ZodOptional<z.ZodString>;
120
+ rating: z.ZodOptional<z.ZodString>;
121
+ marketing: z.ZodOptional<z.ZodString>;
122
+ about: z.ZodOptional<z.ZodString>;
123
+ aboutObject: z.ZodOptional<z.ZodString>;
124
+ }, z.core.$strict>, z.ZodObject<{
125
+ role: z.ZodLiteral<"destination">;
126
+ person: z.ZodString;
127
+ fields: z.ZodArray<z.ZodString>;
128
+ source: z.ZodOptional<z.ZodString>;
129
+ }, z.core.$strict>], "role">;
130
+ export type IntakeManifest = z.infer<typeof intakeManifestSchema>;
131
+ /** Whether a document field's declared schema is exactly `schema` (one the platform owns). */
132
+ export declare function isPlatformSchema(field: FieldManifest | undefined, schema: unknown): boolean;
133
+ /** Reports problems with `objects.<name>.intake` through `contract`. */
134
+ export declare function checkObjectIntake(m: ExtensionManifest, name: string, object: ObjectManifest, contract: (path: string, message: string) => void): void;
package/dist/intake.js ADDED
@@ -0,0 +1,239 @@
1
+ // The `intake` behaviour (stage 29 step 2, ADR 0720): the one reviewed way the public may create
2
+ // a record. An object of a shipped pack opts in, and the host's one door, `intake.submit`, writes
3
+ // it. Its declaration, its manifest schema and its contract checks live here; the host's side (the
4
+ // door, the submitter's Person and the abuse checks) is packages/core/src/intake/.
5
+ //
6
+ // Three roles, each on an object of its own:
7
+ //
8
+ // - `form`: what is answered (a Forms form). A publishable record whose questions are a
9
+ // document field with the platform's QUESTIONS_SCHEMA, in its published content, with the
10
+ // fields the host reads to decide whether it takes answers now and from whom.
11
+ // - `submission`: one set of answers (a Forms submission). The host makes it, and nobody else:
12
+ // it points at the form and the version answered, the submitter's Person and how sure the host
13
+ // is of them, and holds the answers (ANSWERS_SCHEMA).
14
+ // - `destination`: another pack's record a form may also make (a Lead), with the fields a
15
+ // form's questions may fill.
16
+ //
17
+ // Only shipped packs and the org's own extensions declare it: it opens a write to the public.
18
+ import { z } from "zod";
19
+ import { ANSWERS_SCHEMA, QUESTIONS_SCHEMA } from "./questions.js";
20
+ /**
21
+ * How sure the host is whose a submission is (ADR 0721): `account`, a signed-in customer;
22
+ * `link`, a personal link; `staff`, entered by staff; `address`, an address a visitor typed,
23
+ * unconfirmed. An anonymous submission has none.
24
+ */
25
+ export const INTAKE_CERTAINTIES = ["account", "link", "staff", "address"];
26
+ /** Who may answer a form: anyone, signed-in customers, a personal link's holder, or staff. */
27
+ export const INTAKE_ANSWERED_BY = ["public", "customers", "links", "staff"];
28
+ /** Which way a submission came in. */
29
+ export const INTAKE_VIAS = ["site", "app", "page", "link", "staff"];
30
+ /** What the host writes in a destination's `source` field. */
31
+ export const INTAKE_SOURCE = "website";
32
+ const fieldName = z.string().min(1).max(40);
33
+ export const intakeManifestSchema = z.discriminatedUnion("role", [
34
+ z
35
+ .object({
36
+ role: z.literal("form"),
37
+ questions: fieldName,
38
+ answeredBy: fieldName,
39
+ openIn: z.array(z.string().min(1).max(100)).min(1).max(20),
40
+ anonymous: fieldName.optional(),
41
+ destination: fieldName.optional(),
42
+ opensAt: fieldName.optional(),
43
+ closesAt: fieldName.optional(),
44
+ dailyCap: fieldName.optional(),
45
+ notify: fieldName.optional(),
46
+ hourlyCap: fieldName.optional(),
47
+ })
48
+ .strict(),
49
+ z
50
+ .object({
51
+ role: z.literal("submission"),
52
+ form: fieldName,
53
+ version: fieldName,
54
+ answers: fieldName,
55
+ person: fieldName,
56
+ certainty: fieldName,
57
+ spam: z.string().min(1).max(100),
58
+ via: fieldName.optional(),
59
+ rating: fieldName.optional(),
60
+ marketing: fieldName.optional(),
61
+ about: fieldName.optional(),
62
+ aboutObject: fieldName.optional(),
63
+ })
64
+ .strict(),
65
+ z
66
+ .object({
67
+ role: z.literal("destination"),
68
+ person: fieldName,
69
+ fields: z.array(fieldName).min(1).max(40),
70
+ source: fieldName.optional(),
71
+ })
72
+ .strict(),
73
+ ]);
74
+ /** Whether a document field's declared schema is exactly `schema` (one the platform owns). */
75
+ export function isPlatformSchema(field, schema) {
76
+ return (field?.type === "document" &&
77
+ field.schema !== undefined &&
78
+ JSON.stringify(field.schema) === JSON.stringify(schema));
79
+ }
80
+ /** Reports problems with `objects.<name>.intake` through `contract`. */
81
+ export function checkObjectIntake(m, name, object, contract) {
82
+ const intake = object.intake;
83
+ if (!intake)
84
+ return;
85
+ const at = `objects.${name}.intake`;
86
+ const own = (local) => Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
87
+ const single = (local, types) => {
88
+ const field = local === undefined ? undefined : own(local);
89
+ return field !== undefined && types.includes(field.type) && !field.many;
90
+ };
91
+ const need = (key, local, types, what) => {
92
+ if (local !== undefined && !single(local, types)) {
93
+ contract(`${at}.${key}`, `"${local}" must be ${what} field of ${name}`);
94
+ }
95
+ };
96
+ const options = (local) => (own(local)?.options ?? []).map((o) => o.value);
97
+ const states = object.states;
98
+ const stateValues = new Set(states ? options(states.field) : []);
99
+ if (intake.role === "form") {
100
+ const questions = own(intake.questions);
101
+ if (!isPlatformSchema(questions, QUESTIONS_SCHEMA)) {
102
+ contract(`${at}.questions`, `"${intake.questions}" must be a document field of ${name} with the platform's QUESTIONS_SCHEMA`);
103
+ }
104
+ if (!object.publishable?.content.includes(intake.questions)) {
105
+ contract(`${at}.questions`, `${name} must be publishable with "${intake.questions}" in its content: answers are checked against the published version`);
106
+ }
107
+ // Who may answer, anonymity and the destination are part of what is published: an edit
108
+ // changes how a form treats answers only once a person publishes it (ADR 0720).
109
+ for (const key of ["answeredBy", "anonymous", "destination"]) {
110
+ const local = intake[key];
111
+ if (local !== undefined && !object.publishable?.content.includes(local)) {
112
+ contract(`${at}.${key}`, `"${local}" must be in ${name}'s publishable content: it is read from the version answered`);
113
+ }
114
+ }
115
+ need("answeredBy", intake.answeredBy, ["select"], "a select");
116
+ const answeredBy = options(intake.answeredBy);
117
+ if (!answeredBy.length ||
118
+ answeredBy.some((o) => !INTAKE_ANSWERED_BY.includes(o))) {
119
+ contract(`${at}.answeredBy`, `The options of "${intake.answeredBy}" are among ${INTAKE_ANSWERED_BY.join(", ")}`);
120
+ }
121
+ if (!states) {
122
+ contract(`${at}.openIn`, `${name} must declare states: a form takes answers in some`);
123
+ }
124
+ else {
125
+ for (const state of intake.openIn) {
126
+ if (!stateValues.has(state))
127
+ contract(`${at}.openIn`, `"${state}" is not a state of ${name}`);
128
+ }
129
+ }
130
+ need("anonymous", intake.anonymous, ["boolean"], "a boolean");
131
+ need("destination", intake.destination, ["text", "select"], "a text or select");
132
+ need("opensAt", intake.opensAt, ["datetime"], "a datetime");
133
+ need("closesAt", intake.closesAt, ["datetime"], "a datetime");
134
+ need("dailyCap", intake.dailyCap, ["number"], "a number");
135
+ need("notify", intake.notify, ["user"], "a user");
136
+ if (intake.hourlyCap !== undefined) {
137
+ const setting = Object.hasOwn(m.settings, intake.hourlyCap)
138
+ ? m.settings[intake.hourlyCap]
139
+ : undefined;
140
+ if (setting?.type !== "number") {
141
+ contract(`${at}.hourlyCap`, `"${intake.hourlyCap}" must be a number setting of ${m.id}`);
142
+ }
143
+ else if (setting.personApproves !== true) {
144
+ // How many answers the public may send is an abuse setting: never an AI's to change.
145
+ contract(`${at}.hourlyCap`, `"${intake.hourlyCap}" must be a setting a person approves (personApproves)`);
146
+ }
147
+ }
148
+ if (object.customer || object.customerVia) {
149
+ contract(at, "A form is read by the public through its published version: no customer");
150
+ }
151
+ return;
152
+ }
153
+ if (intake.role === "submission") {
154
+ const form = own(intake.form);
155
+ const target = form?.type === "relation" && !form.many ? form.to : undefined;
156
+ const local = target?.startsWith(`${m.id}.`) ? target.slice(m.id.length + 1) : undefined;
157
+ const formObject = local && Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
158
+ if (!formObject || local === name || formObject.intake?.role !== "form") {
159
+ contract(`${at}.form`, `"${intake.form}" must be a relation field (one record) to another of this extension's objects that declares intake with the role form`);
160
+ }
161
+ else {
162
+ const others = Object.entries(m.objects).filter(([other, o]) => other !== name &&
163
+ o.intake?.role === "submission" &&
164
+ o.fields[o.intake.form]?.to === target);
165
+ if (others.length) {
166
+ contract(`${at}.form`, `${target} is answered by ${others[0]?.[0]} already: one each`);
167
+ }
168
+ }
169
+ need("version", intake.version, ["number"], "a number");
170
+ if (!isPlatformSchema(own(intake.answers), ANSWERS_SCHEMA)) {
171
+ contract(`${at}.answers`, `"${intake.answers}" must be a document field of ${name} with the platform's ANSWERS_SCHEMA`);
172
+ }
173
+ const person = own(intake.person);
174
+ if (person?.type !== "relation" || person.many || person.to !== "person") {
175
+ contract(`${at}.person`, `"${intake.person}" must be a relation field (one) to person`);
176
+ }
177
+ need("certainty", intake.certainty, ["select"], "a select");
178
+ const certainty = options(intake.certainty);
179
+ if (certainty.length !== INTAKE_CERTAINTIES.length ||
180
+ INTAKE_CERTAINTIES.some((c) => !certainty.includes(c))) {
181
+ contract(`${at}.certainty`, `The options of "${intake.certainty}" are exactly ${INTAKE_CERTAINTIES.join(", ")}`);
182
+ }
183
+ if (!states) {
184
+ contract(`${at}.spam`, `${name} must declare states: spam is one of them`);
185
+ }
186
+ else if (!stateValues.has(intake.spam)) {
187
+ contract(`${at}.spam`, `"${intake.spam}" is not a state of ${name}`);
188
+ }
189
+ else if (intake.spam !== states.initial &&
190
+ !states.create?.in?.[intake.spam]?.includes("host") &&
191
+ !states.moves.some((move) => move.to === intake.spam &&
192
+ (move.from === states.initial ||
193
+ (Array.isArray(move.from) && move.from.includes(states.initial))) &&
194
+ move.by.includes("host"))) {
195
+ contract(`${at}.spam`, `The host must be able to create ${name} in "${intake.spam}" (create.in, or a host move from "${states.initial}")`);
196
+ }
197
+ if (intake.via !== undefined) {
198
+ need("via", intake.via, ["select"], "a select");
199
+ if (options(intake.via).some((o) => !INTAKE_VIAS.includes(o))) {
200
+ contract(`${at}.via`, `The options of "${intake.via}" are among ${INTAKE_VIAS.join(", ")}`);
201
+ }
202
+ }
203
+ need("rating", intake.rating, ["number"], "a number");
204
+ need("marketing", intake.marketing, ["boolean"], "a boolean");
205
+ need("about", intake.about, ["text"], "a text");
206
+ need("aboutObject", intake.aboutObject, ["text"], "a text");
207
+ if ((intake.about === undefined) !== (intake.aboutObject === undefined)) {
208
+ contract(`${at}.about`, "Declare about and aboutObject together");
209
+ }
210
+ // Nobody outside the org reads a submission, not even its submitter (ADR 0720).
211
+ if (object.customer || object.customerVia || object.public || object.signedIn) {
212
+ contract(at, "A submission is read by staff only: no customer, public or signedIn");
213
+ }
214
+ return;
215
+ }
216
+ const person = own(intake.person);
217
+ if (person?.type !== "relation" || person.many || person.to !== "person") {
218
+ contract(`${at}.person`, `"${intake.person}" must be a relation field (one) to person`);
219
+ }
220
+ intake.fields.forEach((local, i) => {
221
+ const field = own(local);
222
+ if (!field || field.type === "formula" || field.readOnly) {
223
+ contract(`${at}.fields.${i}`, `"${local}" must be a writable field of ${name}`);
224
+ }
225
+ else if (local === intake.person || local === intake.source || local === states?.field) {
226
+ contract(`${at}.fields.${i}`, `"${local}" is the host's to set`);
227
+ }
228
+ });
229
+ if (new Set(intake.fields).size !== intake.fields.length) {
230
+ contract(`${at}.fields`, "List each field once");
231
+ }
232
+ if (intake.source !== undefined) {
233
+ need("source", intake.source, ["text", "select"], "a text or select");
234
+ const source = own(intake.source);
235
+ if (source?.type === "select" && !options(intake.source).includes(INTAKE_SOURCE)) {
236
+ contract(`${at}.source`, `"${intake.source}" must have the option "${INTAKE_SOURCE}"`);
237
+ }
238
+ }
239
+ }
@@ -0,0 +1,89 @@
1
+ import { z } from "zod";
2
+ /** Most signals one extension declares. */
3
+ export declare const JOURNEY_SIGNALS_MAX = 12;
4
+ export interface JourneySignalSpec {
5
+ /** For admins choosing a journey's entry or exit: "Membership started". */
6
+ label: string;
7
+ /** For admins: when it happens, in plain words. */
8
+ description?: string;
9
+ /** An object of this extension (`membership`). */
10
+ object: string;
11
+ /**
12
+ * The relation field naming the Person it is about. Default: the object's customer field. A
13
+ * record with it empty (a guest's held booking) is nobody's signal.
14
+ */
15
+ person?: string;
16
+ /**
17
+ * The states that are the signal: the record entering one of them. Left out, only creating a
18
+ * record is (a membership started, whatever its first state).
19
+ */
20
+ enters?: readonly string[];
21
+ /** Only a move out of one of these states counts (a trial ending: `from: ["trialing"]`). */
22
+ from?: readonly string[];
23
+ /** Whether a record created straight into a state in `enters` counts. Default true. */
24
+ created?: boolean;
25
+ }
26
+ export declare const journeySignalSchema: z.ZodObject<{
27
+ label: z.ZodString;
28
+ description: z.ZodOptional<z.ZodString>;
29
+ object: z.ZodString;
30
+ person: z.ZodOptional<z.ZodString>;
31
+ enters: z.ZodOptional<z.ZodArray<z.ZodString>>;
32
+ from: z.ZodOptional<z.ZodArray<z.ZodString>>;
33
+ created: z.ZodOptional<z.ZodBoolean>;
34
+ }, z.core.$strict>;
35
+ export type JourneySignalManifest = z.infer<typeof journeySignalSchema>;
36
+ export declare const journeySignalsManifestSchema: z.ZodRecord<z.ZodString, z.ZodObject<{
37
+ label: z.ZodString;
38
+ description: z.ZodOptional<z.ZodString>;
39
+ object: z.ZodString;
40
+ person: z.ZodOptional<z.ZodString>;
41
+ enters: z.ZodOptional<z.ZodArray<z.ZodString>>;
42
+ from: z.ZodOptional<z.ZodArray<z.ZodString>>;
43
+ created: z.ZodOptional<z.ZodBoolean>;
44
+ }, z.core.$strict>>;
45
+ type Origins = string | readonly string[];
46
+ interface CheckObject {
47
+ fields: Record<string, {
48
+ type: string;
49
+ to?: string | undefined;
50
+ many?: boolean | undefined;
51
+ }>;
52
+ states?: {
53
+ field: string;
54
+ initial: string;
55
+ moves: readonly {
56
+ from: Origins;
57
+ to: string;
58
+ }[];
59
+ create?: {
60
+ in?: Record<string, unknown> | undefined;
61
+ } | undefined;
62
+ follows?: {
63
+ from: readonly string[];
64
+ when: Record<string, string>;
65
+ } | undefined;
66
+ after?: readonly {
67
+ from: Origins;
68
+ to: string;
69
+ }[] | undefined;
70
+ } | undefined;
71
+ customer?: {
72
+ field: string;
73
+ } | undefined;
74
+ }
75
+ interface CheckManifest {
76
+ objects: Record<string, CheckObject>;
77
+ journeySignals?: Record<string, JourneySignalManifest> | undefined;
78
+ }
79
+ /** The contract's issues with a manifest's `journeySignals`. */
80
+ export declare function journeySignalIssues(manifest: CheckManifest): {
81
+ path: string;
82
+ message: string;
83
+ }[];
84
+ /**
85
+ * Whether a write is the signal. `before` is the record's values before (null on create), `after`
86
+ * as the write leaves it; `stateField` the object's states field, when it has one.
87
+ */
88
+ export declare function journeySignalFires(signal: Pick<JourneySignalSpec, "enters" | "from" | "created">, stateField: string | undefined, before: Record<string, unknown> | null, after: Record<string, unknown>): boolean;
89
+ export {};
@@ -0,0 +1,151 @@
1
+ // Journey signals (stage 53, ADR 0802): the moments about a Person a shipped pack names, that a
2
+ // journey can start or stop on ("membership started", "class attended"). A signal is a record of
3
+ // one of the pack's objects being created, or entering one of its declared states, and the field
4
+ // that names its Person. The host records, in the write's own batch, the last time each Person had
5
+ // each signal (`core_person_signal`); no pack code runs for it, and a pack cannot hook a write.
6
+ //
7
+ // This module is the contract's part: the declaration's shape and checks, and the pure rule the
8
+ // host uses to decide whether a write is the signal.
9
+ import { z } from "zod";
10
+ /** Most signals one extension declares. */
11
+ export const JOURNEY_SIGNALS_MAX = 12;
12
+ const NAME = /^[a-z][a-zA-Z0-9]{0,39}$/;
13
+ const state = z.string().min(1).max(64);
14
+ export const journeySignalSchema = z
15
+ .object({
16
+ label: z.string().min(1).max(80),
17
+ description: z.string().max(300).optional(),
18
+ object: z.string().min(1).max(30),
19
+ person: z.string().min(1).max(40).optional(),
20
+ enters: z.array(state).min(1).max(20).optional(),
21
+ from: z.array(state).min(1).max(20).optional(),
22
+ created: z.boolean().optional(),
23
+ })
24
+ .strict();
25
+ export const journeySignalsManifestSchema = z.record(z.string().regex(NAME, "camelCase letters and digits, starting lowercase"), journeySignalSchema);
26
+ /** The values a states field can hold: its select's options. */
27
+ function stateValues(object) {
28
+ const field = object.states ? object.fields[object.states.field] : undefined;
29
+ const options = field?.options;
30
+ if (!options)
31
+ return null;
32
+ return new Set(options.map((o) => typeof o === "string" ? o : String(o.value ?? "")));
33
+ }
34
+ /**
35
+ * Every way a record of `object` changes state: its moves, its timed moves (`after`) and the
36
+ * moves it follows from a related record (`follows`), each as where from and where to.
37
+ */
38
+ function movesOf(object) {
39
+ const states = object.states;
40
+ if (!states)
41
+ return [];
42
+ const list = (from) => (typeof from === "string" ? [from] : from);
43
+ return [
44
+ ...states.moves.map((m) => ({ from: list(m.from), to: m.to })),
45
+ ...(states.after ?? []).map((m) => ({ from: list(m.from), to: m.to })),
46
+ ...Object.values(states.follows?.when ?? {}).map((to) => ({
47
+ from: states.follows?.from ?? [],
48
+ to,
49
+ })),
50
+ ];
51
+ }
52
+ /** The contract's issues with a manifest's `journeySignals`. */
53
+ export function journeySignalIssues(manifest) {
54
+ const issues = [];
55
+ const contract = (path, message) => issues.push({ path, message });
56
+ const signals = manifest.journeySignals ?? {};
57
+ if (Object.keys(signals).length > JOURNEY_SIGNALS_MAX) {
58
+ contract("journeySignals", `At most ${JOURNEY_SIGNALS_MAX} journey signals`);
59
+ }
60
+ for (const [name, signal] of Object.entries(signals)) {
61
+ const base = `journeySignals.${name}`;
62
+ const object = manifest.objects[signal.object];
63
+ if (!object) {
64
+ contract(`${base}.object`, `"${signal.object}" is not an object of this extension`);
65
+ continue;
66
+ }
67
+ const person = signal.person ?? object.customer?.field;
68
+ if (!person) {
69
+ contract(`${base}.person`, "Name the relation field that holds the Person it is about");
70
+ }
71
+ else {
72
+ const field = object.fields[person];
73
+ if (!field)
74
+ contract(`${base}.person`, `"${person}" is not a field of ${signal.object}`);
75
+ else if (field.type !== "relation" || field.to !== "person" || field.many) {
76
+ contract(`${base}.person`, `"${person}" must be a relation to one person`);
77
+ }
78
+ }
79
+ if (!signal.enters) {
80
+ if (signal.from)
81
+ contract(`${base}.from`, "from needs enters: the states it moves into");
82
+ if (signal.created === false) {
83
+ contract(`${base}.created`, "A signal that is not a create needs enters");
84
+ }
85
+ continue;
86
+ }
87
+ const values = stateValues(object);
88
+ if (!values) {
89
+ contract(`${base}.enters`, `${signal.object} declares no states`);
90
+ continue;
91
+ }
92
+ let known = true;
93
+ for (const [key, list] of [
94
+ ["enters", signal.enters],
95
+ ["from", signal.from ?? []],
96
+ ]) {
97
+ list.forEach((value, i) => {
98
+ if (!values.has(value)) {
99
+ known = false;
100
+ contract(`${base}.${key}.${i}`, `"${value}" is not a state of ${signal.object}`);
101
+ }
102
+ });
103
+ }
104
+ if (!known)
105
+ continue;
106
+ // A signal that can happen: a declared move into one of its states (from one it names), or a
107
+ // create straight into one. A `from` no move leaves for them names nothing.
108
+ const enters = new Set(signal.enters);
109
+ const into = movesOf(object).filter((m) => enters.has(m.to));
110
+ const createdIn = new Set([
111
+ object.states?.initial ?? "",
112
+ ...Object.keys(object.states?.create?.in ?? {}),
113
+ ]);
114
+ const byCreate = signal.created !== false && signal.enters.some((e) => createdIn.has(e));
115
+ const fromOk = (m) => m.from.some((f) => f !== m.to && (!signal.from || signal.from.includes(f)));
116
+ if (!byCreate && !into.some(fromOk)) {
117
+ contract(`${base}.enters`, `No declared move or create of ${signal.object} makes this signal: it can never happen`);
118
+ continue;
119
+ }
120
+ signal.from?.forEach((value, i) => {
121
+ if (!into.some((m) => m.to !== value && m.from.includes(value))) {
122
+ contract(`${base}.from.${i}`, `No declared move of ${signal.object} leaves "${value}" for ${[...enters].join(" or ")}`);
123
+ }
124
+ });
125
+ }
126
+ return issues;
127
+ }
128
+ /** A position value as a record holds it (a select's value). */
129
+ function stateOf(value) {
130
+ return value === null || value === undefined ? null : String(value);
131
+ }
132
+ /**
133
+ * Whether a write is the signal. `before` is the record's values before (null on create), `after`
134
+ * as the write leaves it; `stateField` the object's states field, when it has one.
135
+ */
136
+ export function journeySignalFires(signal, stateField, before, after) {
137
+ const now = stateField ? stateOf(after[stateField]) : null;
138
+ if (before === null) {
139
+ if (signal.created === false)
140
+ return false;
141
+ if (!signal.enters)
142
+ return true;
143
+ return now !== null && signal.enters.includes(now);
144
+ }
145
+ if (!signal.enters || !stateField || now === null)
146
+ return false;
147
+ const was = stateOf(before[stateField]);
148
+ if (was === now || !signal.enters.includes(now))
149
+ return false;
150
+ return !signal.from || (was !== null && signal.from.includes(was));
151
+ }