@composable-os/sdk 0.0.0-stage → 0.1.0-beta.10
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 +77 -0
- package/dist/capacity.js +127 -0
- package/dist/contract.d.ts +819 -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 +1596 -0
- package/dist/manifest.js +1420 -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 +253 -0
- package/dist/sellable.js +593 -0
- package/dist/slug.d.ts +11 -0
- package/dist/slug.js +23 -0
- package/dist/states.d.ts +238 -0
- package/dist/states.js +315 -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 +201 -0
- package/src/contract.ts +884 -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 +1709 -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 +888 -0
- package/src/slug.ts +25 -0
- package/src/states.ts +484 -0
- package/src/testing.ts +847 -0
- package/src/unique.ts +81 -0
- package/README.md +0 -3
package/dist/sellable.js
ADDED
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
// The `sellable` behaviour (stage 27, ADR 0435): an object's records can be put on an order. The
|
|
2
|
+
// host reads the price from the record, refuses a line outside the sale window or over the most
|
|
3
|
+
// one order may hold, and, when the order is first paid, creates what a paid line grants (a
|
|
4
|
+
// ticket per place) in the batch that marks it paid. Its declaration, its manifest schema and its
|
|
5
|
+
// contract checks live here; the host's side is packages/core/src/orders/ (sellable.ts, grants.ts).
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
import { customerWritable, spendsOnList } from "./allowance.js";
|
|
8
|
+
import { moveOrigins } from "./states.js";
|
|
9
|
+
/** The host's subscription states a grant follows (ADR 0485). */
|
|
10
|
+
export const SUBSCRIPTION_GRANT_STATES = [
|
|
11
|
+
"trialing",
|
|
12
|
+
"active",
|
|
13
|
+
"past_due",
|
|
14
|
+
"unpaid",
|
|
15
|
+
"paused",
|
|
16
|
+
"ended",
|
|
17
|
+
];
|
|
18
|
+
/** A product tax code as Stripe's list has them. */
|
|
19
|
+
export const TAX_CODE_PATTERN = /^txcd_\d{8}$/;
|
|
20
|
+
const fieldName = z.string().max(40);
|
|
21
|
+
export const GRANT_SOURCES = [
|
|
22
|
+
"item",
|
|
23
|
+
"order",
|
|
24
|
+
"order.person",
|
|
25
|
+
"order.email",
|
|
26
|
+
"line",
|
|
27
|
+
"line.title",
|
|
28
|
+
"line.unitPrice",
|
|
29
|
+
"code",
|
|
30
|
+
];
|
|
31
|
+
const itemSource = z
|
|
32
|
+
.string()
|
|
33
|
+
.regex(/^item\.[a-z][a-zA-Z0-9]{0,39}$/, "item.<field>");
|
|
34
|
+
const recurringGrantSource = z.union([
|
|
35
|
+
z.enum(["item", "subscription", "subscription.holder", "subscription.payer"]),
|
|
36
|
+
itemSource,
|
|
37
|
+
]);
|
|
38
|
+
const grantSource = z.union([
|
|
39
|
+
z.enum(GRANT_SOURCES),
|
|
40
|
+
z.string().regex(/^item\.[a-z][a-zA-Z0-9]{0,39}$/, "Use item, item.<field>, order, line or code"),
|
|
41
|
+
]);
|
|
42
|
+
export const sellableManifestSchema = z
|
|
43
|
+
.object({
|
|
44
|
+
price: fieldName,
|
|
45
|
+
currency: z
|
|
46
|
+
.string()
|
|
47
|
+
.regex(/^[A-Z]{3}$/)
|
|
48
|
+
.optional(),
|
|
49
|
+
saleWindow: z
|
|
50
|
+
.object({ from: fieldName.optional(), until: fieldName.optional() })
|
|
51
|
+
.strict()
|
|
52
|
+
.optional(),
|
|
53
|
+
maxPerOrder: fieldName.optional(),
|
|
54
|
+
deposit: z
|
|
55
|
+
.object({ amount: fieldName.optional(), balanceDue: fieldName.optional() })
|
|
56
|
+
.strict()
|
|
57
|
+
.optional(),
|
|
58
|
+
grants: z
|
|
59
|
+
.object({
|
|
60
|
+
object: z.string().max(40),
|
|
61
|
+
set: z.record(fieldName, grantSource),
|
|
62
|
+
void: z.string().min(1).max(100),
|
|
63
|
+
valid: z
|
|
64
|
+
.object({
|
|
65
|
+
from: fieldName.optional(),
|
|
66
|
+
until: fieldName.optional(),
|
|
67
|
+
days: itemSource.optional(),
|
|
68
|
+
})
|
|
69
|
+
.strict()
|
|
70
|
+
.optional(),
|
|
71
|
+
})
|
|
72
|
+
.strict()
|
|
73
|
+
.optional(),
|
|
74
|
+
onePerPerson: fieldName.optional(),
|
|
75
|
+
tax: z
|
|
76
|
+
.object({
|
|
77
|
+
code: z.string().regex(TAX_CODE_PATTERN, "A product tax code: txcd_ and eight digits"),
|
|
78
|
+
place: z
|
|
79
|
+
.object({
|
|
80
|
+
via: fieldName.optional(),
|
|
81
|
+
country: fieldName,
|
|
82
|
+
region: fieldName.optional(),
|
|
83
|
+
postcode: fieldName.optional(),
|
|
84
|
+
city: fieldName.optional(),
|
|
85
|
+
line1: fieldName.optional(),
|
|
86
|
+
})
|
|
87
|
+
.strict()
|
|
88
|
+
.optional(),
|
|
89
|
+
})
|
|
90
|
+
.strict()
|
|
91
|
+
.optional(),
|
|
92
|
+
recurring: z
|
|
93
|
+
.object({
|
|
94
|
+
interval: z.enum(["week", "month", "year"]),
|
|
95
|
+
trialDays: fieldName.optional(),
|
|
96
|
+
grants: z
|
|
97
|
+
.object({
|
|
98
|
+
object: z.string().max(40),
|
|
99
|
+
set: z.record(fieldName, recurringGrantSource),
|
|
100
|
+
from: fieldName.optional(),
|
|
101
|
+
until: fieldName.optional(),
|
|
102
|
+
states: z
|
|
103
|
+
.object({
|
|
104
|
+
trialing: z.string().min(1).max(100),
|
|
105
|
+
active: z.string().min(1).max(100),
|
|
106
|
+
past_due: z.string().min(1).max(100),
|
|
107
|
+
unpaid: z.string().min(1).max(100),
|
|
108
|
+
paused: z.string().min(1).max(100),
|
|
109
|
+
ended: z.string().min(1).max(100),
|
|
110
|
+
})
|
|
111
|
+
.strict(),
|
|
112
|
+
})
|
|
113
|
+
.strict()
|
|
114
|
+
.optional(),
|
|
115
|
+
})
|
|
116
|
+
.strict()
|
|
117
|
+
.optional(),
|
|
118
|
+
})
|
|
119
|
+
.strict();
|
|
120
|
+
const NUMERIC = new Set(["number", "currency"]);
|
|
121
|
+
const DATES = new Set(["date", "datetime"]);
|
|
122
|
+
const TEXT = new Set(["text"]);
|
|
123
|
+
/** Reports problems with `objects.<name>.sellable` through `contract`. */
|
|
124
|
+
export function checkObjectSellable(m, name, object, contract) {
|
|
125
|
+
const sellable = object.sellable;
|
|
126
|
+
if (!sellable)
|
|
127
|
+
return;
|
|
128
|
+
const path = `objects.${name}.sellable`;
|
|
129
|
+
const fieldOf = (local) => Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
130
|
+
const single = (local, types) => {
|
|
131
|
+
const field = fieldOf(local);
|
|
132
|
+
return field !== undefined && types.has(field.type) && !field.many;
|
|
133
|
+
};
|
|
134
|
+
if (!single(sellable.price, NUMERIC)) {
|
|
135
|
+
contract(`${path}.price`, `"${sellable.price}" must be a currency or number field of ${name}`);
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
const own = fieldOf(sellable.price)?.currency;
|
|
139
|
+
if (sellable.currency && own && own !== sellable.currency) {
|
|
140
|
+
contract(`${path}.currency`, `The price field is in ${own}; currency says ${sellable.currency}. Declare one`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (sellable.saleWindow) {
|
|
144
|
+
const { from, until } = sellable.saleWindow;
|
|
145
|
+
if (from === undefined && until === undefined) {
|
|
146
|
+
contract(`${path}.saleWindow`, "A sale window names its from field, its until field or both");
|
|
147
|
+
}
|
|
148
|
+
for (const [key, local] of [
|
|
149
|
+
["from", from],
|
|
150
|
+
["until", until],
|
|
151
|
+
]) {
|
|
152
|
+
if (local !== undefined && !single(local, DATES)) {
|
|
153
|
+
contract(`${path}.saleWindow.${key}`, `"${local}" must be a date or datetime field of ${name}`);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
if (sellable.recurring) {
|
|
158
|
+
// A plan is subscribed to, not put on an order (ADR 0485).
|
|
159
|
+
for (const key of ["deposit", "maxPerOrder", "grants"]) {
|
|
160
|
+
if (sellable[key] !== undefined) {
|
|
161
|
+
contract(`${path}.${key}`, `A recurring plan has no ${key}: it is subscribed to, one at a time`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const trial = sellable.recurring.trialDays;
|
|
165
|
+
if (trial !== undefined && !single(trial, new Set(["number"]))) {
|
|
166
|
+
contract(`${path}.recurring.trialDays`, `"${trial}" must be a number field of ${name}`);
|
|
167
|
+
}
|
|
168
|
+
if (sellable.onePerPerson !== undefined) {
|
|
169
|
+
contract(`${path}.onePerPerson`, "A plan's trial is already one per person (ADR 0489); onePerPerson is for things bought once");
|
|
170
|
+
}
|
|
171
|
+
if (sellable.recurring.grants) {
|
|
172
|
+
checkRecurringGrants(m, name, object, sellable.recurring.grants, `${path}.recurring.grants`, contract);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
if (sellable.onePerPerson !== undefined && !single(sellable.onePerPerson, new Set(["boolean"]))) {
|
|
176
|
+
contract(`${path}.onePerPerson`, `"${sellable.onePerPerson}" must be a boolean field of ${name}`);
|
|
177
|
+
}
|
|
178
|
+
if (sellable.maxPerOrder !== undefined && !single(sellable.maxPerOrder, new Set(["number"]))) {
|
|
179
|
+
contract(`${path}.maxPerOrder`, `"${sellable.maxPerOrder}" must be a number field of ${name}`);
|
|
180
|
+
}
|
|
181
|
+
if (sellable.deposit) {
|
|
182
|
+
const { amount, balanceDue } = sellable.deposit;
|
|
183
|
+
if (amount === undefined || balanceDue === undefined) {
|
|
184
|
+
contract(`${path}.deposit`, "A deposit names both fields: amount (the deposit for one) and balanceDue (when the rest is due)");
|
|
185
|
+
}
|
|
186
|
+
if (amount !== undefined && !single(amount, NUMERIC)) {
|
|
187
|
+
contract(`${path}.deposit.amount`, `"${amount}" must be a currency or number field of ${name}`);
|
|
188
|
+
}
|
|
189
|
+
if (balanceDue !== undefined && !single(balanceDue, DATES)) {
|
|
190
|
+
contract(`${path}.deposit.balanceDue`, `"${balanceDue}" must be a date or datetime field of ${name}`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (sellable.grants)
|
|
194
|
+
checkGrants(m, name, object, sellable.grants, `${path}.grants`, contract);
|
|
195
|
+
const place = sellable.tax?.place;
|
|
196
|
+
if (place) {
|
|
197
|
+
let holder = object;
|
|
198
|
+
let holderName = name;
|
|
199
|
+
if (place.via !== undefined) {
|
|
200
|
+
const via = fieldOf(place.via);
|
|
201
|
+
const local = via?.to?.startsWith(`${m.id}.`) ? via.to.slice(m.id.length + 1) : undefined;
|
|
202
|
+
if (!via ||
|
|
203
|
+
via.type !== "relation" ||
|
|
204
|
+
via.many ||
|
|
205
|
+
!local ||
|
|
206
|
+
!Object.hasOwn(m.objects, local)) {
|
|
207
|
+
contract(`${path}.tax.place.via`, `"${place.via}" must be a relation of ${name} to one record of another of this extension's objects`);
|
|
208
|
+
holder = undefined;
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
holder = m.objects[local];
|
|
212
|
+
holderName = local;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (holder) {
|
|
216
|
+
for (const key of ["country", "region", "postcode", "city", "line1"]) {
|
|
217
|
+
const local = place[key];
|
|
218
|
+
if (local === undefined)
|
|
219
|
+
continue;
|
|
220
|
+
const field = Object.hasOwn(holder.fields, local) ? holder.fields[local] : undefined;
|
|
221
|
+
if (!field || field.type !== "text" || field.many) {
|
|
222
|
+
contract(`${path}.tax.place.${key}`, `"${local}" must be a text field of ${holderName}`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
function checkGrants(m, name, item, grants, path, contract) {
|
|
229
|
+
const local = grants.object.startsWith(`${m.id}.`)
|
|
230
|
+
? grants.object.slice(m.id.length + 1)
|
|
231
|
+
: grants.object;
|
|
232
|
+
const granted = Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
|
|
233
|
+
if (!granted) {
|
|
234
|
+
contract(`${path}.object`, `"${grants.object}" must be another of this extension's objects`);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (local === name) {
|
|
238
|
+
contract(`${path}.object`, "An object cannot grant records of itself");
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
if (granted.sellable) {
|
|
242
|
+
contract(`${path}.object`, `${local} is itself sellable; a grant is what was bought, not more for sale`);
|
|
243
|
+
}
|
|
244
|
+
const states = granted.states;
|
|
245
|
+
if (!states) {
|
|
246
|
+
contract(`${path}.object`, `${local} must declare states, so what was granted can be voided (to "${grants.void}")`);
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
const stateField = granted.fields[states.field];
|
|
250
|
+
const values = new Set((stateField?.options ?? []).map((o) => o.value));
|
|
251
|
+
const finals = new Set(states.moves.filter((mv) => mv.final).map((mv) => mv.to));
|
|
252
|
+
if (!values.has(grants.void)) {
|
|
253
|
+
contract(`${path}.void`, `"${grants.void}" is not a state of ${local}`);
|
|
254
|
+
}
|
|
255
|
+
else if (grants.void === states.initial) {
|
|
256
|
+
contract(`${path}.void`, `"${grants.void}" is ${local}'s initial state; void is where a grant ends`);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
for (const state of values) {
|
|
260
|
+
if (state === grants.void || finals.has(state))
|
|
261
|
+
continue;
|
|
262
|
+
const moves = states.moves.some((mv) => mv.to === grants.void && moveOrigins(mv).includes(state) && mv.by.includes("host"));
|
|
263
|
+
if (!moves) {
|
|
264
|
+
contract(`${path}.void`, `${local} needs a move from "${state}" to "${grants.void}" that the host may make (by: ["host", ...])`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
if (Object.hasOwn(grants.set, states.field)) {
|
|
269
|
+
contract(`${path}.set.${states.field}`, `${states.field} is ${local}'s state: a granted record starts in "${states.initial}"`);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
const itemField = (f) => (Object.hasOwn(item.fields, f) ? item.fields[f] : undefined);
|
|
273
|
+
let codes = 0;
|
|
274
|
+
for (const [target, source] of Object.entries(grants.set)) {
|
|
275
|
+
const fp = `${path}.set.${target}`;
|
|
276
|
+
const field = Object.hasOwn(granted.fields, target) ? granted.fields[target] : undefined;
|
|
277
|
+
if (!field || field.type === "formula") {
|
|
278
|
+
contract(fp, `"${target}" must be a field of ${local} other than a formula`);
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
const want = (ok, what) => {
|
|
282
|
+
if (!ok)
|
|
283
|
+
contract(fp, `${source} needs ${what}; "${target}" is a ${field.type} field`);
|
|
284
|
+
};
|
|
285
|
+
const one = !field.many;
|
|
286
|
+
if (source === "item") {
|
|
287
|
+
want(field.type === "relation" && one && field.to === `${m.id}.${name}`, `a relation (one record) to ${m.id}.${name}`);
|
|
288
|
+
}
|
|
289
|
+
else if (source.startsWith("item.")) {
|
|
290
|
+
const from = source.slice("item.".length);
|
|
291
|
+
const own = itemField(from);
|
|
292
|
+
if (!own || own.type === "formula") {
|
|
293
|
+
contract(fp, `"${from}" is not a field of ${name}`);
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
want(own.type === field.type &&
|
|
297
|
+
Boolean(own.many) === Boolean(field.many) &&
|
|
298
|
+
(own.type !== "relation" || own.to === field.to), `a ${own.type} field${own.to ? ` to ${own.to}` : ""}, like ${name}.${from}`);
|
|
299
|
+
if (field.required && !own.required) {
|
|
300
|
+
contract(fp, `"${target}" is required and ${name}.${from} may be empty`);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
else if (source === "order.person") {
|
|
304
|
+
want(field.type === "relation" && one && field.to === "person", "a relation to one person");
|
|
305
|
+
if (field.required) {
|
|
306
|
+
contract(fp, `"${target}" cannot be required: an order may belong to no one yet`);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
else if (source === "order.email") {
|
|
310
|
+
want(field.type === "email" || field.type === "text", "an email or text field");
|
|
311
|
+
}
|
|
312
|
+
else if (source === "order" || source === "line" || source === "line.title") {
|
|
313
|
+
want(TEXT.has(field.type), "a text field");
|
|
314
|
+
}
|
|
315
|
+
else if (source === "line.unitPrice") {
|
|
316
|
+
want(NUMERIC.has(field.type), "a currency or number field");
|
|
317
|
+
}
|
|
318
|
+
else if (source === "code") {
|
|
319
|
+
codes += 1;
|
|
320
|
+
want(TEXT.has(field.type), "a text field");
|
|
321
|
+
if (!field.readOnly) {
|
|
322
|
+
contract(fp, `"${target}" holds the host's code: make it readOnly, so nobody can change it`);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
if (codes > 1)
|
|
327
|
+
contract(`${path}.set`, "A grant has at most one code");
|
|
328
|
+
checkGrantedHolder(m, local, granted, contract);
|
|
329
|
+
if (grants.valid) {
|
|
330
|
+
const { from, until, days } = grants.valid;
|
|
331
|
+
if (from === undefined && until === undefined) {
|
|
332
|
+
contract(`${path}.valid`, "valid names the granted object's from field, until field or both");
|
|
333
|
+
}
|
|
334
|
+
for (const [key, target] of [
|
|
335
|
+
["from", from],
|
|
336
|
+
["until", until],
|
|
337
|
+
]) {
|
|
338
|
+
if (target === undefined)
|
|
339
|
+
continue;
|
|
340
|
+
const field = Object.hasOwn(granted.fields, target) ? granted.fields[target] : undefined;
|
|
341
|
+
if (!field || !DATES.has(field.type) || field.many) {
|
|
342
|
+
contract(`${path}.valid.${key}`, `"${target}" must be a date or datetime field of ${local}`);
|
|
343
|
+
}
|
|
344
|
+
else if (Object.hasOwn(grants.set, target)) {
|
|
345
|
+
contract(`${path}.valid.${key}`, `"${target}" is in set too: the host writes it from valid`);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
if (days !== undefined) {
|
|
349
|
+
const own = itemField(days.slice("item.".length));
|
|
350
|
+
if (own?.type !== "number" || own.many) {
|
|
351
|
+
contract(`${path}.valid.days`, `"${days}" must be item.<a number field of ${name}>`);
|
|
352
|
+
}
|
|
353
|
+
if (until === undefined)
|
|
354
|
+
contract(`${path}.valid.days`, "days needs an until field to fill");
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
// The granted object's own rules must not be able to refuse the host's insert, since that
|
|
358
|
+
// would refuse the payment. What a grant always fills: its set fields whose source always has
|
|
359
|
+
// a value, and fields with a default.
|
|
360
|
+
const always = (source) => source === "order.person"
|
|
361
|
+
? false
|
|
362
|
+
: source.startsWith("item.")
|
|
363
|
+
? Boolean(itemField(source.slice("item.".length))?.required)
|
|
364
|
+
: true;
|
|
365
|
+
const filled = new Set([
|
|
366
|
+
...Object.entries(grants.set)
|
|
367
|
+
.filter(([, source]) => always(source))
|
|
368
|
+
.map(([field]) => field),
|
|
369
|
+
...Object.entries(granted.fields)
|
|
370
|
+
.filter(([, field]) => field.default !== undefined)
|
|
371
|
+
.map(([name]) => name),
|
|
372
|
+
]);
|
|
373
|
+
if (granted.owner !== undefined) {
|
|
374
|
+
contract(`objects.${local}.owner`, `${local} is granted by ${name}: an owner would run assignment rules in the paid batch, which could refuse it. Remove owner`);
|
|
375
|
+
}
|
|
376
|
+
granted.requireOneOf?.forEach((group, i) => {
|
|
377
|
+
if (!group.some((field) => filled.has(field))) {
|
|
378
|
+
contract(`objects.${local}.requireOneOf.${i}`, `${local} is granted by ${name}: set one of ${group.join(", ")} in grants.set from a source that always has a value`);
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
for (const [fieldName, field] of Object.entries(granted.fields)) {
|
|
382
|
+
if (field.type !== "select")
|
|
383
|
+
continue;
|
|
384
|
+
const source = grants.set[fieldName];
|
|
385
|
+
// The choices a granted record can hold when it is created.
|
|
386
|
+
const values = source?.startsWith("item.") === true
|
|
387
|
+
? (field.options ?? []).map((o) => o.value)
|
|
388
|
+
: states && fieldName === states.field
|
|
389
|
+
? [states.initial]
|
|
390
|
+
: field.default !== undefined
|
|
391
|
+
? [String(field.default)]
|
|
392
|
+
: [];
|
|
393
|
+
for (const option of field.options ?? []) {
|
|
394
|
+
if (!values.includes(option.value))
|
|
395
|
+
continue;
|
|
396
|
+
const missing = (option.requires ?? []).filter((required) => !filled.has(required));
|
|
397
|
+
if (missing.length) {
|
|
398
|
+
contract(`objects.${local}.fields.${fieldName}.options`, `${local} is granted by ${name}: "${option.value}" needs ${missing.join(", ")}, which grants.set does not always fill`);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
for (const [fieldName, field] of Object.entries(granted.fields)) {
|
|
403
|
+
if (!field.required || field.type === "formula" || field.default !== undefined)
|
|
404
|
+
continue;
|
|
405
|
+
if (states && fieldName === states.field)
|
|
406
|
+
continue;
|
|
407
|
+
if (!Object.hasOwn(grants.set, fieldName)) {
|
|
408
|
+
contract(`${path}.set`, `${local}.${fieldName} is required: set it from the item, the order or the line`);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* Checks what a plan's subscription grants (ADR 0510), as `checkGrants` checks an order line's:
|
|
414
|
+
* each of these is a way the verified event's batch could otherwise fail, which would leave the
|
|
415
|
+
* provider's event unapplied.
|
|
416
|
+
*/
|
|
417
|
+
function checkRecurringGrants(m, name, plan, grants, path, contract) {
|
|
418
|
+
const local = grants.object.startsWith(`${m.id}.`)
|
|
419
|
+
? grants.object.slice(m.id.length + 1)
|
|
420
|
+
: grants.object;
|
|
421
|
+
const granted = Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
|
|
422
|
+
if (!granted || local === name) {
|
|
423
|
+
contract(`${path}.object`, `"${grants.object}" must be another of this extension's objects`);
|
|
424
|
+
return;
|
|
425
|
+
}
|
|
426
|
+
if (granted.sellable) {
|
|
427
|
+
contract(`${path}.object`, `${local} is itself sellable; a grant is what was bought`);
|
|
428
|
+
}
|
|
429
|
+
if (granted.owner !== undefined) {
|
|
430
|
+
contract(`objects.${local}.owner`, `${local} is granted by ${name}'s subscriptions: an owner would run assignment rules in the provider's event batch. Remove owner`);
|
|
431
|
+
}
|
|
432
|
+
const states = granted.states;
|
|
433
|
+
if (!states) {
|
|
434
|
+
contract(`${path}.object`, `${local} must declare states: its state follows the subscription's`);
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
const stateField = granted.fields[states.field];
|
|
438
|
+
const values = new Set((stateField?.options ?? []).map((o) => o.value));
|
|
439
|
+
const mapped = new Set(Object.values(grants.states));
|
|
440
|
+
for (const [from, to] of Object.entries(grants.states)) {
|
|
441
|
+
if (!values.has(to))
|
|
442
|
+
contract(`${path}.states.${from}`, `"${to}" is not a state of ${local}`);
|
|
443
|
+
}
|
|
444
|
+
if (!mapped.has(states.initial)) {
|
|
445
|
+
contract(`${path}.states`, `${local}'s initial state "${states.initial}" must be one the subscription's states map to`);
|
|
446
|
+
}
|
|
447
|
+
const ended = grants.states.ended;
|
|
448
|
+
const finals = new Set(states.moves.filter((mv) => mv.final).map((mv) => mv.to));
|
|
449
|
+
if (!finals.has(ended)) {
|
|
450
|
+
contract(`${path}.states.ended`, `"${ended}" must be a final state of ${local}`);
|
|
451
|
+
}
|
|
452
|
+
// The provider's reads move a subscription between its states in any order, so the granted
|
|
453
|
+
// record needs a host move from each mapped state to each other one (none out of the final).
|
|
454
|
+
for (const fromState of mapped) {
|
|
455
|
+
if (fromState === ended)
|
|
456
|
+
continue;
|
|
457
|
+
for (const toState of mapped) {
|
|
458
|
+
if (toState === fromState)
|
|
459
|
+
continue;
|
|
460
|
+
const ok = states.moves.some((mv) => mv.to === toState && moveOrigins(mv).includes(fromState) && mv.by.includes("host"));
|
|
461
|
+
if (!ok) {
|
|
462
|
+
contract(`objects.${local}.states`, `${local} needs a move from "${fromState}" to "${toState}" the host may make (by: ["host", ...]): it follows the subscription`);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
if (Object.hasOwn(grants.set, states.field)) {
|
|
467
|
+
contract(`${path}.set.${states.field}`, `${states.field} is ${local}'s state: the host sets it`);
|
|
468
|
+
}
|
|
469
|
+
const filled = new Set();
|
|
470
|
+
for (const [key, target] of [
|
|
471
|
+
["from", grants.from],
|
|
472
|
+
["until", grants.until],
|
|
473
|
+
]) {
|
|
474
|
+
if (target === undefined)
|
|
475
|
+
continue;
|
|
476
|
+
const field = Object.hasOwn(granted.fields, target) ? granted.fields[target] : undefined;
|
|
477
|
+
// A moment, not a day: access stops at the period's end and not a minute after (ADR 0488).
|
|
478
|
+
if (field?.type !== "datetime" || field.many) {
|
|
479
|
+
contract(`${path}.${key}`, `"${target}" must be a datetime field of ${local}`);
|
|
480
|
+
}
|
|
481
|
+
else if (Object.hasOwn(grants.set, target)) {
|
|
482
|
+
contract(`${path}.${key}`, `"${target}" is in set too: the host keeps it`);
|
|
483
|
+
}
|
|
484
|
+
if (key === "from")
|
|
485
|
+
filled.add(target);
|
|
486
|
+
}
|
|
487
|
+
for (const [target, source] of Object.entries(grants.set)) {
|
|
488
|
+
const fp = `${path}.set.${target}`;
|
|
489
|
+
const field = Object.hasOwn(granted.fields, target) ? granted.fields[target] : undefined;
|
|
490
|
+
if (!field || field.type === "formula") {
|
|
491
|
+
contract(fp, `"${target}" must be a field of ${local} other than a formula`);
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
const one = !field.many;
|
|
495
|
+
let always = true;
|
|
496
|
+
if (source === "item") {
|
|
497
|
+
if (!(field.type === "relation" && one && field.to === `${m.id}.${name}`)) {
|
|
498
|
+
contract(fp, `item needs a relation (one record) to ${m.id}.${name}`);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
else if (source.startsWith("item.")) {
|
|
502
|
+
const from = source.slice("item.".length);
|
|
503
|
+
const own = Object.hasOwn(plan.fields, from) ? plan.fields[from] : undefined;
|
|
504
|
+
if (!own || own.type === "formula") {
|
|
505
|
+
contract(fp, `"${from}" is not a field of ${name}`);
|
|
506
|
+
continue;
|
|
507
|
+
}
|
|
508
|
+
if (own.type !== field.type ||
|
|
509
|
+
Boolean(own.many) !== Boolean(field.many) ||
|
|
510
|
+
(own.type === "relation" && own.to !== field.to)) {
|
|
511
|
+
contract(fp, `${source} needs a ${own.type} field like ${name}.${from}; "${target}" is a ${field.type} field`);
|
|
512
|
+
}
|
|
513
|
+
always = Boolean(own.required);
|
|
514
|
+
}
|
|
515
|
+
else if (source === "subscription") {
|
|
516
|
+
if (field.type !== "text") {
|
|
517
|
+
contract(fp, `subscription needs a text field; "${target}" is a ${field.type} field`);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
else if (!(field.type === "relation" && one && field.to === "person")) {
|
|
521
|
+
contract(fp, `${source} needs a relation to one person; "${target}" is a ${field.type} field`);
|
|
522
|
+
}
|
|
523
|
+
else if (source === "subscription.payer") {
|
|
524
|
+
// A payer erased later leaves the field empty; it never fills a required one.
|
|
525
|
+
always = false;
|
|
526
|
+
}
|
|
527
|
+
if (field.required && !always) {
|
|
528
|
+
contract(fp, `"${target}" is required and ${source} may be empty`);
|
|
529
|
+
}
|
|
530
|
+
if (always)
|
|
531
|
+
filled.add(target);
|
|
532
|
+
}
|
|
533
|
+
for (const [fieldName, field] of Object.entries(granted.fields)) {
|
|
534
|
+
if (!field.required || field.type === "formula" || field.default !== undefined)
|
|
535
|
+
continue;
|
|
536
|
+
if (fieldName === states.field || filled.has(fieldName))
|
|
537
|
+
continue;
|
|
538
|
+
contract(`${path}.set`, `${local}.${fieldName} is required: set it from the plan or the subscription`);
|
|
539
|
+
}
|
|
540
|
+
granted.requireOneOf?.forEach((group, i) => {
|
|
541
|
+
if (!group.some((f) => filled.has(f))) {
|
|
542
|
+
contract(`objects.${local}.requireOneOf.${i}`, `${local} is granted by ${name}'s subscriptions: set one of ${group.join(", ")}`);
|
|
543
|
+
}
|
|
544
|
+
});
|
|
545
|
+
checkGrantedHolder(m, local, granted, contract);
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* A granted holder (a pass a paid line grants, a membership a subscription grants) keeps the
|
|
549
|
+
* terms it was granted (ADR 0513): its Person, uses, period, dates, zone and the fields its
|
|
550
|
+
* conditions compare with are read-only and never a customer's to write, and nobody but the host
|
|
551
|
+
* (or a tool only a person with a permission may call) moves it into a state it can be spent in.
|
|
552
|
+
* Otherwise a staff write, an assistant or an automation could give someone uses nobody paid for.
|
|
553
|
+
*/
|
|
554
|
+
export function checkGrantedHolder(m, local, granted, contract) {
|
|
555
|
+
const allowance = granted.allowance;
|
|
556
|
+
if (!allowance || !("holds" in allowance))
|
|
557
|
+
return;
|
|
558
|
+
const h = allowance.holds;
|
|
559
|
+
const op = `objects.${local}`;
|
|
560
|
+
const terms = new Set([h.person, h.uses]);
|
|
561
|
+
if (typeof h.per === "object")
|
|
562
|
+
terms.add(h.per.field);
|
|
563
|
+
for (const f of [h.from, h.until, h.zone])
|
|
564
|
+
if (f)
|
|
565
|
+
terms.add(f);
|
|
566
|
+
for (const target of spendsOnList(h)) {
|
|
567
|
+
for (const clause of target.when ?? []) {
|
|
568
|
+
for (const v of [clause.in, clause.atLeast, clause.below]) {
|
|
569
|
+
if (typeof v === "string")
|
|
570
|
+
terms.add(v);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
const customer = customerWritable(m, local);
|
|
575
|
+
for (const term of terms) {
|
|
576
|
+
const field = Object.hasOwn(granted.fields, term) ? granted.fields[term] : undefined;
|
|
577
|
+
if (field && !field.readOnly) {
|
|
578
|
+
contract(`${op}.fields.${term}`, `${local} is granted: "${term}" is one of its terms, so it must be readOnly (only the host writes it)`);
|
|
579
|
+
}
|
|
580
|
+
if (customer.has(term) && term !== granted.customer?.field) {
|
|
581
|
+
contract(`${op}.fields.${term}`, `${local} is granted: a customer cannot write "${term}"`);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
const live = new Set(h.live ?? []);
|
|
585
|
+
for (const [i, move] of (granted.states?.moves ?? []).entries()) {
|
|
586
|
+
if (!live.has(move.to))
|
|
587
|
+
continue;
|
|
588
|
+
const open = move.by.filter((by) => by === "staff" || by === "customer");
|
|
589
|
+
if (open.length) {
|
|
590
|
+
contract(`${op}.states.moves.${i}`, `${local} is granted: only the host moves it into "${move.to}", where it can be spent (not ${open.join(" or ")})`);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
}
|
package/dist/slug.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** What a slug may be. */
|
|
2
|
+
export declare const SLUG_RE: RegExp;
|
|
3
|
+
/** The longest a slug may be. */
|
|
4
|
+
export declare const SLUG_MAX = 120;
|
|
5
|
+
/** The longest slug made from a title, leaving room for a `-<n>` that tells two apart. */
|
|
6
|
+
export declare const SLUG_FROM_TITLE_MAX = 80;
|
|
7
|
+
/**
|
|
8
|
+
* A slug made from a title: accents dropped, every run of anything that is not a letter or digit
|
|
9
|
+
* a hyphen, at most SLUG_FROM_TITLE_MAX characters. Empty when nothing is left.
|
|
10
|
+
*/
|
|
11
|
+
export declare function slugify(title: string): string;
|
package/dist/slug.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// The `slug` field type (stage 28, ADR 0442): a record's address in its object, such as
|
|
2
|
+
// `summer-launch` in `/blog/summer-launch`. Lowercase letters and digits in words joined by single
|
|
3
|
+
// hyphens. The host keeps it unique among the object's live records and makes one from the title
|
|
4
|
+
// when a record is created without one.
|
|
5
|
+
/** What a slug may be. */
|
|
6
|
+
export const SLUG_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
7
|
+
/** The longest a slug may be. */
|
|
8
|
+
export const SLUG_MAX = 120;
|
|
9
|
+
/** The longest slug made from a title, leaving room for a `-<n>` that tells two apart. */
|
|
10
|
+
export const SLUG_FROM_TITLE_MAX = 80;
|
|
11
|
+
/**
|
|
12
|
+
* A slug made from a title: accents dropped, every run of anything that is not a letter or digit
|
|
13
|
+
* a hyphen, at most SLUG_FROM_TITLE_MAX characters. Empty when nothing is left.
|
|
14
|
+
*/
|
|
15
|
+
export function slugify(title) {
|
|
16
|
+
const plain = title
|
|
17
|
+
.normalize("NFKD")
|
|
18
|
+
.replace(/[̀-ͯ]/g, "")
|
|
19
|
+
.toLowerCase()
|
|
20
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
21
|
+
.replace(/^-+|-+$/g, "");
|
|
22
|
+
return plain.slice(0, SLUG_FROM_TITLE_MAX).replace(/-+$/g, "");
|
|
23
|
+
}
|