@composable-os/sdk 0.1.0-beta.11 → 0.1.0-beta.14
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/dist/mine.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { ExtensionManifest, RoleManifest } from "./manifest.ts";
|
|
3
|
+
/**
|
|
4
|
+
* How a role reaches one object's records through the records that name its holder (ADR 0755).
|
|
5
|
+
* Jobs' crew on its jobs: `{ through: "jobs.assignment", who: "staff", path: "visit.job", live:
|
|
6
|
+
* ["booked", "on_the_way", "on_site"], within: { from: "starts", to: "ends", days: 7 } }`.
|
|
7
|
+
*/
|
|
8
|
+
export interface MineSpec {
|
|
9
|
+
/** The object of this extension (`<id>.<object>`) whose records name the holder. */
|
|
10
|
+
through: string;
|
|
11
|
+
/** A `user` field of `through` (one account, `personApproves`) naming the holder. */
|
|
12
|
+
who: string;
|
|
13
|
+
/**
|
|
14
|
+
* The relations (one record each, each `personApproves`) from `through` to the object reached,
|
|
15
|
+
* dot-separated, at most three: `"visit.job"`. Left out: the object reached is `through` itself.
|
|
16
|
+
*/
|
|
17
|
+
path?: string;
|
|
18
|
+
/** The states of `through`'s `states` (its field `personApproves`) in which its record counts; left out: every record. */
|
|
19
|
+
live?: readonly string[];
|
|
20
|
+
/**
|
|
21
|
+
* A window around `through`'s own datetimes (each `personApproves`): its record counts from
|
|
22
|
+
* `days` days before `from` to `days` days after `to` (`from` when left out).
|
|
23
|
+
*/
|
|
24
|
+
within?: {
|
|
25
|
+
from: string;
|
|
26
|
+
to?: string;
|
|
27
|
+
days: number;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export declare const mineManifestSchema: z.ZodObject<{
|
|
31
|
+
through: z.ZodString;
|
|
32
|
+
who: z.ZodString;
|
|
33
|
+
path: z.ZodOptional<z.ZodString>;
|
|
34
|
+
live: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
35
|
+
within: z.ZodOptional<z.ZodObject<{
|
|
36
|
+
from: z.ZodString;
|
|
37
|
+
to: z.ZodOptional<z.ZodString>;
|
|
38
|
+
days: z.ZodNumber;
|
|
39
|
+
}, z.core.$strict>>;
|
|
40
|
+
}, z.core.$strict>;
|
|
41
|
+
export type MineManifest = z.infer<typeof mineManifestSchema>;
|
|
42
|
+
/** The most relations a path walks: a crew place to its visit to its job to its site. */
|
|
43
|
+
export declare const MINE_PATH_MAX = 3;
|
|
44
|
+
/** Reports problems with `roles.<name>.mine` through `contract`. */
|
|
45
|
+
export declare function checkRoleMine(m: ExtensionManifest, name: string, role: RoleManifest, contract: (path: string, message: string) => void): void;
|
package/dist/mine.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// A pack role's "mine" reach (stage 39 step 3, ADR 0755): a role reaches an object's records only
|
|
2
|
+
// where a live record of its pack names the holder and leads to them (Jobs' crew: the jobs on
|
|
3
|
+
// which the account has a crew place, from a week before the visit to a week after). Its
|
|
4
|
+
// declaration, its manifest schema and its contract checks live here; what the role reaches
|
|
5
|
+
// through it is the host's (packages/core/src/pack-role-mine.ts).
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
const fieldName = z.string().max(40);
|
|
8
|
+
export const mineManifestSchema = z
|
|
9
|
+
.object({
|
|
10
|
+
through: z.string().max(80),
|
|
11
|
+
who: fieldName,
|
|
12
|
+
path: z.string().max(122).optional(),
|
|
13
|
+
live: z.array(z.string().min(1).max(100)).min(1).max(20).optional(),
|
|
14
|
+
within: z
|
|
15
|
+
.object({ from: fieldName, to: fieldName.optional(), days: z.number().int().min(0).max(366) })
|
|
16
|
+
.strict()
|
|
17
|
+
.optional(),
|
|
18
|
+
})
|
|
19
|
+
.strict();
|
|
20
|
+
/** The most relations a path walks: a crew place to its visit to its job to its site. */
|
|
21
|
+
export const MINE_PATH_MAX = 3;
|
|
22
|
+
/** Reports problems with `roles.<name>.mine` through `contract`. */
|
|
23
|
+
export function checkRoleMine(m, name, role, contract) {
|
|
24
|
+
const own = (object) => {
|
|
25
|
+
const local = object.startsWith(`${m.id}.`) ? object.slice(m.id.length + 1) : null;
|
|
26
|
+
return local && Object.hasOwn(m.objects, local) ? m.objects[local] : undefined;
|
|
27
|
+
};
|
|
28
|
+
const fieldOf = (object, field) => Object.hasOwn(object.fields, field) ? object.fields[field] : undefined;
|
|
29
|
+
// Changing one of these widens whom the role reaches: a person's choice (ADR 0889's lesson).
|
|
30
|
+
const approved = (field) => field.personApproves === true;
|
|
31
|
+
const PERSON = "must be a field a person approves (personApproves): who reaches whose records is a person's to change";
|
|
32
|
+
for (const [object, spec] of Object.entries(role.mine ?? {})) {
|
|
33
|
+
const at = `roles.${name}.mine.${object}`;
|
|
34
|
+
if (!own(object)) {
|
|
35
|
+
contract(at, `"${object}" is not an object of this extension`);
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
for (const [kind, limits] of [
|
|
39
|
+
["today", role.today],
|
|
40
|
+
["looksAfter", role.looksAfter],
|
|
41
|
+
]) {
|
|
42
|
+
if (limits && Object.hasOwn(limits, object)) {
|
|
43
|
+
contract(at, `${object} is limited by ${kind} already: one limit an object`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const through = own(spec.through);
|
|
47
|
+
if (!through) {
|
|
48
|
+
contract(`${at}.through`, `"${spec.through}" is not an object of this extension`);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const who = fieldOf(through, spec.who);
|
|
52
|
+
if (who?.type !== "user" || who.many) {
|
|
53
|
+
contract(`${at}.who`, `"${spec.who}" must be a user field (one account) of ${spec.through}`);
|
|
54
|
+
}
|
|
55
|
+
else if (!approved(who)) {
|
|
56
|
+
contract(`${at}.who`, `"${spec.who}" ${PERSON}`);
|
|
57
|
+
}
|
|
58
|
+
// The path: one-record relations of this extension's objects, ending at the object reached.
|
|
59
|
+
const steps = spec.path ? spec.path.split(".") : [];
|
|
60
|
+
if (steps.length > MINE_PATH_MAX) {
|
|
61
|
+
contract(`${at}.path`, `A path walks at most ${MINE_PATH_MAX} relations`);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
let current = through;
|
|
65
|
+
let currentName = spec.through;
|
|
66
|
+
for (const step of steps) {
|
|
67
|
+
const field = current ? fieldOf(current, step) : undefined;
|
|
68
|
+
if (field?.type !== "relation" || field.many || !field.to || !own(field.to)) {
|
|
69
|
+
contract(`${at}.path`, `"${step}" must be a relation field (one record) of ${currentName} to an object of this extension`);
|
|
70
|
+
current = undefined;
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
if (!approved(field))
|
|
74
|
+
contract(`${at}.path`, `"${step}" of ${currentName} ${PERSON}`);
|
|
75
|
+
currentName = field.to;
|
|
76
|
+
current = own(field.to);
|
|
77
|
+
}
|
|
78
|
+
if (current && currentName !== object) {
|
|
79
|
+
contract(`${at}.path`, spec.path
|
|
80
|
+
? `"${spec.path}" leads to ${currentName}, not ${object}`
|
|
81
|
+
: `Without a path the object reached is ${spec.through} itself: name the relations to ${object}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (spec.live) {
|
|
85
|
+
const states = through.states;
|
|
86
|
+
const known = new Set(states ? (fieldOf(through, states.field)?.options ?? []).map((o) => o.value) : []);
|
|
87
|
+
if (!states) {
|
|
88
|
+
contract(`${at}.live`, `${spec.through} has no states: leave live out (every record counts)`);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
for (const [i, state] of spec.live.entries()) {
|
|
92
|
+
if (!known.has(state)) {
|
|
93
|
+
contract(`${at}.live.${i}`, `"${state}" is not a state of ${spec.through}`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
// Moving a record back into a live state widens the reach (a cancelled place booked again).
|
|
97
|
+
const field = fieldOf(through, states.field);
|
|
98
|
+
if (field && !approved(field)) {
|
|
99
|
+
contract(`${at}.live`, `"${states.field}" of ${spec.through} ${PERSON}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (spec.within) {
|
|
104
|
+
for (const key of ["from", "to"]) {
|
|
105
|
+
const value = spec.within[key];
|
|
106
|
+
if (value === undefined)
|
|
107
|
+
continue;
|
|
108
|
+
const field = fieldOf(through, value);
|
|
109
|
+
if (field?.type !== "datetime" || field.many) {
|
|
110
|
+
contract(`${at}.within.${key}`, `"${value}" must be a datetime field of ${spec.through}`);
|
|
111
|
+
}
|
|
112
|
+
else if (!approved(field)) {
|
|
113
|
+
// Moving the times moves the window: a place twenty days out brought into reach.
|
|
114
|
+
contract(`${at}.within.${key}`, `"${value}" ${PERSON}`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { ExtensionManifest, ObjectManifest } from "./manifest.ts";
|
|
3
|
+
/**
|
|
4
|
+
* A claim on a thing (ADR 0765): while the record is live it takes the thing its `thing` field
|
|
5
|
+
* names for its time, and the host refuses, in the database, a second live record of any object
|
|
6
|
+
* on the same thing at an overlapping time, for every actor and however many write at once
|
|
7
|
+
* (`conflict`, `rule: "room_taken"`, naming no other record). One occupant at a time: this is not
|
|
8
|
+
* a capacity.
|
|
9
|
+
*
|
|
10
|
+
* - `thing`: a relation field of this object (one record, or many: one claim per thing) to another
|
|
11
|
+
* of this extension's objects.
|
|
12
|
+
* - `starts`, `ends`: required datetime fields of this object. Ranges are half-open: 09:00 to 10:00 and
|
|
13
|
+
* 10:00 to 11:00 do not clash.
|
|
14
|
+
* - `live`: the states of the object's `states` in which the record takes its thing. A record in
|
|
15
|
+
* any other state, or in the Trash, claims nothing.
|
|
16
|
+
* - `before`, `after` (optional): minutes the claim starts earlier and ends later than the record
|
|
17
|
+
* (a gap kept free): a number field of this object, or `relation.field`, a number field of the
|
|
18
|
+
* record a relation of this object names (a service's buffer).
|
|
19
|
+
* - `only` (optional): a boolean field of the thing. Only things with it set are claimed (a room
|
|
20
|
+
* marked "one booking at a time"); without it, every thing named is.
|
|
21
|
+
*
|
|
22
|
+
* Two keys only say how the room calendar (`rooms.week`, `rooms.check`, ADR 0844) shows a claim;
|
|
23
|
+
* the claim itself is the same with or without them:
|
|
24
|
+
*
|
|
25
|
+
* - `provisional` (optional): those of `live` in which the claim is shown as provisional (a hire
|
|
26
|
+
* held before its deposit). A provisional claim refuses a clash as a firm one does.
|
|
27
|
+
* - `title` (optional): what the calendar shows for the record: a field of this object, or
|
|
28
|
+
* `relation.field`, a field of the record a relation of this object names (an appointment's
|
|
29
|
+
* service, never its client). Left out, or empty on a record, the calendar shows the object's
|
|
30
|
+
* label. Never a field that names a person.
|
|
31
|
+
*/
|
|
32
|
+
export interface OccupiesSpec {
|
|
33
|
+
thing: string;
|
|
34
|
+
starts: string;
|
|
35
|
+
ends: string;
|
|
36
|
+
live: readonly string[];
|
|
37
|
+
before?: {
|
|
38
|
+
field: string;
|
|
39
|
+
};
|
|
40
|
+
after?: {
|
|
41
|
+
field: string;
|
|
42
|
+
};
|
|
43
|
+
only?: string;
|
|
44
|
+
provisional?: readonly string[];
|
|
45
|
+
title?: string;
|
|
46
|
+
}
|
|
47
|
+
export declare const occupiesManifestSchema: z.ZodObject<{
|
|
48
|
+
thing: z.ZodString;
|
|
49
|
+
starts: z.ZodString;
|
|
50
|
+
ends: z.ZodString;
|
|
51
|
+
live: z.ZodArray<z.ZodString>;
|
|
52
|
+
before: z.ZodOptional<z.ZodObject<{
|
|
53
|
+
field: z.ZodString;
|
|
54
|
+
}, z.core.$strict>>;
|
|
55
|
+
after: z.ZodOptional<z.ZodObject<{
|
|
56
|
+
field: z.ZodString;
|
|
57
|
+
}, z.core.$strict>>;
|
|
58
|
+
only: z.ZodOptional<z.ZodString>;
|
|
59
|
+
provisional: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
60
|
+
title: z.ZodOptional<z.ZodString>;
|
|
61
|
+
}, z.core.$strict>;
|
|
62
|
+
export type OccupiesManifest = z.infer<typeof occupiesManifestSchema>;
|
|
63
|
+
/** Reports problems with `objects.<name>.occupies` through `contract`. */
|
|
64
|
+
export declare function checkObjectOccupies(m: ExtensionManifest, name: string, object: ObjectManifest, contract: (path: string, message: string) => void): void;
|
package/dist/occupies.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// The `occupies` behaviour (stage 49 step 2, ADR 0765): a record that takes a thing (a room, a
|
|
2
|
+
// chair, a hall) for its time. Its declaration, its manifest schema and its contract checks live
|
|
3
|
+
// here; the host's side (the claim, refused by the database) is packages/core/src/occupies/.
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
/** Field types a calendar title may be read from: text a staff member typed or chose. */
|
|
6
|
+
const TITLE_TYPES = ["text", "select"];
|
|
7
|
+
const fieldName = z.string().max(40);
|
|
8
|
+
const path = z.string().max(81);
|
|
9
|
+
export const occupiesManifestSchema = z
|
|
10
|
+
.object({
|
|
11
|
+
thing: fieldName,
|
|
12
|
+
starts: fieldName,
|
|
13
|
+
ends: fieldName,
|
|
14
|
+
live: z.array(z.string().min(1).max(100)).min(1).max(20),
|
|
15
|
+
before: z.object({ field: path }).strict().optional(),
|
|
16
|
+
after: z.object({ field: path }).strict().optional(),
|
|
17
|
+
only: fieldName.optional(),
|
|
18
|
+
provisional: z.array(z.string().min(1).max(100)).min(1).max(20).optional(),
|
|
19
|
+
title: path.optional(),
|
|
20
|
+
})
|
|
21
|
+
.strict();
|
|
22
|
+
/** Reports problems with `objects.<name>.occupies` through `contract`. */
|
|
23
|
+
export function checkObjectOccupies(m, name, object, contract) {
|
|
24
|
+
const occupies = object.occupies;
|
|
25
|
+
if (!occupies)
|
|
26
|
+
return;
|
|
27
|
+
const at = `objects.${name}.occupies`;
|
|
28
|
+
const own = (o, local) => Object.hasOwn(o.fields, local) ? o.fields[local] : undefined;
|
|
29
|
+
const single = (field, types) => field !== undefined && types.includes(field.type) && !field.many;
|
|
30
|
+
// Another object of this extension a relation field names (the reconciler builds triggers on
|
|
31
|
+
// its table, so it is this extension's own).
|
|
32
|
+
const named = (field) => {
|
|
33
|
+
const target = field?.type === "relation" ? field.to : undefined;
|
|
34
|
+
const local = target?.startsWith(`${m.id}.`) ? target.slice(m.id.length + 1) : undefined;
|
|
35
|
+
return local && local !== name && Object.hasOwn(m.objects, local)
|
|
36
|
+
? m.objects[local]
|
|
37
|
+
: undefined;
|
|
38
|
+
};
|
|
39
|
+
// The thing.
|
|
40
|
+
const thing = named(own(object, occupies.thing));
|
|
41
|
+
if (!thing) {
|
|
42
|
+
contract(`${at}.thing`, `"${occupies.thing}" must be a relation field of ${name} to another of this extension's objects`);
|
|
43
|
+
}
|
|
44
|
+
else if (occupies.only !== undefined && !single(own(thing, occupies.only), ["boolean"])) {
|
|
45
|
+
contract(`${at}.only`, `"${occupies.only}" must be a boolean field of ${own(object, occupies.thing)?.to}`);
|
|
46
|
+
}
|
|
47
|
+
// The time: required, or a live record with no end would sit in the room claiming nothing.
|
|
48
|
+
for (const key of ["starts", "ends"]) {
|
|
49
|
+
const field = own(object, occupies[key]);
|
|
50
|
+
if (!single(field, ["datetime"])) {
|
|
51
|
+
contract(`${at}.${key}`, `"${occupies[key]}" must be a datetime field of ${name}`);
|
|
52
|
+
}
|
|
53
|
+
else if (!field?.required) {
|
|
54
|
+
contract(`${at}.${key}`, `"${occupies[key]}" must be required: a record with no time would take its ${occupies.thing} claiming nothing`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (occupies.starts === occupies.ends) {
|
|
58
|
+
contract(`${at}.ends`, "A record's start and end are two fields");
|
|
59
|
+
}
|
|
60
|
+
// The states in which it takes its thing.
|
|
61
|
+
const states = object.states;
|
|
62
|
+
if (!states) {
|
|
63
|
+
contract(`${at}.live`, `${name} must declare states: a record takes its thing in some of them`);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
const field = own(object, states.field);
|
|
67
|
+
const values = new Set((field?.options ?? []).map((o) => o.value));
|
|
68
|
+
for (const state of occupies.live) {
|
|
69
|
+
if (!values.has(state))
|
|
70
|
+
contract(`${at}.live`, `"${state}" is not a state of ${name}`);
|
|
71
|
+
}
|
|
72
|
+
if (new Set(occupies.live).size !== occupies.live.length) {
|
|
73
|
+
contract(`${at}.live`, "List each state once");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// The states shown as provisional: some of the live ones.
|
|
77
|
+
if (occupies.provisional) {
|
|
78
|
+
for (const state of occupies.provisional) {
|
|
79
|
+
if (!occupies.live.includes(state)) {
|
|
80
|
+
contract(`${at}.provisional`, `"${state}" is not one of live: only a state that takes the ${occupies.thing} is provisional`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (new Set(occupies.provisional).size !== occupies.provisional.length) {
|
|
84
|
+
contract(`${at}.provisional`, "List each state once");
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// The calendar's title: a text or choice of this object, or of what one of its relations names
|
|
88
|
+
// (an appointment's service). A relation to a Person or an account is never one: the calendar
|
|
89
|
+
// shows no names.
|
|
90
|
+
if (occupies.title !== undefined) {
|
|
91
|
+
const [first, second, ...rest] = occupies.title.split(".");
|
|
92
|
+
const field = first === undefined ? undefined : own(object, first);
|
|
93
|
+
let ok = false;
|
|
94
|
+
if (!rest.length && second === undefined)
|
|
95
|
+
ok = single(field, TITLE_TYPES);
|
|
96
|
+
else if (!rest.length && second !== undefined && single(field, ["relation"])) {
|
|
97
|
+
const source = named(field);
|
|
98
|
+
ok = source !== undefined && single(own(source, second), TITLE_TYPES);
|
|
99
|
+
}
|
|
100
|
+
if (!ok) {
|
|
101
|
+
contract(`${at}.title`, `"${occupies.title}" must be a text or select field of ${name}, or a relation of it (one record, to another of this extension's objects) and a text or select field of what it names`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// The margins: a number field of this object, or of the record one of its relations names.
|
|
105
|
+
for (const key of ["before", "after"]) {
|
|
106
|
+
const declared = occupies[key];
|
|
107
|
+
if (!declared)
|
|
108
|
+
continue;
|
|
109
|
+
const [first, second, ...rest] = declared.field.split(".");
|
|
110
|
+
const field = first === undefined ? undefined : own(object, first);
|
|
111
|
+
let ok = false;
|
|
112
|
+
if (!rest.length && second === undefined)
|
|
113
|
+
ok = single(field, ["number"]);
|
|
114
|
+
else if (!rest.length && second !== undefined && single(field, ["relation"])) {
|
|
115
|
+
const source = named(field);
|
|
116
|
+
ok = source !== undefined && single(own(source, second), ["number"]);
|
|
117
|
+
}
|
|
118
|
+
if (!ok) {
|
|
119
|
+
contract(`${at}.${key}.field`, `"${declared.field}" must be a number field of ${name}, or a relation of it (one record, to another of this extension's objects) and a number field of what it names`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { type DocumentIssue, type DocumentSchema } from "./document.ts";
|
|
2
|
+
/** The kinds of item, in the order a builder lists them. */
|
|
3
|
+
export declare const QUESTION_KINDS: readonly ["text", "email", "phone", "number", "choice", "date", "rating", "yesNo", "consent", "heading", "paragraph"];
|
|
4
|
+
export type QuestionKind = (typeof QUESTION_KINDS)[number];
|
|
5
|
+
/** The kinds that ask nothing: no answer is given to them. */
|
|
6
|
+
export declare const QUESTION_DISPLAY_KINDS: readonly ["heading", "paragraph"];
|
|
7
|
+
/** What a consent line is for. Only `marketing` lines are "keep me updated". */
|
|
8
|
+
export declare const CONSENT_PURPOSES: readonly ["contact", "marketing", "other"];
|
|
9
|
+
export type ConsentPurpose = (typeof CONSENT_PURPOSES)[number];
|
|
10
|
+
/** The Person's fields a question may fill (only where empty: the host's rule, ADR 0721). */
|
|
11
|
+
export declare const QUESTION_PERSON_FIELDS: readonly ["name", "email", "phone"];
|
|
12
|
+
export type QuestionPersonField = (typeof QUESTION_PERSON_FIELDS)[number];
|
|
13
|
+
/** Items in a form, at most. */
|
|
14
|
+
export declare const QUESTIONS_MAX_ITEMS = 60;
|
|
15
|
+
/** Options of a choice, at most. */
|
|
16
|
+
export declare const QUESTION_MAX_OPTIONS = 50;
|
|
17
|
+
/** The most a questions value may be, in bytes of its JSON. */
|
|
18
|
+
export declare const QUESTIONS_MAX_BYTES = 262144;
|
|
19
|
+
/** A short text's longest answer, and its default. */
|
|
20
|
+
export declare const SHORT_TEXT_MAX = 500;
|
|
21
|
+
export declare const SHORT_TEXT_DEFAULT = 200;
|
|
22
|
+
/** A long text's longest answer, and its default. */
|
|
23
|
+
export declare const LONG_TEXT_MAX = 5000;
|
|
24
|
+
/** Links in any one text answer, at most. */
|
|
25
|
+
export declare const ANSWER_MAX_LINKS = 3;
|
|
26
|
+
/** The most a submission's answers may be, in bytes of their JSON. */
|
|
27
|
+
export declare const ANSWERS_MAX_BYTES = 20480;
|
|
28
|
+
/** The one schema of every form's questions. */
|
|
29
|
+
export declare const QUESTIONS_SCHEMA: DocumentSchema;
|
|
30
|
+
/**
|
|
31
|
+
* What a submission stores: the answers, in the questions' order, each with its key and kind.
|
|
32
|
+
* Private: a customer or the public never reads it, not even their own.
|
|
33
|
+
*/
|
|
34
|
+
export declare const ANSWERS_SCHEMA: DocumentSchema;
|
|
35
|
+
export interface QuestionShowIf {
|
|
36
|
+
question: string;
|
|
37
|
+
is: string;
|
|
38
|
+
}
|
|
39
|
+
export type QuestionMap = {
|
|
40
|
+
to: "person";
|
|
41
|
+
field: QuestionPersonField;
|
|
42
|
+
} | {
|
|
43
|
+
to: "destination";
|
|
44
|
+
field: string;
|
|
45
|
+
};
|
|
46
|
+
interface ItemBase {
|
|
47
|
+
key: string;
|
|
48
|
+
showIf?: QuestionShowIf;
|
|
49
|
+
}
|
|
50
|
+
interface AskedBase extends ItemBase {
|
|
51
|
+
label: string;
|
|
52
|
+
help?: string;
|
|
53
|
+
required?: boolean;
|
|
54
|
+
map?: QuestionMap;
|
|
55
|
+
}
|
|
56
|
+
export interface TextQuestion extends AskedBase {
|
|
57
|
+
kind: "text";
|
|
58
|
+
long?: boolean;
|
|
59
|
+
max?: number;
|
|
60
|
+
}
|
|
61
|
+
export interface EmailQuestion extends AskedBase {
|
|
62
|
+
kind: "email";
|
|
63
|
+
}
|
|
64
|
+
export interface PhoneQuestion extends AskedBase {
|
|
65
|
+
kind: "phone";
|
|
66
|
+
}
|
|
67
|
+
export interface NumberQuestion extends AskedBase {
|
|
68
|
+
kind: "number";
|
|
69
|
+
min?: number;
|
|
70
|
+
max?: number;
|
|
71
|
+
integer?: boolean;
|
|
72
|
+
}
|
|
73
|
+
export interface ChoiceQuestion extends AskedBase {
|
|
74
|
+
kind: "choice";
|
|
75
|
+
options: string[];
|
|
76
|
+
multiple?: boolean;
|
|
77
|
+
}
|
|
78
|
+
export interface DateQuestion extends AskedBase {
|
|
79
|
+
kind: "date";
|
|
80
|
+
}
|
|
81
|
+
export interface RatingQuestion extends AskedBase {
|
|
82
|
+
kind: "rating";
|
|
83
|
+
scale: "stars" | "ten";
|
|
84
|
+
}
|
|
85
|
+
export interface YesNoQuestion extends AskedBase {
|
|
86
|
+
kind: "yesNo";
|
|
87
|
+
}
|
|
88
|
+
export interface ConsentQuestion extends ItemBase {
|
|
89
|
+
kind: "consent";
|
|
90
|
+
words: string;
|
|
91
|
+
purpose: ConsentPurpose;
|
|
92
|
+
required?: boolean;
|
|
93
|
+
}
|
|
94
|
+
export interface HeadingItem extends ItemBase {
|
|
95
|
+
kind: "heading";
|
|
96
|
+
text: string;
|
|
97
|
+
}
|
|
98
|
+
export interface ParagraphItem extends ItemBase {
|
|
99
|
+
kind: "paragraph";
|
|
100
|
+
/** Rich text's inline runs (ADR 0440). */
|
|
101
|
+
content: unknown[];
|
|
102
|
+
}
|
|
103
|
+
export type QuestionItem = TextQuestion | EmailQuestion | PhoneQuestion | NumberQuestion | ChoiceQuestion | DateQuestion | RatingQuestion | YesNoQuestion | ConsentQuestion | HeadingItem | ParagraphItem;
|
|
104
|
+
/** A form's questions field. */
|
|
105
|
+
export interface Questions {
|
|
106
|
+
items: QuestionItem[];
|
|
107
|
+
}
|
|
108
|
+
export type Answer = {
|
|
109
|
+
kind: "text" | "email" | "phone" | "date";
|
|
110
|
+
key: string;
|
|
111
|
+
value: string;
|
|
112
|
+
} | {
|
|
113
|
+
kind: "number" | "rating";
|
|
114
|
+
key: string;
|
|
115
|
+
value: number;
|
|
116
|
+
} | {
|
|
117
|
+
kind: "yesNo" | "consent";
|
|
118
|
+
key: string;
|
|
119
|
+
value: boolean;
|
|
120
|
+
} | {
|
|
121
|
+
kind: "choice";
|
|
122
|
+
key: string;
|
|
123
|
+
values: string[];
|
|
124
|
+
};
|
|
125
|
+
/** A submission's answers field. */
|
|
126
|
+
export interface Answers {
|
|
127
|
+
answers: Answer[];
|
|
128
|
+
}
|
|
129
|
+
export interface CheckQuestionsOptions {
|
|
130
|
+
/** An anonymous form (ADR 0725): nothing maps to the Person and there is no destination. */
|
|
131
|
+
anonymous?: boolean;
|
|
132
|
+
/** The fields the form's destination object's intake lists; left out, the form has none. */
|
|
133
|
+
destinationFields?: readonly string[];
|
|
134
|
+
}
|
|
135
|
+
export type QuestionsResult = {
|
|
136
|
+
ok: true;
|
|
137
|
+
questions: Questions;
|
|
138
|
+
bytes: number;
|
|
139
|
+
} | {
|
|
140
|
+
ok: false;
|
|
141
|
+
issues: DocumentIssue[];
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* Checks a form's questions: the document schema, then what it cannot say. Every fault (up to the
|
|
145
|
+
* document check's limit) with its path from the root (`items.3.showIf.question`).
|
|
146
|
+
*/
|
|
147
|
+
export declare function checkQuestions(value: unknown, options?: CheckQuestionsOptions): QuestionsResult;
|
|
148
|
+
/** What is wrong with an answer, for a client to show beside its question. */
|
|
149
|
+
export type AnswerFault = "shape" | "unknown" | "hidden" | "required" | "type" | "length" | "links" | "size";
|
|
150
|
+
/** A fault in the answers: the question's key (null for the whole) and never the value. */
|
|
151
|
+
export interface AnswerIssue {
|
|
152
|
+
key: string | null;
|
|
153
|
+
fault: AnswerFault;
|
|
154
|
+
message: string;
|
|
155
|
+
}
|
|
156
|
+
export type AnswersResult = {
|
|
157
|
+
ok: true;
|
|
158
|
+
answers: Answers;
|
|
159
|
+
bytes: number;
|
|
160
|
+
} | {
|
|
161
|
+
ok: false;
|
|
162
|
+
issues: AnswerIssue[];
|
|
163
|
+
};
|
|
164
|
+
/**
|
|
165
|
+
* Whether an item is shown, given the answers so far: it has no "show only if", or the earlier
|
|
166
|
+
* item it names has that value (a choice of several: among those chosen). The headless entry and
|
|
167
|
+
* `checkAnswers` both read it.
|
|
168
|
+
*/
|
|
169
|
+
export declare function isQuestionShown(item: QuestionItem, questions: Questions, input: Readonly<Record<string, unknown>>): boolean;
|
|
170
|
+
/** Links in a text answer. */
|
|
171
|
+
export declare function countLinks(text: string): number;
|
|
172
|
+
/**
|
|
173
|
+
* Checks what a person sent (`{ [key]: value }`) against one version's questions, already checked
|
|
174
|
+
* by `checkQuestions`: every fault, or the answers document to store. An item that is not shown
|
|
175
|
+
* takes no answer; a question left unanswered is not stored; a consent line left unticked may be
|
|
176
|
+
* sent as false.
|
|
177
|
+
*/
|
|
178
|
+
export declare function checkAnswers(questions: Questions, input: unknown): AnswersResult;
|
|
179
|
+
export {};
|