@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/dist/sellable.js
CHANGED
|
@@ -5,7 +5,28 @@
|
|
|
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
|
+
/** Where goods are sold (stage 37 decision 3, ADR 0682): a select field holds one of these. */
|
|
10
|
+
export const SOLD_IN = ["online", "in_person", "both"];
|
|
11
|
+
/** How a delivery option gets goods to the buyer (ADR 0682). */
|
|
12
|
+
export const DELIVERY_KINDS = ["post", "collect", "here"];
|
|
13
|
+
/** How a sale's money is earned (ADR 0792), as the Unearned report reads it. */
|
|
14
|
+
export const EARNED = ["at_sale", "over_period", "per_use", "at_date"];
|
|
15
|
+
/** What `sellable.finance` comes to when it, or a part of it, is left out (ADR 0792). */
|
|
16
|
+
export const FINANCE_DEFAULTS = {
|
|
17
|
+
category: { key: "other", label: "Other" },
|
|
18
|
+
earned: "at_sale",
|
|
19
|
+
};
|
|
20
|
+
/** A sellable object's finance declaration with its defaults filled (ADR 0792). */
|
|
21
|
+
export function sellableFinance(finance) {
|
|
22
|
+
return {
|
|
23
|
+
category: finance?.category ?? FINANCE_DEFAULTS.category,
|
|
24
|
+
earned: finance?.earned ?? FINANCE_DEFAULTS.earned,
|
|
25
|
+
...(finance?.at !== undefined ? { at: finance.at } : {}),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** The longest cooling-off period the host keeps (days); a longer setting reads as this. */
|
|
29
|
+
export const MAX_COOLING_OFF_DAYS = 90;
|
|
9
30
|
/** The host's subscription states a grant follows (ADR 0485). */
|
|
10
31
|
export const SUBSCRIPTION_GRANT_STATES = [
|
|
11
32
|
"trialing",
|
|
@@ -72,6 +93,23 @@ export const sellableManifestSchema = z
|
|
|
72
93
|
.strict()
|
|
73
94
|
.optional(),
|
|
74
95
|
onePerPerson: fieldName.optional(),
|
|
96
|
+
moves: z
|
|
97
|
+
.object({
|
|
98
|
+
held: z.string().min(1).max(100),
|
|
99
|
+
paid: z.string().min(1).max(100),
|
|
100
|
+
expired: z.string().min(1).max(100),
|
|
101
|
+
refund: z.array(z.string().min(1).max(100)).max(20).optional(),
|
|
102
|
+
price: z
|
|
103
|
+
.object({ field: z.string().max(130).optional(), setting: fieldName.optional() })
|
|
104
|
+
.strict()
|
|
105
|
+
.optional(),
|
|
106
|
+
title: z.string().max(130).optional(),
|
|
107
|
+
order: fieldName.optional(),
|
|
108
|
+
guest: z.boolean().optional(),
|
|
109
|
+
holdCap: z.object({ setting: fieldName }).strict().optional(),
|
|
110
|
+
})
|
|
111
|
+
.strict()
|
|
112
|
+
.optional(),
|
|
75
113
|
tax: z
|
|
76
114
|
.object({
|
|
77
115
|
code: z.string().regex(TAX_CODE_PATTERN, "A product tax code: txcd_ and eight digits"),
|
|
@@ -93,6 +131,11 @@ export const sellableManifestSchema = z
|
|
|
93
131
|
.object({
|
|
94
132
|
interval: z.enum(["week", "month", "year"]),
|
|
95
133
|
trialDays: fieldName.optional(),
|
|
134
|
+
pause: z
|
|
135
|
+
.object({ minDays: fieldName.optional(), maxDays: fieldName.optional() })
|
|
136
|
+
.strict()
|
|
137
|
+
.optional(),
|
|
138
|
+
coolingOff: z.object({ setting: fieldName }).strict().optional(),
|
|
96
139
|
grants: z
|
|
97
140
|
.object({
|
|
98
141
|
object: z.string().max(40),
|
|
@@ -115,6 +158,33 @@ export const sellableManifestSchema = z
|
|
|
115
158
|
})
|
|
116
159
|
.strict()
|
|
117
160
|
.optional(),
|
|
161
|
+
finance: z
|
|
162
|
+
.object({
|
|
163
|
+
category: z
|
|
164
|
+
.object({
|
|
165
|
+
key: z.string().regex(/^[a-z][a-z0-9_]{0,39}$/, "Lower case letters, digits and _"),
|
|
166
|
+
label: z.string().min(1).max(60),
|
|
167
|
+
})
|
|
168
|
+
.strict()
|
|
169
|
+
.optional(),
|
|
170
|
+
earned: z.enum(EARNED).optional(),
|
|
171
|
+
at: z.string().max(130).optional(),
|
|
172
|
+
})
|
|
173
|
+
.strict()
|
|
174
|
+
.optional(),
|
|
175
|
+
goods: z
|
|
176
|
+
.object({ soldIn: z.string().max(130).optional() })
|
|
177
|
+
.strict()
|
|
178
|
+
.optional(),
|
|
179
|
+
delivery: z
|
|
180
|
+
.object({
|
|
181
|
+
kind: fieldName,
|
|
182
|
+
freeOver: fieldName.optional(),
|
|
183
|
+
countries: fieldName.optional(),
|
|
184
|
+
active: fieldName.optional(),
|
|
185
|
+
})
|
|
186
|
+
.strict()
|
|
187
|
+
.optional(),
|
|
118
188
|
})
|
|
119
189
|
.strict();
|
|
120
190
|
const NUMERIC = new Set(["number", "currency"]);
|
|
@@ -165,6 +235,34 @@ export function checkObjectSellable(m, name, object, contract) {
|
|
|
165
235
|
if (trial !== undefined && !single(trial, new Set(["number"]))) {
|
|
166
236
|
contract(`${path}.recurring.trialDays`, `"${trial}" must be a number field of ${name}`);
|
|
167
237
|
}
|
|
238
|
+
for (const key of ["minDays", "maxDays"]) {
|
|
239
|
+
const days = sellable.recurring.pause?.[key];
|
|
240
|
+
if (days !== undefined && !single(days, new Set(["number"]))) {
|
|
241
|
+
contract(`${path}.recurring.pause.${key}`, `"${days}" must be a number field of ${name}`);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
const coolingOff = sellable.recurring.coolingOff;
|
|
245
|
+
if (coolingOff) {
|
|
246
|
+
const settings = m.settings ?? {};
|
|
247
|
+
const setting = Object.hasOwn(settings, coolingOff.setting)
|
|
248
|
+
? settings[coolingOff.setting]
|
|
249
|
+
: undefined;
|
|
250
|
+
const at = `${path}.recurring.coolingOff.setting`;
|
|
251
|
+
if (setting?.type !== "number") {
|
|
252
|
+
contract(at, `"${coolingOff.setting}" must be a number setting of ${m.id}`);
|
|
253
|
+
}
|
|
254
|
+
else if (setting.personApproves !== true) {
|
|
255
|
+
// Refunds follow from it: a person's to change, never an AI's (decision 11).
|
|
256
|
+
contract(at, `"${coolingOff.setting}" must be a setting a person approves (personApproves): the cooling-off days are never an AI's to change`);
|
|
257
|
+
}
|
|
258
|
+
else if (setting.min === undefined ||
|
|
259
|
+
setting.min < 0 ||
|
|
260
|
+
setting.max === undefined ||
|
|
261
|
+
setting.max > MAX_COOLING_OFF_DAYS ||
|
|
262
|
+
(typeof setting.default === "number" && setting.default !== 0)) {
|
|
263
|
+
contract(at, `"${coolingOff.setting}" must be bounded from 0 to at most ${MAX_COOLING_OFF_DAYS} days, and off (0 or no default) until a person sets it`);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
168
266
|
if (sellable.onePerPerson !== undefined) {
|
|
169
267
|
contract(`${path}.onePerPerson`, "A plan's trial is already one per person (ADR 0489); onePerPerson is for things bought once");
|
|
170
268
|
}
|
|
@@ -192,6 +290,13 @@ export function checkObjectSellable(m, name, object, contract) {
|
|
|
192
290
|
}
|
|
193
291
|
if (sellable.grants)
|
|
194
292
|
checkGrants(m, name, object, sellable.grants, `${path}.grants`, contract);
|
|
293
|
+
if (sellable.moves)
|
|
294
|
+
checkMoves(m, name, object, sellable.moves, `${path}.moves`, contract);
|
|
295
|
+
if (sellable.goods || sellable.delivery)
|
|
296
|
+
checkGoods(m, name, object, path, contract);
|
|
297
|
+
if (sellable.finance) {
|
|
298
|
+
checkFinance(m, name, object, sellable.finance, `${path}.finance`, contract);
|
|
299
|
+
}
|
|
195
300
|
const place = sellable.tax?.place;
|
|
196
301
|
if (place) {
|
|
197
302
|
let holder = object;
|
|
@@ -225,6 +330,242 @@ export function checkObjectSellable(m, name, object, contract) {
|
|
|
225
330
|
}
|
|
226
331
|
}
|
|
227
332
|
}
|
|
333
|
+
/** A select field's option values (empty when it is not a select field). */
|
|
334
|
+
function selectValues(field) {
|
|
335
|
+
if (!field || field.type !== "select" || field.many)
|
|
336
|
+
return null;
|
|
337
|
+
return (field.options ?? []).map((option) => typeof option === "string" ? option : option.value);
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Checks `sellable.goods` and `sellable.delivery` (ADR 0682): where goods are sold is a select of
|
|
341
|
+
* `online`, `in_person` and `both`; a delivery option's kind a select of `post`, `collect` and
|
|
342
|
+
* `here`, with its other fields of the right types; and neither is bought by deposit, by
|
|
343
|
+
* subscription or by moving a record. A delivery is one per order and has no stock.
|
|
344
|
+
*/
|
|
345
|
+
function checkGoods(m, name, object, path, contract) {
|
|
346
|
+
const sellable = object.sellable;
|
|
347
|
+
const fieldOf = (local) => Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
348
|
+
if (sellable.goods && sellable.delivery) {
|
|
349
|
+
contract(`${path}.delivery`, "A record is goods or a delivery option, not both");
|
|
350
|
+
}
|
|
351
|
+
const what = sellable.goods ? "goods" : "a delivery option";
|
|
352
|
+
for (const key of ["deposit", "recurring", "moves"]) {
|
|
353
|
+
if (sellable[key] !== undefined) {
|
|
354
|
+
contract(`${path}.${key}`, `${name} is ${what}, sold whole on an order: it has no ${key}`);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
const soldIn = sellable.goods?.soldIn;
|
|
358
|
+
if (soldIn !== undefined) {
|
|
359
|
+
const values = selectValues(resolveFieldPath(m, object, soldIn));
|
|
360
|
+
if (!values || values.some((v) => !SOLD_IN.includes(v))) {
|
|
361
|
+
contract(`${path}.goods.soldIn`, `"${soldIn}" must be a select field (of ${name}, or through its relations) whose values are among ${SOLD_IN.join(", ")}`);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
const delivery = sellable.delivery;
|
|
365
|
+
if (!delivery)
|
|
366
|
+
return;
|
|
367
|
+
for (const key of ["onePerPerson", "maxPerOrder"]) {
|
|
368
|
+
if (sellable[key] !== undefined) {
|
|
369
|
+
contract(`${path}.${key}`, `A delivery option is one per order: it has no ${key}`);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (object.stock) {
|
|
373
|
+
contract(`objects.${name}.stock`, "A delivery option is not counted on a shelf: no stock");
|
|
374
|
+
}
|
|
375
|
+
const kinds = selectValues(fieldOf(delivery.kind));
|
|
376
|
+
if (!kinds?.length || kinds.some((v) => !DELIVERY_KINDS.includes(v))) {
|
|
377
|
+
contract(`${path}.delivery.kind`, `"${delivery.kind}" must be a select field of ${name} whose values are among ${DELIVERY_KINDS.join(", ")}`);
|
|
378
|
+
}
|
|
379
|
+
if (delivery.freeOver !== undefined) {
|
|
380
|
+
const field = fieldOf(delivery.freeOver);
|
|
381
|
+
if (!field || !NUMERIC.has(field.type) || field.many) {
|
|
382
|
+
contract(`${path}.delivery.freeOver`, `"${delivery.freeOver}" must be a currency or number field of ${name}`);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
if (delivery.countries !== undefined) {
|
|
386
|
+
const field = fieldOf(delivery.countries);
|
|
387
|
+
if (!field || !TEXT.has(field.type) || field.many) {
|
|
388
|
+
contract(`${path}.delivery.countries`, `"${delivery.countries}" must be a text field of ${name}`);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (delivery.active !== undefined) {
|
|
392
|
+
const field = fieldOf(delivery.active);
|
|
393
|
+
if (!field || field.type !== "boolean" || field.many) {
|
|
394
|
+
contract(`${path}.delivery.active`, `"${delivery.active}" must be a boolean field of ${name}`);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Checks `sellable.finance` (ADR 0792): each way of earning names what the host reads to work it
|
|
400
|
+
* out, and one category key has one label across this extension.
|
|
401
|
+
*/
|
|
402
|
+
function checkFinance(m, name, object, finance, path, contract) {
|
|
403
|
+
const sellable = object.sellable;
|
|
404
|
+
const earned = finance.earned ?? FINANCE_DEFAULTS.earned;
|
|
405
|
+
if (earned === "at_date") {
|
|
406
|
+
if (finance.at === undefined) {
|
|
407
|
+
contract(`${path}.at`, "at_date names its date: at, a date or datetime field");
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
const at = resolveFieldPath(m, object, finance.at);
|
|
411
|
+
if (!at || at.many || !DATES.has(at.type)) {
|
|
412
|
+
contract(`${path}.at`, `"${finance.at}" must be a date or datetime field of ${name}, or <relation>.<field> through up to two relations, every field on the way declared by this extension`);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
else if (finance.at !== undefined) {
|
|
417
|
+
contract(`${path}.at`, "at is the date of at_date: declare earned: at_date, or remove at");
|
|
418
|
+
}
|
|
419
|
+
if (earned === "over_period" && !sellable.recurring) {
|
|
420
|
+
contract(`${path}.earned`, "over_period is earned over a paid period: only a plan (recurring) has one");
|
|
421
|
+
}
|
|
422
|
+
if (earned === "per_use") {
|
|
423
|
+
const grants = sellable.grants;
|
|
424
|
+
const local = grants?.object.startsWith(`${m.id}.`)
|
|
425
|
+
? grants.object.slice(m.id.length + 1)
|
|
426
|
+
: grants?.object;
|
|
427
|
+
const granted = local !== undefined && Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
|
|
428
|
+
if (!granted?.allowance || !("holds" in granted.allowance)) {
|
|
429
|
+
contract(`${path}.earned`, "per_use is earned as uses are spent: grants must grant an object that holds uses (allowance.holds)");
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
const category = finance.category;
|
|
433
|
+
if (category) {
|
|
434
|
+
for (const [other, o] of Object.entries(m.objects)) {
|
|
435
|
+
if (other === name)
|
|
436
|
+
continue;
|
|
437
|
+
const theirs = o.sellable?.finance?.category;
|
|
438
|
+
if (theirs?.key === category.key && theirs.label !== category.label) {
|
|
439
|
+
contract(`${path}.category.label`, `${other} names category "${category.key}" "${theirs.label}": one category has one label`);
|
|
440
|
+
break;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Checks `sellable.moves` (ADR 0560): each of these is a way the order's batch could otherwise fail
|
|
447
|
+
* (a payment the host could not apply), or a record someone could hold with no order.
|
|
448
|
+
*/
|
|
449
|
+
function checkMoves(m, name, object, moves, path, contract) {
|
|
450
|
+
const sellable = object.sellable;
|
|
451
|
+
for (const key of [
|
|
452
|
+
"grants",
|
|
453
|
+
"deposit",
|
|
454
|
+
"recurring",
|
|
455
|
+
"onePerPerson",
|
|
456
|
+
"maxPerOrder",
|
|
457
|
+
"saleWindow",
|
|
458
|
+
]) {
|
|
459
|
+
if (sellable[key] !== undefined) {
|
|
460
|
+
contract(`objects.${name}.sellable.${key}`, `A record bought with moves is one record on one order: it has no ${key}`);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const field = (local) => Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
464
|
+
const price = field(sellable.price);
|
|
465
|
+
if (price && !price.readOnly) {
|
|
466
|
+
contract(`objects.${name}.fields.${sellable.price}`, `"${sellable.price}" holds what the host priced it at: make it readOnly`);
|
|
467
|
+
}
|
|
468
|
+
if (moves.order !== undefined) {
|
|
469
|
+
const order = field(moves.order);
|
|
470
|
+
if (order?.type !== "text" || order.many || !order.readOnly) {
|
|
471
|
+
contract(`${path}.order`, `"${moves.order}" must be a readOnly text field of ${name}`);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
if (moves.price) {
|
|
475
|
+
if (moves.price.field === undefined && moves.price.setting === undefined) {
|
|
476
|
+
contract(`${path}.price`, "price names a field, a setting or both");
|
|
477
|
+
}
|
|
478
|
+
if (moves.price.field !== undefined) {
|
|
479
|
+
const at = resolveFieldPath(m, object, moves.price.field);
|
|
480
|
+
if (!at || at.many || (at.type !== "number" && at.type !== "currency")) {
|
|
481
|
+
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`);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
if (moves.price.setting !== undefined) {
|
|
485
|
+
const settings = m.settings ?? {};
|
|
486
|
+
const setting = Object.hasOwn(settings, moves.price.setting)
|
|
487
|
+
? settings[moves.price.setting]
|
|
488
|
+
: undefined;
|
|
489
|
+
if (setting?.type !== "number") {
|
|
490
|
+
contract(`${path}.price.setting`, `"${moves.price.setting}" must be a number setting of ${m.id}`);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
if (moves.title !== undefined) {
|
|
495
|
+
const at = resolveFieldPath(m, object, moves.title);
|
|
496
|
+
if (!at || at.many || at.type !== "text") {
|
|
497
|
+
contract(`${path}.title`, `"${moves.title}" must be a text field of ${name}, or through up to two relations`);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
if (!object.customer)
|
|
501
|
+
contract(path, `${name} must declare customer: whose record it is`);
|
|
502
|
+
if (moves.guest && object.customer) {
|
|
503
|
+
const owner = field(object.customer.field);
|
|
504
|
+
if (owner?.required) {
|
|
505
|
+
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}")`);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
if (moves.holdCap) {
|
|
509
|
+
const settings = m.settings ?? {};
|
|
510
|
+
const setting = Object.hasOwn(settings, moves.holdCap.setting)
|
|
511
|
+
? settings[moves.holdCap.setting]
|
|
512
|
+
: undefined;
|
|
513
|
+
if (setting?.type !== "number") {
|
|
514
|
+
contract(`${path}.holdCap.setting`, `"${moves.holdCap.setting}" must be a number setting of ${m.id}`);
|
|
515
|
+
}
|
|
516
|
+
if (!moves.guest)
|
|
517
|
+
contract(`${path}.holdCap`, "holdCap limits guests' holds: declare guest");
|
|
518
|
+
if (!object.claims)
|
|
519
|
+
contract(`${path}.holdCap`, `${name} must claim places (claims)`);
|
|
520
|
+
}
|
|
521
|
+
const states = object.states;
|
|
522
|
+
if (!states) {
|
|
523
|
+
contract(path, `${name} must declare states: the order moves it`);
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
526
|
+
const values = new Set((object.fields[states.field]?.options ?? []).map((o) => o.value));
|
|
527
|
+
const finals = new Set(states.moves.filter((mv) => mv.final).map((mv) => mv.to));
|
|
528
|
+
for (const key of ["held", "paid", "expired"]) {
|
|
529
|
+
if (!values.has(moves[key])) {
|
|
530
|
+
contract(`${path}.${key}`, `"${moves[key]}" is not a state of ${name}`);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
if (new Set([moves.held, moves.paid, moves.expired]).size !== 3) {
|
|
534
|
+
contract(path, "held, paid and expired are three different states");
|
|
535
|
+
}
|
|
536
|
+
const creators = states.create?.in?.[moves.held] ?? [];
|
|
537
|
+
if (!creators.includes("host")) {
|
|
538
|
+
contract(`${path}.held`, `states.create.in must let the host create ${name} in "${moves.held}"`);
|
|
539
|
+
}
|
|
540
|
+
if (creators.some((by) => by !== "host")) {
|
|
541
|
+
contract(`${path}.held`, `Only the host creates ${name} in "${moves.held}": anyone else would hold it with no order`);
|
|
542
|
+
}
|
|
543
|
+
const hostMove = (to) => states.moves.some((mv) => mv.to === to && moveOrigins(mv).includes(moves.held) && mv.by.includes("host"));
|
|
544
|
+
for (const key of ["paid", "expired"]) {
|
|
545
|
+
if (!hostMove(moves[key])) {
|
|
546
|
+
contract(`${path}.${key}`, `${name} needs a move from "${moves.held}" to "${moves[key]}" the host may make`);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
if (!finals.has(moves.expired)) {
|
|
550
|
+
contract(`${path}.expired`, `"${moves.expired}" must be a final state of ${name}`);
|
|
551
|
+
}
|
|
552
|
+
for (const mv of states.moves) {
|
|
553
|
+
if (!moveOrigins(mv).includes(moves.held))
|
|
554
|
+
continue;
|
|
555
|
+
if (mv.by.some((by) => by !== "host")) {
|
|
556
|
+
contract(`${path}.held`, `Only the host moves ${name} out of "${moves.held}" (to "${mv.to}"): its order says where it goes`);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
for (const [i, state] of (moves.refund ?? []).entries()) {
|
|
560
|
+
if (!values.has(state) || state === moves.held || state === moves.expired) {
|
|
561
|
+
contract(`${path}.refund.${i}`, `"${state}" must be a state of ${name} other than held and expired`);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
const allowance = object.allowance;
|
|
565
|
+
if (allowance && "spends" in allowance && !allowance.spends.waived) {
|
|
566
|
+
contract(path, `${name} spends uses: declare allowance.spends.waived, which the host sets on a record its order pays for`);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
228
569
|
function checkGrants(m, name, item, grants, path, contract) {
|
|
229
570
|
const local = grants.object.startsWith(`${m.id}.`)
|
|
230
571
|
? grants.object.slice(m.id.length + 1)
|
package/dist/slug.d.ts
CHANGED
|
@@ -9,3 +9,5 @@ export declare const SLUG_FROM_TITLE_MAX = 80;
|
|
|
9
9
|
* a hyphen, at most SLUG_FROM_TITLE_MAX characters. Empty when nothing is left.
|
|
10
10
|
*/
|
|
11
11
|
export declare function slugify(title: string): string;
|
|
12
|
+
/** Text as words of lowercase letters and digits joined by hyphens, accents dropped, at most `max`. */
|
|
13
|
+
export declare function hyphenate(text: string, max: number): string;
|
package/dist/slug.js
CHANGED
|
@@ -13,11 +13,15 @@ export const SLUG_FROM_TITLE_MAX = 80;
|
|
|
13
13
|
* a hyphen, at most SLUG_FROM_TITLE_MAX characters. Empty when nothing is left.
|
|
14
14
|
*/
|
|
15
15
|
export function slugify(title) {
|
|
16
|
-
|
|
16
|
+
return hyphenate(title, SLUG_FROM_TITLE_MAX);
|
|
17
|
+
}
|
|
18
|
+
/** Text as words of lowercase letters and digits joined by hyphens, accents dropped, at most `max`. */
|
|
19
|
+
export function hyphenate(text, max) {
|
|
20
|
+
const plain = text
|
|
17
21
|
.normalize("NFKD")
|
|
18
22
|
.replace(/[̀-ͯ]/g, "")
|
|
19
23
|
.toLowerCase()
|
|
20
24
|
.replace(/[^a-z0-9]+/g, "-")
|
|
21
25
|
.replace(/^-+|-+$/g, "");
|
|
22
|
-
return plain.slice(0,
|
|
26
|
+
return plain.slice(0, max).replace(/-+$/g, "");
|
|
23
27
|
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { ExtensionManifest, ObjectManifest } from "./manifest.ts";
|
|
3
|
+
/**
|
|
4
|
+
* A shift (ADR 0621): a time, a kind of work, and at most one staff account working it. The host
|
|
5
|
+
* keeps a **time claim** for every live, covered shift and refuses, in the database, a second
|
|
6
|
+
* shift that overlaps one account's time, for every actor and however many write at once
|
|
7
|
+
* (`conflict`, `rule: "double_booked"`). It answers a write with **warnings** it does not refuse:
|
|
8
|
+
* the person is away, not qualified for the kind, or the shift is left with no one.
|
|
9
|
+
*
|
|
10
|
+
* - `who`: the person. `{ field: "staff" }` is a `user` field of this object (the staff account
|
|
11
|
+
* itself). `{ field: "coach", account: "user" }` is a relation (one record) to another of this
|
|
12
|
+
* extension's objects whose `user` field holds the account (a class's coach profile); a profile
|
|
13
|
+
* with no account covers the shift for members but claims no one's time.
|
|
14
|
+
* - `starts`, `ends`: datetime fields of this object, the shift's own time. Ranges are half-open:
|
|
15
|
+
* 06:00 to 07:00 and 07:00 to 08:00 do not clash.
|
|
16
|
+
* - `zone`: a timezone field (optional), the shift's local zone.
|
|
17
|
+
* - `kind`: a relation (one record) to what qualifications are for (a class type, a duty).
|
|
18
|
+
* - `live`: the states of the object's `states` in which the record is a shift. A record in any
|
|
19
|
+
* other state, or in the Trash, claims nothing.
|
|
20
|
+
* - `place` (optional): a field, or `relation.field`, shown on the roster and in the feed.
|
|
21
|
+
* - `pattern` (optional): the series field that is the shift's regular person (`link` is the
|
|
22
|
+
* series' `recurring.link` on this object, `who` the series field copied onto `who.field`).
|
|
23
|
+
* - `payBefore`, `payAfter` (optional): `kind.field` number paths, minutes counted for pay only
|
|
24
|
+
* (preparation and pack-down); they never enter the time claim.
|
|
25
|
+
* - `claimBefore`, `claimAfter` (optional, ADR 0760): `kind.field` number paths, minutes kept free
|
|
26
|
+
* before and after the shift (a service's buffer). They enter the time claim, never the shift's
|
|
27
|
+
* own times or pay; the kind must be another of this extension's objects, so the host follows a
|
|
28
|
+
* change of its minutes. Classes declare none: two back to back stay allowed.
|
|
29
|
+
* - `account` (optional, ADR 0760): `"required"`: a live shift whose person is a profile holding
|
|
30
|
+
* no account is refused, at write and by the database, so no shift with a person escapes the
|
|
31
|
+
* claim (an appointment). A shift with nobody on it stays allowed (uncovered). Only with
|
|
32
|
+
* `who.account`: a user field always is the account.
|
|
33
|
+
* - `private` (optional, ADR 0761): the record's title may name a client (an appointment). The
|
|
34
|
+
* roster shows the shift as its kind to anyone but its person and those who roster, a clash
|
|
35
|
+
* answer names it by its kind, and it is never offered to others. The feed never shows a title.
|
|
36
|
+
* - `open` (optional, ADRs 0762 and 0763): the shift is booked at free times (an appointment).
|
|
37
|
+
* The host answers the free start times of a kind (`staffing.free`): its providers' open hours,
|
|
38
|
+
* less time off, their claims with margins, taken rooms, closures and the client's own shifts,
|
|
39
|
+
* on the kind's step; and staff writes are warned `outside_hours` and `client_busy`.
|
|
40
|
+
* - `length`: `kind.field`, the minutes a shift of the kind lasts (a service's length).
|
|
41
|
+
* - `step` (optional): `kind.field`, the minutes between start times (15, 30 or 60; 30 when
|
|
42
|
+
* empty), counted from the organisation's local midnight.
|
|
43
|
+
* - `rooms` (optional): `kind.field`, a relation (many) of the kind to the things this object
|
|
44
|
+
* `occupies`, in the order they are offered; empty on a kind record, it needs none.
|
|
45
|
+
* - `client` (optional): a relation field (one record) of this object to a Person: one client's
|
|
46
|
+
* live shifts of this object do not overlap.
|
|
47
|
+
* - `closedBy` (optional): another of this extension's objects whose records are date ranges
|
|
48
|
+
* (`from`, `until`: date fields): nothing is free on their dates.
|
|
49
|
+
* - `customer` (optional): `true`: a create or move by a customer or the public must be on a
|
|
50
|
+
* free time, by every door, in the batch that writes it.
|
|
51
|
+
* - `evidence` (optional): what shows a shift was worked, beside its pay line (ADR 0627): another
|
|
52
|
+
* object of this extension whose records name the shift (`link`, a relation to this object) and
|
|
53
|
+
* the staff account that marked each (`by`, a user field): a class's bookings, ticked by whoever
|
|
54
|
+
* took the list. The host reads only that the list was taken, and by whom.
|
|
55
|
+
* - `timesFrom` (optional, ADR 0751): a relation (one record, required) to another of this
|
|
56
|
+
* extension's objects whose time this shift is: a crew member's place on a visit. The host
|
|
57
|
+
* copies the record's `starts`, `ends`, `zone` (when declared) and `kind` fields, which that
|
|
58
|
+
* object has under the same names, onto the shift at create, when it is pointed elsewhere, and
|
|
59
|
+
* whenever the record changes them, in the same statement (triggers the reconciler builds), so
|
|
60
|
+
* every place moves with its visit and the time claim is checked for each: a move that would put
|
|
61
|
+
* one person on two overlapping shifts fails whole, naming them. The shift's own time fields are
|
|
62
|
+
* then read-only, written by the host alone. Not with a series making the shift (`pattern`).
|
|
63
|
+
*
|
|
64
|
+
* Only shipped packs and the org's own extensions declare it: who works when feeds pay.
|
|
65
|
+
*/
|
|
66
|
+
export interface StaffedSpec {
|
|
67
|
+
who: {
|
|
68
|
+
field: string;
|
|
69
|
+
account?: string;
|
|
70
|
+
};
|
|
71
|
+
starts: string;
|
|
72
|
+
ends: string;
|
|
73
|
+
zone?: string;
|
|
74
|
+
kind: string;
|
|
75
|
+
live: readonly string[];
|
|
76
|
+
place?: string;
|
|
77
|
+
pattern?: {
|
|
78
|
+
link: string;
|
|
79
|
+
who: string;
|
|
80
|
+
};
|
|
81
|
+
payBefore?: {
|
|
82
|
+
field: string;
|
|
83
|
+
};
|
|
84
|
+
payAfter?: {
|
|
85
|
+
field: string;
|
|
86
|
+
};
|
|
87
|
+
claimBefore?: {
|
|
88
|
+
field: string;
|
|
89
|
+
};
|
|
90
|
+
claimAfter?: {
|
|
91
|
+
field: string;
|
|
92
|
+
};
|
|
93
|
+
account?: "required";
|
|
94
|
+
private?: boolean;
|
|
95
|
+
open?: StaffedOpenSpec;
|
|
96
|
+
evidence?: {
|
|
97
|
+
object: string;
|
|
98
|
+
link: string;
|
|
99
|
+
by: string;
|
|
100
|
+
};
|
|
101
|
+
timesFrom?: string;
|
|
102
|
+
}
|
|
103
|
+
/** A shift booked at free times (ADRs 0762 and 0763): see `StaffedSpec.open`. */
|
|
104
|
+
export interface StaffedOpenSpec {
|
|
105
|
+
length: {
|
|
106
|
+
field: string;
|
|
107
|
+
};
|
|
108
|
+
step?: {
|
|
109
|
+
field: string;
|
|
110
|
+
};
|
|
111
|
+
rooms?: {
|
|
112
|
+
field: string;
|
|
113
|
+
};
|
|
114
|
+
client?: string;
|
|
115
|
+
closedBy?: {
|
|
116
|
+
object: string;
|
|
117
|
+
from: string;
|
|
118
|
+
until: string;
|
|
119
|
+
};
|
|
120
|
+
customer?: true;
|
|
121
|
+
}
|
|
122
|
+
export declare const staffedManifestSchema: z.ZodObject<{
|
|
123
|
+
who: z.ZodObject<{
|
|
124
|
+
field: z.ZodString;
|
|
125
|
+
account: z.ZodOptional<z.ZodString>;
|
|
126
|
+
}, z.core.$strict>;
|
|
127
|
+
starts: z.ZodString;
|
|
128
|
+
ends: z.ZodString;
|
|
129
|
+
zone: z.ZodOptional<z.ZodString>;
|
|
130
|
+
kind: z.ZodString;
|
|
131
|
+
live: z.ZodArray<z.ZodString>;
|
|
132
|
+
place: z.ZodOptional<z.ZodString>;
|
|
133
|
+
pattern: z.ZodOptional<z.ZodObject<{
|
|
134
|
+
link: z.ZodString;
|
|
135
|
+
who: z.ZodString;
|
|
136
|
+
}, z.core.$strict>>;
|
|
137
|
+
payBefore: z.ZodOptional<z.ZodObject<{
|
|
138
|
+
field: z.ZodString;
|
|
139
|
+
}, z.core.$strict>>;
|
|
140
|
+
payAfter: z.ZodOptional<z.ZodObject<{
|
|
141
|
+
field: z.ZodString;
|
|
142
|
+
}, z.core.$strict>>;
|
|
143
|
+
claimBefore: z.ZodOptional<z.ZodObject<{
|
|
144
|
+
field: z.ZodString;
|
|
145
|
+
}, z.core.$strict>>;
|
|
146
|
+
claimAfter: z.ZodOptional<z.ZodObject<{
|
|
147
|
+
field: z.ZodString;
|
|
148
|
+
}, z.core.$strict>>;
|
|
149
|
+
account: z.ZodOptional<z.ZodLiteral<"required">>;
|
|
150
|
+
private: z.ZodOptional<z.ZodBoolean>;
|
|
151
|
+
open: z.ZodOptional<z.ZodObject<{
|
|
152
|
+
length: z.ZodObject<{
|
|
153
|
+
field: z.ZodString;
|
|
154
|
+
}, z.core.$strict>;
|
|
155
|
+
step: z.ZodOptional<z.ZodObject<{
|
|
156
|
+
field: z.ZodString;
|
|
157
|
+
}, z.core.$strict>>;
|
|
158
|
+
rooms: z.ZodOptional<z.ZodObject<{
|
|
159
|
+
field: z.ZodString;
|
|
160
|
+
}, z.core.$strict>>;
|
|
161
|
+
client: z.ZodOptional<z.ZodString>;
|
|
162
|
+
closedBy: z.ZodOptional<z.ZodObject<{
|
|
163
|
+
object: z.ZodString;
|
|
164
|
+
from: z.ZodString;
|
|
165
|
+
until: z.ZodString;
|
|
166
|
+
}, z.core.$strict>>;
|
|
167
|
+
customer: z.ZodOptional<z.ZodLiteral<true>>;
|
|
168
|
+
}, z.core.$strict>>;
|
|
169
|
+
evidence: z.ZodOptional<z.ZodObject<{
|
|
170
|
+
object: z.ZodString;
|
|
171
|
+
link: z.ZodString;
|
|
172
|
+
by: z.ZodString;
|
|
173
|
+
}, z.core.$strict>>;
|
|
174
|
+
timesFrom: z.ZodOptional<z.ZodString>;
|
|
175
|
+
}, z.core.$strict>;
|
|
176
|
+
export type StaffedManifest = z.infer<typeof staffedManifestSchema>;
|
|
177
|
+
/** Reports problems with `objects.<name>.staffed` through `contract`. */
|
|
178
|
+
export declare function checkObjectStaffed(m: ExtensionManifest, name: string, object: ObjectManifest, contract: (path: string, message: string) => void): void;
|