@composable-os/sdk 0.1.0-beta.10 → 0.1.0-beta.11
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/capacity.d.ts +69 -0
- package/dist/capacity.js +62 -0
- package/dist/contract.d.ts +32 -0
- package/dist/manifest.d.ts +116 -4
- package/dist/manifest.js +75 -1
- package/dist/sellable.d.ts +78 -0
- package/dist/sellable.js +154 -1
- package/dist/states.d.ts +57 -3
- package/dist/states.js +115 -47
- package/package.json +1 -1
- package/src/capacity.ts +102 -0
- package/src/contract.ts +25 -0
- package/src/manifest.ts +77 -1
- package/src/sellable.ts +251 -1
- package/src/states.ts +163 -49
package/src/states.ts
CHANGED
|
@@ -41,18 +41,42 @@ 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
|
+
|
|
56
80
|
/**
|
|
57
81
|
* A number a window is worked out from (ADR 0505): a number; a number field named as a window's
|
|
58
82
|
* `at` is; or `{ field?, setting? }`, the field's value and, when it is empty, this extension's
|
|
@@ -141,6 +165,8 @@ export interface StatesSpec {
|
|
|
141
165
|
when: Readonly<Record<string, string>>;
|
|
142
166
|
from: readonly string[];
|
|
143
167
|
};
|
|
168
|
+
/** Moves the host makes once a moment has passed (ADR 0550), at most four. */
|
|
169
|
+
after?: readonly TimedMove[];
|
|
144
170
|
}
|
|
145
171
|
|
|
146
172
|
const fieldName = z.string().max(40);
|
|
@@ -155,7 +181,7 @@ const windowNumber = (number: z.ZodNumber) =>
|
|
|
155
181
|
.strict()
|
|
156
182
|
.refine((n) => n.field !== undefined || n.setting !== undefined, "Name a field or a setting"),
|
|
157
183
|
]);
|
|
158
|
-
const
|
|
184
|
+
export const windowBoundSchema = z.union([
|
|
159
185
|
fieldPath,
|
|
160
186
|
z
|
|
161
187
|
.object({
|
|
@@ -168,7 +194,7 @@ const windowBound = z.union([
|
|
|
168
194
|
]);
|
|
169
195
|
const staffOnlyPaths = z.union([fieldPath, z.array(fieldPath).min(1).max(4)]);
|
|
170
196
|
const moveWindow = z
|
|
171
|
-
.object({ from:
|
|
197
|
+
.object({ from: windowBoundSchema.optional(), until: windowBoundSchema.optional() })
|
|
172
198
|
.strict()
|
|
173
199
|
.refine((w) => w.from !== undefined || w.until !== undefined, "A window needs from or until");
|
|
174
200
|
|
|
@@ -216,6 +242,21 @@ export const statesManifestSchema = z.object({
|
|
|
216
242
|
})
|
|
217
243
|
.strict()
|
|
218
244
|
.optional(),
|
|
245
|
+
after: z
|
|
246
|
+
.array(
|
|
247
|
+
z
|
|
248
|
+
.object({
|
|
249
|
+
from: z.union([stateValue, z.array(stateValue).min(1).max(20)]),
|
|
250
|
+
to: stateValue,
|
|
251
|
+
at: fieldPath,
|
|
252
|
+
minutesAfter: z.number().int().min(0).max(10_080).optional(),
|
|
253
|
+
ifAnyIn: z.array(stateValue).min(1).max(20).optional(),
|
|
254
|
+
})
|
|
255
|
+
.strict(),
|
|
256
|
+
)
|
|
257
|
+
.min(1)
|
|
258
|
+
.max(4)
|
|
259
|
+
.optional(),
|
|
219
260
|
});
|
|
220
261
|
export type StatesManifest = z.infer<typeof statesManifestSchema>;
|
|
221
262
|
|
|
@@ -317,48 +358,18 @@ export function checkObjectStates(
|
|
|
317
358
|
});
|
|
318
359
|
|
|
319
360
|
// 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
|
-
};
|
|
361
|
+
const { checkPath, checkWindow } = windowChecks(m, object, name, contract);
|
|
358
362
|
states.moves.forEach((move, i) => {
|
|
359
363
|
const mp = `${sp}.moves.${i}`;
|
|
360
|
-
|
|
361
|
-
|
|
364
|
+
// A window binds customers, or staff too with bindsStaff (ADR 0551); staffOnly customers.
|
|
365
|
+
if (move.staffOnly && !move.by.includes("customer")) {
|
|
366
|
+
contract(mp, "staffOnly binds customers: the move must list customer in by");
|
|
367
|
+
}
|
|
368
|
+
if (move.window && !move.by.includes("customer") && !move.bindsStaff) {
|
|
369
|
+
contract(
|
|
370
|
+
mp,
|
|
371
|
+
"A window binds customers: the move must list customer in by, or bind staff (bindsStaff)",
|
|
372
|
+
);
|
|
362
373
|
}
|
|
363
374
|
checkWindow(move.window, `${mp}.window`);
|
|
364
375
|
for (const p of staffOnlyPathsOf(move.staffOnly)) checkPath(p, `${mp}.staffOnly`, "boolean");
|
|
@@ -367,15 +378,15 @@ export function checkObjectStates(
|
|
|
367
378
|
const spec = m.tools?.[tool];
|
|
368
379
|
if (!spec) {
|
|
369
380
|
contract(`${mp}.tools`, `"${tool}" is not a tool of this extension`);
|
|
370
|
-
} else if (!spec.humanOnly || !spec.permission) {
|
|
381
|
+
} else if ((!spec.humanOnly && !spec.personApproves) || !spec.permission) {
|
|
371
382
|
contract(
|
|
372
383
|
`${mp}.tools`,
|
|
373
|
-
`"${tool}" must be humanOnly and declare a permission: the move is a person's act with it`,
|
|
384
|
+
`"${tool}" must be humanOnly or personApproves and declare a permission: the move is a person's act with it`,
|
|
374
385
|
);
|
|
375
386
|
}
|
|
376
387
|
}
|
|
377
|
-
if (move.bindsStaff && (!move.window || !move.by.includes("staff"))) {
|
|
378
|
-
contract(`${mp}.bindsStaff`, "bindsStaff needs a window and staff in by");
|
|
388
|
+
if (move.bindsStaff && (!move.window || (!move.by.includes("staff") && !move.tools))) {
|
|
389
|
+
contract(`${mp}.bindsStaff`, "bindsStaff needs a window, and staff in by or named tools");
|
|
379
390
|
}
|
|
380
391
|
if (move.tools && move.by.includes("staff")) {
|
|
381
392
|
contract(
|
|
@@ -413,6 +424,19 @@ export function checkObjectStates(
|
|
|
413
424
|
`"${f.relation}" must be a relation (one record) of ${name} to another of this extension's objects that declares states`,
|
|
414
425
|
);
|
|
415
426
|
} else {
|
|
427
|
+
// A parent whose places other records draw on as a pool: its counter counts claims that
|
|
428
|
+
// are not its own, which the cascade's check at commit cannot read (ADR 0550).
|
|
429
|
+
const pooled = Object.values(m.objects).some((other) => {
|
|
430
|
+
const poolField = other.capacity?.pool;
|
|
431
|
+
const relTo = poolField ? other.fields[poolField]?.to : undefined;
|
|
432
|
+
return relTo === rel?.to || relTo === rel?.to?.slice(m.id.length + 1);
|
|
433
|
+
});
|
|
434
|
+
if (pooled) {
|
|
435
|
+
contract(
|
|
436
|
+
`${fp}.relation`,
|
|
437
|
+
`${rel?.to ?? f.relation} is a pool other records draw places from: a cascade cannot follow it`,
|
|
438
|
+
);
|
|
439
|
+
}
|
|
416
440
|
const parentStates = new Set(
|
|
417
441
|
(target.fields[target.states.field]?.options ?? []).map((o) => o.value),
|
|
418
442
|
);
|
|
@@ -435,6 +459,43 @@ export function checkObjectStates(
|
|
|
435
459
|
}
|
|
436
460
|
}
|
|
437
461
|
}
|
|
462
|
+
// Timed moves (ADR 0550): a declared host move each, and a moment the sweep reads in SQL.
|
|
463
|
+
(states.after ?? []).forEach((rule, i) => {
|
|
464
|
+
const tp = `${sp}.after.${i}`;
|
|
465
|
+
if (!isState(rule.to)) contract(`${tp}.to`, `"${rule.to}" is not an option of ${states.field}`);
|
|
466
|
+
for (const from of moveOrigins(rule)) {
|
|
467
|
+
if (!isState(from)) {
|
|
468
|
+
contract(`${tp}.from`, `"${from}" is not an option of ${states.field}`);
|
|
469
|
+
} else if (
|
|
470
|
+
!states.moves.some(
|
|
471
|
+
(move) =>
|
|
472
|
+
move.to === rule.to && move.by.includes("host") && moveOrigins(move).includes(from),
|
|
473
|
+
)
|
|
474
|
+
) {
|
|
475
|
+
contract(`${tp}.from`, `${name} needs a host move from "${from}" to "${rule.to}"`);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
const parts = rule.at.split(".");
|
|
479
|
+
const at = parts.length <= 2 ? resolveFieldPath(m, object, rule.at) : undefined;
|
|
480
|
+
const via = parts.length === 2 ? fieldOf(parts[0] as string) : undefined;
|
|
481
|
+
const ownTarget = via?.type === "relation" && !via.many && via.to?.startsWith(`${m.id}.`);
|
|
482
|
+
if (at?.type !== "datetime" || at.many || (parts.length === 2 && !ownTarget)) {
|
|
483
|
+
contract(
|
|
484
|
+
`${tp}.at`,
|
|
485
|
+
`"${rule.at}" must be a datetime field of ${name}, or <relation>.<field> on another of this extension's objects`,
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
if (rule.ifAnyIn) {
|
|
489
|
+
if (parts.length !== 2) {
|
|
490
|
+
contract(`${tp}.ifAnyIn`, "ifAnyIn needs at one relation away: the records it compares");
|
|
491
|
+
}
|
|
492
|
+
for (const state of rule.ifAnyIn) {
|
|
493
|
+
if (!isState(state)) {
|
|
494
|
+
contract(`${tp}.ifAnyIn`, `"${state}" is not an option of ${states.field}`);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
});
|
|
438
499
|
for (const [request, targets] of Object.entries(states.requests ?? {})) {
|
|
439
500
|
const rp = `${sp}.requests.${request}`;
|
|
440
501
|
if (isState(request)) contract(rp, `"${request}" is a state; a request needs its own name`);
|
|
@@ -448,6 +509,59 @@ export function checkObjectStates(
|
|
|
448
509
|
}
|
|
449
510
|
}
|
|
450
511
|
|
|
512
|
+
/**
|
|
513
|
+
* The checks of window paths and numbers (ADRs 0503 and 0505): for a `states` declaration, and
|
|
514
|
+
* for any other declaration that names a moment the same way (a waitlist's cut-off, ADR 0520).
|
|
515
|
+
*/
|
|
516
|
+
export function windowChecks(
|
|
517
|
+
m: ExtensionManifest,
|
|
518
|
+
object: ObjectManifest,
|
|
519
|
+
name: string,
|
|
520
|
+
contract: (path: string, message: string) => void,
|
|
521
|
+
) {
|
|
522
|
+
const checkPath = (at: string, path: string, type: string) => {
|
|
523
|
+
const field = resolveFieldPath(m, object, at);
|
|
524
|
+
// A staff-only rule may read a formula: the host computes it in the view (a boolean one).
|
|
525
|
+
const formulaOk = type === "boolean" && field?.type === "formula";
|
|
526
|
+
if ((field?.type !== type && !formulaOk) || field?.many) {
|
|
527
|
+
contract(
|
|
528
|
+
path,
|
|
529
|
+
`"${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`,
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
const checkNumber = (value: WindowNumber | undefined, path: string) => {
|
|
534
|
+
if (typeof value === "string") checkPath(value, path, "number");
|
|
535
|
+
else if (typeof value === "object") {
|
|
536
|
+
if (value.field !== undefined) checkPath(value.field, `${path}.field`, "number");
|
|
537
|
+
if (value.setting !== undefined) {
|
|
538
|
+
const settings = m.settings ?? {};
|
|
539
|
+
const setting = Object.hasOwn(settings, value.setting)
|
|
540
|
+
? settings[value.setting]
|
|
541
|
+
: undefined;
|
|
542
|
+
if (setting?.type !== "number") {
|
|
543
|
+
contract(`${path}.setting`, `"${value.setting}" must be a number setting of ${m.id}`);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
const checkBound = (bound: string | WindowBound, path: string) => {
|
|
549
|
+
const b = typeof bound === "string" ? { at: bound } : bound;
|
|
550
|
+
checkPath(b.at, `${path}.at`, "datetime");
|
|
551
|
+
for (const key of ["daysBefore", "minutesBefore"] as const) {
|
|
552
|
+
checkNumber(b[key], `${path}.${key}`);
|
|
553
|
+
}
|
|
554
|
+
if (b.zone !== undefined) checkPath(b.zone, `${path}.zone`, "timezone");
|
|
555
|
+
};
|
|
556
|
+
const checkWindow = (window: MoveWindow | undefined, path: string) => {
|
|
557
|
+
for (const end of ["from", "until"] as const) {
|
|
558
|
+
const bound = window?.[end];
|
|
559
|
+
if (bound !== undefined) checkBound(bound, `${path}.${end}`);
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
return { checkPath, checkNumber, checkBound, checkWindow };
|
|
563
|
+
}
|
|
564
|
+
|
|
451
565
|
/**
|
|
452
566
|
* The field a path names (ADR 0503; two relations from ADR 0505): `field` on the object, or
|
|
453
567
|
* `relation.field` / `relation.relation.field` through relations of one record. Every field on
|