@composable-os/sdk 0.1.0-beta.11 → 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.js +1 -2
- package/dist/checklists.d.ts +41 -0
- package/dist/checklists.js +75 -0
- package/dist/contract.d.ts +495 -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 +944 -56
- package/dist/manifest.js +1036 -60
- 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 +153 -2
- package/dist/sellable.js +188 -0
- 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 +17 -1
- package/dist/states.js +48 -20
- 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 +1 -2
- package/src/checklists.ts +118 -0
- package/src/contract.ts +489 -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 +1236 -65
- 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 +344 -0
- package/src/slug.ts +7 -2
- package/src/staffed.ts +474 -0
- package/src/states.ts +68 -26
- 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/runtime.ts
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
durationMs,
|
|
17
17
|
notificationInput,
|
|
18
18
|
} from "./automation.ts";
|
|
19
|
+
import { DEFAULT_WAIT_TIMEOUT, NOTIFY_OPERATION } from "./automation-check.ts";
|
|
19
20
|
import type {
|
|
20
21
|
AiModule,
|
|
21
22
|
Extension,
|
|
@@ -27,6 +28,7 @@ import type {
|
|
|
27
28
|
StorageModule,
|
|
28
29
|
} from "./contract.ts";
|
|
29
30
|
import { toManifest } from "./manifest.ts";
|
|
31
|
+
import type { RoutineMailModule, RoutineSendResult } from "./routine-mail.ts";
|
|
30
32
|
|
|
31
33
|
type HostFn = (...args: unknown[]) => Promise<unknown>;
|
|
32
34
|
|
|
@@ -36,6 +38,8 @@ export interface RawHostModules {
|
|
|
36
38
|
storage?: { get: HostFn; set: HostFn; del: HostFn; list: HostFn };
|
|
37
39
|
log?: { write: HostFn };
|
|
38
40
|
ai?: { summarise: HostFn; extract: HostFn; classify: HostFn };
|
|
41
|
+
/** `codemode:routine-mail` (ADR 0590). */
|
|
42
|
+
"routine-mail"?: { send: HostFn };
|
|
39
43
|
/** `codemode:<gatekeeper>` modules, by gatekeeper name. */
|
|
40
44
|
[gatekeeper: string]: Record<string, HostFn> | undefined;
|
|
41
45
|
}
|
|
@@ -56,9 +60,7 @@ export type RunResponse =
|
|
|
56
60
|
| { ok: false; error: { code: string; message: string; details?: unknown } };
|
|
57
61
|
|
|
58
62
|
/** What `codemode:ops`'s `call` returns. */
|
|
59
|
-
export type OpsReply =
|
|
60
|
-
| { ok: true; value: unknown }
|
|
61
|
-
| { ok: false; error: { code: string; message: string; details?: unknown } };
|
|
63
|
+
export type OpsReply = RunResponse;
|
|
62
64
|
|
|
63
65
|
/** Thrown by `ctx.ops.call` when the operation fails; `code` is the pipeline's error code. */
|
|
64
66
|
export class OpsCallError extends Error {
|
|
@@ -72,6 +74,13 @@ export class OpsCallError extends Error {
|
|
|
72
74
|
}
|
|
73
75
|
}
|
|
74
76
|
|
|
77
|
+
// The host answers with an envelope so error codes survive the isolate boundary.
|
|
78
|
+
function unwrap(reply: unknown): unknown {
|
|
79
|
+
const r = reply as OpsReply;
|
|
80
|
+
if (r.ok) return r.value;
|
|
81
|
+
throw new OpsCallError(r.error.code, r.error.message, r.error.details);
|
|
82
|
+
}
|
|
83
|
+
|
|
75
84
|
function missing(name: string): never {
|
|
76
85
|
throw new Error(
|
|
77
86
|
`Host module "${name}" is not declared. Add "${name}" to permissions.modules in defineExtension.`,
|
|
@@ -97,10 +106,7 @@ export function buildContext(
|
|
|
97
106
|
const ops: OpsModule = raw.ops
|
|
98
107
|
? {
|
|
99
108
|
call: async (operation, input) => {
|
|
100
|
-
|
|
101
|
-
const reply = (await raw.ops?.call(operation, input ?? {})) as OpsReply;
|
|
102
|
-
if (reply.ok) return reply.value as never;
|
|
103
|
-
throw new OpsCallError(reply.error.code, reply.error.message, reply.error.details);
|
|
109
|
+
return unwrap(await raw.ops?.call(operation, input ?? {})) as never;
|
|
104
110
|
},
|
|
105
111
|
}
|
|
106
112
|
: absent("ops");
|
|
@@ -130,15 +136,21 @@ export function buildContext(
|
|
|
130
136
|
classify: (text, labels) => raw.ai?.classify(text, labels) as Promise<never>,
|
|
131
137
|
}
|
|
132
138
|
: absent("ai");
|
|
139
|
+
const routine = raw["routine-mail"];
|
|
140
|
+
const routineMail: RoutineMailModule = routine?.send
|
|
141
|
+
? {
|
|
142
|
+
send: async (input) => {
|
|
143
|
+
return unwrap(await routine.send?.(input)) as RoutineSendResult;
|
|
144
|
+
},
|
|
145
|
+
}
|
|
146
|
+
: absent("routine-mail");
|
|
133
147
|
const gatekeeper = (name: string): GatekeeperModule => {
|
|
134
148
|
const mod = raw[name] as Record<string, HostFn> | undefined;
|
|
135
149
|
if (!mod?.call) return absent<GatekeeperModule>(name);
|
|
136
150
|
return {
|
|
137
151
|
connections: () => mod.connections?.() as Promise<never>,
|
|
138
152
|
call: async (connectionId, resource, method, ...args) => {
|
|
139
|
-
|
|
140
|
-
if (reply.ok) return reply.value as never;
|
|
141
|
-
throw new OpsCallError(reply.error.code, reply.error.message, reply.error.details);
|
|
153
|
+
return unwrap(await mod.call?.(connectionId, resource, method, args)) as never;
|
|
142
154
|
},
|
|
143
155
|
types: (connectionId, resource) => mod.types?.(connectionId, resource) as Promise<string>,
|
|
144
156
|
};
|
|
@@ -150,6 +162,7 @@ export function buildContext(
|
|
|
150
162
|
log,
|
|
151
163
|
ai,
|
|
152
164
|
gatekeeper: gatekeeper as ExtensionContext["gatekeeper"],
|
|
165
|
+
routineMail,
|
|
153
166
|
...(caller ? { caller: Object.freeze({ ...caller }) as StartedBy } : {}),
|
|
154
167
|
};
|
|
155
168
|
}
|
|
@@ -262,7 +275,7 @@ export function buildAutomationContext(
|
|
|
262
275
|
const n = (counts.get(step) ?? 0) + 1;
|
|
263
276
|
counts.set(step, n);
|
|
264
277
|
const input = notificationInput(event, to, message, `${step}:${n}`);
|
|
265
|
-
const out = (await base.ops.call(
|
|
278
|
+
const out = (await base.ops.call(NOTIFY_OPERATION as never, input as never)) as {
|
|
266
279
|
sent?: number;
|
|
267
280
|
} | null;
|
|
268
281
|
return { sent: typeof out?.sent === "number" ? out.sent : 0 };
|
|
@@ -380,7 +393,7 @@ export async function runAutomation(
|
|
|
380
393
|
};
|
|
381
394
|
const r = replayed(name, "waitForEvent");
|
|
382
395
|
if (r) return r.value ?? null;
|
|
383
|
-
const timeoutMs = durationMs(declaredWait.timeout ??
|
|
396
|
+
const timeoutMs = durationMs(declaredWait.timeout ?? DEFAULT_WAIT_TIMEOUT);
|
|
384
397
|
if (test) {
|
|
385
398
|
testSteps.push({ name, kind: "waitForEvent", skippedMs: timeoutMs });
|
|
386
399
|
return null;
|
package/src/screens.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// What decides a shipped pack's staff screens (stage 47, ADR 0660), as one string: the screens it
|
|
2
|
+
// declares and everything their gates are judged by (each gate tool's risk, audiences, permissions
|
|
3
|
+
// and `humanOnly`, the pack's restricted and grantable permissions and its roles). The admin's
|
|
4
|
+
// build computes it from each pack in the repository (screens-plugin.ts) and hands the host the
|
|
5
|
+
// list; `screens.mine` lists a pack's screens only when the installed row's manifest gives the same
|
|
6
|
+
// string. So a row that holds a pack's id with another manifest (an org's own extension taking an
|
|
7
|
+
// unlisted pack's id, a copy whose gate tools say less) borrows none of the pack's built screens.
|
|
8
|
+
import type { ExtensionManifest } from "./manifest.ts";
|
|
9
|
+
|
|
10
|
+
/** JSON with object keys sorted, so the same manifest gives the same string. */
|
|
11
|
+
function canonical(value: unknown): string {
|
|
12
|
+
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
|
13
|
+
if (value && typeof value === "object") {
|
|
14
|
+
const entries = Object.entries(value as Record<string, unknown>)
|
|
15
|
+
.filter(([, v]) => v !== undefined)
|
|
16
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
17
|
+
return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(",")}}`;
|
|
18
|
+
}
|
|
19
|
+
return JSON.stringify(value ?? null);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const sorted = (list: readonly string[] | undefined) => [...(list ?? [])].sort();
|
|
23
|
+
|
|
24
|
+
/** The gate fingerprint of a manifest's screens, or null when it declares none. */
|
|
25
|
+
export function screenGateFingerprint(m: Partial<ExtensionManifest>): string | null {
|
|
26
|
+
const screens = m.screens ?? {};
|
|
27
|
+
if (!Object.keys(screens).length) return null;
|
|
28
|
+
const toolNames = new Set(Object.values(screens).flatMap((s) => s.opens ?? []));
|
|
29
|
+
const tools = Object.fromEntries(
|
|
30
|
+
[...toolNames].sort().map((name) => {
|
|
31
|
+
const tool = m.tools?.[name];
|
|
32
|
+
return [
|
|
33
|
+
name,
|
|
34
|
+
tool
|
|
35
|
+
? {
|
|
36
|
+
risk: tool.risk,
|
|
37
|
+
audiences: sorted(tool.audiences ?? ["staff"]),
|
|
38
|
+
permission: sorted(
|
|
39
|
+
tool.permission === undefined
|
|
40
|
+
? []
|
|
41
|
+
: typeof tool.permission === "string"
|
|
42
|
+
? [tool.permission]
|
|
43
|
+
: tool.permission,
|
|
44
|
+
),
|
|
45
|
+
humanOnly: tool.humanOnly === true,
|
|
46
|
+
grantLiftsRoleLimits: tool.grantLiftsRoleLimits === true,
|
|
47
|
+
// ADR 0652: only when set, so a pack declaring neither keeps its fingerprint.
|
|
48
|
+
...(tool.anyStaff ? { anyStaff: true } : {}),
|
|
49
|
+
...(tool.corePermission ? { corePermission: tool.corePermission } : {}),
|
|
50
|
+
}
|
|
51
|
+
: null,
|
|
52
|
+
];
|
|
53
|
+
}),
|
|
54
|
+
);
|
|
55
|
+
return canonical({
|
|
56
|
+
id: m.id,
|
|
57
|
+
screens: Object.fromEntries(
|
|
58
|
+
Object.entries(screens).map(([key, s]) => [
|
|
59
|
+
key,
|
|
60
|
+
{
|
|
61
|
+
label: s.label,
|
|
62
|
+
block: s.block,
|
|
63
|
+
export: s.export,
|
|
64
|
+
nav: s.nav ? { group: s.nav.group, order: s.nav.order ?? null } : null,
|
|
65
|
+
opens: [...(s.opens ?? [])],
|
|
66
|
+
writes: [...(s.writes ?? [])],
|
|
67
|
+
record: s.record ? { object: s.record.object, as: s.record.as } : null,
|
|
68
|
+
},
|
|
69
|
+
]),
|
|
70
|
+
),
|
|
71
|
+
tools,
|
|
72
|
+
restricted: sorted(m.restricted),
|
|
73
|
+
grantable: sorted(m.grantable),
|
|
74
|
+
roles: Object.fromEntries(
|
|
75
|
+
Object.entries(m.roles ?? {})
|
|
76
|
+
.sort(([a], [b]) => (a < b ? -1 : 1))
|
|
77
|
+
.map(([name, r]) => [
|
|
78
|
+
name,
|
|
79
|
+
{
|
|
80
|
+
permissions: sorted(r.permissions),
|
|
81
|
+
read: sorted(r.read),
|
|
82
|
+
today: r.today ?? null,
|
|
83
|
+
// Left out when absent, so a role without it keeps its fingerprint (ADR 0889).
|
|
84
|
+
looksAfter: r.looksAfter,
|
|
85
|
+
// Left out when absent too (ADR 0755).
|
|
86
|
+
mine: r.mine,
|
|
87
|
+
},
|
|
88
|
+
]),
|
|
89
|
+
),
|
|
90
|
+
});
|
|
91
|
+
}
|
package/src/sellable.ts
CHANGED
|
@@ -114,6 +114,121 @@ export interface SellableSpec {
|
|
|
114
114
|
* `grants`, `deposit`, `recurring`, `onePerPerson`, `maxPerOrder` or `saleWindow`.
|
|
115
115
|
*/
|
|
116
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
|
+
};
|
|
117
232
|
}
|
|
118
233
|
|
|
119
234
|
/**
|
|
@@ -192,8 +307,23 @@ export interface RecurringSpec {
|
|
|
192
307
|
* `subscriptions:manage`, as a person; while paused nothing is charged, granted or spent.
|
|
193
308
|
*/
|
|
194
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 };
|
|
195
322
|
}
|
|
196
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
|
+
|
|
197
327
|
/** Where a field of what a subscription grants gets its value (see RecurringSpec.grants). */
|
|
198
328
|
export type RecurringGrantSource =
|
|
199
329
|
| "item"
|
|
@@ -349,6 +479,7 @@ export const sellableManifestSchema = z
|
|
|
349
479
|
.object({ minDays: fieldName.optional(), maxDays: fieldName.optional() })
|
|
350
480
|
.strict()
|
|
351
481
|
.optional(),
|
|
482
|
+
coolingOff: z.object({ setting: fieldName }).strict().optional(),
|
|
352
483
|
grants: z
|
|
353
484
|
.object({
|
|
354
485
|
object: z.string().max(40),
|
|
@@ -371,6 +502,33 @@ export const sellableManifestSchema = z
|
|
|
371
502
|
})
|
|
372
503
|
.strict()
|
|
373
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(),
|
|
374
532
|
})
|
|
375
533
|
.strict();
|
|
376
534
|
export type SellableManifest = z.infer<typeof sellableManifestSchema>;
|
|
@@ -444,6 +602,34 @@ export function checkObjectSellable(
|
|
|
444
602
|
contract(`${path}.recurring.pause.${key}`, `"${days}" must be a number field of ${name}`);
|
|
445
603
|
}
|
|
446
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
|
+
}
|
|
447
633
|
if (sellable.onePerPerson !== undefined) {
|
|
448
634
|
contract(
|
|
449
635
|
`${path}.onePerPerson`,
|
|
@@ -493,6 +679,10 @@ export function checkObjectSellable(
|
|
|
493
679
|
}
|
|
494
680
|
if (sellable.grants) checkGrants(m, name, object, sellable.grants, `${path}.grants`, contract);
|
|
495
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
|
+
}
|
|
496
686
|
const place = sellable.tax?.place;
|
|
497
687
|
if (place) {
|
|
498
688
|
let holder: ObjectManifest | undefined = object;
|
|
@@ -530,6 +720,160 @@ export function checkObjectSellable(
|
|
|
530
720
|
}
|
|
531
721
|
}
|
|
532
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
|
+
|
|
533
877
|
/**
|
|
534
878
|
* Checks `sellable.moves` (ADR 0560): each of these is a way the order's batch could otherwise fail
|
|
535
879
|
* (a payment the host could not apply), or a record someone could hold with no order.
|
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
|
}
|