@composable-os/cli 0.1.0-beta.7 → 0.1.0-beta.9
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/accounts-D6N77JGP.js +44 -0
- package/dist/{blocks-HVTAQ5W2.js → blocks-4CA4PP3P.js} +4 -4
- package/dist/{chunk-X6I2LWB3.js → chunk-2MLAPQU7.js} +1471 -359
- package/dist/{chunk-2S7375SC.js → chunk-3MIEYXRY.js} +3 -301
- package/dist/chunk-56A6RJIU.js +1040 -0
- package/dist/chunk-7CQGMVDO.js +63 -0
- package/dist/{chunk-STJZJIRA.js → chunk-7O4F3NU4.js} +1039 -1380
- package/dist/chunk-7U3OZLTK.js +1370 -0
- package/dist/{chunk-ZRWF6QZS.js → chunk-DU6OIFOU.js} +1 -0
- package/dist/chunk-E4WCYTCT.js +347 -0
- package/dist/{chunk-E5JLTLRM.js → chunk-GYTEW66E.js} +1 -1
- package/dist/chunk-LPD2VR5M.js +284 -0
- package/dist/chunk-NDKVIG23.js +2641 -0
- package/dist/chunk-NSSUOI3Z.js +333 -0
- package/dist/chunk-OV3OFYPI.js +746 -0
- package/dist/{chunk-2P4AZTFL.js → chunk-P6YZMS3F.js} +11 -5
- package/dist/{chunk-Y7PQ7NIL.js → chunk-RFJOPI42.js} +138 -14
- package/dist/{chunk-VL62IMBN.js → chunk-RXRD34QW.js} +10 -8
- package/dist/{chunk-BEKL3CA2.js → chunk-STBMGUTM.js} +7065 -4580
- package/dist/chunk-VTQNOQGA.js +150 -0
- package/dist/chunk-XSE37HK5.js +426 -0
- package/dist/{chunk-MINQ46RU.js → chunk-Z52IX5WL.js} +257 -844
- package/dist/chunk-ZSR5H4O7.js +552 -0
- package/dist/{cli-XHC6QBQW.js → cli-7A5XHYML.js} +16 -9
- package/dist/{cli-BRSACHSG.js → cli-T6MJM7TU.js} +20 -8
- package/dist/cli.js +4 -4
- package/dist/{client-repo-G55G4OLV.js → client-repo-PC4ISVEZ.js} +4 -4
- package/dist/create.js +4 -4
- package/dist/{fields-64BQJA62.js → fields-L24XAIOA.js} +13 -11
- package/dist/invoke-TLYT6CFI.js +33 -0
- package/dist/order-IKQBUFCV.js +19 -0
- package/dist/{person-AZFEPZOE.js → person-DXEOOH2A.js} +9 -5
- package/dist/plan-on-sale-LRCK5MJG.js +24 -0
- package/dist/platform/migrations/0510_video_and_file_media.sql +109 -0
- package/dist/platform/migrations/0516_order_tax.sql +63 -0
- package/dist/platform/migrations/0520_publish_requests.sql +20 -0
- package/dist/platform/migrations/0530_subscriptions.sql +173 -0
- package/dist/platform/migrations/0531_subscription_trials.sql +56 -0
- package/dist/platform/migrations/0532_subscription_offers.sql +80 -0
- package/dist/platform/migrations/0533_allowances.sql +262 -0
- package/dist/platform/migrations/0560_recurring.sql +75 -0
- package/dist/platform/sdk/allowance.ts +508 -0
- package/dist/platform/sdk/capacity.ts +89 -0
- package/dist/platform/sdk/contract.ts +97 -1
- package/dist/platform/sdk/document.ts +161 -14
- package/dist/platform/sdk/field-types.ts +5 -0
- package/dist/platform/sdk/index.ts +3 -0
- package/dist/platform/sdk/manifest.ts +206 -5
- package/dist/platform/sdk/media.ts +62 -0
- package/dist/platform/sdk/recurring.ts +314 -0
- package/dist/platform/sdk/rich-text.ts +52 -4
- package/dist/platform/sdk/sellable.ts +120 -0
- package/dist/platform/sdk/states.ts +162 -1
- package/dist/platform/sdk/unique.ts +81 -0
- package/dist/run-BMSZKTN3.js +648 -0
- package/dist/run-GMQBAV6H.js +309 -0
- package/dist/{run-4JNFLYXV.js → run-TNHMG5RI.js} +51 -390
- package/dist/src-7DXXGZG2.js +38 -0
- package/dist/{src-J27WXNLY.js → src-BDG7PERB.js} +73 -1
- package/dist/visibility-XSBZTO66.js +33 -0
- package/package.json +2 -2
- package/dist/chunk-MAXLUQKB.js +0 -1287
- package/dist/invoke-5SR5ZWW7.js +0 -26
- package/dist/src-SSVOBH6L.js +0 -31
|
@@ -0,0 +1,508 @@
|
|
|
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
|
+
*
|
|
107
|
+
* Every field a holder's condition reads must be one the host fills (`copy` or `local`), so no
|
|
108
|
+
* caller (a customer, staff, an AI) sets what pays for it.
|
|
109
|
+
*/
|
|
110
|
+
export interface AllowanceSpenderSpec {
|
|
111
|
+
person: string;
|
|
112
|
+
holder: string;
|
|
113
|
+
spendIn: readonly string[];
|
|
114
|
+
returnIn: readonly string[];
|
|
115
|
+
at?: string;
|
|
116
|
+
copy?: Readonly<Record<string, string>>;
|
|
117
|
+
local?: { weekday?: string; minute?: string; zone?: string };
|
|
118
|
+
movedFrom?: { field: string; state: string };
|
|
119
|
+
paidBy?: readonly string[];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The allowance behaviour: an object holds uses or spends them, never both. */
|
|
123
|
+
export type AllowanceSpec = { holds: AllowanceHolderSpec } | { spends: AllowanceSpenderSpec };
|
|
124
|
+
|
|
125
|
+
const fieldName = z.string().max(40);
|
|
126
|
+
const path = z.string().max(81);
|
|
127
|
+
const stateValue = z.string().min(1).max(100);
|
|
128
|
+
|
|
129
|
+
const conditionSchema = z
|
|
130
|
+
.object({
|
|
131
|
+
field: fieldName,
|
|
132
|
+
in: z.union([fieldName, z.array(z.string().max(200)).min(1).max(100)]).optional(),
|
|
133
|
+
atLeast: z.union([fieldName, z.number()]).optional(),
|
|
134
|
+
below: z.union([fieldName, z.number()]).optional(),
|
|
135
|
+
})
|
|
136
|
+
.strict();
|
|
137
|
+
|
|
138
|
+
const spendsOnSchema = z
|
|
139
|
+
.object({ object: path, when: z.array(conditionSchema).max(10).optional() })
|
|
140
|
+
.strict();
|
|
141
|
+
|
|
142
|
+
export const allowanceManifestSchema = z.union([
|
|
143
|
+
z
|
|
144
|
+
.object({
|
|
145
|
+
holds: z
|
|
146
|
+
.object({
|
|
147
|
+
person: fieldName,
|
|
148
|
+
uses: fieldName,
|
|
149
|
+
per: z.union([z.enum(ALLOWANCE_PERIODS), z.object({ field: fieldName }).strict()]),
|
|
150
|
+
spendsOn: z.union([spendsOnSchema, z.array(spendsOnSchema).min(1).max(8)]),
|
|
151
|
+
from: fieldName.optional(),
|
|
152
|
+
until: fieldName.optional(),
|
|
153
|
+
live: z.array(stateValue).min(1).max(20).optional(),
|
|
154
|
+
zone: fieldName.optional(),
|
|
155
|
+
emptyIsUnlimited: z.boolean().optional(),
|
|
156
|
+
})
|
|
157
|
+
.strict(),
|
|
158
|
+
})
|
|
159
|
+
.strict(),
|
|
160
|
+
z
|
|
161
|
+
.object({
|
|
162
|
+
spends: z
|
|
163
|
+
.object({
|
|
164
|
+
person: fieldName,
|
|
165
|
+
holder: fieldName,
|
|
166
|
+
spendIn: z.array(stateValue).min(1).max(20),
|
|
167
|
+
returnIn: z.array(stateValue).max(20),
|
|
168
|
+
at: path.optional(),
|
|
169
|
+
copy: z.record(fieldName, path).optional(),
|
|
170
|
+
local: z
|
|
171
|
+
.object({
|
|
172
|
+
weekday: fieldName.optional(),
|
|
173
|
+
minute: fieldName.optional(),
|
|
174
|
+
zone: path.optional(),
|
|
175
|
+
})
|
|
176
|
+
.strict()
|
|
177
|
+
.optional(),
|
|
178
|
+
movedFrom: z.object({ field: fieldName, state: stateValue }).strict().optional(),
|
|
179
|
+
paidBy: z.array(path).min(1).max(20).optional(),
|
|
180
|
+
})
|
|
181
|
+
.strict(),
|
|
182
|
+
})
|
|
183
|
+
.strict(),
|
|
184
|
+
]);
|
|
185
|
+
export type AllowanceManifest = z.infer<typeof allowanceManifestSchema>;
|
|
186
|
+
|
|
187
|
+
/** The spendsOn declarations of a holder, as a list. */
|
|
188
|
+
export function spendsOnList(
|
|
189
|
+
holds: Pick<AllowanceHolderSpec, "spendsOn">,
|
|
190
|
+
): readonly AllowanceSpendsOn[] {
|
|
191
|
+
return Array.isArray(holds.spendsOn)
|
|
192
|
+
? (holds.spendsOn as readonly AllowanceSpendsOn[])
|
|
193
|
+
: [holds.spendsOn as AllowanceSpendsOn];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const DATES = new Set(["date", "datetime"]);
|
|
197
|
+
|
|
198
|
+
/** The fields a spender's host fills: what its holders' conditions may read. */
|
|
199
|
+
export function hostFilledFields(spends: Pick<AllowanceSpenderSpec, "copy" | "local">): string[] {
|
|
200
|
+
return [
|
|
201
|
+
...Object.keys(spends.copy ?? {}),
|
|
202
|
+
...(spends.local?.weekday ? [spends.local.weekday] : []),
|
|
203
|
+
...(spends.local?.minute ? [spends.local.minute] : []),
|
|
204
|
+
];
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Whether a spender (of extension `spenderExtension`) lets a holder object pay for it:
|
|
209
|
+
* `paidBy` names the holder's object or its extension; left out, its own extension's only.
|
|
210
|
+
*/
|
|
211
|
+
export function spenderAllows(
|
|
212
|
+
spends: Pick<AllowanceSpenderSpec, "paidBy">,
|
|
213
|
+
spenderExtension: string,
|
|
214
|
+
holderObject: string,
|
|
215
|
+
): boolean {
|
|
216
|
+
const holderExtension = holderObject.split(".")[0];
|
|
217
|
+
if (holderExtension === spenderExtension) return true;
|
|
218
|
+
return (spends.paidBy ?? []).some((p) => p === holderObject || p === holderExtension);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** The fields of `object` (local name `name`) a customer can write by any route. */
|
|
222
|
+
export function customerWritable(m: ExtensionManifest, name: string): Set<string> {
|
|
223
|
+
const object = m.objects[name];
|
|
224
|
+
const out = new Set(object?.customer?.write ?? []);
|
|
225
|
+
if (object?.customer?.field) out.add(object.customer.field);
|
|
226
|
+
for (const tool of Object.values(m.tools ?? {})) {
|
|
227
|
+
for (const field of tool.customerFields?.[`${m.id}.${name}`] ?? []) out.add(field);
|
|
228
|
+
}
|
|
229
|
+
return out;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Reports problems with `objects.<name>.allowance` through `contract`. */
|
|
233
|
+
export function checkObjectAllowance(
|
|
234
|
+
m: ExtensionManifest,
|
|
235
|
+
name: string,
|
|
236
|
+
object: ObjectManifest,
|
|
237
|
+
contract: (path: string, message: string) => void,
|
|
238
|
+
): void {
|
|
239
|
+
const allowance = object.allowance;
|
|
240
|
+
if (!allowance) return;
|
|
241
|
+
const ap = `objects.${name}.allowance`;
|
|
242
|
+
const fieldOf = (o: ObjectManifest, local: string): FieldManifest | undefined =>
|
|
243
|
+
Object.hasOwn(o.fields, local) ? o.fields[local] : undefined;
|
|
244
|
+
const customerWrites = customerWritable(m, name);
|
|
245
|
+
// A holder's Person is whose record it is: a customer sets it to themselves on create, so it
|
|
246
|
+
// is not a term they choose; it is checked against the spender's Person, never trusted.
|
|
247
|
+
if (object.customer?.field) customerWrites.delete(object.customer.field);
|
|
248
|
+
const ownObject = (target: string): [string, ObjectManifest] | undefined => {
|
|
249
|
+
const local = target.startsWith(`${m.id}.`)
|
|
250
|
+
? target.slice(m.id.length + 1)
|
|
251
|
+
: target.includes(".")
|
|
252
|
+
? undefined
|
|
253
|
+
: target;
|
|
254
|
+
return local !== undefined && Object.hasOwn(m.objects, local)
|
|
255
|
+
? [local, m.objects[local] as ObjectManifest]
|
|
256
|
+
: undefined;
|
|
257
|
+
};
|
|
258
|
+
const states = new Set(
|
|
259
|
+
object.states ? (fieldOf(object, object.states.field)?.options ?? []).map((o) => o.value) : [],
|
|
260
|
+
);
|
|
261
|
+
const notCustomerWritten = (local: string, p: string, what: string) => {
|
|
262
|
+
if (customerWrites.has(local)) {
|
|
263
|
+
contract(
|
|
264
|
+
p,
|
|
265
|
+
`"${local}" is ${what}, so a customer cannot write it (customer.write or a tool's customerFields)`,
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
if ("holds" in allowance) {
|
|
271
|
+
const h = allowance.holds;
|
|
272
|
+
const hp = `${ap}.holds`;
|
|
273
|
+
const person = fieldOf(object, h.person);
|
|
274
|
+
if (person?.type !== "relation" || person.many || person.to !== "person") {
|
|
275
|
+
contract(`${hp}.person`, `"${h.person}" must be a relation field (one record) to person`);
|
|
276
|
+
}
|
|
277
|
+
// The holder's Person: whose uses they are. Only the customer field itself is set to the
|
|
278
|
+
// caller by the host; any other person field must not be a customer's to write.
|
|
279
|
+
if (h.person !== object.customer?.field) {
|
|
280
|
+
notCustomerWritten(h.person, `${hp}.person`, "whose uses they are");
|
|
281
|
+
}
|
|
282
|
+
const uses = fieldOf(object, h.uses);
|
|
283
|
+
if (uses?.type !== "number" || uses.many) {
|
|
284
|
+
contract(`${hp}.uses`, `"${h.uses}" must be a number field of ${name}`);
|
|
285
|
+
}
|
|
286
|
+
notCustomerWritten(h.uses, `${hp}.uses`, "the holder's uses");
|
|
287
|
+
if (typeof h.per === "object") {
|
|
288
|
+
const per = fieldOf(object, h.per.field);
|
|
289
|
+
const options = (per?.options ?? []).map((o) => o.value);
|
|
290
|
+
if (per?.type !== "select" || per.many) {
|
|
291
|
+
contract(`${hp}.per.field`, `"${h.per.field}" must be a select field of ${name}`);
|
|
292
|
+
} else if (
|
|
293
|
+
options.some((o) => !(ALLOWANCE_PERIODS as readonly string[]).includes(o)) ||
|
|
294
|
+
per.editableOptions
|
|
295
|
+
) {
|
|
296
|
+
contract(
|
|
297
|
+
`${hp}.per.field`,
|
|
298
|
+
`"${h.per.field}"'s options must be fixed and among ${ALLOWANCE_PERIODS.join(", ")}`,
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
notCustomerWritten(h.per.field, `${hp}.per.field`, "the holder's period");
|
|
302
|
+
}
|
|
303
|
+
for (const key of ["from", "until"] as const) {
|
|
304
|
+
const local = h[key];
|
|
305
|
+
if (local === undefined) continue;
|
|
306
|
+
const field = fieldOf(object, local);
|
|
307
|
+
if (!field || !DATES.has(field.type) || field.many) {
|
|
308
|
+
contract(`${hp}.${key}`, `"${local}" must be a date or datetime field of ${name}`);
|
|
309
|
+
}
|
|
310
|
+
notCustomerWritten(local, `${hp}.${key}`, `the holder's ${key} date`);
|
|
311
|
+
}
|
|
312
|
+
if (h.zone !== undefined) {
|
|
313
|
+
const zone = fieldOf(object, h.zone);
|
|
314
|
+
if (zone?.type !== "timezone" || zone.many) {
|
|
315
|
+
contract(`${hp}.zone`, `"${h.zone}" must be a timezone field of ${name}`);
|
|
316
|
+
}
|
|
317
|
+
notCustomerWritten(h.zone, `${hp}.zone`, "the holder's zone");
|
|
318
|
+
}
|
|
319
|
+
if (object.states) {
|
|
320
|
+
if (!h.live) {
|
|
321
|
+
contract(`${hp}.live`, "A holder with states names the states it can be spent in (live)");
|
|
322
|
+
}
|
|
323
|
+
for (const state of h.live ?? []) {
|
|
324
|
+
if (!states.has(state)) {
|
|
325
|
+
contract(`${hp}.live`, `"${state}" is not an option of ${object.states.field}`);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
} else if (h.live) {
|
|
329
|
+
contract(`${hp}.live`, "live names states, and this object has none");
|
|
330
|
+
}
|
|
331
|
+
for (const reserved of ALLOWANCE_VALUES) {
|
|
332
|
+
if (Object.hasOwn(object.fields, reserved)) {
|
|
333
|
+
contract(
|
|
334
|
+
`objects.${name}.fields.${reserved}`,
|
|
335
|
+
`"${reserved}" is a value the host adds to an allowance holder; rename the field`,
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
spendsOnList(h).forEach((target, i) => {
|
|
340
|
+
const sp = Array.isArray(h.spendsOn) ? `${hp}.spendsOn.${i}` : `${hp}.spendsOn`;
|
|
341
|
+
const own = ownObject(target.object);
|
|
342
|
+
if (!own) {
|
|
343
|
+
if (!target.object.includes(".") || target.object.startsWith(`${m.id}.`)) {
|
|
344
|
+
contract(`${sp}.object`, `"${target.object}" is not an object of this extension`);
|
|
345
|
+
}
|
|
346
|
+
// Another extension's object: checked by the host when both are installed.
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
const [spenderName, spender] = own;
|
|
350
|
+
if (!spender.allowance || !("spends" in spender.allowance)) {
|
|
351
|
+
contract(`${sp}.object`, `${spenderName} must declare allowance.spends`);
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
const filledBy = new Set(hostFilledFields(spender.allowance.spends));
|
|
355
|
+
(target.when ?? []).forEach((clause, j) => {
|
|
356
|
+
const cp = `${sp}.when.${j}`;
|
|
357
|
+
const ops = (["in", "atLeast", "below"] as const).filter((k) => clause[k] !== undefined);
|
|
358
|
+
if (ops.length !== 1) {
|
|
359
|
+
contract(cp, "A condition compares with exactly one of in, atLeast and below");
|
|
360
|
+
}
|
|
361
|
+
const field = fieldOf(spender, clause.field);
|
|
362
|
+
if (!field || field.type === "formula") {
|
|
363
|
+
contract(`${cp}.field`, `"${clause.field}" must be a field of ${spenderName}`);
|
|
364
|
+
} else if (!filledBy.has(clause.field)) {
|
|
365
|
+
contract(
|
|
366
|
+
`${cp}.field`,
|
|
367
|
+
`"${clause.field}" must be a field ${spenderName}'s host fills (its copy or local), so no caller sets what pays for it`,
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
if (typeof clause.in === "string" && !fieldOf(object, clause.in)) {
|
|
371
|
+
contract(`${cp}.in`, `"${clause.in}" is not a field of ${name}`);
|
|
372
|
+
}
|
|
373
|
+
if (typeof clause.in === "string") notCustomerWritten(clause.in, `${cp}.in`, "a limit");
|
|
374
|
+
for (const bound of ["atLeast", "below"] as const) {
|
|
375
|
+
const value = clause[bound];
|
|
376
|
+
if (typeof value !== "string") continue;
|
|
377
|
+
const limit = fieldOf(object, value);
|
|
378
|
+
if (limit?.type !== "number" || limit.many) {
|
|
379
|
+
contract(`${cp}.${bound}`, `"${value}" must be a number field of ${name}`);
|
|
380
|
+
}
|
|
381
|
+
notCustomerWritten(value, `${cp}.${bound}`, "a limit");
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const s = allowance.spends;
|
|
389
|
+
const sp = `${ap}.spends`;
|
|
390
|
+
if (!object.states) {
|
|
391
|
+
contract(sp, "A spender declares states: it spends on entering a state");
|
|
392
|
+
}
|
|
393
|
+
const person = fieldOf(object, s.person);
|
|
394
|
+
if (person?.type !== "relation" || person.many || person.to !== "person") {
|
|
395
|
+
contract(`${sp}.person`, `"${s.person}" must be a relation field (one record) to person`);
|
|
396
|
+
}
|
|
397
|
+
const holder = fieldOf(object, s.holder);
|
|
398
|
+
if (holder?.type !== "text" || holder.many) {
|
|
399
|
+
contract(`${sp}.holder`, `"${s.holder}" must be a text field of ${name} (a holder's id)`);
|
|
400
|
+
}
|
|
401
|
+
const spendIn = new Set(s.spendIn);
|
|
402
|
+
for (const [key, list] of [
|
|
403
|
+
["spendIn", s.spendIn],
|
|
404
|
+
["returnIn", s.returnIn],
|
|
405
|
+
] as const) {
|
|
406
|
+
for (const state of list) {
|
|
407
|
+
if (object.states && !states.has(state)) {
|
|
408
|
+
contract(`${sp}.${key}`, `"${state}" is not an option of ${object.states.field}`);
|
|
409
|
+
}
|
|
410
|
+
if (key === "returnIn" && spendIn.has(state)) {
|
|
411
|
+
contract(`${sp}.returnIn`, `"${state}" cannot both spend and return a use`);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
/** A path of one hop: the relation (one record) and the target field, when known. */
|
|
416
|
+
const hop = (value: string, p: string): FieldManifest | null | undefined => {
|
|
417
|
+
const [rel, target, extra] = value.split(".");
|
|
418
|
+
if (extra !== undefined || !rel || !target) {
|
|
419
|
+
contract(p, `"${value}" must be a field of ${name} or one relation away (relation.field)`);
|
|
420
|
+
return undefined;
|
|
421
|
+
}
|
|
422
|
+
const relation = fieldOf(object, rel);
|
|
423
|
+
if (relation?.type !== "relation" || relation.many || !relation.to) {
|
|
424
|
+
contract(p, `"${rel}" must be a relation field (one record) of ${name}`);
|
|
425
|
+
return undefined;
|
|
426
|
+
}
|
|
427
|
+
const other = ownObject(relation.to);
|
|
428
|
+
if (!other) return null; // Another extension's object or a core one: the host checks.
|
|
429
|
+
const field = fieldOf(other[1], target);
|
|
430
|
+
if (!field) contract(p, `"${target}" is not a field of ${other[0]}`);
|
|
431
|
+
return field;
|
|
432
|
+
};
|
|
433
|
+
if (s.at !== undefined) {
|
|
434
|
+
if (s.at.includes(".")) {
|
|
435
|
+
const field = hop(s.at, `${sp}.at`);
|
|
436
|
+
if (field && field.type !== "datetime") {
|
|
437
|
+
contract(`${sp}.at`, `"${s.at}" must be a datetime field`);
|
|
438
|
+
}
|
|
439
|
+
} else {
|
|
440
|
+
const field = fieldOf(object, s.at);
|
|
441
|
+
if (field?.type !== "datetime" || field.many) {
|
|
442
|
+
contract(`${sp}.at`, `"${s.at}" must be a datetime field of ${name}`);
|
|
443
|
+
}
|
|
444
|
+
notCustomerWritten(s.at, `${sp}.at`, "the time a use is counted at");
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
const filled = (local: string, p: string, type?: string) => {
|
|
448
|
+
const field = fieldOf(object, local);
|
|
449
|
+
if (!field || field.many || !field.readOnly || (type && field.type !== type)) {
|
|
450
|
+
contract(
|
|
451
|
+
p,
|
|
452
|
+
`"${local}" must be a read-only ${type ? `${type} ` : ""}field of ${name}: the host fills it`,
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
for (const [local, source] of Object.entries(s.copy ?? {})) {
|
|
457
|
+
filled(local, `${sp}.copy.${local}`);
|
|
458
|
+
if (!source.includes(".")) {
|
|
459
|
+
contract(`${sp}.copy.${local}`, `"${source}" must be one relation away (relation.field)`);
|
|
460
|
+
} else hop(source, `${sp}.copy.${local}`);
|
|
461
|
+
}
|
|
462
|
+
if (s.local) {
|
|
463
|
+
if (s.local.weekday !== undefined) filled(s.local.weekday, `${sp}.local.weekday`, "number");
|
|
464
|
+
if (s.local.minute !== undefined) filled(s.local.minute, `${sp}.local.minute`, "number");
|
|
465
|
+
if (s.local.zone !== undefined) {
|
|
466
|
+
if (s.local.zone.includes(".")) {
|
|
467
|
+
const field = hop(s.local.zone, `${sp}.local.zone`);
|
|
468
|
+
if (field && field.type !== "timezone") {
|
|
469
|
+
contract(`${sp}.local.zone`, `"${s.local.zone}" must be a timezone field`);
|
|
470
|
+
}
|
|
471
|
+
} else {
|
|
472
|
+
if (fieldOf(object, s.local.zone)?.type !== "timezone") {
|
|
473
|
+
contract(`${sp}.local.zone`, `"${s.local.zone}" must be a timezone field of ${name}`);
|
|
474
|
+
}
|
|
475
|
+
notCustomerWritten(s.local.zone, `${sp}.local.zone`, "the zone its local time is read in");
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
for (const [i, payer] of (s.paidBy ?? []).entries()) {
|
|
480
|
+
if (!/^[a-z][a-z0-9]{1,29}(\.[a-z][a-z0-9]{0,29})?$/.test(payer)) {
|
|
481
|
+
contract(`${sp}.paidBy.${i}`, `"${payer}" must be an extension id or <extension>.<object>`);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
if (s.movedFrom) {
|
|
485
|
+
const rel = fieldOf(object, s.movedFrom.field);
|
|
486
|
+
if (rel?.type !== "relation" || rel.many || (rel.to !== `${m.id}.${name}` && rel.to !== name)) {
|
|
487
|
+
contract(
|
|
488
|
+
`${sp}.movedFrom.field`,
|
|
489
|
+
`"${s.movedFrom.field}" must be a relation field (one record) to ${m.id}.${name}`,
|
|
490
|
+
);
|
|
491
|
+
}
|
|
492
|
+
if (!s.returnIn.includes(s.movedFrom.state)) {
|
|
493
|
+
contract(
|
|
494
|
+
`${sp}.movedFrom.state`,
|
|
495
|
+
`"${s.movedFrom.state}" must be one of returnIn: the moved record gives its use back`,
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
if (object.states) {
|
|
499
|
+
const into = object.states.moves.filter((move) => move.to === s.movedFrom?.state);
|
|
500
|
+
if (!into.some((move) => move.by.includes("staff"))) {
|
|
501
|
+
contract(
|
|
502
|
+
`${sp}.movedFrom.state`,
|
|
503
|
+
`A move into "${s.movedFrom.state}" by staff must be declared: staff move a record's use`,
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
}
|
|
@@ -110,3 +110,92 @@ export function checkObjectCapacity(
|
|
|
110
110
|
contract(`${path}.lowAt`, 'show: "low" needs lowAt, the number at or below which it says so');
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* A capacity claimant (stage 31, ADR 0501): a record whose own states hold and release a claim of
|
|
116
|
+
* one place on the record its relation `on` points to (a booking on its class), which declares a
|
|
117
|
+
* capacity. Entering a `held` state holds a place, entering a `confirmed` state takes one (from
|
|
118
|
+
* the hold, or claimed and taken at once), and leaving them gives it back, each in the batch of
|
|
119
|
+
* the write that makes the move, refused by the database past the places as an order's claim is.
|
|
120
|
+
* Moving between two `held` states, or two `confirmed` ones, changes nothing.
|
|
121
|
+
*
|
|
122
|
+
* - The object declares `states`; `held` and `confirmed` are its states, apart. No move goes from
|
|
123
|
+
* a confirmed state back to a held one.
|
|
124
|
+
* - While a record is in one of them, `on` cannot be changed: a move to another class is a new
|
|
125
|
+
* record. A record holding a place cannot be moved to Trash.
|
|
126
|
+
*/
|
|
127
|
+
export interface ClaimsSpec {
|
|
128
|
+
on: string;
|
|
129
|
+
held?: readonly string[];
|
|
130
|
+
confirmed?: readonly string[];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export const claimsManifestSchema = z
|
|
134
|
+
.object({
|
|
135
|
+
on: fieldName,
|
|
136
|
+
held: z.array(z.string().min(1).max(100)).max(20).optional(),
|
|
137
|
+
confirmed: z.array(z.string().min(1).max(100)).max(20).optional(),
|
|
138
|
+
})
|
|
139
|
+
.strict();
|
|
140
|
+
export type ClaimsManifest = z.infer<typeof claimsManifestSchema>;
|
|
141
|
+
|
|
142
|
+
/** Reports problems with `objects.<name>.claims` through `contract`. */
|
|
143
|
+
export function checkObjectClaims(
|
|
144
|
+
m: ExtensionManifest,
|
|
145
|
+
name: string,
|
|
146
|
+
object: ObjectManifest,
|
|
147
|
+
contract: (path: string, message: string) => void,
|
|
148
|
+
): void {
|
|
149
|
+
const claims = object.claims;
|
|
150
|
+
if (!claims) return;
|
|
151
|
+
const path = `objects.${name}.claims`;
|
|
152
|
+
const on = Object.hasOwn(object.fields, claims.on) ? object.fields[claims.on] : undefined;
|
|
153
|
+
const target = on?.type === "relation" && !on.many ? on.to : undefined;
|
|
154
|
+
const local = target?.startsWith(`${m.id}.`) ? target.slice(m.id.length + 1) : undefined;
|
|
155
|
+
const targetObject =
|
|
156
|
+
local !== undefined && Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
|
|
157
|
+
if (!targetObject || local === name) {
|
|
158
|
+
contract(
|
|
159
|
+
`${path}.on`,
|
|
160
|
+
`"${claims.on}" must be a relation field (one record) to another of this extension's objects`,
|
|
161
|
+
);
|
|
162
|
+
} else if (!targetObject.capacity) {
|
|
163
|
+
contract(`${path}.on`, `${target} must declare a capacity for its places to be claimed`);
|
|
164
|
+
}
|
|
165
|
+
const held = claims.held ?? [];
|
|
166
|
+
const confirmed = claims.confirmed ?? [];
|
|
167
|
+
if (!held.length && !confirmed.length) {
|
|
168
|
+
contract(path, "Name the states that hold a place (held) or take one (confirmed)");
|
|
169
|
+
}
|
|
170
|
+
const states = object.states;
|
|
171
|
+
if (!states) {
|
|
172
|
+
contract(path, `${name} must declare states: its states hold and release the claim`);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const field = Object.hasOwn(object.fields, states.field)
|
|
176
|
+
? object.fields[states.field]
|
|
177
|
+
: undefined;
|
|
178
|
+
const values = new Set((field?.options ?? []).map((o) => o.value));
|
|
179
|
+
for (const [key, list] of [
|
|
180
|
+
["held", held],
|
|
181
|
+
["confirmed", confirmed],
|
|
182
|
+
] as const) {
|
|
183
|
+
if (new Set(list).size !== list.length) contract(`${path}.${key}`, "List each state once");
|
|
184
|
+
for (const state of list) {
|
|
185
|
+
if (!values.has(state)) contract(`${path}.${key}`, `"${state}" is not a state of ${name}`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const both = held.filter((state) => confirmed.includes(state));
|
|
189
|
+
if (both.length) {
|
|
190
|
+
contract(path, `"${both[0]}" cannot both hold and take a place`);
|
|
191
|
+
}
|
|
192
|
+
states.moves.forEach((move, i) => {
|
|
193
|
+
const from = typeof move.from === "string" ? [move.from] : move.from;
|
|
194
|
+
if (held.includes(move.to) && from.some((state) => confirmed.includes(state))) {
|
|
195
|
+
contract(
|
|
196
|
+
`objects.${name}.states.moves.${i}`,
|
|
197
|
+
`A move from a state that takes a place cannot go back to one that only holds it ("${move.to}")`,
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}
|