@composable-os/sdk 0.0.0-stage → 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/LICENSE +109 -0
- package/dist/allowance.d.ts +198 -0
- package/dist/allowance.js +374 -0
- package/dist/automation-check.d.ts +38 -0
- package/dist/automation-check.js +454 -0
- package/dist/automation.d.ts +767 -0
- package/dist/automation.js +303 -0
- package/dist/behaviour.d.ts +3 -0
- package/dist/behaviour.js +153 -0
- package/dist/capacity.d.ts +146 -0
- package/dist/capacity.js +189 -0
- package/dist/contract.d.ts +851 -0
- package/dist/contract.js +42 -0
- package/dist/document.d.ts +211 -0
- package/dist/document.js +726 -0
- package/dist/field-types.d.ts +11 -0
- package/dist/field-types.js +98 -0
- package/dist/formula/ast.d.ts +67 -0
- package/dist/formula/ast.js +22 -0
- package/dist/formula/compile.d.ts +37 -0
- package/dist/formula/compile.js +379 -0
- package/dist/formula/index.d.ts +3 -0
- package/dist/formula/index.js +4 -0
- package/dist/formula/parser.d.ts +13 -0
- package/dist/formula/parser.js +365 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +24 -0
- package/dist/manifest.d.ts +1708 -0
- package/dist/manifest.js +1494 -0
- package/dist/media.d.ts +41 -0
- package/dist/media.js +90 -0
- package/dist/permissions.d.ts +17 -0
- package/dist/permissions.js +44 -0
- package/dist/recurring.d.ts +114 -0
- package/dist/recurring.js +173 -0
- package/dist/renamed.d.ts +16 -0
- package/dist/renamed.js +59 -0
- package/dist/rich-text-fixture.d.ts +121 -0
- package/dist/rich-text-fixture.js +67 -0
- package/dist/rich-text.d.ts +48 -0
- package/dist/rich-text.js +184 -0
- package/dist/runtime.d.ts +95 -0
- package/dist/runtime.js +312 -0
- package/dist/sellable.d.ts +331 -0
- package/dist/sellable.js +746 -0
- package/dist/slug.d.ts +11 -0
- package/dist/slug.js +23 -0
- package/dist/states.d.ts +292 -0
- package/dist/states.js +383 -0
- package/dist/testing.d.ts +151 -0
- package/dist/testing.js +679 -0
- package/dist/unique.d.ts +20 -0
- package/dist/unique.js +58 -0
- package/package.json +34 -4
- package/src/allowance.ts +522 -0
- package/src/automation-check.ts +534 -0
- package/src/automation.ts +743 -0
- package/src/behaviour.ts +215 -0
- package/src/capacity.ts +303 -0
- package/src/contract.ts +909 -0
- package/src/document.ts +935 -0
- package/src/field-types.ts +111 -0
- package/src/formula/ast.ts +72 -0
- package/src/formula/compile.ts +454 -0
- package/src/formula/index.ts +4 -0
- package/src/formula/parser.ts +417 -0
- package/src/index.ts +36 -0
- package/src/manifest.ts +1785 -0
- package/src/media.ts +108 -0
- package/src/permissions.ts +60 -0
- package/src/recurring.ts +321 -0
- package/src/renamed.ts +63 -0
- package/src/rich-text-fixture.ts +68 -0
- package/src/rich-text.ts +221 -0
- package/src/runtime.ts +410 -0
- package/src/sellable.ts +1138 -0
- package/src/slug.ts +25 -0
- package/src/states.ts +598 -0
- package/src/testing.ts +847 -0
- package/src/unique.ts +81 -0
- package/README.md +0 -3
package/dist/unique.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { ExtensionManifest, ObjectManifest } from "./manifest.ts";
|
|
3
|
+
/**
|
|
4
|
+
* One uniqueness rule: no two live records with the same values of `fields` (one to four
|
|
5
|
+
* single-valued fields of this object; a record with any of them empty is not counted) while
|
|
6
|
+
* their state (the object's `states` field) is one of `states`. Left without `states`, every
|
|
7
|
+
* live record counts. The database refuses a second record, or a move back into a counted state,
|
|
8
|
+
* as a conflict (`rule: "unique"`).
|
|
9
|
+
*/
|
|
10
|
+
export interface UniqueSpec {
|
|
11
|
+
fields: readonly string[];
|
|
12
|
+
states?: readonly string[];
|
|
13
|
+
}
|
|
14
|
+
export declare const uniqueManifestSchema: z.ZodArray<z.ZodObject<{
|
|
15
|
+
fields: z.ZodArray<z.ZodString>;
|
|
16
|
+
states: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
17
|
+
}, z.core.$strict>>;
|
|
18
|
+
export type UniqueManifest = z.infer<typeof uniqueManifestSchema>;
|
|
19
|
+
/** Reports problems with `objects.<name>.unique` through `contract`. */
|
|
20
|
+
export declare function checkObjectUnique(_m: ExtensionManifest, name: string, object: ObjectManifest, contract: (path: string, message: string) => void): void;
|
package/dist/unique.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Declared uniqueness (stage 31, ADR 0502): an object says that among its live records in some
|
|
2
|
+
// states, no two share the values of some fields (one active booking per Person per class). The
|
|
3
|
+
// host refuses the second in the database, for every actor, however many write at once.
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
export const uniqueManifestSchema = z
|
|
6
|
+
.array(z
|
|
7
|
+
.object({
|
|
8
|
+
fields: z.array(z.string().max(40)).min(1).max(4),
|
|
9
|
+
states: z.array(z.string().min(1).max(100)).min(1).max(20).optional(),
|
|
10
|
+
})
|
|
11
|
+
.strict())
|
|
12
|
+
.min(1)
|
|
13
|
+
.max(4);
|
|
14
|
+
const UNIQUE_TYPES = new Set([
|
|
15
|
+
"text",
|
|
16
|
+
"number",
|
|
17
|
+
"date",
|
|
18
|
+
"datetime",
|
|
19
|
+
"boolean",
|
|
20
|
+
"select",
|
|
21
|
+
"relation",
|
|
22
|
+
"user",
|
|
23
|
+
"email",
|
|
24
|
+
"phone",
|
|
25
|
+
"url",
|
|
26
|
+
"slug",
|
|
27
|
+
"timezone",
|
|
28
|
+
]);
|
|
29
|
+
/** Reports problems with `objects.<name>.unique` through `contract`. */
|
|
30
|
+
export function checkObjectUnique(_m, name, object, contract) {
|
|
31
|
+
(object.unique ?? []).forEach((rule, i) => {
|
|
32
|
+
const path = `objects.${name}.unique.${i}`;
|
|
33
|
+
if (new Set(rule.fields).size !== rule.fields.length) {
|
|
34
|
+
contract(`${path}.fields`, "List each field once");
|
|
35
|
+
}
|
|
36
|
+
for (const local of rule.fields) {
|
|
37
|
+
const field = Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
38
|
+
if (!field || field.many || !UNIQUE_TYPES.has(field.type)) {
|
|
39
|
+
contract(`${path}.fields`, `"${local}" must be a single-valued, stored field of ${name}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (rule.states) {
|
|
43
|
+
const states = object.states;
|
|
44
|
+
if (!states) {
|
|
45
|
+
contract(`${path}.states`, `${name} has no states`);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const field = Object.hasOwn(object.fields, states.field)
|
|
49
|
+
? object.fields[states.field]
|
|
50
|
+
: undefined;
|
|
51
|
+
const values = new Set((field?.options ?? []).map((o) => o.value));
|
|
52
|
+
for (const state of rule.states) {
|
|
53
|
+
if (!values.has(state))
|
|
54
|
+
contract(`${path}.states`, `"${state}" is not a state of ${name}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@composable-os/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-beta.11",
|
|
4
|
+
"license": "FSL-1.1-ALv2",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/timoconnellaus/composable-os.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://composableos.dev",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"provenance": true
|
|
14
|
+
},
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./formula": {
|
|
21
|
+
"types": "./dist/formula/index.d.ts",
|
|
22
|
+
"default": "./dist/formula/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./testing": {
|
|
25
|
+
"types": "./dist/testing.d.ts",
|
|
26
|
+
"default": "./dist/testing.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"src"
|
|
32
|
+
],
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"zod": "4.6.7"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/allowance.ts
ADDED
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
// The `allowance` behaviour (stage 32b, ADRs 0493 and 0494): a record that holds a number of uses
|
|
2
|
+
// (a pass, a membership), and a record that spends one (a booking, a visit). The host keeps the
|
|
3
|
+
// count and refuses, in the database, a spend past it in the spender's own batch, for every actor.
|
|
4
|
+
// Its declaration, its manifest schema and its contract checks live here; the host's side is
|
|
5
|
+
// packages/core/src/allowance/.
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import type { ExtensionManifest, FieldManifest, ObjectManifest } from "./manifest.ts";
|
|
8
|
+
|
|
9
|
+
/** What a holder's uses are counted over. Weeks start on Monday; both in the holder's zone. */
|
|
10
|
+
export const ALLOWANCE_PERIODS = ["total", "week", "month"] as const;
|
|
11
|
+
export type AllowancePeriod = (typeof ALLOWANCE_PERIODS)[number];
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The read-only values the host adds to every record of a holder object: uses spent in the
|
|
15
|
+
* current period, and uses left now (0 when the holder cannot be spent now; empty when
|
|
16
|
+
* unlimited). A holder object cannot have fields of these names.
|
|
17
|
+
*/
|
|
18
|
+
export const ALLOWANCE_VALUES = ["used", "left"] as const;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* One condition a spender's record must meet to be paid by a holder. `field` is a field of the
|
|
22
|
+
* spender (one the host copies onto it, such as a class type or a local time: see `copy` and
|
|
23
|
+
* `local`); exactly one of `in`, `atLeast` and `below` compares it.
|
|
24
|
+
*
|
|
25
|
+
* - `in`: a field of the holder holding a list (a many relation, a multi-select) or one value, or
|
|
26
|
+
* a list of values written here. An empty holder field allows any value ("all class types").
|
|
27
|
+
* - `atLeast` / `below`: a number field of the holder, or a number written here. The spender's
|
|
28
|
+
* value must be at least it, or below it (a time of day from 09:00, `atLeast: 540`, and before
|
|
29
|
+
* 16:00, `below: 960`). An empty holder field sets no bound.
|
|
30
|
+
*
|
|
31
|
+
* A spender with no value for `field` meets no condition that sets a limit.
|
|
32
|
+
*/
|
|
33
|
+
export interface AllowanceCondition {
|
|
34
|
+
field: string;
|
|
35
|
+
in?: string | readonly string[];
|
|
36
|
+
atLeast?: string | number;
|
|
37
|
+
below?: string | number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** What a holder may be spent on: an object that declares `allowance.spends`, and conditions. */
|
|
41
|
+
export interface AllowanceSpendsOn {
|
|
42
|
+
/** This extension's object (`booking`) or another extension's (`bookings.booking`). */
|
|
43
|
+
object: string;
|
|
44
|
+
when?: readonly AllowanceCondition[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A holder: its records grant uses (ADR 0493).
|
|
49
|
+
*
|
|
50
|
+
* - `person`: the relation to the Person whose uses they are. Only that Person's spenders spend
|
|
51
|
+
* them (a payer who is not the holder pays; the holder spends).
|
|
52
|
+
* - `uses`: a number field, how many uses per period. Empty means none, unless the object
|
|
53
|
+
* declares `emptyIsUnlimited: true`: then empty is unlimited (a use is still written, so
|
|
54
|
+
* attendance counts). Unlimited is always the object's explicit choice, never what a missing
|
|
55
|
+
* value falls into.
|
|
56
|
+
* - `per`: `total`, `week` or `month`, or `{ field }`, a select field whose options are those.
|
|
57
|
+
* Unused uses never carry over: each period starts at nought.
|
|
58
|
+
* - `from`, `until`: date or datetime fields. A spender whose time is before `from` or at or
|
|
59
|
+
* after `until` cannot be paid by it; a date `until` includes that day.
|
|
60
|
+
* - `live`: with `states`, the states in which it can be spent (`active`); required then, and
|
|
61
|
+
* left out on an object without states.
|
|
62
|
+
* - `zone`: a timezone field; weeks, months and dates are in it. Left out (or empty), the
|
|
63
|
+
* organisation's zone.
|
|
64
|
+
* - `spendsOn`: what it pays for, one or several.
|
|
65
|
+
*
|
|
66
|
+
* None of these fields, nor the holder fields a condition compares with, may be writable by a
|
|
67
|
+
* customer by any route (`customer.write`, or a tool's `customerFields`): a customer does not set
|
|
68
|
+
* their own uses.
|
|
69
|
+
*/
|
|
70
|
+
export interface AllowanceHolderSpec {
|
|
71
|
+
person: string;
|
|
72
|
+
uses: string;
|
|
73
|
+
per: AllowancePeriod | { field: string };
|
|
74
|
+
spendsOn: AllowanceSpendsOn | readonly AllowanceSpendsOn[];
|
|
75
|
+
from?: string;
|
|
76
|
+
until?: string;
|
|
77
|
+
live?: readonly string[];
|
|
78
|
+
zone?: string;
|
|
79
|
+
emptyIsUnlimited?: boolean;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* A spender: its records spend a use when they enter a state of `spendIn`, and give it back when
|
|
84
|
+
* they enter a state of `returnIn` (ADR 0493). Any other move keeps the use (a late cancel, a
|
|
85
|
+
* no-show, an attendance). The object must declare `states`.
|
|
86
|
+
*
|
|
87
|
+
* - `person`: the relation to the Person spending. The holder must be theirs.
|
|
88
|
+
* - `holder`: a text field naming the holder spent from (a record id). A caller may set it to
|
|
89
|
+
* choose one of the Person's holders; left empty, the host chooses (ADR 0494) and fills it.
|
|
90
|
+
* - `at`: the spender's time, a datetime field of its own or one relation away
|
|
91
|
+
* (`class.startsAt`). The use counts in the holder's period containing it, and the holder must
|
|
92
|
+
* be live then. Left out: the time of the spend.
|
|
93
|
+
* - `copy`: read-only fields of its own the host fills, at create and at each spend, from one
|
|
94
|
+
* relation away (`{ classType: "class.classType" }`), so a holder's `when` can read them and no
|
|
95
|
+
* caller can set them.
|
|
96
|
+
* - `local`: read-only number fields of its own the host fills from `at` in `zone` (a timezone
|
|
97
|
+
* field of its own or one relation away; the organisation's when left out or empty):
|
|
98
|
+
* `weekday` (1 Monday to 7 Sunday) and `minute` (minutes since local midnight).
|
|
99
|
+
* - `movedFrom`: a relation to another record of this object, and the state it is moved into.
|
|
100
|
+
* A record created into a spend state with it set takes the use of the record it names: in the
|
|
101
|
+
* same batch the host moves that record to `state` (a return state) and spends from the same
|
|
102
|
+
* holder, so a move never fails for want of a use the member already had.
|
|
103
|
+
* - `paidBy`: holders of other extensions that may pay for it, by object (`memberships.pass`) or
|
|
104
|
+
* by extension (`memberships`). Left out, only this extension's holders. A holder naming a
|
|
105
|
+
* spender of another extension that does not list it is refused at install.
|
|
106
|
+
* - `waived` (ADR 0506): a read-only boolean field of its own. A record created with it true
|
|
107
|
+
* enters its spend states without spending (a walk-in who paid at the desk, a drop-in paid by
|
|
108
|
+
* its order). Only the host and this extension's own code set it, never a customer, and it
|
|
109
|
+
* cannot change while the record is in a spend state.
|
|
110
|
+
*
|
|
111
|
+
* Every field a holder's condition reads must be one the host fills (`copy` or `local`), so no
|
|
112
|
+
* caller (a customer, staff, an AI) sets what pays for it.
|
|
113
|
+
*/
|
|
114
|
+
export interface AllowanceSpenderSpec {
|
|
115
|
+
person: string;
|
|
116
|
+
holder: string;
|
|
117
|
+
spendIn: readonly string[];
|
|
118
|
+
returnIn: readonly string[];
|
|
119
|
+
at?: string;
|
|
120
|
+
copy?: Readonly<Record<string, string>>;
|
|
121
|
+
local?: { weekday?: string; minute?: string; zone?: string };
|
|
122
|
+
movedFrom?: { field: string; state: string };
|
|
123
|
+
paidBy?: readonly string[];
|
|
124
|
+
waived?: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The allowance behaviour: an object holds uses or spends them, never both. */
|
|
128
|
+
export type AllowanceSpec = { holds: AllowanceHolderSpec } | { spends: AllowanceSpenderSpec };
|
|
129
|
+
|
|
130
|
+
const fieldName = z.string().max(40);
|
|
131
|
+
const path = z.string().max(81);
|
|
132
|
+
const stateValue = z.string().min(1).max(100);
|
|
133
|
+
|
|
134
|
+
const conditionSchema = z
|
|
135
|
+
.object({
|
|
136
|
+
field: fieldName,
|
|
137
|
+
in: z.union([fieldName, z.array(z.string().max(200)).min(1).max(100)]).optional(),
|
|
138
|
+
atLeast: z.union([fieldName, z.number()]).optional(),
|
|
139
|
+
below: z.union([fieldName, z.number()]).optional(),
|
|
140
|
+
})
|
|
141
|
+
.strict();
|
|
142
|
+
|
|
143
|
+
const spendsOnSchema = z
|
|
144
|
+
.object({ object: path, when: z.array(conditionSchema).max(10).optional() })
|
|
145
|
+
.strict();
|
|
146
|
+
|
|
147
|
+
export const allowanceManifestSchema = z.union([
|
|
148
|
+
z
|
|
149
|
+
.object({
|
|
150
|
+
holds: z
|
|
151
|
+
.object({
|
|
152
|
+
person: fieldName,
|
|
153
|
+
uses: fieldName,
|
|
154
|
+
per: z.union([z.enum(ALLOWANCE_PERIODS), z.object({ field: fieldName }).strict()]),
|
|
155
|
+
spendsOn: z.union([spendsOnSchema, z.array(spendsOnSchema).min(1).max(8)]),
|
|
156
|
+
from: fieldName.optional(),
|
|
157
|
+
until: fieldName.optional(),
|
|
158
|
+
live: z.array(stateValue).min(1).max(20).optional(),
|
|
159
|
+
zone: fieldName.optional(),
|
|
160
|
+
emptyIsUnlimited: z.boolean().optional(),
|
|
161
|
+
})
|
|
162
|
+
.strict(),
|
|
163
|
+
})
|
|
164
|
+
.strict(),
|
|
165
|
+
z
|
|
166
|
+
.object({
|
|
167
|
+
spends: z
|
|
168
|
+
.object({
|
|
169
|
+
person: fieldName,
|
|
170
|
+
holder: fieldName,
|
|
171
|
+
spendIn: z.array(stateValue).min(1).max(20),
|
|
172
|
+
returnIn: z.array(stateValue).max(20),
|
|
173
|
+
at: path.optional(),
|
|
174
|
+
copy: z.record(fieldName, path).optional(),
|
|
175
|
+
local: z
|
|
176
|
+
.object({
|
|
177
|
+
weekday: fieldName.optional(),
|
|
178
|
+
minute: fieldName.optional(),
|
|
179
|
+
zone: path.optional(),
|
|
180
|
+
})
|
|
181
|
+
.strict()
|
|
182
|
+
.optional(),
|
|
183
|
+
movedFrom: z.object({ field: fieldName, state: stateValue }).strict().optional(),
|
|
184
|
+
paidBy: z.array(path).min(1).max(20).optional(),
|
|
185
|
+
waived: fieldName.optional(),
|
|
186
|
+
})
|
|
187
|
+
.strict(),
|
|
188
|
+
})
|
|
189
|
+
.strict(),
|
|
190
|
+
]);
|
|
191
|
+
export type AllowanceManifest = z.infer<typeof allowanceManifestSchema>;
|
|
192
|
+
|
|
193
|
+
/** The spendsOn declarations of a holder, as a list. */
|
|
194
|
+
export function spendsOnList(
|
|
195
|
+
holds: Pick<AllowanceHolderSpec, "spendsOn">,
|
|
196
|
+
): readonly AllowanceSpendsOn[] {
|
|
197
|
+
return Array.isArray(holds.spendsOn)
|
|
198
|
+
? (holds.spendsOn as readonly AllowanceSpendsOn[])
|
|
199
|
+
: [holds.spendsOn as AllowanceSpendsOn];
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const DATES = new Set(["date", "datetime"]);
|
|
203
|
+
|
|
204
|
+
/** The fields a spender's host fills: what its holders' conditions may read. */
|
|
205
|
+
export function hostFilledFields(spends: Pick<AllowanceSpenderSpec, "copy" | "local">): string[] {
|
|
206
|
+
return [
|
|
207
|
+
...Object.keys(spends.copy ?? {}),
|
|
208
|
+
...(spends.local?.weekday ? [spends.local.weekday] : []),
|
|
209
|
+
...(spends.local?.minute ? [spends.local.minute] : []),
|
|
210
|
+
];
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Whether a spender (of extension `spenderExtension`) lets a holder object pay for it:
|
|
215
|
+
* `paidBy` names the holder's object or its extension; left out, its own extension's only.
|
|
216
|
+
*/
|
|
217
|
+
export function spenderAllows(
|
|
218
|
+
spends: Pick<AllowanceSpenderSpec, "paidBy">,
|
|
219
|
+
spenderExtension: string,
|
|
220
|
+
holderObject: string,
|
|
221
|
+
): boolean {
|
|
222
|
+
const holderExtension = holderObject.split(".")[0];
|
|
223
|
+
if (holderExtension === spenderExtension) return true;
|
|
224
|
+
return (spends.paidBy ?? []).some((p) => p === holderObject || p === holderExtension);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** The fields of `object` (local name `name`) a customer can write by any route. */
|
|
228
|
+
export function customerWritable(m: ExtensionManifest, name: string): Set<string> {
|
|
229
|
+
const object = m.objects[name];
|
|
230
|
+
const out = new Set(object?.customer?.write ?? []);
|
|
231
|
+
if (object?.customer?.field) out.add(object.customer.field);
|
|
232
|
+
for (const tool of Object.values(m.tools ?? {})) {
|
|
233
|
+
for (const field of tool.customerFields?.[`${m.id}.${name}`] ?? []) out.add(field);
|
|
234
|
+
}
|
|
235
|
+
return out;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Reports problems with `objects.<name>.allowance` through `contract`. */
|
|
239
|
+
export function checkObjectAllowance(
|
|
240
|
+
m: ExtensionManifest,
|
|
241
|
+
name: string,
|
|
242
|
+
object: ObjectManifest,
|
|
243
|
+
contract: (path: string, message: string) => void,
|
|
244
|
+
): void {
|
|
245
|
+
const allowance = object.allowance;
|
|
246
|
+
if (!allowance) return;
|
|
247
|
+
const ap = `objects.${name}.allowance`;
|
|
248
|
+
const fieldOf = (o: ObjectManifest, local: string): FieldManifest | undefined =>
|
|
249
|
+
Object.hasOwn(o.fields, local) ? o.fields[local] : undefined;
|
|
250
|
+
const customerWrites = customerWritable(m, name);
|
|
251
|
+
// A holder's Person is whose record it is: a customer sets it to themselves on create, so it
|
|
252
|
+
// is not a term they choose; it is checked against the spender's Person, never trusted.
|
|
253
|
+
if (object.customer?.field) customerWrites.delete(object.customer.field);
|
|
254
|
+
const ownObject = (target: string): [string, ObjectManifest] | undefined => {
|
|
255
|
+
const local = target.startsWith(`${m.id}.`)
|
|
256
|
+
? target.slice(m.id.length + 1)
|
|
257
|
+
: target.includes(".")
|
|
258
|
+
? undefined
|
|
259
|
+
: target;
|
|
260
|
+
return local !== undefined && Object.hasOwn(m.objects, local)
|
|
261
|
+
? [local, m.objects[local] as ObjectManifest]
|
|
262
|
+
: undefined;
|
|
263
|
+
};
|
|
264
|
+
const states = new Set(
|
|
265
|
+
object.states ? (fieldOf(object, object.states.field)?.options ?? []).map((o) => o.value) : [],
|
|
266
|
+
);
|
|
267
|
+
const notCustomerWritten = (local: string, p: string, what: string) => {
|
|
268
|
+
if (customerWrites.has(local)) {
|
|
269
|
+
contract(
|
|
270
|
+
p,
|
|
271
|
+
`"${local}" is ${what}, so a customer cannot write it (customer.write or a tool's customerFields)`,
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
if ("holds" in allowance) {
|
|
277
|
+
const h = allowance.holds;
|
|
278
|
+
const hp = `${ap}.holds`;
|
|
279
|
+
const person = fieldOf(object, h.person);
|
|
280
|
+
if (person?.type !== "relation" || person.many || person.to !== "person") {
|
|
281
|
+
contract(`${hp}.person`, `"${h.person}" must be a relation field (one record) to person`);
|
|
282
|
+
}
|
|
283
|
+
// The holder's Person: whose uses they are. Only the customer field itself is set to the
|
|
284
|
+
// caller by the host; any other person field must not be a customer's to write.
|
|
285
|
+
if (h.person !== object.customer?.field) {
|
|
286
|
+
notCustomerWritten(h.person, `${hp}.person`, "whose uses they are");
|
|
287
|
+
}
|
|
288
|
+
const uses = fieldOf(object, h.uses);
|
|
289
|
+
if (uses?.type !== "number" || uses.many) {
|
|
290
|
+
contract(`${hp}.uses`, `"${h.uses}" must be a number field of ${name}`);
|
|
291
|
+
}
|
|
292
|
+
notCustomerWritten(h.uses, `${hp}.uses`, "the holder's uses");
|
|
293
|
+
if (typeof h.per === "object") {
|
|
294
|
+
const per = fieldOf(object, h.per.field);
|
|
295
|
+
const options = (per?.options ?? []).map((o) => o.value);
|
|
296
|
+
if (per?.type !== "select" || per.many) {
|
|
297
|
+
contract(`${hp}.per.field`, `"${h.per.field}" must be a select field of ${name}`);
|
|
298
|
+
} else if (
|
|
299
|
+
options.some((o) => !(ALLOWANCE_PERIODS as readonly string[]).includes(o)) ||
|
|
300
|
+
per.editableOptions
|
|
301
|
+
) {
|
|
302
|
+
contract(
|
|
303
|
+
`${hp}.per.field`,
|
|
304
|
+
`"${h.per.field}"'s options must be fixed and among ${ALLOWANCE_PERIODS.join(", ")}`,
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
notCustomerWritten(h.per.field, `${hp}.per.field`, "the holder's period");
|
|
308
|
+
}
|
|
309
|
+
for (const key of ["from", "until"] as const) {
|
|
310
|
+
const local = h[key];
|
|
311
|
+
if (local === undefined) continue;
|
|
312
|
+
const field = fieldOf(object, local);
|
|
313
|
+
if (!field || !DATES.has(field.type) || field.many) {
|
|
314
|
+
contract(`${hp}.${key}`, `"${local}" must be a date or datetime field of ${name}`);
|
|
315
|
+
}
|
|
316
|
+
notCustomerWritten(local, `${hp}.${key}`, `the holder's ${key} date`);
|
|
317
|
+
}
|
|
318
|
+
if (h.zone !== undefined) {
|
|
319
|
+
const zone = fieldOf(object, h.zone);
|
|
320
|
+
if (zone?.type !== "timezone" || zone.many) {
|
|
321
|
+
contract(`${hp}.zone`, `"${h.zone}" must be a timezone field of ${name}`);
|
|
322
|
+
}
|
|
323
|
+
notCustomerWritten(h.zone, `${hp}.zone`, "the holder's zone");
|
|
324
|
+
}
|
|
325
|
+
if (object.states) {
|
|
326
|
+
if (!h.live) {
|
|
327
|
+
contract(`${hp}.live`, "A holder with states names the states it can be spent in (live)");
|
|
328
|
+
}
|
|
329
|
+
for (const state of h.live ?? []) {
|
|
330
|
+
if (!states.has(state)) {
|
|
331
|
+
contract(`${hp}.live`, `"${state}" is not an option of ${object.states.field}`);
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
} else if (h.live) {
|
|
335
|
+
contract(`${hp}.live`, "live names states, and this object has none");
|
|
336
|
+
}
|
|
337
|
+
for (const reserved of ALLOWANCE_VALUES) {
|
|
338
|
+
if (Object.hasOwn(object.fields, reserved)) {
|
|
339
|
+
contract(
|
|
340
|
+
`objects.${name}.fields.${reserved}`,
|
|
341
|
+
`"${reserved}" is a value the host adds to an allowance holder; rename the field`,
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
spendsOnList(h).forEach((target, i) => {
|
|
346
|
+
const sp = Array.isArray(h.spendsOn) ? `${hp}.spendsOn.${i}` : `${hp}.spendsOn`;
|
|
347
|
+
const own = ownObject(target.object);
|
|
348
|
+
if (!own) {
|
|
349
|
+
if (!target.object.includes(".") || target.object.startsWith(`${m.id}.`)) {
|
|
350
|
+
contract(`${sp}.object`, `"${target.object}" is not an object of this extension`);
|
|
351
|
+
}
|
|
352
|
+
// Another extension's object: checked by the host when both are installed.
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const [spenderName, spender] = own;
|
|
356
|
+
if (!spender.allowance || !("spends" in spender.allowance)) {
|
|
357
|
+
contract(`${sp}.object`, `${spenderName} must declare allowance.spends`);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
const filledBy = new Set(hostFilledFields(spender.allowance.spends));
|
|
361
|
+
(target.when ?? []).forEach((clause, j) => {
|
|
362
|
+
const cp = `${sp}.when.${j}`;
|
|
363
|
+
const ops = (["in", "atLeast", "below"] as const).filter((k) => clause[k] !== undefined);
|
|
364
|
+
if (ops.length !== 1) {
|
|
365
|
+
contract(cp, "A condition compares with exactly one of in, atLeast and below");
|
|
366
|
+
}
|
|
367
|
+
const field = fieldOf(spender, clause.field);
|
|
368
|
+
if (!field || field.type === "formula") {
|
|
369
|
+
contract(`${cp}.field`, `"${clause.field}" must be a field of ${spenderName}`);
|
|
370
|
+
} else if (!filledBy.has(clause.field)) {
|
|
371
|
+
contract(
|
|
372
|
+
`${cp}.field`,
|
|
373
|
+
`"${clause.field}" must be a field ${spenderName}'s host fills (its copy or local), so no caller sets what pays for it`,
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
if (typeof clause.in === "string" && !fieldOf(object, clause.in)) {
|
|
377
|
+
contract(`${cp}.in`, `"${clause.in}" is not a field of ${name}`);
|
|
378
|
+
}
|
|
379
|
+
if (typeof clause.in === "string") notCustomerWritten(clause.in, `${cp}.in`, "a limit");
|
|
380
|
+
for (const bound of ["atLeast", "below"] as const) {
|
|
381
|
+
const value = clause[bound];
|
|
382
|
+
if (typeof value !== "string") continue;
|
|
383
|
+
const limit = fieldOf(object, value);
|
|
384
|
+
if (limit?.type !== "number" || limit.many) {
|
|
385
|
+
contract(`${cp}.${bound}`, `"${value}" must be a number field of ${name}`);
|
|
386
|
+
}
|
|
387
|
+
notCustomerWritten(value, `${cp}.${bound}`, "a limit");
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
});
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const s = allowance.spends;
|
|
395
|
+
const sp = `${ap}.spends`;
|
|
396
|
+
if (!object.states) {
|
|
397
|
+
contract(sp, "A spender declares states: it spends on entering a state");
|
|
398
|
+
}
|
|
399
|
+
const person = fieldOf(object, s.person);
|
|
400
|
+
if (person?.type !== "relation" || person.many || person.to !== "person") {
|
|
401
|
+
contract(`${sp}.person`, `"${s.person}" must be a relation field (one record) to person`);
|
|
402
|
+
}
|
|
403
|
+
const holder = fieldOf(object, s.holder);
|
|
404
|
+
if (holder?.type !== "text" || holder.many) {
|
|
405
|
+
contract(`${sp}.holder`, `"${s.holder}" must be a text field of ${name} (a holder's id)`);
|
|
406
|
+
}
|
|
407
|
+
const spendIn = new Set(s.spendIn);
|
|
408
|
+
for (const [key, list] of [
|
|
409
|
+
["spendIn", s.spendIn],
|
|
410
|
+
["returnIn", s.returnIn],
|
|
411
|
+
] as const) {
|
|
412
|
+
for (const state of list) {
|
|
413
|
+
if (object.states && !states.has(state)) {
|
|
414
|
+
contract(`${sp}.${key}`, `"${state}" is not an option of ${object.states.field}`);
|
|
415
|
+
}
|
|
416
|
+
if (key === "returnIn" && spendIn.has(state)) {
|
|
417
|
+
contract(`${sp}.returnIn`, `"${state}" cannot both spend and return a use`);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
/** A path of one hop: the relation (one record) and the target field, when known. */
|
|
422
|
+
const hop = (value: string, p: string): FieldManifest | null | undefined => {
|
|
423
|
+
const [rel, target, extra] = value.split(".");
|
|
424
|
+
if (extra !== undefined || !rel || !target) {
|
|
425
|
+
contract(p, `"${value}" must be a field of ${name} or one relation away (relation.field)`);
|
|
426
|
+
return undefined;
|
|
427
|
+
}
|
|
428
|
+
const relation = fieldOf(object, rel);
|
|
429
|
+
if (relation?.type !== "relation" || relation.many || !relation.to) {
|
|
430
|
+
contract(p, `"${rel}" must be a relation field (one record) of ${name}`);
|
|
431
|
+
return undefined;
|
|
432
|
+
}
|
|
433
|
+
const other = ownObject(relation.to);
|
|
434
|
+
if (!other) return null; // Another extension's object or a core one: the host checks.
|
|
435
|
+
const field = fieldOf(other[1], target);
|
|
436
|
+
if (!field) contract(p, `"${target}" is not a field of ${other[0]}`);
|
|
437
|
+
return field;
|
|
438
|
+
};
|
|
439
|
+
if (s.at !== undefined) {
|
|
440
|
+
if (s.at.includes(".")) {
|
|
441
|
+
const field = hop(s.at, `${sp}.at`);
|
|
442
|
+
if (field && field.type !== "datetime") {
|
|
443
|
+
contract(`${sp}.at`, `"${s.at}" must be a datetime field`);
|
|
444
|
+
}
|
|
445
|
+
} else {
|
|
446
|
+
const field = fieldOf(object, s.at);
|
|
447
|
+
if (field?.type !== "datetime" || field.many) {
|
|
448
|
+
contract(`${sp}.at`, `"${s.at}" must be a datetime field of ${name}`);
|
|
449
|
+
}
|
|
450
|
+
notCustomerWritten(s.at, `${sp}.at`, "the time a use is counted at");
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
const filled = (local: string, p: string, type?: string) => {
|
|
454
|
+
const field = fieldOf(object, local);
|
|
455
|
+
if (!field || field.many || !field.readOnly || (type && field.type !== type)) {
|
|
456
|
+
contract(
|
|
457
|
+
p,
|
|
458
|
+
`"${local}" must be a read-only ${type ? `${type} ` : ""}field of ${name}: the host fills it`,
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
for (const [local, source] of Object.entries(s.copy ?? {})) {
|
|
463
|
+
filled(local, `${sp}.copy.${local}`);
|
|
464
|
+
if (!source.includes(".")) {
|
|
465
|
+
contract(`${sp}.copy.${local}`, `"${source}" must be one relation away (relation.field)`);
|
|
466
|
+
} else hop(source, `${sp}.copy.${local}`);
|
|
467
|
+
}
|
|
468
|
+
if (s.local) {
|
|
469
|
+
if (s.local.weekday !== undefined) filled(s.local.weekday, `${sp}.local.weekday`, "number");
|
|
470
|
+
if (s.local.minute !== undefined) filled(s.local.minute, `${sp}.local.minute`, "number");
|
|
471
|
+
if (s.local.zone !== undefined) {
|
|
472
|
+
if (s.local.zone.includes(".")) {
|
|
473
|
+
const field = hop(s.local.zone, `${sp}.local.zone`);
|
|
474
|
+
if (field && field.type !== "timezone") {
|
|
475
|
+
contract(`${sp}.local.zone`, `"${s.local.zone}" must be a timezone field`);
|
|
476
|
+
}
|
|
477
|
+
} else {
|
|
478
|
+
if (fieldOf(object, s.local.zone)?.type !== "timezone") {
|
|
479
|
+
contract(`${sp}.local.zone`, `"${s.local.zone}" must be a timezone field of ${name}`);
|
|
480
|
+
}
|
|
481
|
+
notCustomerWritten(s.local.zone, `${sp}.local.zone`, "the zone its local time is read in");
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
for (const [i, payer] of (s.paidBy ?? []).entries()) {
|
|
486
|
+
if (!/^[a-z][a-z0-9]{1,29}(\.[a-z][a-z0-9]{0,29})?$/.test(payer)) {
|
|
487
|
+
contract(`${sp}.paidBy.${i}`, `"${payer}" must be an extension id or <extension>.<object>`);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
if (s.waived !== undefined) {
|
|
491
|
+
// Paid another way (ADR 0506): only the host and the extension's own code say so.
|
|
492
|
+
const waived = fieldOf(object, s.waived);
|
|
493
|
+
if (waived?.type !== "boolean" || !waived.readOnly) {
|
|
494
|
+
contract(`${sp}.waived`, `"${s.waived}" must be a read-only boolean field of ${name}`);
|
|
495
|
+
}
|
|
496
|
+
notCustomerWritten(s.waived, `${sp}.waived`, "whether it spends a use");
|
|
497
|
+
}
|
|
498
|
+
if (s.movedFrom) {
|
|
499
|
+
const rel = fieldOf(object, s.movedFrom.field);
|
|
500
|
+
if (rel?.type !== "relation" || rel.many || (rel.to !== `${m.id}.${name}` && rel.to !== name)) {
|
|
501
|
+
contract(
|
|
502
|
+
`${sp}.movedFrom.field`,
|
|
503
|
+
`"${s.movedFrom.field}" must be a relation field (one record) to ${m.id}.${name}`,
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
if (!s.returnIn.includes(s.movedFrom.state)) {
|
|
507
|
+
contract(
|
|
508
|
+
`${sp}.movedFrom.state`,
|
|
509
|
+
`"${s.movedFrom.state}" must be one of returnIn: the moved record gives its use back`,
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
if (object.states) {
|
|
513
|
+
const into = object.states.moves.filter((move) => move.to === s.movedFrom?.state);
|
|
514
|
+
if (!into.some((move) => move.by.includes("staff"))) {
|
|
515
|
+
contract(
|
|
516
|
+
`${sp}.movedFrom.state`,
|
|
517
|
+
`A move into "${s.movedFrom.state}" by staff must be declared: staff move a record's use`,
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|