@composable-os/sdk 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/allowance.d.ts +192 -0
- package/dist/allowance.js +365 -0
- package/dist/capacity.d.ts +26 -0
- package/dist/capacity.js +59 -0
- package/dist/contract.d.ts +92 -1
- package/dist/document.d.ts +52 -6
- package/dist/document.js +101 -11
- package/dist/field-types.d.ts +1 -1
- package/dist/field-types.js +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/manifest.d.ts +356 -17
- package/dist/manifest.js +179 -5
- package/dist/media.d.ts +32 -0
- package/dist/media.js +47 -0
- package/dist/recurring.d.ts +114 -0
- package/dist/recurring.js +166 -0
- package/dist/rich-text.d.ts +16 -2
- package/dist/rich-text.js +50 -4
- package/dist/sellable.d.ts +62 -0
- package/dist/sellable.js +70 -0
- package/dist/states.d.ts +94 -2
- package/dist/states.js +109 -1
- package/dist/unique.d.ts +20 -0
- package/dist/unique.js +58 -0
- package/package.json +1 -1
- package/src/allowance.ts +508 -0
- package/src/capacity.ts +89 -0
- package/src/contract.ts +97 -1
- package/src/document.ts +161 -14
- package/src/field-types.ts +5 -0
- package/src/index.ts +3 -0
- package/src/manifest.ts +206 -5
- package/src/media.ts +62 -0
- package/src/recurring.ts +314 -0
- package/src/rich-text.ts +52 -4
- package/src/sellable.ts +120 -0
- package/src/states.ts +162 -1
- package/src/unique.ts +81 -0
package/src/rich-text.ts
CHANGED
|
@@ -62,7 +62,12 @@ const blocks: Record<RichTextBlockType, DocumentObjectNode> = {
|
|
|
62
62
|
list: listShape(),
|
|
63
63
|
quote: object({ content: inlines, attribution: optional(prose(300)) }),
|
|
64
64
|
image: object({
|
|
65
|
-
media: {
|
|
65
|
+
media: {
|
|
66
|
+
type: "ref",
|
|
67
|
+
to: RICH_TEXT_MEDIA_OBJECT,
|
|
68
|
+
media: "image",
|
|
69
|
+
description: "The picture, from media",
|
|
70
|
+
},
|
|
66
71
|
alt: prose(500, { description: "What the picture shows, for people who cannot see it" }),
|
|
67
72
|
caption: optional(inlines),
|
|
68
73
|
}),
|
|
@@ -129,7 +134,12 @@ export const RICH_TEXT_SCHEMA: DocumentSchema = {
|
|
|
129
134
|
*/
|
|
130
135
|
export const IMAGE_SCHEMA: DocumentSchema = {
|
|
131
136
|
root: object({
|
|
132
|
-
media: {
|
|
137
|
+
media: {
|
|
138
|
+
type: "ref",
|
|
139
|
+
to: RICH_TEXT_MEDIA_OBJECT,
|
|
140
|
+
media: "image",
|
|
141
|
+
description: "The picture, from media",
|
|
142
|
+
},
|
|
133
143
|
alt: prose(500, { description: "What the picture shows, for people who cannot see it" }),
|
|
134
144
|
focal: optional(
|
|
135
145
|
object({
|
|
@@ -143,9 +153,45 @@ export const IMAGE_SCHEMA: DocumentSchema = {
|
|
|
143
153
|
/** The most an image field's value may be, in bytes of its JSON. */
|
|
144
154
|
export const IMAGE_MAX_BYTES = 2048;
|
|
145
155
|
|
|
146
|
-
/**
|
|
156
|
+
/**
|
|
157
|
+
* A video from the media library (stage 42, ADR 0475), as a node a declared document may use: the
|
|
158
|
+
* video (a ready one, checked on write) and, optionally, a picture to show before it plays.
|
|
159
|
+
*/
|
|
160
|
+
export const VIDEO_NODE: DocumentObjectNode = object({
|
|
161
|
+
media: {
|
|
162
|
+
type: "ref",
|
|
163
|
+
to: RICH_TEXT_MEDIA_OBJECT,
|
|
164
|
+
media: "video",
|
|
165
|
+
description: "The video, from media (a ready one)",
|
|
166
|
+
},
|
|
167
|
+
poster: optional({
|
|
168
|
+
type: "ref",
|
|
169
|
+
to: RICH_TEXT_MEDIA_OBJECT,
|
|
170
|
+
media: "image",
|
|
171
|
+
description: "A picture shown before it plays",
|
|
172
|
+
}),
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
/** A downloadable file from the media library (a PDF), as a node a declared document may use. */
|
|
176
|
+
export const FILE_NODE: DocumentNode = {
|
|
177
|
+
type: "ref",
|
|
178
|
+
to: RICH_TEXT_MEDIA_OBJECT,
|
|
179
|
+
media: "file",
|
|
180
|
+
description: "The file, from media",
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/** The `video` field type (stage 42, ADR 0475): `{ media, poster? }`. */
|
|
184
|
+
export const VIDEO_SCHEMA: DocumentSchema = { root: VIDEO_NODE };
|
|
185
|
+
|
|
186
|
+
/** The most a video field's value may be, in bytes of its JSON. */
|
|
187
|
+
export const VIDEO_MAX_BYTES = 1024;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* The field types whose value is a document (ADR 0445): `document`, `richText`, `image` and
|
|
191
|
+
* `video`.
|
|
192
|
+
*/
|
|
147
193
|
export function isDocumentType(type: string | undefined): boolean {
|
|
148
|
-
return type === "document" || type === "richText" || type === "image";
|
|
194
|
+
return type === "document" || type === "richText" || type === "image" || type === "video";
|
|
149
195
|
}
|
|
150
196
|
|
|
151
197
|
/**
|
|
@@ -158,6 +204,7 @@ export function documentSchemaOf(field: {
|
|
|
158
204
|
}): DocumentSchema | undefined {
|
|
159
205
|
if (field.type === "richText") return RICH_TEXT_SCHEMA;
|
|
160
206
|
if (field.type === "image") return IMAGE_SCHEMA;
|
|
207
|
+
if (field.type === "video") return VIDEO_SCHEMA;
|
|
161
208
|
if (field.type === "document") return field.schema as DocumentSchema | undefined;
|
|
162
209
|
return undefined;
|
|
163
210
|
}
|
|
@@ -168,6 +215,7 @@ export function documentMaxBytesOf(field: {
|
|
|
168
215
|
maxBytes?: number | undefined;
|
|
169
216
|
}): number | undefined {
|
|
170
217
|
if (field.type === "image") return IMAGE_MAX_BYTES;
|
|
218
|
+
if (field.type === "video") return VIDEO_MAX_BYTES;
|
|
171
219
|
if (field.maxBytes !== undefined) return field.maxBytes;
|
|
172
220
|
return field.type === "richText" ? RICH_TEXT_DEFAULT_BYTES : undefined;
|
|
173
221
|
}
|
package/src/sellable.ts
CHANGED
|
@@ -73,6 +73,11 @@ export interface GrantSpec {
|
|
|
73
73
|
* - `deposit`: paying by deposit (stage 25): the field holding the deposit for one, and the date
|
|
74
74
|
* field saying when the balance is due. A record offers a deposit when both have a value.
|
|
75
75
|
* - `grants`: what a paid line grants (see GrantSpec).
|
|
76
|
+
* - `tax`: tax at checkout (ADR 0479), see TaxSpec. A line is taxed only when this is declared and
|
|
77
|
+
* a human admin has turned tax on for the deployment.
|
|
78
|
+
* - `recurring`: a plan, paid for every period (stage 32a, ADR 0485), see RecurringSpec. Its records
|
|
79
|
+
* are subscribed to, never put on an order: `price` is the price of one period. Not with
|
|
80
|
+
* `deposit`, `maxPerOrder` or `grants` (32b adds what a subscription grants).
|
|
76
81
|
*/
|
|
77
82
|
export interface SellableSpec {
|
|
78
83
|
price: string;
|
|
@@ -81,8 +86,49 @@ export interface SellableSpec {
|
|
|
81
86
|
maxPerOrder?: string;
|
|
82
87
|
deposit?: { amount: string; balanceDue: string };
|
|
83
88
|
grants?: GrantSpec;
|
|
89
|
+
tax?: TaxSpec;
|
|
90
|
+
recurring?: RecurringSpec;
|
|
84
91
|
}
|
|
85
92
|
|
|
93
|
+
/**
|
|
94
|
+
* How a plan recurs (ADR 0485). The payment provider charges every `interval` on its own schedule;
|
|
95
|
+
* the host learns of each paid period from its verified events and writes a paid order for it.
|
|
96
|
+
*
|
|
97
|
+
* - `interval`: `week`, `month` or `year`.
|
|
98
|
+
* - `trialDays`: a number field of the plan holding the days of a free first period (the card is
|
|
99
|
+
* taken at the start, the first charge is when it ends). Empty or zero: no trial.
|
|
100
|
+
*/
|
|
101
|
+
export interface RecurringSpec {
|
|
102
|
+
interval: "week" | "month" | "year";
|
|
103
|
+
trialDays?: string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* How the payment provider taxes a line of this object (ADR 0479). Prices stay what the buyer
|
|
108
|
+
* pays: the provider works the tax out inside them.
|
|
109
|
+
*
|
|
110
|
+
* - `code`: the provider's product tax code (Stripe's list: `txcd_` and eight digits). A human
|
|
111
|
+
* admin may override it per object in Settings → Orders.
|
|
112
|
+
* - `place`: for admission to an in-person event, where it is held, so the provider taxes it
|
|
113
|
+
* there and not where the buyer lives. Text fields of this object, or of the record its
|
|
114
|
+
* relation `via` points at (a ticket type's event): `country` (two letters, ISO 3166-1) and,
|
|
115
|
+
* when given, `region`, `postcode`, `city` and `line1`.
|
|
116
|
+
*/
|
|
117
|
+
export interface TaxSpec {
|
|
118
|
+
code: string;
|
|
119
|
+
place?: {
|
|
120
|
+
via?: string;
|
|
121
|
+
country: string;
|
|
122
|
+
region?: string;
|
|
123
|
+
postcode?: string;
|
|
124
|
+
city?: string;
|
|
125
|
+
line1?: string;
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** A product tax code as Stripe's list has them. */
|
|
130
|
+
export const TAX_CODE_PATTERN = /^txcd_\d{8}$/;
|
|
131
|
+
|
|
86
132
|
const fieldName = z.string().max(40);
|
|
87
133
|
|
|
88
134
|
export const GRANT_SOURCES = [
|
|
@@ -125,6 +171,30 @@ export const sellableManifestSchema = z
|
|
|
125
171
|
})
|
|
126
172
|
.strict()
|
|
127
173
|
.optional(),
|
|
174
|
+
tax: z
|
|
175
|
+
.object({
|
|
176
|
+
code: z.string().regex(TAX_CODE_PATTERN, "A product tax code: txcd_ and eight digits"),
|
|
177
|
+
place: z
|
|
178
|
+
.object({
|
|
179
|
+
via: fieldName.optional(),
|
|
180
|
+
country: fieldName,
|
|
181
|
+
region: fieldName.optional(),
|
|
182
|
+
postcode: fieldName.optional(),
|
|
183
|
+
city: fieldName.optional(),
|
|
184
|
+
line1: fieldName.optional(),
|
|
185
|
+
})
|
|
186
|
+
.strict()
|
|
187
|
+
.optional(),
|
|
188
|
+
})
|
|
189
|
+
.strict()
|
|
190
|
+
.optional(),
|
|
191
|
+
recurring: z
|
|
192
|
+
.object({
|
|
193
|
+
interval: z.enum(["week", "month", "year"]),
|
|
194
|
+
trialDays: fieldName.optional(),
|
|
195
|
+
})
|
|
196
|
+
.strict()
|
|
197
|
+
.optional(),
|
|
128
198
|
})
|
|
129
199
|
.strict();
|
|
130
200
|
export type SellableManifest = z.infer<typeof sellableManifestSchema>;
|
|
@@ -178,6 +248,21 @@ export function checkObjectSellable(
|
|
|
178
248
|
}
|
|
179
249
|
}
|
|
180
250
|
}
|
|
251
|
+
if (sellable.recurring) {
|
|
252
|
+
// A plan is subscribed to, not put on an order (ADR 0485).
|
|
253
|
+
for (const key of ["deposit", "maxPerOrder", "grants"] as const) {
|
|
254
|
+
if (sellable[key] !== undefined) {
|
|
255
|
+
contract(
|
|
256
|
+
`${path}.${key}`,
|
|
257
|
+
`A recurring plan has no ${key}: it is subscribed to, one at a time`,
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
const trial = sellable.recurring.trialDays;
|
|
262
|
+
if (trial !== undefined && !single(trial, new Set(["number"]))) {
|
|
263
|
+
contract(`${path}.recurring.trialDays`, `"${trial}" must be a number field of ${name}`);
|
|
264
|
+
}
|
|
265
|
+
}
|
|
181
266
|
if (sellable.maxPerOrder !== undefined && !single(sellable.maxPerOrder, new Set(["number"]))) {
|
|
182
267
|
contract(`${path}.maxPerOrder`, `"${sellable.maxPerOrder}" must be a number field of ${name}`);
|
|
183
268
|
}
|
|
@@ -203,6 +288,41 @@ export function checkObjectSellable(
|
|
|
203
288
|
}
|
|
204
289
|
}
|
|
205
290
|
if (sellable.grants) checkGrants(m, name, object, sellable.grants, `${path}.grants`, contract);
|
|
291
|
+
const place = sellable.tax?.place;
|
|
292
|
+
if (place) {
|
|
293
|
+
let holder: ObjectManifest | undefined = object;
|
|
294
|
+
let holderName = name;
|
|
295
|
+
if (place.via !== undefined) {
|
|
296
|
+
const via = fieldOf(place.via);
|
|
297
|
+
const local = via?.to?.startsWith(`${m.id}.`) ? via.to.slice(m.id.length + 1) : undefined;
|
|
298
|
+
if (
|
|
299
|
+
!via ||
|
|
300
|
+
via.type !== "relation" ||
|
|
301
|
+
via.many ||
|
|
302
|
+
!local ||
|
|
303
|
+
!Object.hasOwn(m.objects, local)
|
|
304
|
+
) {
|
|
305
|
+
contract(
|
|
306
|
+
`${path}.tax.place.via`,
|
|
307
|
+
`"${place.via}" must be a relation of ${name} to one record of another of this extension's objects`,
|
|
308
|
+
);
|
|
309
|
+
holder = undefined;
|
|
310
|
+
} else {
|
|
311
|
+
holder = m.objects[local];
|
|
312
|
+
holderName = local;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (holder) {
|
|
316
|
+
for (const key of ["country", "region", "postcode", "city", "line1"] as const) {
|
|
317
|
+
const local = place[key];
|
|
318
|
+
if (local === undefined) continue;
|
|
319
|
+
const field = Object.hasOwn(holder.fields, local) ? holder.fields[local] : undefined;
|
|
320
|
+
if (!field || field.type !== "text" || field.many) {
|
|
321
|
+
contract(`${path}.tax.place.${key}`, `"${local}" must be a text field of ${holderName}`);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
206
326
|
}
|
|
207
327
|
|
|
208
328
|
function checkGrants(
|
package/src/states.ts
CHANGED
|
@@ -26,6 +26,37 @@ export interface StateMove {
|
|
|
26
26
|
requires?: readonly string[];
|
|
27
27
|
/** The move ends the lifecycle: nothing moves out of `to`. */
|
|
28
28
|
final?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* When a customer may make it (stage 31, ADR 0503): from `from` (inclusive) until `until`
|
|
31
|
+
* (exclusive), by the host's clock in the write. Staff and the host are not bound.
|
|
32
|
+
*/
|
|
33
|
+
window?: MoveWindow;
|
|
34
|
+
/**
|
|
35
|
+
* A boolean field (named as a window's `at` is): while it is true, only staff and the host may
|
|
36
|
+
* make the move ("this class type is booked by the gym").
|
|
37
|
+
*/
|
|
38
|
+
staffOnly?: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* One end of a window (ADR 0503): the moment in the datetime field `at` (a field of the record,
|
|
43
|
+
* or `<relation>.<field>` on the record a relation of one record points to), less `daysBefore`
|
|
44
|
+
* whole days at the same local time in `zone` (a timezone field named the same way; the
|
|
45
|
+
* organisation's zone when left out) and then `minutesBefore` minutes. Each of the two is a
|
|
46
|
+
* number, or a number field named as `at` is. A string is short for `{ at }`. When any of them
|
|
47
|
+
* has no value the window cannot be worked out, and a customer's move is refused.
|
|
48
|
+
*/
|
|
49
|
+
export interface WindowBound {
|
|
50
|
+
at: string;
|
|
51
|
+
daysBefore?: number | string;
|
|
52
|
+
minutesBefore?: number | string;
|
|
53
|
+
zone?: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A window on a move or a create (ADR 0503). */
|
|
57
|
+
export interface MoveWindow {
|
|
58
|
+
from?: string | WindowBound;
|
|
59
|
+
until?: string | WindowBound;
|
|
29
60
|
}
|
|
30
61
|
|
|
31
62
|
/**
|
|
@@ -46,10 +77,46 @@ export interface StatesSpec {
|
|
|
46
77
|
/** The state a new record starts in. */
|
|
47
78
|
initial: string;
|
|
48
79
|
moves: readonly StateMove[];
|
|
80
|
+
/**
|
|
81
|
+
* Rules on every create, whatever state the record is created in (ADR 0503): `in`, the states
|
|
82
|
+
* a record may be created in besides `initial`, each with who may create it there (it is then
|
|
83
|
+
* not judged as a move from `initial`: a booking is created booked or waiting by a customer,
|
|
84
|
+
* pending only by the host); the `window` in which a customer
|
|
85
|
+
* may create one; and `staffOnly`, a boolean field while true only staff and the host create.
|
|
86
|
+
* Named as a move's are.
|
|
87
|
+
*/
|
|
88
|
+
create?: {
|
|
89
|
+
in?: Readonly<Record<string, readonly Mover[]>>;
|
|
90
|
+
window?: MoveWindow;
|
|
91
|
+
staffOnly?: string;
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Moves a caller asks for by name, resolved by the host (ADR 0503): writing the name (`cancel`)
|
|
95
|
+
* into the state field moves the record to the first of the listed states whose move from where
|
|
96
|
+
* it is the caller may make now, windows included (`["cancelled", "late_cancelled"]`). A name
|
|
97
|
+
* is not a state.
|
|
98
|
+
*/
|
|
99
|
+
requests?: Readonly<Record<string, readonly string[]>>;
|
|
49
100
|
}
|
|
50
101
|
|
|
51
102
|
const fieldName = z.string().max(40);
|
|
52
103
|
const stateValue = z.string().min(1).max(100);
|
|
104
|
+
const fieldPath = z.string().min(1).max(81);
|
|
105
|
+
const windowBound = z.union([
|
|
106
|
+
fieldPath,
|
|
107
|
+
z
|
|
108
|
+
.object({
|
|
109
|
+
at: fieldPath,
|
|
110
|
+
daysBefore: z.union([z.number().int().min(-366).max(366), fieldPath]).optional(),
|
|
111
|
+
minutesBefore: z.union([z.number().int().min(-527_040).max(527_040), fieldPath]).optional(),
|
|
112
|
+
zone: fieldPath.optional(),
|
|
113
|
+
})
|
|
114
|
+
.strict(),
|
|
115
|
+
]);
|
|
116
|
+
const moveWindow = z
|
|
117
|
+
.object({ from: windowBound.optional(), until: windowBound.optional() })
|
|
118
|
+
.strict()
|
|
119
|
+
.refine((w) => w.from !== undefined || w.until !== undefined, "A window needs from or until");
|
|
53
120
|
|
|
54
121
|
export const statesManifestSchema = z.object({
|
|
55
122
|
field: fieldName,
|
|
@@ -62,9 +129,26 @@ export const statesManifestSchema = z.object({
|
|
|
62
129
|
by: z.array(z.enum(MOVERS)).min(1).max(MOVERS.length),
|
|
63
130
|
requires: z.array(fieldName).max(50).optional(),
|
|
64
131
|
final: z.boolean().optional(),
|
|
132
|
+
window: moveWindow.optional(),
|
|
133
|
+
staffOnly: fieldPath.optional(),
|
|
65
134
|
}),
|
|
66
135
|
)
|
|
67
136
|
.max(200),
|
|
137
|
+
create: z
|
|
138
|
+
.object({
|
|
139
|
+
in: z
|
|
140
|
+
.record(stateValue, z.array(z.enum(MOVERS)).min(1).max(MOVERS.length))
|
|
141
|
+
.refine((r) => Object.keys(r).length <= 20, "At most 20 states")
|
|
142
|
+
.optional(),
|
|
143
|
+
window: moveWindow.optional(),
|
|
144
|
+
staffOnly: fieldPath.optional(),
|
|
145
|
+
})
|
|
146
|
+
.strict()
|
|
147
|
+
.optional(),
|
|
148
|
+
requests: z
|
|
149
|
+
.record(z.string().regex(/^[a-z][a-z0-9_]{0,39}$/), z.array(stateValue).min(1).max(10))
|
|
150
|
+
.refine((r) => Object.keys(r).length <= 10, "At most 10 requests")
|
|
151
|
+
.optional(),
|
|
68
152
|
});
|
|
69
153
|
export type StatesManifest = z.infer<typeof statesManifestSchema>;
|
|
70
154
|
|
|
@@ -75,7 +159,7 @@ export function moveOrigins(move: { from: string | readonly string[] }): readonl
|
|
|
75
159
|
|
|
76
160
|
/** Reports problems with `objects.<name>.states` through `contract`. */
|
|
77
161
|
export function checkObjectStates(
|
|
78
|
-
|
|
162
|
+
m: ExtensionManifest,
|
|
79
163
|
name: string,
|
|
80
164
|
object: ObjectManifest,
|
|
81
165
|
contract: (path: string, message: string) => void,
|
|
@@ -159,4 +243,81 @@ export function checkObjectStates(
|
|
|
159
243
|
}
|
|
160
244
|
}
|
|
161
245
|
});
|
|
246
|
+
|
|
247
|
+
// Windows, staff-only rules and requests (ADR 0503).
|
|
248
|
+
const checkPath = (at: string, path: string, type: string) => {
|
|
249
|
+
const field = resolveFieldPath(m, object, at);
|
|
250
|
+
if (field?.type !== type || field.many) {
|
|
251
|
+
contract(
|
|
252
|
+
path,
|
|
253
|
+
`"${at}" must be a ${type} field of ${name}, or <relation>.<field> through a relation (one record) to another of this extension's objects`,
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
const checkWindow = (window: MoveWindow | undefined, path: string) => {
|
|
258
|
+
for (const end of ["from", "until"] as const) {
|
|
259
|
+
const bound = window?.[end];
|
|
260
|
+
if (bound === undefined) continue;
|
|
261
|
+
const b = typeof bound === "string" ? { at: bound } : bound;
|
|
262
|
+
checkPath(b.at, `${path}.${end}.at`, "datetime");
|
|
263
|
+
for (const key of ["daysBefore", "minutesBefore"] as const) {
|
|
264
|
+
const value = b[key];
|
|
265
|
+
if (typeof value === "string") checkPath(value, `${path}.${end}.${key}`, "number");
|
|
266
|
+
}
|
|
267
|
+
if (b.zone !== undefined) checkPath(b.zone, `${path}.${end}.zone`, "timezone");
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
states.moves.forEach((move, i) => {
|
|
271
|
+
const mp = `${sp}.moves.${i}`;
|
|
272
|
+
if ((move.window || move.staffOnly) && !move.by.includes("customer")) {
|
|
273
|
+
contract(mp, "A window or staffOnly binds customers: the move must list customer in by");
|
|
274
|
+
}
|
|
275
|
+
checkWindow(move.window, `${mp}.window`);
|
|
276
|
+
if (move.staffOnly) checkPath(move.staffOnly, `${mp}.staffOnly`, "boolean");
|
|
277
|
+
});
|
|
278
|
+
if (states.create) {
|
|
279
|
+
for (const state of Object.keys(states.create.in ?? {})) {
|
|
280
|
+
if (!isState(state))
|
|
281
|
+
contract(`${sp}.create.in`, `"${state}" is not an option of ${states.field}`);
|
|
282
|
+
if (finals.has(state))
|
|
283
|
+
contract(`${sp}.create.in`, `"${state}" is final: nothing is created in it`);
|
|
284
|
+
}
|
|
285
|
+
checkWindow(states.create.window, `${sp}.create.window`);
|
|
286
|
+
if (states.create.staffOnly)
|
|
287
|
+
checkPath(states.create.staffOnly, `${sp}.create.staffOnly`, "boolean");
|
|
288
|
+
}
|
|
289
|
+
for (const [request, targets] of Object.entries(states.requests ?? {})) {
|
|
290
|
+
const rp = `${sp}.requests.${request}`;
|
|
291
|
+
if (isState(request)) contract(rp, `"${request}" is a state; a request needs its own name`);
|
|
292
|
+
if (new Set(targets).size !== targets.length) contract(rp, "List each state once");
|
|
293
|
+
for (const to of targets) {
|
|
294
|
+
if (!isState(to)) contract(rp, `"${to}" is not an option of ${states.field}`);
|
|
295
|
+
else if (!states.moves.some((move) => move.to === to)) {
|
|
296
|
+
contract(rp, `No move goes to "${to}"`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* The field a path names (ADR 0503): `field` on the object, or `relation.field` on the object a
|
|
304
|
+
* relation of one record points to, within the same extension.
|
|
305
|
+
*/
|
|
306
|
+
export function resolveFieldPath(
|
|
307
|
+
m: ExtensionManifest,
|
|
308
|
+
object: ObjectManifest,
|
|
309
|
+
path: string,
|
|
310
|
+
): FieldManifest | undefined {
|
|
311
|
+
const own = (o: ObjectManifest, local: string) =>
|
|
312
|
+
Object.hasOwn(o.fields, local) ? o.fields[local] : undefined;
|
|
313
|
+
const parts = path.split(".");
|
|
314
|
+
if (parts.length === 1) return own(object, path);
|
|
315
|
+
if (parts.length !== 2) return undefined;
|
|
316
|
+
const [relation, local] = parts as [string, string];
|
|
317
|
+
const via = own(object, relation);
|
|
318
|
+
const to = via?.type === "relation" && !via.many ? via.to : undefined;
|
|
319
|
+
const target = to?.startsWith(`${m.id}.`) ? to.slice(m.id.length + 1) : undefined;
|
|
320
|
+
const targetObject =
|
|
321
|
+
target !== undefined && Object.hasOwn(m.objects, target) ? m.objects[target] : undefined;
|
|
322
|
+
return targetObject ? own(targetObject, local) : undefined;
|
|
162
323
|
}
|
package/src/unique.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
import type { ExtensionManifest, ObjectManifest } from "./manifest.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* One uniqueness rule: no two live records with the same values of `fields` (one to four
|
|
9
|
+
* single-valued fields of this object; a record with any of them empty is not counted) while
|
|
10
|
+
* their state (the object's `states` field) is one of `states`. Left without `states`, every
|
|
11
|
+
* live record counts. The database refuses a second record, or a move back into a counted state,
|
|
12
|
+
* as a conflict (`rule: "unique"`).
|
|
13
|
+
*/
|
|
14
|
+
export interface UniqueSpec {
|
|
15
|
+
fields: readonly string[];
|
|
16
|
+
states?: readonly string[];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const uniqueManifestSchema = z
|
|
20
|
+
.array(
|
|
21
|
+
z
|
|
22
|
+
.object({
|
|
23
|
+
fields: z.array(z.string().max(40)).min(1).max(4),
|
|
24
|
+
states: z.array(z.string().min(1).max(100)).min(1).max(20).optional(),
|
|
25
|
+
})
|
|
26
|
+
.strict(),
|
|
27
|
+
)
|
|
28
|
+
.min(1)
|
|
29
|
+
.max(4);
|
|
30
|
+
export type UniqueManifest = z.infer<typeof uniqueManifestSchema>;
|
|
31
|
+
|
|
32
|
+
const UNIQUE_TYPES = new Set([
|
|
33
|
+
"text",
|
|
34
|
+
"number",
|
|
35
|
+
"date",
|
|
36
|
+
"datetime",
|
|
37
|
+
"boolean",
|
|
38
|
+
"select",
|
|
39
|
+
"relation",
|
|
40
|
+
"user",
|
|
41
|
+
"email",
|
|
42
|
+
"phone",
|
|
43
|
+
"url",
|
|
44
|
+
"slug",
|
|
45
|
+
"timezone",
|
|
46
|
+
]);
|
|
47
|
+
|
|
48
|
+
/** Reports problems with `objects.<name>.unique` through `contract`. */
|
|
49
|
+
export function checkObjectUnique(
|
|
50
|
+
_m: ExtensionManifest,
|
|
51
|
+
name: string,
|
|
52
|
+
object: ObjectManifest,
|
|
53
|
+
contract: (path: string, message: string) => void,
|
|
54
|
+
): void {
|
|
55
|
+
(object.unique ?? []).forEach((rule, i) => {
|
|
56
|
+
const path = `objects.${name}.unique.${i}`;
|
|
57
|
+
if (new Set(rule.fields).size !== rule.fields.length) {
|
|
58
|
+
contract(`${path}.fields`, "List each field once");
|
|
59
|
+
}
|
|
60
|
+
for (const local of rule.fields) {
|
|
61
|
+
const field = Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
62
|
+
if (!field || field.many || !UNIQUE_TYPES.has(field.type)) {
|
|
63
|
+
contract(`${path}.fields`, `"${local}" must be a single-valued, stored field of ${name}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (rule.states) {
|
|
67
|
+
const states = object.states;
|
|
68
|
+
if (!states) {
|
|
69
|
+
contract(`${path}.states`, `${name} has no states`);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const field = Object.hasOwn(object.fields, states.field)
|
|
73
|
+
? object.fields[states.field]
|
|
74
|
+
: undefined;
|
|
75
|
+
const values = new Set((field?.options ?? []).map((o) => o.value));
|
|
76
|
+
for (const state of rule.states) {
|
|
77
|
+
if (!values.has(state)) contract(`${path}.states`, `"${state}" is not a state of ${name}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|