@composable-os/sdk 0.1.0-beta.10 → 0.1.0-beta.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/acknowledge.d.ts +38 -0
- package/dist/acknowledge.js +30 -0
- package/dist/agreements.d.ts +80 -0
- package/dist/agreements.js +109 -0
- package/dist/allowance.d.ts +47 -5
- package/dist/allowance.js +59 -4
- package/dist/automation-check.d.ts +6 -1
- package/dist/automation-check.js +93 -8
- package/dist/automation.d.ts +62 -4
- package/dist/automation.js +32 -9
- package/dist/behaviour.js +25 -6
- package/dist/calendar-entry.d.ts +97 -0
- package/dist/calendar-entry.js +180 -0
- package/dist/capacity.d.ts +69 -0
- package/dist/capacity.js +63 -2
- package/dist/checklists.d.ts +41 -0
- package/dist/checklists.js +75 -0
- package/dist/contract.d.ts +527 -26
- package/dist/contract.js +21 -3
- package/dist/discount.d.ts +135 -0
- package/dist/discount.js +281 -0
- package/dist/document.d.ts +2 -1
- package/dist/document.js +2 -1
- package/dist/field-types.d.ts +1 -1
- package/dist/field-types.js +5 -0
- package/dist/formula/compile.d.ts +15 -0
- package/dist/formula/compile.js +9 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/intake.d.ts +134 -0
- package/dist/intake.js +239 -0
- package/dist/journey-signals.d.ts +89 -0
- package/dist/journey-signals.js +151 -0
- package/dist/manifest.d.ts +1060 -60
- package/dist/manifest.js +1111 -61
- package/dist/mine.d.ts +45 -0
- package/dist/mine.js +119 -0
- package/dist/occupies.d.ts +64 -0
- package/dist/occupies.js +122 -0
- package/dist/questions.d.ts +179 -0
- package/dist/questions.js +514 -0
- package/dist/recurring.d.ts +33 -4
- package/dist/recurring.js +50 -3
- package/dist/restricted-fields.d.ts +15 -0
- package/dist/restricted-fields.js +97 -0
- package/dist/rich-text.d.ts +22 -0
- package/dist/rich-text.js +91 -0
- package/dist/routine-mail.d.ts +400 -0
- package/dist/routine-mail.js +955 -0
- package/dist/runtime.d.ts +5 -11
- package/dist/runtime.js +21 -11
- package/dist/screens.d.ts +3 -0
- package/dist/screens.js +74 -0
- package/dist/sellable.d.ts +231 -2
- package/dist/sellable.js +342 -1
- package/dist/slug.d.ts +2 -0
- package/dist/slug.js +6 -2
- package/dist/staffed.d.ts +178 -0
- package/dist/staffed.js +287 -0
- package/dist/states.d.ts +74 -4
- package/dist/states.js +154 -58
- package/dist/stock.d.ts +129 -0
- package/dist/stock.js +205 -0
- package/dist/team.d.ts +28 -0
- package/dist/team.js +52 -0
- package/dist/testing.d.ts +22 -1
- package/dist/testing.js +47 -5
- package/dist/worked-on.d.ts +42 -0
- package/dist/worked-on.js +79 -0
- package/package.json +9 -1
- package/src/acknowledge.ts +100 -0
- package/src/agreements.ts +185 -0
- package/src/allowance.ts +98 -8
- package/src/automation-check.ts +120 -9
- package/src/automation.ts +78 -10
- package/src/behaviour.ts +26 -6
- package/src/calendar-entry.ts +258 -0
- package/src/capacity.ts +103 -2
- package/src/checklists.ts +118 -0
- package/src/contract.ts +514 -27
- package/src/discount.ts +408 -0
- package/src/document.ts +4 -2
- package/src/field-types.ts +5 -0
- package/src/formula/compile.ts +10 -0
- package/src/index.ts +17 -0
- package/src/intake.ts +380 -0
- package/src/journey-signals.ts +215 -0
- package/src/manifest.ts +1313 -66
- package/src/mine.ts +161 -0
- package/src/occupies.ts +192 -0
- package/src/questions.ts +681 -0
- package/src/recurring.ts +90 -9
- package/src/restricted-fields.ts +129 -0
- package/src/rich-text.ts +112 -0
- package/src/routine-mail.ts +1272 -0
- package/src/runtime.ts +25 -12
- package/src/screens.ts +91 -0
- package/src/sellable.ts +595 -1
- package/src/slug.ts +7 -2
- package/src/staffed.ts +474 -0
- package/src/states.ts +220 -64
- package/src/stock.ts +332 -0
- package/src/team.ts +83 -0
- package/src/testing.ts +70 -6
- package/src/worked-on.ts +134 -0
package/src/states.ts
CHANGED
|
@@ -41,18 +41,47 @@ export interface StateMove {
|
|
|
41
41
|
* Tools of this extension that may also make the move for whoever called them (stage 32b,
|
|
42
42
|
* ADR 0513): a move by `["host"]` with `tools: ["voidPass"]` is made by the host and by that
|
|
43
43
|
* tool, and by no other staff write (not `record.update`, not the assistant, not an automation).
|
|
44
|
-
* Each tool must be `humanOnly
|
|
45
|
-
*
|
|
44
|
+
* Each tool must be `humanOnly`, or `personApproves` (ADR 0552: anyone else's call waits for a
|
|
45
|
+
* person's approval), and declare a `permission`, so the move is a person's act with that
|
|
46
|
+
* permission.
|
|
46
47
|
*/
|
|
47
48
|
tools?: readonly string[];
|
|
48
49
|
/**
|
|
49
50
|
* Its window binds staff too (ADR 0507), every staff caller, a person included: a cancel that
|
|
50
51
|
* gives a use back is in time for everyone. The host is not bound. A person who must make the
|
|
51
|
-
* move outside it does so through another move only a person's tool makes (`tools`).
|
|
52
|
+
* move outside it does so through another move only a person's tool makes (`tools`). A move
|
|
53
|
+
* staff alone make may have a window with it (ADR 0551): a coach ticks a name from an hour
|
|
54
|
+
* before the class, not days ahead.
|
|
52
55
|
*/
|
|
53
56
|
bindsStaff?: boolean;
|
|
54
57
|
}
|
|
55
58
|
|
|
59
|
+
/**
|
|
60
|
+
* A move the host makes by itself once a moment has passed (ADR 0550): a booking still `booked`
|
|
61
|
+
* 30 minutes after its class ends becomes a `no_show`, if anyone on the class was ticked. The
|
|
62
|
+
* runner's minute cron finds the records due and moves each as the host, from where it was or not
|
|
63
|
+
* at all. Each needs a declared `host` move from each of `from` to `to`.
|
|
64
|
+
*/
|
|
65
|
+
export interface TimedMove {
|
|
66
|
+
/** The state, or states, the record moves from. */
|
|
67
|
+
from: string | readonly string[];
|
|
68
|
+
to: string;
|
|
69
|
+
/** A datetime field of the record, or `<relation>.<field>` one relation away (`class.endsAt`). */
|
|
70
|
+
at: string;
|
|
71
|
+
/** Minutes after `at` the move is due (0 to 10,080). */
|
|
72
|
+
minutesAfter?: number;
|
|
73
|
+
/**
|
|
74
|
+
* Only while another live record of this object pointing at the same record (through `at`'s
|
|
75
|
+
* relation) is in one of these states: a class whose attendance was taken has someone ticked.
|
|
76
|
+
*/
|
|
77
|
+
ifAnyIn?: readonly string[];
|
|
78
|
+
/**
|
|
79
|
+
* Only while each of these fields of the record has a value (stage 49, ADR 0764): an appointment
|
|
80
|
+
* whose coach left is never marked attended for a session nobody gave.
|
|
81
|
+
*/
|
|
82
|
+
ifSet?: readonly string[];
|
|
83
|
+
}
|
|
84
|
+
|
|
56
85
|
/**
|
|
57
86
|
* A number a window is worked out from (ADR 0505): a number; a number field named as a window's
|
|
58
87
|
* `at` is; or `{ field?, setting? }`, the field's value and, when it is empty, this extension's
|
|
@@ -120,6 +149,12 @@ export interface StatesSpec {
|
|
|
120
149
|
* one, staff included (no booking into a class that was cancelled or is over).
|
|
121
150
|
*/
|
|
122
151
|
closed?: string | readonly string[];
|
|
152
|
+
/**
|
|
153
|
+
* Fields every create must give a value (stage 49, ADR 0764), whoever makes it, the host
|
|
154
|
+
* included: an appointment is made for a client. Unlike `required`, the field may be emptied
|
|
155
|
+
* later (erasing the Person keeps the record for the business's books, ADR 0320).
|
|
156
|
+
*/
|
|
157
|
+
requires?: readonly string[];
|
|
123
158
|
};
|
|
124
159
|
/**
|
|
125
160
|
* Moves a caller asks for by name, resolved by the host (ADR 0503): writing the name (`cancel`)
|
|
@@ -134,13 +169,18 @@ export interface StatesSpec {
|
|
|
134
169
|
* state named in `when`, the host moves every live record of this object pointing at it, and in
|
|
135
170
|
* one of `from`, to the state `when` gives, in the same batch, as the host (a class cancelled
|
|
136
171
|
* moves its bookings to `cancelled_by_us`: their places released, their uses given back). Each
|
|
137
|
-
* target needs a `host` move from each of `from`.
|
|
172
|
+
* target needs a `host` move from each of `from`. The parent is another of this extension's
|
|
173
|
+
* objects, or one of an extension this one `requires`, named in full (`ticketing.event`); a
|
|
174
|
+
* parent others draw places from as a pool is followed only by an object that draws none
|
|
175
|
+
* (ADR 0841).
|
|
138
176
|
*/
|
|
139
177
|
follows?: {
|
|
140
178
|
relation: string;
|
|
141
179
|
when: Readonly<Record<string, string>>;
|
|
142
180
|
from: readonly string[];
|
|
143
181
|
};
|
|
182
|
+
/** Moves the host makes once a moment has passed (ADR 0550), at most four. */
|
|
183
|
+
after?: readonly TimedMove[];
|
|
144
184
|
}
|
|
145
185
|
|
|
146
186
|
const fieldName = z.string().max(40);
|
|
@@ -155,7 +195,7 @@ const windowNumber = (number: z.ZodNumber) =>
|
|
|
155
195
|
.strict()
|
|
156
196
|
.refine((n) => n.field !== undefined || n.setting !== undefined, "Name a field or a setting"),
|
|
157
197
|
]);
|
|
158
|
-
const
|
|
198
|
+
export const windowBoundSchema = z.union([
|
|
159
199
|
fieldPath,
|
|
160
200
|
z
|
|
161
201
|
.object({
|
|
@@ -168,7 +208,7 @@ const windowBound = z.union([
|
|
|
168
208
|
]);
|
|
169
209
|
const staffOnlyPaths = z.union([fieldPath, z.array(fieldPath).min(1).max(4)]);
|
|
170
210
|
const moveWindow = z
|
|
171
|
-
.object({ from:
|
|
211
|
+
.object({ from: windowBoundSchema.optional(), until: windowBoundSchema.optional() })
|
|
172
212
|
.strict()
|
|
173
213
|
.refine((w) => w.from !== undefined || w.until !== undefined, "A window needs from or until");
|
|
174
214
|
|
|
@@ -199,6 +239,7 @@ export const statesManifestSchema = z.object({
|
|
|
199
239
|
window: moveWindow.optional(),
|
|
200
240
|
staffOnly: staffOnlyPaths.optional(),
|
|
201
241
|
closed: staffOnlyPaths.optional(),
|
|
242
|
+
requires: z.array(fieldName).min(1).max(20).optional(),
|
|
202
243
|
})
|
|
203
244
|
.strict()
|
|
204
245
|
.optional(),
|
|
@@ -216,6 +257,22 @@ export const statesManifestSchema = z.object({
|
|
|
216
257
|
})
|
|
217
258
|
.strict()
|
|
218
259
|
.optional(),
|
|
260
|
+
after: z
|
|
261
|
+
.array(
|
|
262
|
+
z
|
|
263
|
+
.object({
|
|
264
|
+
from: z.union([stateValue, z.array(stateValue).min(1).max(20)]),
|
|
265
|
+
to: stateValue,
|
|
266
|
+
at: fieldPath,
|
|
267
|
+
minutesAfter: z.number().int().min(0).max(10_080).optional(),
|
|
268
|
+
ifAnyIn: z.array(stateValue).min(1).max(20).optional(),
|
|
269
|
+
ifSet: z.array(fieldName).min(1).max(5).optional(),
|
|
270
|
+
})
|
|
271
|
+
.strict(),
|
|
272
|
+
)
|
|
273
|
+
.min(1)
|
|
274
|
+
.max(4)
|
|
275
|
+
.optional(),
|
|
219
276
|
});
|
|
220
277
|
export type StatesManifest = z.infer<typeof statesManifestSchema>;
|
|
221
278
|
|
|
@@ -317,48 +374,18 @@ export function checkObjectStates(
|
|
|
317
374
|
});
|
|
318
375
|
|
|
319
376
|
// Windows, staff-only rules and requests (ADR 0503; two relations and settings, ADR 0505).
|
|
320
|
-
const checkPath = (
|
|
321
|
-
const field = resolveFieldPath(m, object, at);
|
|
322
|
-
// A staff-only rule may read a formula: the host computes it in the view (a boolean one).
|
|
323
|
-
const formulaOk = type === "boolean" && field?.type === "formula";
|
|
324
|
-
if ((field?.type !== type && !formulaOk) || field?.many) {
|
|
325
|
-
contract(
|
|
326
|
-
path,
|
|
327
|
-
`"${at}" must be a ${type} field of ${name}, or <relation>.<field> through up to two relations (one record each), every field on the way declared by this extension`,
|
|
328
|
-
);
|
|
329
|
-
}
|
|
330
|
-
};
|
|
331
|
-
const checkNumber = (value: WindowNumber | undefined, path: string) => {
|
|
332
|
-
if (typeof value === "string") checkPath(value, path, "number");
|
|
333
|
-
else if (typeof value === "object") {
|
|
334
|
-
if (value.field !== undefined) checkPath(value.field, `${path}.field`, "number");
|
|
335
|
-
if (value.setting !== undefined) {
|
|
336
|
-
const settings = m.settings ?? {};
|
|
337
|
-
const setting = Object.hasOwn(settings, value.setting)
|
|
338
|
-
? settings[value.setting]
|
|
339
|
-
: undefined;
|
|
340
|
-
if (setting?.type !== "number") {
|
|
341
|
-
contract(`${path}.setting`, `"${value.setting}" must be a number setting of ${m.id}`);
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
};
|
|
346
|
-
const checkWindow = (window: MoveWindow | undefined, path: string) => {
|
|
347
|
-
for (const end of ["from", "until"] as const) {
|
|
348
|
-
const bound = window?.[end];
|
|
349
|
-
if (bound === undefined) continue;
|
|
350
|
-
const b = typeof bound === "string" ? { at: bound } : bound;
|
|
351
|
-
checkPath(b.at, `${path}.${end}.at`, "datetime");
|
|
352
|
-
for (const key of ["daysBefore", "minutesBefore"] as const) {
|
|
353
|
-
checkNumber(b[key], `${path}.${end}.${key}`);
|
|
354
|
-
}
|
|
355
|
-
if (b.zone !== undefined) checkPath(b.zone, `${path}.${end}.zone`, "timezone");
|
|
356
|
-
}
|
|
357
|
-
};
|
|
377
|
+
const { checkPath, checkWindow } = windowChecks(m, object, name, contract);
|
|
358
378
|
states.moves.forEach((move, i) => {
|
|
359
379
|
const mp = `${sp}.moves.${i}`;
|
|
360
|
-
|
|
361
|
-
|
|
380
|
+
// A window binds customers, or staff too with bindsStaff (ADR 0551); staffOnly customers.
|
|
381
|
+
if (move.staffOnly && !move.by.includes("customer")) {
|
|
382
|
+
contract(mp, "staffOnly binds customers: the move must list customer in by");
|
|
383
|
+
}
|
|
384
|
+
if (move.window && !move.by.includes("customer") && !move.bindsStaff) {
|
|
385
|
+
contract(
|
|
386
|
+
mp,
|
|
387
|
+
"A window binds customers: the move must list customer in by, or bind staff (bindsStaff)",
|
|
388
|
+
);
|
|
362
389
|
}
|
|
363
390
|
checkWindow(move.window, `${mp}.window`);
|
|
364
391
|
for (const p of staffOnlyPathsOf(move.staffOnly)) checkPath(p, `${mp}.staffOnly`, "boolean");
|
|
@@ -367,15 +394,15 @@ export function checkObjectStates(
|
|
|
367
394
|
const spec = m.tools?.[tool];
|
|
368
395
|
if (!spec) {
|
|
369
396
|
contract(`${mp}.tools`, `"${tool}" is not a tool of this extension`);
|
|
370
|
-
} else if (!spec.humanOnly || !spec.permission) {
|
|
397
|
+
} else if ((!spec.humanOnly && !spec.personApproves) || !spec.permission) {
|
|
371
398
|
contract(
|
|
372
399
|
`${mp}.tools`,
|
|
373
|
-
`"${tool}" must be humanOnly and declare a permission: the move is a person's act with it`,
|
|
400
|
+
`"${tool}" must be humanOnly or personApproves and declare a permission: the move is a person's act with it`,
|
|
374
401
|
);
|
|
375
402
|
}
|
|
376
403
|
}
|
|
377
|
-
if (move.bindsStaff && (!move.window || !move.by.includes("staff"))) {
|
|
378
|
-
contract(`${mp}.bindsStaff`, "bindsStaff needs a window and staff in by");
|
|
404
|
+
if (move.bindsStaff && (!move.window || (!move.by.includes("staff") && !move.tools))) {
|
|
405
|
+
contract(`${mp}.bindsStaff`, "bindsStaff needs a window, and staff in by or named tools");
|
|
379
406
|
}
|
|
380
407
|
if (move.tools && move.by.includes("staff")) {
|
|
381
408
|
contract(
|
|
@@ -398,27 +425,63 @@ export function checkObjectStates(
|
|
|
398
425
|
for (const p of staffOnlyPathsOf(states.create.closed)) {
|
|
399
426
|
checkPath(p, `${sp}.create.closed`, "boolean");
|
|
400
427
|
}
|
|
428
|
+
for (const required of states.create.requires ?? []) {
|
|
429
|
+
if (!fieldOf(required)) {
|
|
430
|
+
contract(`${sp}.create.requires`, `"${required}" is not a field of ${name}`);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
401
433
|
}
|
|
402
434
|
if (states.follows) {
|
|
403
435
|
const fp = `${sp}.follows`;
|
|
404
436
|
const f = states.follows;
|
|
405
437
|
const rel = fieldOf(f.relation);
|
|
406
|
-
const
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
438
|
+
const to = rel?.type === "relation" && !rel.many ? rel.to : undefined;
|
|
439
|
+
const own = to?.startsWith(`${m.id}.`) ? to.slice(m.id.length + 1) : undefined;
|
|
440
|
+
const target = own !== undefined && Object.hasOwn(m.objects, own) ? m.objects[own] : undefined;
|
|
441
|
+
// A parent in another extension this one requires (ADR 0841): its states are the host's to
|
|
442
|
+
// check, at install and on every upgrade of either extension (core's checkManifest).
|
|
443
|
+
const otherExt = to && own === undefined && to.includes(".") ? to.split(".")[0] : undefined;
|
|
444
|
+
const required = otherExt !== undefined && (m.requires ?? []).includes(otherExt);
|
|
445
|
+
if (!target?.states && !required) {
|
|
411
446
|
contract(
|
|
412
447
|
`${fp}.relation`,
|
|
413
|
-
`"${f.relation}" must be a relation (one record) of ${name} to another of this extension's objects that declares states`,
|
|
448
|
+
`"${f.relation}" must be a relation (one record) of ${name} to another of this extension's objects that declares states, or to an object of an extension this one requires`,
|
|
414
449
|
);
|
|
415
450
|
} else {
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
);
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
451
|
+
// A parent whose places other records draw on as a pool counts claims that are not its
|
|
452
|
+
// own, which the cascade's check at commit cannot read (ADR 0550). A child that draws on
|
|
453
|
+
// it (a member of the pool, or claiming places on the parent or on a member) is refused;
|
|
454
|
+
// one that draws nothing moves no places and may follow it (ADR 0841). A pool is always in
|
|
455
|
+
// the parent's own extension, so a parent in another extension is never drawn on from here.
|
|
456
|
+
if (target && own !== undefined) {
|
|
457
|
+
const localOf = (ref: string | undefined) =>
|
|
458
|
+
ref?.startsWith(`${m.id}.`) ? ref.slice(m.id.length + 1) : ref;
|
|
459
|
+
const poolOf = (other: ObjectManifest | undefined) => {
|
|
460
|
+
const poolField = other?.capacity?.pool;
|
|
461
|
+
return poolField ? localOf(other?.fields[poolField]?.to) : undefined;
|
|
462
|
+
};
|
|
463
|
+
const members = new Set(
|
|
464
|
+
Object.entries(m.objects)
|
|
465
|
+
.filter(([, other]) => poolOf(other) === own)
|
|
466
|
+
.map(([key]) => key),
|
|
467
|
+
);
|
|
468
|
+
const claimsOn = object.claims ? localOf(object.fields[object.claims.on]?.to) : undefined;
|
|
469
|
+
const draws =
|
|
470
|
+
members.has(name) ||
|
|
471
|
+
(claimsOn !== undefined && (claimsOn === own || members.has(claimsOn)));
|
|
472
|
+
if (members.size && draws) {
|
|
473
|
+
contract(
|
|
474
|
+
`${fp}.relation`,
|
|
475
|
+
`${to} is a pool other records draw places from, and ${name} draws places from it: a cascade cannot follow it`,
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
const parentStates = target?.states
|
|
480
|
+
? new Set((target.fields[target.states.field]?.options ?? []).map((o) => o.value))
|
|
481
|
+
: null;
|
|
482
|
+
for (const [when, into] of Object.entries(f.when)) {
|
|
483
|
+
if (parentStates && !parentStates.has(when)) {
|
|
484
|
+
contract(`${fp}.when`, `"${when}" is not a state of ${to ?? f.relation}`);
|
|
422
485
|
}
|
|
423
486
|
for (const from of f.from) {
|
|
424
487
|
if (!isState(from))
|
|
@@ -426,15 +489,55 @@ export function checkObjectStates(
|
|
|
426
489
|
else if (
|
|
427
490
|
!states.moves.some(
|
|
428
491
|
(move) =>
|
|
429
|
-
move.to ===
|
|
492
|
+
move.to === into && move.by.includes("host") && moveOrigins(move).includes(from),
|
|
430
493
|
)
|
|
431
494
|
) {
|
|
432
|
-
contract(`${fp}.when`, `${name} needs a host move from "${from}" to "${
|
|
495
|
+
contract(`${fp}.when`, `${name} needs a host move from "${from}" to "${into}"`);
|
|
433
496
|
}
|
|
434
497
|
}
|
|
435
498
|
}
|
|
436
499
|
}
|
|
437
500
|
}
|
|
501
|
+
// Timed moves (ADR 0550): a declared host move each, and a moment the sweep reads in SQL.
|
|
502
|
+
(states.after ?? []).forEach((rule, i) => {
|
|
503
|
+
const tp = `${sp}.after.${i}`;
|
|
504
|
+
if (!isState(rule.to)) contract(`${tp}.to`, `"${rule.to}" is not an option of ${states.field}`);
|
|
505
|
+
for (const from of moveOrigins(rule)) {
|
|
506
|
+
if (!isState(from)) {
|
|
507
|
+
contract(`${tp}.from`, `"${from}" is not an option of ${states.field}`);
|
|
508
|
+
} else if (
|
|
509
|
+
!states.moves.some(
|
|
510
|
+
(move) =>
|
|
511
|
+
move.to === rule.to && move.by.includes("host") && moveOrigins(move).includes(from),
|
|
512
|
+
)
|
|
513
|
+
) {
|
|
514
|
+
contract(`${tp}.from`, `${name} needs a host move from "${from}" to "${rule.to}"`);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
const parts = rule.at.split(".");
|
|
518
|
+
const at = parts.length <= 2 ? resolveFieldPath(m, object, rule.at) : undefined;
|
|
519
|
+
const via = parts.length === 2 ? fieldOf(parts[0] as string) : undefined;
|
|
520
|
+
const ownTarget = via?.type === "relation" && !via.many && via.to?.startsWith(`${m.id}.`);
|
|
521
|
+
if (at?.type !== "datetime" || at.many || (parts.length === 2 && !ownTarget)) {
|
|
522
|
+
contract(
|
|
523
|
+
`${tp}.at`,
|
|
524
|
+
`"${rule.at}" must be a datetime field of ${name}, or <relation>.<field> on another of this extension's objects`,
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
for (const f of rule.ifSet ?? []) {
|
|
528
|
+
if (!fieldOf(f)) contract(`${tp}.ifSet`, `"${f}" is not a field of ${name}`);
|
|
529
|
+
}
|
|
530
|
+
if (rule.ifAnyIn) {
|
|
531
|
+
if (parts.length !== 2) {
|
|
532
|
+
contract(`${tp}.ifAnyIn`, "ifAnyIn needs at one relation away: the records it compares");
|
|
533
|
+
}
|
|
534
|
+
for (const state of rule.ifAnyIn) {
|
|
535
|
+
if (!isState(state)) {
|
|
536
|
+
contract(`${tp}.ifAnyIn`, `"${state}" is not an option of ${states.field}`);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
});
|
|
438
541
|
for (const [request, targets] of Object.entries(states.requests ?? {})) {
|
|
439
542
|
const rp = `${sp}.requests.${request}`;
|
|
440
543
|
if (isState(request)) contract(rp, `"${request}" is a state; a request needs its own name`);
|
|
@@ -448,6 +551,59 @@ export function checkObjectStates(
|
|
|
448
551
|
}
|
|
449
552
|
}
|
|
450
553
|
|
|
554
|
+
/**
|
|
555
|
+
* The checks of window paths and numbers (ADRs 0503 and 0505): for a `states` declaration, and
|
|
556
|
+
* for any other declaration that names a moment the same way (a waitlist's cut-off, ADR 0520).
|
|
557
|
+
*/
|
|
558
|
+
export function windowChecks(
|
|
559
|
+
m: ExtensionManifest,
|
|
560
|
+
object: ObjectManifest,
|
|
561
|
+
name: string,
|
|
562
|
+
contract: (path: string, message: string) => void,
|
|
563
|
+
) {
|
|
564
|
+
const checkPath = (at: string, path: string, type: string) => {
|
|
565
|
+
const field = resolveFieldPath(m, object, at);
|
|
566
|
+
// A staff-only rule may read a formula: the host computes it in the view (a boolean one).
|
|
567
|
+
const formulaOk = type === "boolean" && field?.type === "formula";
|
|
568
|
+
if ((field?.type !== type && !formulaOk) || field?.many) {
|
|
569
|
+
contract(
|
|
570
|
+
path,
|
|
571
|
+
`"${at}" must be a ${type} field of ${name}, or <relation>.<field> through up to two relations (one record each), every field on the way declared by this extension`,
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
};
|
|
575
|
+
const checkNumber = (value: WindowNumber | undefined, path: string) => {
|
|
576
|
+
if (typeof value === "string") checkPath(value, path, "number");
|
|
577
|
+
else if (typeof value === "object") {
|
|
578
|
+
if (value.field !== undefined) checkPath(value.field, `${path}.field`, "number");
|
|
579
|
+
if (value.setting !== undefined) {
|
|
580
|
+
const settings = m.settings ?? {};
|
|
581
|
+
const setting = Object.hasOwn(settings, value.setting)
|
|
582
|
+
? settings[value.setting]
|
|
583
|
+
: undefined;
|
|
584
|
+
if (setting?.type !== "number") {
|
|
585
|
+
contract(`${path}.setting`, `"${value.setting}" must be a number setting of ${m.id}`);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
const checkBound = (bound: string | WindowBound, path: string) => {
|
|
591
|
+
const b = typeof bound === "string" ? { at: bound } : bound;
|
|
592
|
+
checkPath(b.at, `${path}.at`, "datetime");
|
|
593
|
+
for (const key of ["daysBefore", "minutesBefore"] as const) {
|
|
594
|
+
checkNumber(b[key], `${path}.${key}`);
|
|
595
|
+
}
|
|
596
|
+
if (b.zone !== undefined) checkPath(b.zone, `${path}.zone`, "timezone");
|
|
597
|
+
};
|
|
598
|
+
const checkWindow = (window: MoveWindow | undefined, path: string) => {
|
|
599
|
+
for (const end of ["from", "until"] as const) {
|
|
600
|
+
const bound = window?.[end];
|
|
601
|
+
if (bound !== undefined) checkBound(bound, `${path}.${end}`);
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
return { checkPath, checkNumber, checkBound, checkWindow };
|
|
605
|
+
}
|
|
606
|
+
|
|
451
607
|
/**
|
|
452
608
|
* The field a path names (ADR 0503; two relations from ADR 0505): `field` on the object, or
|
|
453
609
|
* `relation.field` / `relation.relation.field` through relations of one record. Every field on
|