@composable-os/sdk 0.1.0-beta.10 → 0.1.0-beta.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/acknowledge.d.ts +38 -0
- package/dist/acknowledge.js +30 -0
- package/dist/agreements.d.ts +80 -0
- package/dist/agreements.js +109 -0
- package/dist/allowance.d.ts +47 -5
- package/dist/allowance.js +59 -4
- package/dist/automation-check.d.ts +6 -1
- package/dist/automation-check.js +93 -8
- package/dist/automation.d.ts +62 -4
- package/dist/automation.js +32 -9
- package/dist/behaviour.js +25 -6
- package/dist/calendar-entry.d.ts +97 -0
- package/dist/calendar-entry.js +180 -0
- package/dist/capacity.d.ts +69 -0
- package/dist/capacity.js +63 -2
- package/dist/checklists.d.ts +41 -0
- package/dist/checklists.js +75 -0
- package/dist/contract.d.ts +527 -26
- package/dist/contract.js +21 -3
- package/dist/discount.d.ts +135 -0
- package/dist/discount.js +281 -0
- package/dist/document.d.ts +2 -1
- package/dist/document.js +2 -1
- package/dist/field-types.d.ts +1 -1
- package/dist/field-types.js +5 -0
- package/dist/formula/compile.d.ts +15 -0
- package/dist/formula/compile.js +9 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/intake.d.ts +134 -0
- package/dist/intake.js +239 -0
- package/dist/journey-signals.d.ts +89 -0
- package/dist/journey-signals.js +151 -0
- package/dist/manifest.d.ts +1060 -60
- package/dist/manifest.js +1111 -61
- package/dist/mine.d.ts +45 -0
- package/dist/mine.js +119 -0
- package/dist/occupies.d.ts +64 -0
- package/dist/occupies.js +122 -0
- package/dist/questions.d.ts +179 -0
- package/dist/questions.js +514 -0
- package/dist/recurring.d.ts +33 -4
- package/dist/recurring.js +50 -3
- package/dist/restricted-fields.d.ts +15 -0
- package/dist/restricted-fields.js +97 -0
- package/dist/rich-text.d.ts +22 -0
- package/dist/rich-text.js +91 -0
- package/dist/routine-mail.d.ts +400 -0
- package/dist/routine-mail.js +955 -0
- package/dist/runtime.d.ts +5 -11
- package/dist/runtime.js +21 -11
- package/dist/screens.d.ts +3 -0
- package/dist/screens.js +74 -0
- package/dist/sellable.d.ts +231 -2
- package/dist/sellable.js +342 -1
- package/dist/slug.d.ts +2 -0
- package/dist/slug.js +6 -2
- package/dist/staffed.d.ts +178 -0
- package/dist/staffed.js +287 -0
- package/dist/states.d.ts +74 -4
- package/dist/states.js +154 -58
- package/dist/stock.d.ts +129 -0
- package/dist/stock.js +205 -0
- package/dist/team.d.ts +28 -0
- package/dist/team.js +52 -0
- package/dist/testing.d.ts +22 -1
- package/dist/testing.js +47 -5
- package/dist/worked-on.d.ts +42 -0
- package/dist/worked-on.js +79 -0
- package/package.json +9 -1
- package/src/acknowledge.ts +100 -0
- package/src/agreements.ts +185 -0
- package/src/allowance.ts +98 -8
- package/src/automation-check.ts +120 -9
- package/src/automation.ts +78 -10
- package/src/behaviour.ts +26 -6
- package/src/calendar-entry.ts +258 -0
- package/src/capacity.ts +103 -2
- package/src/checklists.ts +118 -0
- package/src/contract.ts +514 -27
- package/src/discount.ts +408 -0
- package/src/document.ts +4 -2
- package/src/field-types.ts +5 -0
- package/src/formula/compile.ts +10 -0
- package/src/index.ts +17 -0
- package/src/intake.ts +380 -0
- package/src/journey-signals.ts +215 -0
- package/src/manifest.ts +1313 -66
- package/src/mine.ts +161 -0
- package/src/occupies.ts +192 -0
- package/src/questions.ts +681 -0
- package/src/recurring.ts +90 -9
- package/src/restricted-fields.ts +129 -0
- package/src/rich-text.ts +112 -0
- package/src/routine-mail.ts +1272 -0
- package/src/runtime.ts +25 -12
- package/src/screens.ts +91 -0
- package/src/sellable.ts +595 -1
- package/src/slug.ts +7 -2
- package/src/staffed.ts +474 -0
- package/src/states.ts +220 -64
- package/src/stock.ts +332 -0
- package/src/team.ts +83 -0
- package/src/testing.ts +70 -6
- package/src/worked-on.ts +134 -0
package/src/sellable.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { z } from "zod";
|
|
7
7
|
import { customerWritable, spendsOnList } from "./allowance.ts";
|
|
8
8
|
import type { ExtensionManifest, FieldManifest, ObjectManifest } from "./manifest.ts";
|
|
9
|
-
import { moveOrigins } from "./states.ts";
|
|
9
|
+
import { moveOrigins, resolveFieldPath } from "./states.ts";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Where a granted record's field gets its value, when the host creates it:
|
|
@@ -107,6 +107,164 @@ export interface SellableSpec {
|
|
|
107
107
|
* "sign_in"`). Such a line holds one, and an order at most one such line. Not on a plan.
|
|
108
108
|
*/
|
|
109
109
|
onePerPerson?: string;
|
|
110
|
+
/**
|
|
111
|
+
* A paid line moves a record that already exists (stage 31a step 8, ADR 0560), see MovesSpec.
|
|
112
|
+
* A record of such an object is bought once, when it is made: `order.startHeld` makes it in
|
|
113
|
+
* `held` and starts its order in one batch. It is never put on `order.start`. Not with
|
|
114
|
+
* `grants`, `deposit`, `recurring`, `onePerPerson`, `maxPerOrder` or `saleWindow`.
|
|
115
|
+
*/
|
|
116
|
+
moves?: MovesSpec;
|
|
117
|
+
/**
|
|
118
|
+
* What kind of sale this is in the finance reports, and when its money is earned (stage 52,
|
|
119
|
+
* ADR 0792), see FinanceSpec. Left out: category `other`, earned `at_sale`.
|
|
120
|
+
*/
|
|
121
|
+
finance?: FinanceSpec;
|
|
122
|
+
/**
|
|
123
|
+
* The records are goods (stage 37, ADR 0682): things handed over or sent (a variant of an item
|
|
124
|
+
* in a shop), see GoodsSpec. An order with goods needs a delivery (`delivery`), and each goods
|
|
125
|
+
* line must be sold in the order's channel. Not with `deposit`, `recurring`, `moves` or
|
|
126
|
+
* `delivery`.
|
|
127
|
+
*/
|
|
128
|
+
goods?: GoodsSpec;
|
|
129
|
+
/**
|
|
130
|
+
* The records are ways of getting goods to the buyer (stage 37, ADR 0682): a delivery option,
|
|
131
|
+
* see DeliverySpec. Never a line of `lines`: the buyer chooses one in `order.start`'s and
|
|
132
|
+
* `order.quote`'s `delivery`, and the host prices it (ADR 0685). Not with `goods`, `deposit`,
|
|
133
|
+
* `recurring`, `moves`, `onePerPerson` or `maxPerOrder` (one per order), nor beside `stock`.
|
|
134
|
+
*/
|
|
135
|
+
delivery?: DeliverySpec;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Where goods are sold (stage 37 decision 3, ADR 0682): a select field holds one of these. */
|
|
139
|
+
export const SOLD_IN = ["online", "in_person", "both"] as const;
|
|
140
|
+
export type SoldIn = (typeof SOLD_IN)[number];
|
|
141
|
+
|
|
142
|
+
/** How a delivery option gets goods to the buyer (ADR 0682). */
|
|
143
|
+
export const DELIVERY_KINDS = ["post", "collect", "here"] as const;
|
|
144
|
+
export type DeliveryKind = (typeof DELIVERY_KINDS)[number];
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Goods (ADR 0682). Every order with goods has a channel the host works out, never the caller's:
|
|
148
|
+
* a `post` or `collect` delivery is `online`, a `here` delivery ("take it now") is `in_person`.
|
|
149
|
+
*
|
|
150
|
+
* - `soldIn`: where a record is sold: a select field whose values are among `online`,
|
|
151
|
+
* `in_person` and `both`, of this object or through up to two relations (`item.whereSold`: a
|
|
152
|
+
* variant is sold where its item is). Left out or empty: both. A line not sold in the order's
|
|
153
|
+
* channel is refused (`rule: "not_sold_here"`), on every path.
|
|
154
|
+
*/
|
|
155
|
+
export interface GoodsSpec {
|
|
156
|
+
soldIn?: string;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* A delivery option (ADR 0682, priced by ADR 0685). A record is one way of getting an order's
|
|
161
|
+
* goods to the buyer; its object is sellable, so it has a price (which may be 0), a tax code and
|
|
162
|
+
* what a paid line grants (a shipment to pack, for `post` and `collect`; nothing is granted for a
|
|
163
|
+
* `here` record).
|
|
164
|
+
*
|
|
165
|
+
* - `kind`: a select field whose values are among `post` (an address is needed, in one of the
|
|
166
|
+
* option's countries), `collect` (at a place the record says; no address) and `here` (take it
|
|
167
|
+
* now, in person; no address).
|
|
168
|
+
* - `freeOver`: a currency or number field: when the goods on the order cost at least this
|
|
169
|
+
* (before the delivery), the delivery costs nothing. Empty: never free.
|
|
170
|
+
* - `countries`: a text field: the two-letter country codes (ISO 3166-1) a `post` record posts
|
|
171
|
+
* to, separated by commas or spaces; or `*`, everywhere no other post option of the object
|
|
172
|
+
* names. Empty: it posts nowhere yet.
|
|
173
|
+
* - `active`: a boolean field: a record with it off cannot be chosen.
|
|
174
|
+
*/
|
|
175
|
+
export interface DeliverySpec {
|
|
176
|
+
kind: string;
|
|
177
|
+
freeOver?: string;
|
|
178
|
+
countries?: string;
|
|
179
|
+
active?: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** How a sale's money is earned (ADR 0792), as the Unearned report reads it. */
|
|
183
|
+
export const EARNED = ["at_sale", "over_period", "per_use", "at_date"] as const;
|
|
184
|
+
export type Earned = (typeof EARNED)[number];
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* What kind of sale a sellable object is in the finance reports (stage 52, ADR 0792). It only
|
|
188
|
+
* labels the object's own sales: it prices nothing, grants nothing and moves no money. Which way
|
|
189
|
+
* the books treat a sale is the business's accountant's call; this says what the reports show.
|
|
190
|
+
*
|
|
191
|
+
* - `category`: the kind of sale its lines are counted under in Sales, Tax, Refunds and the book
|
|
192
|
+
* entries' `sales:<key>` and `refunds:<key>` headings. `key` is stable (lower case, digits and
|
|
193
|
+
* `_`); `label` is what the reports call it until an admin renames it. Two objects with the same
|
|
194
|
+
* key are one category, so this extension gives them one label.
|
|
195
|
+
* - `earned`: when the money is earned, for the Unearned report (cash is counted when it moved,
|
|
196
|
+
* whatever this says):
|
|
197
|
+
* - `at_sale`: when it is paid (a course, goods);
|
|
198
|
+
* - `over_period`: day by day over the order's period, from its start to its end (a plan's
|
|
199
|
+
* paid period); only on a plan (`recurring`);
|
|
200
|
+
* - `per_use`: as the uses of what a paid line grants are spent, the rest when it expires (a
|
|
201
|
+
* class pass); only when `grants` grants an object that holds uses (`allowance.holds`);
|
|
202
|
+
* - `at_date`: on the date in `at` (an event's start).
|
|
203
|
+
* - `at`: with `at_date`, a date or datetime field of this object, or `<relation>.<field>` through
|
|
204
|
+
* up to two relations (`event.startsAt`, `class.startsAt`).
|
|
205
|
+
*
|
|
206
|
+
* A line whose object issues stored value or a voucher (stage 34) is held for customers, not a
|
|
207
|
+
* sale, whatever it declares.
|
|
208
|
+
*/
|
|
209
|
+
export interface FinanceSpec {
|
|
210
|
+
category?: { key: string; label: string };
|
|
211
|
+
earned?: Earned;
|
|
212
|
+
at?: string;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** What `sellable.finance` comes to when it, or a part of it, is left out (ADR 0792). */
|
|
216
|
+
export const FINANCE_DEFAULTS = {
|
|
217
|
+
category: { key: "other", label: "Other" },
|
|
218
|
+
earned: "at_sale",
|
|
219
|
+
} as const satisfies Required<Omit<FinanceSpec, "at">>;
|
|
220
|
+
|
|
221
|
+
/** A sellable object's finance declaration with its defaults filled (ADR 0792). */
|
|
222
|
+
export function sellableFinance(finance: FinanceSpec | undefined): {
|
|
223
|
+
category: { key: string; label: string };
|
|
224
|
+
earned: Earned;
|
|
225
|
+
at?: string;
|
|
226
|
+
} {
|
|
227
|
+
return {
|
|
228
|
+
category: finance?.category ?? FINANCE_DEFAULTS.category,
|
|
229
|
+
earned: finance?.earned ?? FINANCE_DEFAULTS.earned,
|
|
230
|
+
...(finance?.at !== undefined ? { at: finance.at } : {}),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* What an order does to the record it was started for (ADR 0560): a drop-in booking held while
|
|
236
|
+
* its order is unpaid. The host makes every move, as the host, in the batch that moves the order.
|
|
237
|
+
*
|
|
238
|
+
* - `held`: the state `order.startHeld` makes the record in (`states.create.in` lets the host,
|
|
239
|
+
* and only the host, create it there). A capacity claimant holds its place in it.
|
|
240
|
+
* - `paid`: the order is paid: the record moves `held` -> `paid`.
|
|
241
|
+
* - `expired`: the order expired or was cancelled unpaid: `held` -> `expired`, a final state.
|
|
242
|
+
* A record that leaves `held` any other way (a class the gym cancels) cancels its unpaid order.
|
|
243
|
+
* - `refund`: states that give the money back. A record whose order is paid entering one of them
|
|
244
|
+
* queues the order's refund for a person with `orders:refund` to approve (never an AI).
|
|
245
|
+
* - `price`: where the price of one is read when the record is made: a number field through up
|
|
246
|
+
* to two relations (`class.classType.dropInPrice`), else the extension's number setting. The
|
|
247
|
+
* host writes it into the read-only `price` field, and the order's line is priced from it.
|
|
248
|
+
* - `title`: the order line's title, a text field through up to two relations (`class.title`).
|
|
249
|
+
* - `order`: a read-only text field the host writes the order's id into.
|
|
250
|
+
* - `guest`: a buyer who is not signed in is matched to no Person before payment: their record is
|
|
251
|
+
* held with its customer field empty (so it is no one's, blocks no one's own, and tells the
|
|
252
|
+
* buyer nothing about who has that address), and is given its Person when the payment is
|
|
253
|
+
* verified, by the rule a guest's order uses. The customer field must not be `required`; the
|
|
254
|
+
* host keeps it set in every state but `held`. Without it, a guest cannot buy the object.
|
|
255
|
+
* - `holdCap`: a number setting of the extension: the percent of the claimed record's places that
|
|
256
|
+
* unpaid guest holds may take at once (at least one). Over it a guest is told to try again soon.
|
|
257
|
+
*/
|
|
258
|
+
export interface MovesSpec {
|
|
259
|
+
held: string;
|
|
260
|
+
paid: string;
|
|
261
|
+
expired: string;
|
|
262
|
+
refund?: readonly string[];
|
|
263
|
+
price?: { field?: string; setting?: string };
|
|
264
|
+
title?: string;
|
|
265
|
+
order?: string;
|
|
266
|
+
guest?: boolean;
|
|
267
|
+
holdCap?: { setting: string };
|
|
110
268
|
}
|
|
111
269
|
|
|
112
270
|
/**
|
|
@@ -141,8 +299,31 @@ export interface RecurringSpec {
|
|
|
141
299
|
* `sellable.recurring`, and its `grants` is what the subscription gives while it runs.
|
|
142
300
|
*/
|
|
143
301
|
grants?: RecurringGrantSpec;
|
|
302
|
+
/**
|
|
303
|
+
* Whether staff may pause a subscription to the plan (stage 32b, decision 10, ADR 0531), and
|
|
304
|
+
* for how long: `minDays` and `maxDays` name number fields of the plan holding the shortest and
|
|
305
|
+
* longest pause the company allows (empty: 1 day, and 366 days, the most the host asks the
|
|
306
|
+
* provider for). Left out, a subscription to the plan is never paused. Only staff pause, with
|
|
307
|
+
* `subscriptions:manage`, as a person; while paused nothing is charged, granted or spent.
|
|
308
|
+
*/
|
|
309
|
+
pause?: { minDays?: string; maxDays?: string };
|
|
310
|
+
/**
|
|
311
|
+
* A cooling-off period (stage 44 step 7, decision 11, ADR 0647): `setting` names a number setting
|
|
312
|
+
* of this extension holding whole days (0: off), which a person changes (`personApproves`),
|
|
313
|
+
* between 0 and `MAX_COOLING_OFF_DAYS`. A new member (a subscription started here without a
|
|
314
|
+
* continuation offer) may end their subscription within that many days of the provider-confirmed
|
|
315
|
+
* start, counted in the business's time zone to the end of the last day, for a full refund of
|
|
316
|
+
* what they paid; each refund waits for a person with `orders:refund` (ADR 0429). Once per
|
|
317
|
+
* holder. The days in force when a subscription started are kept with it: a setting shortened
|
|
318
|
+
* later never shortens that member's window. What any law requires is the business's to
|
|
319
|
+
* confirm; the platform only holds the number.
|
|
320
|
+
*/
|
|
321
|
+
coolingOff?: { setting: string };
|
|
144
322
|
}
|
|
145
323
|
|
|
324
|
+
/** The longest cooling-off period the host keeps (days); a longer setting reads as this. */
|
|
325
|
+
export const MAX_COOLING_OFF_DAYS = 90;
|
|
326
|
+
|
|
146
327
|
/** Where a field of what a subscription grants gets its value (see RecurringSpec.grants). */
|
|
147
328
|
export type RecurringGrantSource =
|
|
148
329
|
| "item"
|
|
@@ -256,6 +437,23 @@ export const sellableManifestSchema = z
|
|
|
256
437
|
.strict()
|
|
257
438
|
.optional(),
|
|
258
439
|
onePerPerson: fieldName.optional(),
|
|
440
|
+
moves: z
|
|
441
|
+
.object({
|
|
442
|
+
held: z.string().min(1).max(100),
|
|
443
|
+
paid: z.string().min(1).max(100),
|
|
444
|
+
expired: z.string().min(1).max(100),
|
|
445
|
+
refund: z.array(z.string().min(1).max(100)).max(20).optional(),
|
|
446
|
+
price: z
|
|
447
|
+
.object({ field: z.string().max(130).optional(), setting: fieldName.optional() })
|
|
448
|
+
.strict()
|
|
449
|
+
.optional(),
|
|
450
|
+
title: z.string().max(130).optional(),
|
|
451
|
+
order: fieldName.optional(),
|
|
452
|
+
guest: z.boolean().optional(),
|
|
453
|
+
holdCap: z.object({ setting: fieldName }).strict().optional(),
|
|
454
|
+
})
|
|
455
|
+
.strict()
|
|
456
|
+
.optional(),
|
|
259
457
|
tax: z
|
|
260
458
|
.object({
|
|
261
459
|
code: z.string().regex(TAX_CODE_PATTERN, "A product tax code: txcd_ and eight digits"),
|
|
@@ -277,6 +475,11 @@ export const sellableManifestSchema = z
|
|
|
277
475
|
.object({
|
|
278
476
|
interval: z.enum(["week", "month", "year"]),
|
|
279
477
|
trialDays: fieldName.optional(),
|
|
478
|
+
pause: z
|
|
479
|
+
.object({ minDays: fieldName.optional(), maxDays: fieldName.optional() })
|
|
480
|
+
.strict()
|
|
481
|
+
.optional(),
|
|
482
|
+
coolingOff: z.object({ setting: fieldName }).strict().optional(),
|
|
280
483
|
grants: z
|
|
281
484
|
.object({
|
|
282
485
|
object: z.string().max(40),
|
|
@@ -299,6 +502,33 @@ export const sellableManifestSchema = z
|
|
|
299
502
|
})
|
|
300
503
|
.strict()
|
|
301
504
|
.optional(),
|
|
505
|
+
finance: z
|
|
506
|
+
.object({
|
|
507
|
+
category: z
|
|
508
|
+
.object({
|
|
509
|
+
key: z.string().regex(/^[a-z][a-z0-9_]{0,39}$/, "Lower case letters, digits and _"),
|
|
510
|
+
label: z.string().min(1).max(60),
|
|
511
|
+
})
|
|
512
|
+
.strict()
|
|
513
|
+
.optional(),
|
|
514
|
+
earned: z.enum(EARNED).optional(),
|
|
515
|
+
at: z.string().max(130).optional(),
|
|
516
|
+
})
|
|
517
|
+
.strict()
|
|
518
|
+
.optional(),
|
|
519
|
+
goods: z
|
|
520
|
+
.object({ soldIn: z.string().max(130).optional() })
|
|
521
|
+
.strict()
|
|
522
|
+
.optional(),
|
|
523
|
+
delivery: z
|
|
524
|
+
.object({
|
|
525
|
+
kind: fieldName,
|
|
526
|
+
freeOver: fieldName.optional(),
|
|
527
|
+
countries: fieldName.optional(),
|
|
528
|
+
active: fieldName.optional(),
|
|
529
|
+
})
|
|
530
|
+
.strict()
|
|
531
|
+
.optional(),
|
|
302
532
|
})
|
|
303
533
|
.strict();
|
|
304
534
|
export type SellableManifest = z.infer<typeof sellableManifestSchema>;
|
|
@@ -366,6 +596,40 @@ export function checkObjectSellable(
|
|
|
366
596
|
if (trial !== undefined && !single(trial, new Set(["number"]))) {
|
|
367
597
|
contract(`${path}.recurring.trialDays`, `"${trial}" must be a number field of ${name}`);
|
|
368
598
|
}
|
|
599
|
+
for (const key of ["minDays", "maxDays"] as const) {
|
|
600
|
+
const days = sellable.recurring.pause?.[key];
|
|
601
|
+
if (days !== undefined && !single(days, new Set(["number"]))) {
|
|
602
|
+
contract(`${path}.recurring.pause.${key}`, `"${days}" must be a number field of ${name}`);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
const coolingOff = sellable.recurring.coolingOff;
|
|
606
|
+
if (coolingOff) {
|
|
607
|
+
const settings = m.settings ?? {};
|
|
608
|
+
const setting = Object.hasOwn(settings, coolingOff.setting)
|
|
609
|
+
? settings[coolingOff.setting]
|
|
610
|
+
: undefined;
|
|
611
|
+
const at = `${path}.recurring.coolingOff.setting`;
|
|
612
|
+
if (setting?.type !== "number") {
|
|
613
|
+
contract(at, `"${coolingOff.setting}" must be a number setting of ${m.id}`);
|
|
614
|
+
} else if (setting.personApproves !== true) {
|
|
615
|
+
// Refunds follow from it: a person's to change, never an AI's (decision 11).
|
|
616
|
+
contract(
|
|
617
|
+
at,
|
|
618
|
+
`"${coolingOff.setting}" must be a setting a person approves (personApproves): the cooling-off days are never an AI's to change`,
|
|
619
|
+
);
|
|
620
|
+
} else if (
|
|
621
|
+
setting.min === undefined ||
|
|
622
|
+
setting.min < 0 ||
|
|
623
|
+
setting.max === undefined ||
|
|
624
|
+
setting.max > MAX_COOLING_OFF_DAYS ||
|
|
625
|
+
(typeof setting.default === "number" && setting.default !== 0)
|
|
626
|
+
) {
|
|
627
|
+
contract(
|
|
628
|
+
at,
|
|
629
|
+
`"${coolingOff.setting}" must be bounded from 0 to at most ${MAX_COOLING_OFF_DAYS} days, and off (0 or no default) until a person sets it`,
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
369
633
|
if (sellable.onePerPerson !== undefined) {
|
|
370
634
|
contract(
|
|
371
635
|
`${path}.onePerPerson`,
|
|
@@ -414,6 +678,11 @@ export function checkObjectSellable(
|
|
|
414
678
|
}
|
|
415
679
|
}
|
|
416
680
|
if (sellable.grants) checkGrants(m, name, object, sellable.grants, `${path}.grants`, contract);
|
|
681
|
+
if (sellable.moves) checkMoves(m, name, object, sellable.moves, `${path}.moves`, contract);
|
|
682
|
+
if (sellable.goods || sellable.delivery) checkGoods(m, name, object, path, contract);
|
|
683
|
+
if (sellable.finance) {
|
|
684
|
+
checkFinance(m, name, object, sellable.finance, `${path}.finance`, contract);
|
|
685
|
+
}
|
|
417
686
|
const place = sellable.tax?.place;
|
|
418
687
|
if (place) {
|
|
419
688
|
let holder: ObjectManifest | undefined = object;
|
|
@@ -451,6 +720,331 @@ export function checkObjectSellable(
|
|
|
451
720
|
}
|
|
452
721
|
}
|
|
453
722
|
|
|
723
|
+
/** A select field's option values (empty when it is not a select field). */
|
|
724
|
+
function selectValues(field: FieldManifest | undefined): string[] | null {
|
|
725
|
+
if (!field || field.type !== "select" || field.many) return null;
|
|
726
|
+
return (field.options ?? []).map((option) =>
|
|
727
|
+
typeof option === "string" ? option : (option as { value: string }).value,
|
|
728
|
+
);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* Checks `sellable.goods` and `sellable.delivery` (ADR 0682): where goods are sold is a select of
|
|
733
|
+
* `online`, `in_person` and `both`; a delivery option's kind a select of `post`, `collect` and
|
|
734
|
+
* `here`, with its other fields of the right types; and neither is bought by deposit, by
|
|
735
|
+
* subscription or by moving a record. A delivery is one per order and has no stock.
|
|
736
|
+
*/
|
|
737
|
+
function checkGoods(
|
|
738
|
+
m: ExtensionManifest,
|
|
739
|
+
name: string,
|
|
740
|
+
object: ObjectManifest,
|
|
741
|
+
path: string,
|
|
742
|
+
contract: (path: string, message: string) => void,
|
|
743
|
+
): void {
|
|
744
|
+
const sellable = object.sellable as SellableManifest;
|
|
745
|
+
const fieldOf = (local: string): FieldManifest | undefined =>
|
|
746
|
+
Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
747
|
+
if (sellable.goods && sellable.delivery) {
|
|
748
|
+
contract(`${path}.delivery`, "A record is goods or a delivery option, not both");
|
|
749
|
+
}
|
|
750
|
+
const what = sellable.goods ? "goods" : "a delivery option";
|
|
751
|
+
for (const key of ["deposit", "recurring", "moves"] as const) {
|
|
752
|
+
if (sellable[key] !== undefined) {
|
|
753
|
+
contract(`${path}.${key}`, `${name} is ${what}, sold whole on an order: it has no ${key}`);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
const soldIn = sellable.goods?.soldIn;
|
|
757
|
+
if (soldIn !== undefined) {
|
|
758
|
+
const values = selectValues(resolveFieldPath(m, object, soldIn));
|
|
759
|
+
if (!values || values.some((v) => !(SOLD_IN as readonly string[]).includes(v))) {
|
|
760
|
+
contract(
|
|
761
|
+
`${path}.goods.soldIn`,
|
|
762
|
+
`"${soldIn}" must be a select field (of ${name}, or through its relations) whose values are among ${SOLD_IN.join(", ")}`,
|
|
763
|
+
);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
const delivery = sellable.delivery;
|
|
767
|
+
if (!delivery) return;
|
|
768
|
+
for (const key of ["onePerPerson", "maxPerOrder"] as const) {
|
|
769
|
+
if (sellable[key] !== undefined) {
|
|
770
|
+
contract(`${path}.${key}`, `A delivery option is one per order: it has no ${key}`);
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
if (object.stock) {
|
|
774
|
+
contract(`objects.${name}.stock`, "A delivery option is not counted on a shelf: no stock");
|
|
775
|
+
}
|
|
776
|
+
const kinds = selectValues(fieldOf(delivery.kind));
|
|
777
|
+
if (!kinds?.length || kinds.some((v) => !(DELIVERY_KINDS as readonly string[]).includes(v))) {
|
|
778
|
+
contract(
|
|
779
|
+
`${path}.delivery.kind`,
|
|
780
|
+
`"${delivery.kind}" must be a select field of ${name} whose values are among ${DELIVERY_KINDS.join(", ")}`,
|
|
781
|
+
);
|
|
782
|
+
}
|
|
783
|
+
if (delivery.freeOver !== undefined) {
|
|
784
|
+
const field = fieldOf(delivery.freeOver);
|
|
785
|
+
if (!field || !NUMERIC.has(field.type) || field.many) {
|
|
786
|
+
contract(
|
|
787
|
+
`${path}.delivery.freeOver`,
|
|
788
|
+
`"${delivery.freeOver}" must be a currency or number field of ${name}`,
|
|
789
|
+
);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
if (delivery.countries !== undefined) {
|
|
793
|
+
const field = fieldOf(delivery.countries);
|
|
794
|
+
if (!field || !TEXT.has(field.type) || field.many) {
|
|
795
|
+
contract(
|
|
796
|
+
`${path}.delivery.countries`,
|
|
797
|
+
`"${delivery.countries}" must be a text field of ${name}`,
|
|
798
|
+
);
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
if (delivery.active !== undefined) {
|
|
802
|
+
const field = fieldOf(delivery.active);
|
|
803
|
+
if (!field || field.type !== "boolean" || field.many) {
|
|
804
|
+
contract(
|
|
805
|
+
`${path}.delivery.active`,
|
|
806
|
+
`"${delivery.active}" must be a boolean field of ${name}`,
|
|
807
|
+
);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
/**
|
|
813
|
+
* Checks `sellable.finance` (ADR 0792): each way of earning names what the host reads to work it
|
|
814
|
+
* out, and one category key has one label across this extension.
|
|
815
|
+
*/
|
|
816
|
+
function checkFinance(
|
|
817
|
+
m: ExtensionManifest,
|
|
818
|
+
name: string,
|
|
819
|
+
object: ObjectManifest,
|
|
820
|
+
finance: NonNullable<SellableManifest["finance"]>,
|
|
821
|
+
path: string,
|
|
822
|
+
contract: (path: string, message: string) => void,
|
|
823
|
+
): void {
|
|
824
|
+
const sellable = object.sellable as SellableManifest;
|
|
825
|
+
const earned = finance.earned ?? FINANCE_DEFAULTS.earned;
|
|
826
|
+
if (earned === "at_date") {
|
|
827
|
+
if (finance.at === undefined) {
|
|
828
|
+
contract(`${path}.at`, "at_date names its date: at, a date or datetime field");
|
|
829
|
+
} else {
|
|
830
|
+
const at = resolveFieldPath(m, object, finance.at);
|
|
831
|
+
if (!at || at.many || !DATES.has(at.type)) {
|
|
832
|
+
contract(
|
|
833
|
+
`${path}.at`,
|
|
834
|
+
`"${finance.at}" must be a date or datetime field of ${name}, or <relation>.<field> through up to two relations, every field on the way declared by this extension`,
|
|
835
|
+
);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
} else if (finance.at !== undefined) {
|
|
839
|
+
contract(`${path}.at`, "at is the date of at_date: declare earned: at_date, or remove at");
|
|
840
|
+
}
|
|
841
|
+
if (earned === "over_period" && !sellable.recurring) {
|
|
842
|
+
contract(
|
|
843
|
+
`${path}.earned`,
|
|
844
|
+
"over_period is earned over a paid period: only a plan (recurring) has one",
|
|
845
|
+
);
|
|
846
|
+
}
|
|
847
|
+
if (earned === "per_use") {
|
|
848
|
+
const grants = sellable.grants;
|
|
849
|
+
const local = grants?.object.startsWith(`${m.id}.`)
|
|
850
|
+
? grants.object.slice(m.id.length + 1)
|
|
851
|
+
: grants?.object;
|
|
852
|
+
const granted =
|
|
853
|
+
local !== undefined && Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
|
|
854
|
+
if (!granted?.allowance || !("holds" in granted.allowance)) {
|
|
855
|
+
contract(
|
|
856
|
+
`${path}.earned`,
|
|
857
|
+
"per_use is earned as uses are spent: grants must grant an object that holds uses (allowance.holds)",
|
|
858
|
+
);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
const category = finance.category;
|
|
862
|
+
if (category) {
|
|
863
|
+
for (const [other, o] of Object.entries(m.objects)) {
|
|
864
|
+
if (other === name) continue;
|
|
865
|
+
const theirs = o.sellable?.finance?.category;
|
|
866
|
+
if (theirs?.key === category.key && theirs.label !== category.label) {
|
|
867
|
+
contract(
|
|
868
|
+
`${path}.category.label`,
|
|
869
|
+
`${other} names category "${category.key}" "${theirs.label}": one category has one label`,
|
|
870
|
+
);
|
|
871
|
+
break;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
/**
|
|
878
|
+
* Checks `sellable.moves` (ADR 0560): each of these is a way the order's batch could otherwise fail
|
|
879
|
+
* (a payment the host could not apply), or a record someone could hold with no order.
|
|
880
|
+
*/
|
|
881
|
+
function checkMoves(
|
|
882
|
+
m: ExtensionManifest,
|
|
883
|
+
name: string,
|
|
884
|
+
object: ObjectManifest,
|
|
885
|
+
moves: NonNullable<SellableManifest["moves"]>,
|
|
886
|
+
path: string,
|
|
887
|
+
contract: (path: string, message: string) => void,
|
|
888
|
+
): void {
|
|
889
|
+
const sellable = object.sellable as SellableManifest;
|
|
890
|
+
for (const key of [
|
|
891
|
+
"grants",
|
|
892
|
+
"deposit",
|
|
893
|
+
"recurring",
|
|
894
|
+
"onePerPerson",
|
|
895
|
+
"maxPerOrder",
|
|
896
|
+
"saleWindow",
|
|
897
|
+
] as const) {
|
|
898
|
+
if (sellable[key] !== undefined) {
|
|
899
|
+
contract(
|
|
900
|
+
`objects.${name}.sellable.${key}`,
|
|
901
|
+
`A record bought with moves is one record on one order: it has no ${key}`,
|
|
902
|
+
);
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
const field = (local: string) =>
|
|
906
|
+
Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
907
|
+
const price = field(sellable.price);
|
|
908
|
+
if (price && !price.readOnly) {
|
|
909
|
+
contract(
|
|
910
|
+
`objects.${name}.fields.${sellable.price}`,
|
|
911
|
+
`"${sellable.price}" holds what the host priced it at: make it readOnly`,
|
|
912
|
+
);
|
|
913
|
+
}
|
|
914
|
+
if (moves.order !== undefined) {
|
|
915
|
+
const order = field(moves.order);
|
|
916
|
+
if (order?.type !== "text" || order.many || !order.readOnly) {
|
|
917
|
+
contract(`${path}.order`, `"${moves.order}" must be a readOnly text field of ${name}`);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
if (moves.price) {
|
|
921
|
+
if (moves.price.field === undefined && moves.price.setting === undefined) {
|
|
922
|
+
contract(`${path}.price`, "price names a field, a setting or both");
|
|
923
|
+
}
|
|
924
|
+
if (moves.price.field !== undefined) {
|
|
925
|
+
const at = resolveFieldPath(m, object, moves.price.field);
|
|
926
|
+
if (!at || at.many || (at.type !== "number" && at.type !== "currency")) {
|
|
927
|
+
contract(
|
|
928
|
+
`${path}.price.field`,
|
|
929
|
+
`"${moves.price.field}" must be a number field of ${name}, or <relation>.<field> through up to two relations, every field on the way declared by this extension`,
|
|
930
|
+
);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
if (moves.price.setting !== undefined) {
|
|
934
|
+
const settings = m.settings ?? {};
|
|
935
|
+
const setting = Object.hasOwn(settings, moves.price.setting)
|
|
936
|
+
? settings[moves.price.setting]
|
|
937
|
+
: undefined;
|
|
938
|
+
if (setting?.type !== "number") {
|
|
939
|
+
contract(
|
|
940
|
+
`${path}.price.setting`,
|
|
941
|
+
`"${moves.price.setting}" must be a number setting of ${m.id}`,
|
|
942
|
+
);
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
if (moves.title !== undefined) {
|
|
947
|
+
const at = resolveFieldPath(m, object, moves.title);
|
|
948
|
+
if (!at || at.many || at.type !== "text") {
|
|
949
|
+
contract(
|
|
950
|
+
`${path}.title`,
|
|
951
|
+
`"${moves.title}" must be a text field of ${name}, or through up to two relations`,
|
|
952
|
+
);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
if (!object.customer) contract(path, `${name} must declare customer: whose record it is`);
|
|
956
|
+
if (moves.guest && object.customer) {
|
|
957
|
+
const owner = field(object.customer.field);
|
|
958
|
+
if (owner?.required) {
|
|
959
|
+
contract(
|
|
960
|
+
`objects.${name}.fields.${object.customer.field}`,
|
|
961
|
+
`A guest's ${name} is held with no ${object.customer.field} until it is paid: it cannot be required (the host keeps it set outside "${moves.held}")`,
|
|
962
|
+
);
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
if (moves.holdCap) {
|
|
966
|
+
const settings = m.settings ?? {};
|
|
967
|
+
const setting = Object.hasOwn(settings, moves.holdCap.setting)
|
|
968
|
+
? settings[moves.holdCap.setting]
|
|
969
|
+
: undefined;
|
|
970
|
+
if (setting?.type !== "number") {
|
|
971
|
+
contract(
|
|
972
|
+
`${path}.holdCap.setting`,
|
|
973
|
+
`"${moves.holdCap.setting}" must be a number setting of ${m.id}`,
|
|
974
|
+
);
|
|
975
|
+
}
|
|
976
|
+
if (!moves.guest) contract(`${path}.holdCap`, "holdCap limits guests' holds: declare guest");
|
|
977
|
+
if (!object.claims) contract(`${path}.holdCap`, `${name} must claim places (claims)`);
|
|
978
|
+
}
|
|
979
|
+
const states = object.states;
|
|
980
|
+
if (!states) {
|
|
981
|
+
contract(path, `${name} must declare states: the order moves it`);
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
984
|
+
const values = new Set((object.fields[states.field]?.options ?? []).map((o) => o.value));
|
|
985
|
+
const finals = new Set(states.moves.filter((mv) => mv.final).map((mv) => mv.to));
|
|
986
|
+
for (const key of ["held", "paid", "expired"] as const) {
|
|
987
|
+
if (!values.has(moves[key])) {
|
|
988
|
+
contract(`${path}.${key}`, `"${moves[key]}" is not a state of ${name}`);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
if (new Set([moves.held, moves.paid, moves.expired]).size !== 3) {
|
|
992
|
+
contract(path, "held, paid and expired are three different states");
|
|
993
|
+
}
|
|
994
|
+
const creators = states.create?.in?.[moves.held] ?? [];
|
|
995
|
+
if (!creators.includes("host")) {
|
|
996
|
+
contract(
|
|
997
|
+
`${path}.held`,
|
|
998
|
+
`states.create.in must let the host create ${name} in "${moves.held}"`,
|
|
999
|
+
);
|
|
1000
|
+
}
|
|
1001
|
+
if (creators.some((by) => by !== "host")) {
|
|
1002
|
+
contract(
|
|
1003
|
+
`${path}.held`,
|
|
1004
|
+
`Only the host creates ${name} in "${moves.held}": anyone else would hold it with no order`,
|
|
1005
|
+
);
|
|
1006
|
+
}
|
|
1007
|
+
const hostMove = (to: string) =>
|
|
1008
|
+
states.moves.some(
|
|
1009
|
+
(mv) => mv.to === to && moveOrigins(mv).includes(moves.held) && mv.by.includes("host"),
|
|
1010
|
+
);
|
|
1011
|
+
for (const key of ["paid", "expired"] as const) {
|
|
1012
|
+
if (!hostMove(moves[key])) {
|
|
1013
|
+
contract(
|
|
1014
|
+
`${path}.${key}`,
|
|
1015
|
+
`${name} needs a move from "${moves.held}" to "${moves[key]}" the host may make`,
|
|
1016
|
+
);
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
if (!finals.has(moves.expired)) {
|
|
1020
|
+
contract(`${path}.expired`, `"${moves.expired}" must be a final state of ${name}`);
|
|
1021
|
+
}
|
|
1022
|
+
for (const mv of states.moves) {
|
|
1023
|
+
if (!moveOrigins(mv).includes(moves.held)) continue;
|
|
1024
|
+
if (mv.by.some((by) => by !== "host")) {
|
|
1025
|
+
contract(
|
|
1026
|
+
`${path}.held`,
|
|
1027
|
+
`Only the host moves ${name} out of "${moves.held}" (to "${mv.to}"): its order says where it goes`,
|
|
1028
|
+
);
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
for (const [i, state] of (moves.refund ?? []).entries()) {
|
|
1032
|
+
if (!values.has(state) || state === moves.held || state === moves.expired) {
|
|
1033
|
+
contract(
|
|
1034
|
+
`${path}.refund.${i}`,
|
|
1035
|
+
`"${state}" must be a state of ${name} other than held and expired`,
|
|
1036
|
+
);
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
const allowance = object.allowance;
|
|
1040
|
+
if (allowance && "spends" in allowance && !allowance.spends.waived) {
|
|
1041
|
+
contract(
|
|
1042
|
+
path,
|
|
1043
|
+
`${name} spends uses: declare allowance.spends.waived, which the host sets on a record its order pays for`,
|
|
1044
|
+
);
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
|
|
454
1048
|
function checkGrants(
|
|
455
1049
|
m: ExtensionManifest,
|
|
456
1050
|
name: string,
|
package/src/slug.ts
CHANGED
|
@@ -15,11 +15,16 @@ export const SLUG_FROM_TITLE_MAX = 80;
|
|
|
15
15
|
* a hyphen, at most SLUG_FROM_TITLE_MAX characters. Empty when nothing is left.
|
|
16
16
|
*/
|
|
17
17
|
export function slugify(title: string): string {
|
|
18
|
-
|
|
18
|
+
return hyphenate(title, SLUG_FROM_TITLE_MAX);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Text as words of lowercase letters and digits joined by hyphens, accents dropped, at most `max`. */
|
|
22
|
+
export function hyphenate(text: string, max: number): string {
|
|
23
|
+
const plain = text
|
|
19
24
|
.normalize("NFKD")
|
|
20
25
|
.replace(/[̀-ͯ]/g, "")
|
|
21
26
|
.toLowerCase()
|
|
22
27
|
.replace(/[^a-z0-9]+/g, "-")
|
|
23
28
|
.replace(/^-+|-+$/g, "");
|
|
24
|
-
return plain.slice(0,
|
|
29
|
+
return plain.slice(0, max).replace(/-+$/g, "");
|
|
25
30
|
}
|