@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.
@@ -98,6 +98,53 @@ export interface SellableSpec {
98
98
  * "sign_in"`). Such a line holds one, and an order at most one such line. Not on a plan.
99
99
  */
100
100
  onePerPerson?: string;
101
+ /**
102
+ * A paid line moves a record that already exists (stage 31a step 8, ADR 0560), see MovesSpec.
103
+ * A record of such an object is bought once, when it is made: `order.startHeld` makes it in
104
+ * `held` and starts its order in one batch. It is never put on `order.start`. Not with
105
+ * `grants`, `deposit`, `recurring`, `onePerPerson`, `maxPerOrder` or `saleWindow`.
106
+ */
107
+ moves?: MovesSpec;
108
+ }
109
+ /**
110
+ * What an order does to the record it was started for (ADR 0560): a drop-in booking held while
111
+ * its order is unpaid. The host makes every move, as the host, in the batch that moves the order.
112
+ *
113
+ * - `held`: the state `order.startHeld` makes the record in (`states.create.in` lets the host,
114
+ * and only the host, create it there). A capacity claimant holds its place in it.
115
+ * - `paid`: the order is paid: the record moves `held` -> `paid`.
116
+ * - `expired`: the order expired or was cancelled unpaid: `held` -> `expired`, a final state.
117
+ * A record that leaves `held` any other way (a class the gym cancels) cancels its unpaid order.
118
+ * - `refund`: states that give the money back. A record whose order is paid entering one of them
119
+ * queues the order's refund for a person with `orders:refund` to approve (never an AI).
120
+ * - `price`: where the price of one is read when the record is made: a number field through up
121
+ * to two relations (`class.classType.dropInPrice`), else the extension's number setting. The
122
+ * host writes it into the read-only `price` field, and the order's line is priced from it.
123
+ * - `title`: the order line's title, a text field through up to two relations (`class.title`).
124
+ * - `order`: a read-only text field the host writes the order's id into.
125
+ * - `guest`: a buyer who is not signed in is matched to no Person before payment: their record is
126
+ * held with its customer field empty (so it is no one's, blocks no one's own, and tells the
127
+ * buyer nothing about who has that address), and is given its Person when the payment is
128
+ * verified, by the rule a guest's order uses. The customer field must not be `required`; the
129
+ * host keeps it set in every state but `held`. Without it, a guest cannot buy the object.
130
+ * - `holdCap`: a number setting of the extension: the percent of the claimed record's places that
131
+ * unpaid guest holds may take at once (at least one). Over it a guest is told to try again soon.
132
+ */
133
+ export interface MovesSpec {
134
+ held: string;
135
+ paid: string;
136
+ expired: string;
137
+ refund?: readonly string[];
138
+ price?: {
139
+ field?: string;
140
+ setting?: string;
141
+ };
142
+ title?: string;
143
+ order?: string;
144
+ guest?: boolean;
145
+ holdCap?: {
146
+ setting: string;
147
+ };
101
148
  }
102
149
  /**
103
150
  * How a plan recurs (ADR 0485). The payment provider charges every `interval` on its own schedule;
@@ -131,6 +178,17 @@ export interface RecurringSpec {
131
178
  * `sellable.recurring`, and its `grants` is what the subscription gives while it runs.
132
179
  */
133
180
  grants?: RecurringGrantSpec;
181
+ /**
182
+ * Whether staff may pause a subscription to the plan (stage 32b, decision 10, ADR 0531), and
183
+ * for how long: `minDays` and `maxDays` name number fields of the plan holding the shortest and
184
+ * longest pause the company allows (empty: 1 day, and 366 days, the most the host asks the
185
+ * provider for). Left out, a subscription to the plan is never paused. Only staff pause, with
186
+ * `subscriptions:manage`, as a person; while paused nothing is charged, granted or spent.
187
+ */
188
+ pause?: {
189
+ minDays?: string;
190
+ maxDays?: string;
191
+ };
134
192
  }
135
193
  /** Where a field of what a subscription grants gets its value (see RecurringSpec.grants). */
136
194
  export type RecurringGrantSource = "item" | `item.${string}` | "subscription" | "subscription.holder" | "subscription.payer";
@@ -201,6 +259,22 @@ export declare const sellableManifestSchema: z.ZodObject<{
201
259
  }, z.core.$strict>>;
202
260
  }, z.core.$strict>>;
203
261
  onePerPerson: z.ZodOptional<z.ZodString>;
262
+ moves: z.ZodOptional<z.ZodObject<{
263
+ held: z.ZodString;
264
+ paid: z.ZodString;
265
+ expired: z.ZodString;
266
+ refund: z.ZodOptional<z.ZodArray<z.ZodString>>;
267
+ price: z.ZodOptional<z.ZodObject<{
268
+ field: z.ZodOptional<z.ZodString>;
269
+ setting: z.ZodOptional<z.ZodString>;
270
+ }, z.core.$strict>>;
271
+ title: z.ZodOptional<z.ZodString>;
272
+ order: z.ZodOptional<z.ZodString>;
273
+ guest: z.ZodOptional<z.ZodBoolean>;
274
+ holdCap: z.ZodOptional<z.ZodObject<{
275
+ setting: z.ZodString;
276
+ }, z.core.$strict>>;
277
+ }, z.core.$strict>>;
204
278
  tax: z.ZodOptional<z.ZodObject<{
205
279
  code: z.ZodString;
206
280
  place: z.ZodOptional<z.ZodObject<{
@@ -219,6 +293,10 @@ export declare const sellableManifestSchema: z.ZodObject<{
219
293
  year: "year";
220
294
  }>;
221
295
  trialDays: z.ZodOptional<z.ZodString>;
296
+ pause: z.ZodOptional<z.ZodObject<{
297
+ minDays: z.ZodOptional<z.ZodString>;
298
+ maxDays: z.ZodOptional<z.ZodString>;
299
+ }, z.core.$strict>>;
222
300
  grants: z.ZodOptional<z.ZodObject<{
223
301
  object: z.ZodString;
224
302
  set: z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodEnum<{
package/dist/sellable.js CHANGED
@@ -5,7 +5,7 @@
5
5
  // contract checks live here; the host's side is packages/core/src/orders/ (sellable.ts, grants.ts).
6
6
  import { z } from "zod";
7
7
  import { customerWritable, spendsOnList } from "./allowance.js";
8
- import { moveOrigins } from "./states.js";
8
+ import { moveOrigins, resolveFieldPath } from "./states.js";
9
9
  /** The host's subscription states a grant follows (ADR 0485). */
10
10
  export const SUBSCRIPTION_GRANT_STATES = [
11
11
  "trialing",
@@ -72,6 +72,23 @@ export const sellableManifestSchema = z
72
72
  .strict()
73
73
  .optional(),
74
74
  onePerPerson: fieldName.optional(),
75
+ moves: z
76
+ .object({
77
+ held: z.string().min(1).max(100),
78
+ paid: z.string().min(1).max(100),
79
+ expired: z.string().min(1).max(100),
80
+ refund: z.array(z.string().min(1).max(100)).max(20).optional(),
81
+ price: z
82
+ .object({ field: z.string().max(130).optional(), setting: fieldName.optional() })
83
+ .strict()
84
+ .optional(),
85
+ title: z.string().max(130).optional(),
86
+ order: fieldName.optional(),
87
+ guest: z.boolean().optional(),
88
+ holdCap: z.object({ setting: fieldName }).strict().optional(),
89
+ })
90
+ .strict()
91
+ .optional(),
75
92
  tax: z
76
93
  .object({
77
94
  code: z.string().regex(TAX_CODE_PATTERN, "A product tax code: txcd_ and eight digits"),
@@ -93,6 +110,10 @@ export const sellableManifestSchema = z
93
110
  .object({
94
111
  interval: z.enum(["week", "month", "year"]),
95
112
  trialDays: fieldName.optional(),
113
+ pause: z
114
+ .object({ minDays: fieldName.optional(), maxDays: fieldName.optional() })
115
+ .strict()
116
+ .optional(),
96
117
  grants: z
97
118
  .object({
98
119
  object: z.string().max(40),
@@ -165,6 +186,12 @@ export function checkObjectSellable(m, name, object, contract) {
165
186
  if (trial !== undefined && !single(trial, new Set(["number"]))) {
166
187
  contract(`${path}.recurring.trialDays`, `"${trial}" must be a number field of ${name}`);
167
188
  }
189
+ for (const key of ["minDays", "maxDays"]) {
190
+ const days = sellable.recurring.pause?.[key];
191
+ if (days !== undefined && !single(days, new Set(["number"]))) {
192
+ contract(`${path}.recurring.pause.${key}`, `"${days}" must be a number field of ${name}`);
193
+ }
194
+ }
168
195
  if (sellable.onePerPerson !== undefined) {
169
196
  contract(`${path}.onePerPerson`, "A plan's trial is already one per person (ADR 0489); onePerPerson is for things bought once");
170
197
  }
@@ -192,6 +219,8 @@ export function checkObjectSellable(m, name, object, contract) {
192
219
  }
193
220
  if (sellable.grants)
194
221
  checkGrants(m, name, object, sellable.grants, `${path}.grants`, contract);
222
+ if (sellable.moves)
223
+ checkMoves(m, name, object, sellable.moves, `${path}.moves`, contract);
195
224
  const place = sellable.tax?.place;
196
225
  if (place) {
197
226
  let holder = object;
@@ -225,6 +254,130 @@ export function checkObjectSellable(m, name, object, contract) {
225
254
  }
226
255
  }
227
256
  }
257
+ /**
258
+ * Checks `sellable.moves` (ADR 0560): each of these is a way the order's batch could otherwise fail
259
+ * (a payment the host could not apply), or a record someone could hold with no order.
260
+ */
261
+ function checkMoves(m, name, object, moves, path, contract) {
262
+ const sellable = object.sellable;
263
+ for (const key of [
264
+ "grants",
265
+ "deposit",
266
+ "recurring",
267
+ "onePerPerson",
268
+ "maxPerOrder",
269
+ "saleWindow",
270
+ ]) {
271
+ if (sellable[key] !== undefined) {
272
+ contract(`objects.${name}.sellable.${key}`, `A record bought with moves is one record on one order: it has no ${key}`);
273
+ }
274
+ }
275
+ const field = (local) => Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
276
+ const price = field(sellable.price);
277
+ if (price && !price.readOnly) {
278
+ contract(`objects.${name}.fields.${sellable.price}`, `"${sellable.price}" holds what the host priced it at: make it readOnly`);
279
+ }
280
+ if (moves.order !== undefined) {
281
+ const order = field(moves.order);
282
+ if (order?.type !== "text" || order.many || !order.readOnly) {
283
+ contract(`${path}.order`, `"${moves.order}" must be a readOnly text field of ${name}`);
284
+ }
285
+ }
286
+ if (moves.price) {
287
+ if (moves.price.field === undefined && moves.price.setting === undefined) {
288
+ contract(`${path}.price`, "price names a field, a setting or both");
289
+ }
290
+ if (moves.price.field !== undefined) {
291
+ const at = resolveFieldPath(m, object, moves.price.field);
292
+ if (!at || at.many || (at.type !== "number" && at.type !== "currency")) {
293
+ contract(`${path}.price.field`, `"${moves.price.field}" must be a number field of ${name}, or <relation>.<field> through up to two relations, every field on the way declared by this extension`);
294
+ }
295
+ }
296
+ if (moves.price.setting !== undefined) {
297
+ const settings = m.settings ?? {};
298
+ const setting = Object.hasOwn(settings, moves.price.setting)
299
+ ? settings[moves.price.setting]
300
+ : undefined;
301
+ if (setting?.type !== "number") {
302
+ contract(`${path}.price.setting`, `"${moves.price.setting}" must be a number setting of ${m.id}`);
303
+ }
304
+ }
305
+ }
306
+ if (moves.title !== undefined) {
307
+ const at = resolveFieldPath(m, object, moves.title);
308
+ if (!at || at.many || at.type !== "text") {
309
+ contract(`${path}.title`, `"${moves.title}" must be a text field of ${name}, or through up to two relations`);
310
+ }
311
+ }
312
+ if (!object.customer)
313
+ contract(path, `${name} must declare customer: whose record it is`);
314
+ if (moves.guest && object.customer) {
315
+ const owner = field(object.customer.field);
316
+ if (owner?.required) {
317
+ contract(`objects.${name}.fields.${object.customer.field}`, `A guest's ${name} is held with no ${object.customer.field} until it is paid: it cannot be required (the host keeps it set outside "${moves.held}")`);
318
+ }
319
+ }
320
+ if (moves.holdCap) {
321
+ const settings = m.settings ?? {};
322
+ const setting = Object.hasOwn(settings, moves.holdCap.setting)
323
+ ? settings[moves.holdCap.setting]
324
+ : undefined;
325
+ if (setting?.type !== "number") {
326
+ contract(`${path}.holdCap.setting`, `"${moves.holdCap.setting}" must be a number setting of ${m.id}`);
327
+ }
328
+ if (!moves.guest)
329
+ contract(`${path}.holdCap`, "holdCap limits guests' holds: declare guest");
330
+ if (!object.claims)
331
+ contract(`${path}.holdCap`, `${name} must claim places (claims)`);
332
+ }
333
+ const states = object.states;
334
+ if (!states) {
335
+ contract(path, `${name} must declare states: the order moves it`);
336
+ return;
337
+ }
338
+ const values = new Set((object.fields[states.field]?.options ?? []).map((o) => o.value));
339
+ const finals = new Set(states.moves.filter((mv) => mv.final).map((mv) => mv.to));
340
+ for (const key of ["held", "paid", "expired"]) {
341
+ if (!values.has(moves[key])) {
342
+ contract(`${path}.${key}`, `"${moves[key]}" is not a state of ${name}`);
343
+ }
344
+ }
345
+ if (new Set([moves.held, moves.paid, moves.expired]).size !== 3) {
346
+ contract(path, "held, paid and expired are three different states");
347
+ }
348
+ const creators = states.create?.in?.[moves.held] ?? [];
349
+ if (!creators.includes("host")) {
350
+ contract(`${path}.held`, `states.create.in must let the host create ${name} in "${moves.held}"`);
351
+ }
352
+ if (creators.some((by) => by !== "host")) {
353
+ contract(`${path}.held`, `Only the host creates ${name} in "${moves.held}": anyone else would hold it with no order`);
354
+ }
355
+ const hostMove = (to) => states.moves.some((mv) => mv.to === to && moveOrigins(mv).includes(moves.held) && mv.by.includes("host"));
356
+ for (const key of ["paid", "expired"]) {
357
+ if (!hostMove(moves[key])) {
358
+ contract(`${path}.${key}`, `${name} needs a move from "${moves.held}" to "${moves[key]}" the host may make`);
359
+ }
360
+ }
361
+ if (!finals.has(moves.expired)) {
362
+ contract(`${path}.expired`, `"${moves.expired}" must be a final state of ${name}`);
363
+ }
364
+ for (const mv of states.moves) {
365
+ if (!moveOrigins(mv).includes(moves.held))
366
+ continue;
367
+ if (mv.by.some((by) => by !== "host")) {
368
+ contract(`${path}.held`, `Only the host moves ${name} out of "${moves.held}" (to "${mv.to}"): its order says where it goes`);
369
+ }
370
+ }
371
+ for (const [i, state] of (moves.refund ?? []).entries()) {
372
+ if (!values.has(state) || state === moves.held || state === moves.expired) {
373
+ contract(`${path}.refund.${i}`, `"${state}" must be a state of ${name} other than held and expired`);
374
+ }
375
+ }
376
+ const allowance = object.allowance;
377
+ if (allowance && "spends" in allowance && !allowance.spends.waived) {
378
+ contract(path, `${name} spends uses: declare allowance.spends.waived, which the host sets on a record its order pays for`);
379
+ }
380
+ }
228
381
  function checkGrants(m, name, item, grants, path, contract) {
229
382
  const local = grants.object.startsWith(`${m.id}.`)
230
383
  ? grants.object.slice(m.id.length + 1)
package/dist/states.d.ts CHANGED
@@ -35,17 +35,40 @@ export interface StateMove {
35
35
  * Tools of this extension that may also make the move for whoever called them (stage 32b,
36
36
  * ADR 0513): a move by `["host"]` with `tools: ["voidPass"]` is made by the host and by that
37
37
  * tool, and by no other staff write (not `record.update`, not the assistant, not an automation).
38
- * Each tool must be `humanOnly` and declare a `permission`, so the move is a person's act with
39
- * that permission.
38
+ * Each tool must be `humanOnly`, or `personApproves` (ADR 0552: anyone else's call waits for a
39
+ * person's approval), and declare a `permission`, so the move is a person's act with that
40
+ * permission.
40
41
  */
41
42
  tools?: readonly string[];
42
43
  /**
43
44
  * Its window binds staff too (ADR 0507), every staff caller, a person included: a cancel that
44
45
  * gives a use back is in time for everyone. The host is not bound. A person who must make the
45
- * move outside it does so through another move only a person's tool makes (`tools`).
46
+ * move outside it does so through another move only a person's tool makes (`tools`). A move
47
+ * staff alone make may have a window with it (ADR 0551): a coach ticks a name from an hour
48
+ * before the class, not days ahead.
46
49
  */
47
50
  bindsStaff?: boolean;
48
51
  }
52
+ /**
53
+ * A move the host makes by itself once a moment has passed (ADR 0550): a booking still `booked`
54
+ * 30 minutes after its class ends becomes a `no_show`, if anyone on the class was ticked. The
55
+ * runner's minute cron finds the records due and moves each as the host, from where it was or not
56
+ * at all. Each needs a declared `host` move from each of `from` to `to`.
57
+ */
58
+ export interface TimedMove {
59
+ /** The state, or states, the record moves from. */
60
+ from: string | readonly string[];
61
+ to: string;
62
+ /** A datetime field of the record, or `<relation>.<field>` one relation away (`class.endsAt`). */
63
+ at: string;
64
+ /** Minutes after `at` the move is due (0 to 10,080). */
65
+ minutesAfter?: number;
66
+ /**
67
+ * Only while another live record of this object pointing at the same record (through `at`'s
68
+ * relation) is in one of these states: a class whose attendance was taken has someone ticked.
69
+ */
70
+ ifAnyIn?: readonly string[];
71
+ }
49
72
  /**
50
73
  * A number a window is worked out from (ADR 0505): a number; a number field named as a window's
51
74
  * `at` is; or `{ field?, setting? }`, the field's value and, when it is empty, this extension's
@@ -134,7 +157,21 @@ export interface StatesSpec {
134
157
  when: Readonly<Record<string, string>>;
135
158
  from: readonly string[];
136
159
  };
160
+ /** Moves the host makes once a moment has passed (ADR 0550), at most four. */
161
+ after?: readonly TimedMove[];
137
162
  }
163
+ export declare const windowBoundSchema: z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
164
+ at: z.ZodString;
165
+ daysBefore: z.ZodOptional<z.ZodUnion<readonly [z.ZodNumber, z.ZodString, z.ZodObject<{
166
+ field: z.ZodOptional<z.ZodString>;
167
+ setting: z.ZodOptional<z.ZodString>;
168
+ }, z.core.$strict>]>>;
169
+ minutesBefore: z.ZodOptional<z.ZodUnion<readonly [z.ZodNumber, z.ZodString, z.ZodObject<{
170
+ field: z.ZodOptional<z.ZodString>;
171
+ setting: z.ZodOptional<z.ZodString>;
172
+ }, z.core.$strict>]>>;
173
+ zone: z.ZodOptional<z.ZodString>;
174
+ }, z.core.$strict>]>;
138
175
  export declare const statesManifestSchema: z.ZodObject<{
139
176
  field: z.ZodString;
140
177
  initial: z.ZodString;
@@ -219,6 +256,13 @@ export declare const statesManifestSchema: z.ZodObject<{
219
256
  when: z.ZodRecord<z.ZodString, z.ZodString>;
220
257
  from: z.ZodArray<z.ZodString>;
221
258
  }, z.core.$strict>>;
259
+ after: z.ZodOptional<z.ZodArray<z.ZodObject<{
260
+ from: z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>;
261
+ to: z.ZodString;
262
+ at: z.ZodString;
263
+ minutesAfter: z.ZodOptional<z.ZodNumber>;
264
+ ifAnyIn: z.ZodOptional<z.ZodArray<z.ZodString>>;
265
+ }, z.core.$strict>>>;
222
266
  }, z.core.$strip>;
223
267
  export type StatesManifest = z.infer<typeof statesManifestSchema>;
224
268
  /** A staff-only rule's paths, as a list (ADR 0505). */
@@ -229,6 +273,16 @@ export declare function moveOrigins(move: {
229
273
  }): readonly string[];
230
274
  /** Reports problems with `objects.<name>.states` through `contract`. */
231
275
  export declare function checkObjectStates(m: ExtensionManifest, name: string, object: ObjectManifest, contract: (path: string, message: string) => void): void;
276
+ /**
277
+ * The checks of window paths and numbers (ADRs 0503 and 0505): for a `states` declaration, and
278
+ * for any other declaration that names a moment the same way (a waitlist's cut-off, ADR 0520).
279
+ */
280
+ export declare function windowChecks(m: ExtensionManifest, object: ObjectManifest, name: string, contract: (path: string, message: string) => void): {
281
+ checkPath: (at: string, path: string, type: string) => void;
282
+ checkNumber: (value: WindowNumber | undefined, path: string) => void;
283
+ checkBound: (bound: string | WindowBound, path: string) => void;
284
+ checkWindow: (window: MoveWindow | undefined, path: string) => void;
285
+ };
232
286
  /**
233
287
  * The field a path names (ADR 0503; two relations from ADR 0505): `field` on the object, or
234
288
  * `relation.field` / `relation.relation.field` through relations of one record. Every field on
package/dist/states.js CHANGED
@@ -21,7 +21,7 @@ const windowNumber = (number) => z.union([
21
21
  .strict()
22
22
  .refine((n) => n.field !== undefined || n.setting !== undefined, "Name a field or a setting"),
23
23
  ]);
24
- const windowBound = z.union([
24
+ export const windowBoundSchema = z.union([
25
25
  fieldPath,
26
26
  z
27
27
  .object({
@@ -34,7 +34,7 @@ const windowBound = z.union([
34
34
  ]);
35
35
  const staffOnlyPaths = z.union([fieldPath, z.array(fieldPath).min(1).max(4)]);
36
36
  const moveWindow = z
37
- .object({ from: windowBound.optional(), until: windowBound.optional() })
37
+ .object({ from: windowBoundSchema.optional(), until: windowBoundSchema.optional() })
38
38
  .strict()
39
39
  .refine((w) => w.from !== undefined || w.until !== undefined, "A window needs from or until");
40
40
  export const statesManifestSchema = z.object({
@@ -79,6 +79,19 @@ export const statesManifestSchema = z.object({
79
79
  })
80
80
  .strict()
81
81
  .optional(),
82
+ after: z
83
+ .array(z
84
+ .object({
85
+ from: z.union([stateValue, z.array(stateValue).min(1).max(20)]),
86
+ to: stateValue,
87
+ at: fieldPath,
88
+ minutesAfter: z.number().int().min(0).max(10_080).optional(),
89
+ ifAnyIn: z.array(stateValue).min(1).max(20).optional(),
90
+ })
91
+ .strict())
92
+ .min(1)
93
+ .max(4)
94
+ .optional(),
82
95
  });
83
96
  /** A staff-only rule's paths, as a list (ADR 0505). */
84
97
  export function staffOnlyPathsOf(value) {
@@ -158,49 +171,15 @@ export function checkObjectStates(m, name, object, contract) {
158
171
  }
159
172
  });
160
173
  // Windows, staff-only rules and requests (ADR 0503; two relations and settings, ADR 0505).
161
- const checkPath = (at, path, type) => {
162
- const field = resolveFieldPath(m, object, at);
163
- // A staff-only rule may read a formula: the host computes it in the view (a boolean one).
164
- const formulaOk = type === "boolean" && field?.type === "formula";
165
- if ((field?.type !== type && !formulaOk) || field?.many) {
166
- contract(path, `"${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`);
167
- }
168
- };
169
- const checkNumber = (value, path) => {
170
- if (typeof value === "string")
171
- checkPath(value, path, "number");
172
- else if (typeof value === "object") {
173
- if (value.field !== undefined)
174
- checkPath(value.field, `${path}.field`, "number");
175
- if (value.setting !== undefined) {
176
- const settings = m.settings ?? {};
177
- const setting = Object.hasOwn(settings, value.setting)
178
- ? settings[value.setting]
179
- : undefined;
180
- if (setting?.type !== "number") {
181
- contract(`${path}.setting`, `"${value.setting}" must be a number setting of ${m.id}`);
182
- }
183
- }
184
- }
185
- };
186
- const checkWindow = (window, path) => {
187
- for (const end of ["from", "until"]) {
188
- const bound = window?.[end];
189
- if (bound === undefined)
190
- continue;
191
- const b = typeof bound === "string" ? { at: bound } : bound;
192
- checkPath(b.at, `${path}.${end}.at`, "datetime");
193
- for (const key of ["daysBefore", "minutesBefore"]) {
194
- checkNumber(b[key], `${path}.${end}.${key}`);
195
- }
196
- if (b.zone !== undefined)
197
- checkPath(b.zone, `${path}.${end}.zone`, "timezone");
198
- }
199
- };
174
+ const { checkPath, checkWindow } = windowChecks(m, object, name, contract);
200
175
  states.moves.forEach((move, i) => {
201
176
  const mp = `${sp}.moves.${i}`;
202
- if ((move.window || move.staffOnly) && !move.by.includes("customer")) {
203
- contract(mp, "A window or staffOnly binds customers: the move must list customer in by");
177
+ // A window binds customers, or staff too with bindsStaff (ADR 0551); staffOnly customers.
178
+ if (move.staffOnly && !move.by.includes("customer")) {
179
+ contract(mp, "staffOnly binds customers: the move must list customer in by");
180
+ }
181
+ if (move.window && !move.by.includes("customer") && !move.bindsStaff) {
182
+ contract(mp, "A window binds customers: the move must list customer in by, or bind staff (bindsStaff)");
204
183
  }
205
184
  checkWindow(move.window, `${mp}.window`);
206
185
  for (const p of staffOnlyPathsOf(move.staffOnly))
@@ -211,12 +190,12 @@ export function checkObjectStates(m, name, object, contract) {
211
190
  if (!spec) {
212
191
  contract(`${mp}.tools`, `"${tool}" is not a tool of this extension`);
213
192
  }
214
- else if (!spec.humanOnly || !spec.permission) {
215
- contract(`${mp}.tools`, `"${tool}" must be humanOnly and declare a permission: the move is a person's act with it`);
193
+ else if ((!spec.humanOnly && !spec.personApproves) || !spec.permission) {
194
+ contract(`${mp}.tools`, `"${tool}" must be humanOnly or personApproves and declare a permission: the move is a person's act with it`);
216
195
  }
217
196
  }
218
- if (move.bindsStaff && (!move.window || !move.by.includes("staff"))) {
219
- contract(`${mp}.bindsStaff`, "bindsStaff needs a window and staff in by");
197
+ if (move.bindsStaff && (!move.window || (!move.by.includes("staff") && !move.tools))) {
198
+ contract(`${mp}.bindsStaff`, "bindsStaff needs a window, and staff in by or named tools");
220
199
  }
221
200
  if (move.tools && move.by.includes("staff")) {
222
201
  contract(`${mp}.tools`, "A move named tools may make is not also open to all staff: drop staff from by");
@@ -248,6 +227,16 @@ export function checkObjectStates(m, name, object, contract) {
248
227
  contract(`${fp}.relation`, `"${f.relation}" must be a relation (one record) of ${name} to another of this extension's objects that declares states`);
249
228
  }
250
229
  else {
230
+ // A parent whose places other records draw on as a pool: its counter counts claims that
231
+ // are not its own, which the cascade's check at commit cannot read (ADR 0550).
232
+ const pooled = Object.values(m.objects).some((other) => {
233
+ const poolField = other.capacity?.pool;
234
+ const relTo = poolField ? other.fields[poolField]?.to : undefined;
235
+ return relTo === rel?.to || relTo === rel?.to?.slice(m.id.length + 1);
236
+ });
237
+ if (pooled) {
238
+ contract(`${fp}.relation`, `${rel?.to ?? f.relation} is a pool other records draw places from: a cascade cannot follow it`);
239
+ }
251
240
  const parentStates = new Set((target.fields[target.states.field]?.options ?? []).map((o) => o.value));
252
241
  for (const [when, to] of Object.entries(f.when)) {
253
242
  if (!parentStates.has(when)) {
@@ -263,6 +252,37 @@ export function checkObjectStates(m, name, object, contract) {
263
252
  }
264
253
  }
265
254
  }
255
+ // Timed moves (ADR 0550): a declared host move each, and a moment the sweep reads in SQL.
256
+ (states.after ?? []).forEach((rule, i) => {
257
+ const tp = `${sp}.after.${i}`;
258
+ if (!isState(rule.to))
259
+ contract(`${tp}.to`, `"${rule.to}" is not an option of ${states.field}`);
260
+ for (const from of moveOrigins(rule)) {
261
+ if (!isState(from)) {
262
+ contract(`${tp}.from`, `"${from}" is not an option of ${states.field}`);
263
+ }
264
+ else if (!states.moves.some((move) => move.to === rule.to && move.by.includes("host") && moveOrigins(move).includes(from))) {
265
+ contract(`${tp}.from`, `${name} needs a host move from "${from}" to "${rule.to}"`);
266
+ }
267
+ }
268
+ const parts = rule.at.split(".");
269
+ const at = parts.length <= 2 ? resolveFieldPath(m, object, rule.at) : undefined;
270
+ const via = parts.length === 2 ? fieldOf(parts[0]) : undefined;
271
+ const ownTarget = via?.type === "relation" && !via.many && via.to?.startsWith(`${m.id}.`);
272
+ if (at?.type !== "datetime" || at.many || (parts.length === 2 && !ownTarget)) {
273
+ contract(`${tp}.at`, `"${rule.at}" must be a datetime field of ${name}, or <relation>.<field> on another of this extension's objects`);
274
+ }
275
+ if (rule.ifAnyIn) {
276
+ if (parts.length !== 2) {
277
+ contract(`${tp}.ifAnyIn`, "ifAnyIn needs at one relation away: the records it compares");
278
+ }
279
+ for (const state of rule.ifAnyIn) {
280
+ if (!isState(state)) {
281
+ contract(`${tp}.ifAnyIn`, `"${state}" is not an option of ${states.field}`);
282
+ }
283
+ }
284
+ }
285
+ });
266
286
  for (const [request, targets] of Object.entries(states.requests ?? {})) {
267
287
  const rp = `${sp}.requests.${request}`;
268
288
  if (isState(request))
@@ -278,6 +298,54 @@ export function checkObjectStates(m, name, object, contract) {
278
298
  }
279
299
  }
280
300
  }
301
+ /**
302
+ * The checks of window paths and numbers (ADRs 0503 and 0505): for a `states` declaration, and
303
+ * for any other declaration that names a moment the same way (a waitlist's cut-off, ADR 0520).
304
+ */
305
+ export function windowChecks(m, object, name, contract) {
306
+ const checkPath = (at, path, type) => {
307
+ const field = resolveFieldPath(m, object, at);
308
+ // A staff-only rule may read a formula: the host computes it in the view (a boolean one).
309
+ const formulaOk = type === "boolean" && field?.type === "formula";
310
+ if ((field?.type !== type && !formulaOk) || field?.many) {
311
+ contract(path, `"${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`);
312
+ }
313
+ };
314
+ const checkNumber = (value, path) => {
315
+ if (typeof value === "string")
316
+ checkPath(value, path, "number");
317
+ else if (typeof value === "object") {
318
+ if (value.field !== undefined)
319
+ checkPath(value.field, `${path}.field`, "number");
320
+ if (value.setting !== undefined) {
321
+ const settings = m.settings ?? {};
322
+ const setting = Object.hasOwn(settings, value.setting)
323
+ ? settings[value.setting]
324
+ : undefined;
325
+ if (setting?.type !== "number") {
326
+ contract(`${path}.setting`, `"${value.setting}" must be a number setting of ${m.id}`);
327
+ }
328
+ }
329
+ }
330
+ };
331
+ const checkBound = (bound, path) => {
332
+ const b = typeof bound === "string" ? { at: bound } : bound;
333
+ checkPath(b.at, `${path}.at`, "datetime");
334
+ for (const key of ["daysBefore", "minutesBefore"]) {
335
+ checkNumber(b[key], `${path}.${key}`);
336
+ }
337
+ if (b.zone !== undefined)
338
+ checkPath(b.zone, `${path}.zone`, "timezone");
339
+ };
340
+ const checkWindow = (window, path) => {
341
+ for (const end of ["from", "until"]) {
342
+ const bound = window?.[end];
343
+ if (bound !== undefined)
344
+ checkBound(bound, `${path}.${end}`);
345
+ }
346
+ };
347
+ return { checkPath, checkNumber, checkBound, checkWindow };
348
+ }
281
349
  /**
282
350
  * The field a path names (ADR 0503; two relations from ADR 0505): `field` on the object, or
283
351
  * `relation.field` / `relation.relation.field` through relations of one record. Every field on
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@composable-os/sdk",
3
- "version": "0.1.0-beta.10",
3
+ "version": "0.1.0-beta.11",
4
4
  "license": "FSL-1.1-ALv2",
5
5
  "type": "module",
6
6
  "repository": {