@composable-os/sdk 0.0.0-stage → 0.1.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +109 -0
- package/dist/allowance.d.ts +198 -0
- package/dist/allowance.js +374 -0
- package/dist/automation-check.d.ts +38 -0
- package/dist/automation-check.js +454 -0
- package/dist/automation.d.ts +767 -0
- package/dist/automation.js +303 -0
- package/dist/behaviour.d.ts +3 -0
- package/dist/behaviour.js +153 -0
- package/dist/capacity.d.ts +77 -0
- package/dist/capacity.js +127 -0
- package/dist/contract.d.ts +819 -0
- package/dist/contract.js +42 -0
- package/dist/document.d.ts +211 -0
- package/dist/document.js +726 -0
- package/dist/field-types.d.ts +11 -0
- package/dist/field-types.js +98 -0
- package/dist/formula/ast.d.ts +67 -0
- package/dist/formula/ast.js +22 -0
- package/dist/formula/compile.d.ts +37 -0
- package/dist/formula/compile.js +379 -0
- package/dist/formula/index.d.ts +3 -0
- package/dist/formula/index.js +4 -0
- package/dist/formula/parser.d.ts +13 -0
- package/dist/formula/parser.js +365 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +24 -0
- package/dist/manifest.d.ts +1596 -0
- package/dist/manifest.js +1420 -0
- package/dist/media.d.ts +41 -0
- package/dist/media.js +90 -0
- package/dist/permissions.d.ts +17 -0
- package/dist/permissions.js +44 -0
- package/dist/recurring.d.ts +114 -0
- package/dist/recurring.js +173 -0
- package/dist/renamed.d.ts +16 -0
- package/dist/renamed.js +59 -0
- package/dist/rich-text-fixture.d.ts +121 -0
- package/dist/rich-text-fixture.js +67 -0
- package/dist/rich-text.d.ts +48 -0
- package/dist/rich-text.js +184 -0
- package/dist/runtime.d.ts +95 -0
- package/dist/runtime.js +312 -0
- package/dist/sellable.d.ts +253 -0
- package/dist/sellable.js +593 -0
- package/dist/slug.d.ts +11 -0
- package/dist/slug.js +23 -0
- package/dist/states.d.ts +238 -0
- package/dist/states.js +315 -0
- package/dist/testing.d.ts +151 -0
- package/dist/testing.js +679 -0
- package/dist/unique.d.ts +20 -0
- package/dist/unique.js +58 -0
- package/package.json +34 -4
- package/src/allowance.ts +522 -0
- package/src/automation-check.ts +534 -0
- package/src/automation.ts +743 -0
- package/src/behaviour.ts +215 -0
- package/src/capacity.ts +201 -0
- package/src/contract.ts +884 -0
- package/src/document.ts +935 -0
- package/src/field-types.ts +111 -0
- package/src/formula/ast.ts +72 -0
- package/src/formula/compile.ts +454 -0
- package/src/formula/index.ts +4 -0
- package/src/formula/parser.ts +417 -0
- package/src/index.ts +36 -0
- package/src/manifest.ts +1709 -0
- package/src/media.ts +108 -0
- package/src/permissions.ts +60 -0
- package/src/recurring.ts +321 -0
- package/src/renamed.ts +63 -0
- package/src/rich-text-fixture.ts +68 -0
- package/src/rich-text.ts +221 -0
- package/src/runtime.ts +410 -0
- package/src/sellable.ts +888 -0
- package/src/slug.ts +25 -0
- package/src/states.ts +484 -0
- package/src/testing.ts +847 -0
- package/src/unique.ts +81 -0
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
2
|
+
|
|
3
|
+
> This licence covers the repository except the directories listed in NOTICE. In particular, the
|
|
4
|
+
> assistant packages `packages/agent-*` (agent-core, agent-a2ui, agent-voice, agent-plugins) are
|
|
5
|
+
> licensed under the MIT License; see the LICENSE file in each of those directories (ADR 0350).
|
|
6
|
+
|
|
7
|
+
## Abbreviation
|
|
8
|
+
|
|
9
|
+
FSL-1.1-ALv2
|
|
10
|
+
|
|
11
|
+
## Notice
|
|
12
|
+
|
|
13
|
+
Copyright 2026 Tim O'Connell
|
|
14
|
+
|
|
15
|
+
## Terms and Conditions
|
|
16
|
+
|
|
17
|
+
### Licensor ("We")
|
|
18
|
+
|
|
19
|
+
The party offering the Software under these Terms and Conditions.
|
|
20
|
+
|
|
21
|
+
### The Software
|
|
22
|
+
|
|
23
|
+
The "Software" is each version of the software that we make available under
|
|
24
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
25
|
+
Conditions with the Software.
|
|
26
|
+
|
|
27
|
+
### License Grant
|
|
28
|
+
|
|
29
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
30
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
31
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
32
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
33
|
+
|
|
34
|
+
### Permitted Purpose
|
|
35
|
+
|
|
36
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
37
|
+
means making the Software available to others in a commercial product or
|
|
38
|
+
service that:
|
|
39
|
+
|
|
40
|
+
1. substitutes for the Software;
|
|
41
|
+
|
|
42
|
+
2. substitutes for any other product or service we offer using the Software
|
|
43
|
+
that exists as of the date we make the Software available; or
|
|
44
|
+
|
|
45
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
46
|
+
|
|
47
|
+
Permitted Purposes specifically include using the Software:
|
|
48
|
+
|
|
49
|
+
1. for your internal use and access;
|
|
50
|
+
|
|
51
|
+
2. for non-commercial education;
|
|
52
|
+
|
|
53
|
+
3. for non-commercial research; and
|
|
54
|
+
|
|
55
|
+
4. in connection with professional services that you provide to a licensee
|
|
56
|
+
using the Software in accordance with these Terms and Conditions.
|
|
57
|
+
|
|
58
|
+
### Patents
|
|
59
|
+
|
|
60
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
61
|
+
patents, the license grant above includes a license under our patents. If you
|
|
62
|
+
make a claim against any party that the Software infringes or contributes to
|
|
63
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
64
|
+
immediately.
|
|
65
|
+
|
|
66
|
+
### Redistribution
|
|
67
|
+
|
|
68
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
69
|
+
the Software.
|
|
70
|
+
|
|
71
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
72
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
73
|
+
remove any copyright notices provided in or with the Software.
|
|
74
|
+
|
|
75
|
+
### Disclaimer
|
|
76
|
+
|
|
77
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
78
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
79
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
80
|
+
|
|
81
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
82
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
83
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
84
|
+
|
|
85
|
+
### Trademarks
|
|
86
|
+
|
|
87
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
88
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
89
|
+
trademarks, trade names, service marks or product names.
|
|
90
|
+
|
|
91
|
+
## Grant of Future License
|
|
92
|
+
|
|
93
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
94
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
95
|
+
the date we make the Software available. On or after that date, you may use the
|
|
96
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
97
|
+
will apply:
|
|
98
|
+
|
|
99
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
100
|
+
this file except in compliance with the License.
|
|
101
|
+
|
|
102
|
+
You may obtain a copy of the License at
|
|
103
|
+
|
|
104
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
105
|
+
|
|
106
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
107
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
108
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
109
|
+
specific language governing permissions and limitations under the License.
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { ExtensionManifest, ObjectManifest } from "./manifest.ts";
|
|
3
|
+
/** What a holder's uses are counted over. Weeks start on Monday; both in the holder's zone. */
|
|
4
|
+
export declare const ALLOWANCE_PERIODS: readonly ["total", "week", "month"];
|
|
5
|
+
export type AllowancePeriod = (typeof ALLOWANCE_PERIODS)[number];
|
|
6
|
+
/**
|
|
7
|
+
* The read-only values the host adds to every record of a holder object: uses spent in the
|
|
8
|
+
* current period, and uses left now (0 when the holder cannot be spent now; empty when
|
|
9
|
+
* unlimited). A holder object cannot have fields of these names.
|
|
10
|
+
*/
|
|
11
|
+
export declare const ALLOWANCE_VALUES: readonly ["used", "left"];
|
|
12
|
+
/**
|
|
13
|
+
* One condition a spender's record must meet to be paid by a holder. `field` is a field of the
|
|
14
|
+
* spender (one the host copies onto it, such as a class type or a local time: see `copy` and
|
|
15
|
+
* `local`); exactly one of `in`, `atLeast` and `below` compares it.
|
|
16
|
+
*
|
|
17
|
+
* - `in`: a field of the holder holding a list (a many relation, a multi-select) or one value, or
|
|
18
|
+
* a list of values written here. An empty holder field allows any value ("all class types").
|
|
19
|
+
* - `atLeast` / `below`: a number field of the holder, or a number written here. The spender's
|
|
20
|
+
* value must be at least it, or below it (a time of day from 09:00, `atLeast: 540`, and before
|
|
21
|
+
* 16:00, `below: 960`). An empty holder field sets no bound.
|
|
22
|
+
*
|
|
23
|
+
* A spender with no value for `field` meets no condition that sets a limit.
|
|
24
|
+
*/
|
|
25
|
+
export interface AllowanceCondition {
|
|
26
|
+
field: string;
|
|
27
|
+
in?: string | readonly string[];
|
|
28
|
+
atLeast?: string | number;
|
|
29
|
+
below?: string | number;
|
|
30
|
+
}
|
|
31
|
+
/** What a holder may be spent on: an object that declares `allowance.spends`, and conditions. */
|
|
32
|
+
export interface AllowanceSpendsOn {
|
|
33
|
+
/** This extension's object (`booking`) or another extension's (`bookings.booking`). */
|
|
34
|
+
object: string;
|
|
35
|
+
when?: readonly AllowanceCondition[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A holder: its records grant uses (ADR 0493).
|
|
39
|
+
*
|
|
40
|
+
* - `person`: the relation to the Person whose uses they are. Only that Person's spenders spend
|
|
41
|
+
* them (a payer who is not the holder pays; the holder spends).
|
|
42
|
+
* - `uses`: a number field, how many uses per period. Empty means none, unless the object
|
|
43
|
+
* declares `emptyIsUnlimited: true`: then empty is unlimited (a use is still written, so
|
|
44
|
+
* attendance counts). Unlimited is always the object's explicit choice, never what a missing
|
|
45
|
+
* value falls into.
|
|
46
|
+
* - `per`: `total`, `week` or `month`, or `{ field }`, a select field whose options are those.
|
|
47
|
+
* Unused uses never carry over: each period starts at nought.
|
|
48
|
+
* - `from`, `until`: date or datetime fields. A spender whose time is before `from` or at or
|
|
49
|
+
* after `until` cannot be paid by it; a date `until` includes that day.
|
|
50
|
+
* - `live`: with `states`, the states in which it can be spent (`active`); required then, and
|
|
51
|
+
* left out on an object without states.
|
|
52
|
+
* - `zone`: a timezone field; weeks, months and dates are in it. Left out (or empty), the
|
|
53
|
+
* organisation's zone.
|
|
54
|
+
* - `spendsOn`: what it pays for, one or several.
|
|
55
|
+
*
|
|
56
|
+
* None of these fields, nor the holder fields a condition compares with, may be writable by a
|
|
57
|
+
* customer by any route (`customer.write`, or a tool's `customerFields`): a customer does not set
|
|
58
|
+
* their own uses.
|
|
59
|
+
*/
|
|
60
|
+
export interface AllowanceHolderSpec {
|
|
61
|
+
person: string;
|
|
62
|
+
uses: string;
|
|
63
|
+
per: AllowancePeriod | {
|
|
64
|
+
field: string;
|
|
65
|
+
};
|
|
66
|
+
spendsOn: AllowanceSpendsOn | readonly AllowanceSpendsOn[];
|
|
67
|
+
from?: string;
|
|
68
|
+
until?: string;
|
|
69
|
+
live?: readonly string[];
|
|
70
|
+
zone?: string;
|
|
71
|
+
emptyIsUnlimited?: boolean;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* A spender: its records spend a use when they enter a state of `spendIn`, and give it back when
|
|
75
|
+
* they enter a state of `returnIn` (ADR 0493). Any other move keeps the use (a late cancel, a
|
|
76
|
+
* no-show, an attendance). The object must declare `states`.
|
|
77
|
+
*
|
|
78
|
+
* - `person`: the relation to the Person spending. The holder must be theirs.
|
|
79
|
+
* - `holder`: a text field naming the holder spent from (a record id). A caller may set it to
|
|
80
|
+
* choose one of the Person's holders; left empty, the host chooses (ADR 0494) and fills it.
|
|
81
|
+
* - `at`: the spender's time, a datetime field of its own or one relation away
|
|
82
|
+
* (`class.startsAt`). The use counts in the holder's period containing it, and the holder must
|
|
83
|
+
* be live then. Left out: the time of the spend.
|
|
84
|
+
* - `copy`: read-only fields of its own the host fills, at create and at each spend, from one
|
|
85
|
+
* relation away (`{ classType: "class.classType" }`), so a holder's `when` can read them and no
|
|
86
|
+
* caller can set them.
|
|
87
|
+
* - `local`: read-only number fields of its own the host fills from `at` in `zone` (a timezone
|
|
88
|
+
* field of its own or one relation away; the organisation's when left out or empty):
|
|
89
|
+
* `weekday` (1 Monday to 7 Sunday) and `minute` (minutes since local midnight).
|
|
90
|
+
* - `movedFrom`: a relation to another record of this object, and the state it is moved into.
|
|
91
|
+
* A record created into a spend state with it set takes the use of the record it names: in the
|
|
92
|
+
* same batch the host moves that record to `state` (a return state) and spends from the same
|
|
93
|
+
* holder, so a move never fails for want of a use the member already had.
|
|
94
|
+
* - `paidBy`: holders of other extensions that may pay for it, by object (`memberships.pass`) or
|
|
95
|
+
* by extension (`memberships`). Left out, only this extension's holders. A holder naming a
|
|
96
|
+
* spender of another extension that does not list it is refused at install.
|
|
97
|
+
* - `waived` (ADR 0506): a read-only boolean field of its own. A record created with it true
|
|
98
|
+
* enters its spend states without spending (a walk-in who paid at the desk, a drop-in paid by
|
|
99
|
+
* its order). Only the host and this extension's own code set it, never a customer, and it
|
|
100
|
+
* cannot change while the record is in a spend state.
|
|
101
|
+
*
|
|
102
|
+
* Every field a holder's condition reads must be one the host fills (`copy` or `local`), so no
|
|
103
|
+
* caller (a customer, staff, an AI) sets what pays for it.
|
|
104
|
+
*/
|
|
105
|
+
export interface AllowanceSpenderSpec {
|
|
106
|
+
person: string;
|
|
107
|
+
holder: string;
|
|
108
|
+
spendIn: readonly string[];
|
|
109
|
+
returnIn: readonly string[];
|
|
110
|
+
at?: string;
|
|
111
|
+
copy?: Readonly<Record<string, string>>;
|
|
112
|
+
local?: {
|
|
113
|
+
weekday?: string;
|
|
114
|
+
minute?: string;
|
|
115
|
+
zone?: string;
|
|
116
|
+
};
|
|
117
|
+
movedFrom?: {
|
|
118
|
+
field: string;
|
|
119
|
+
state: string;
|
|
120
|
+
};
|
|
121
|
+
paidBy?: readonly string[];
|
|
122
|
+
waived?: string;
|
|
123
|
+
}
|
|
124
|
+
/** The allowance behaviour: an object holds uses or spends them, never both. */
|
|
125
|
+
export type AllowanceSpec = {
|
|
126
|
+
holds: AllowanceHolderSpec;
|
|
127
|
+
} | {
|
|
128
|
+
spends: AllowanceSpenderSpec;
|
|
129
|
+
};
|
|
130
|
+
export declare const allowanceManifestSchema: z.ZodUnion<readonly [z.ZodObject<{
|
|
131
|
+
holds: z.ZodObject<{
|
|
132
|
+
person: z.ZodString;
|
|
133
|
+
uses: z.ZodString;
|
|
134
|
+
per: z.ZodUnion<readonly [z.ZodEnum<{
|
|
135
|
+
week: "week";
|
|
136
|
+
month: "month";
|
|
137
|
+
total: "total";
|
|
138
|
+
}>, z.ZodObject<{
|
|
139
|
+
field: z.ZodString;
|
|
140
|
+
}, z.core.$strict>]>;
|
|
141
|
+
spendsOn: z.ZodUnion<readonly [z.ZodObject<{
|
|
142
|
+
object: z.ZodString;
|
|
143
|
+
when: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
144
|
+
field: z.ZodString;
|
|
145
|
+
in: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>>;
|
|
146
|
+
atLeast: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
|
|
147
|
+
below: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
|
|
148
|
+
}, z.core.$strict>>>;
|
|
149
|
+
}, z.core.$strict>, z.ZodArray<z.ZodObject<{
|
|
150
|
+
object: z.ZodString;
|
|
151
|
+
when: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
152
|
+
field: z.ZodString;
|
|
153
|
+
in: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodArray<z.ZodString>]>>;
|
|
154
|
+
atLeast: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
|
|
155
|
+
below: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
|
|
156
|
+
}, z.core.$strict>>>;
|
|
157
|
+
}, z.core.$strict>>]>;
|
|
158
|
+
from: z.ZodOptional<z.ZodString>;
|
|
159
|
+
until: z.ZodOptional<z.ZodString>;
|
|
160
|
+
live: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
161
|
+
zone: z.ZodOptional<z.ZodString>;
|
|
162
|
+
emptyIsUnlimited: z.ZodOptional<z.ZodBoolean>;
|
|
163
|
+
}, z.core.$strict>;
|
|
164
|
+
}, z.core.$strict>, z.ZodObject<{
|
|
165
|
+
spends: z.ZodObject<{
|
|
166
|
+
person: z.ZodString;
|
|
167
|
+
holder: z.ZodString;
|
|
168
|
+
spendIn: z.ZodArray<z.ZodString>;
|
|
169
|
+
returnIn: z.ZodArray<z.ZodString>;
|
|
170
|
+
at: z.ZodOptional<z.ZodString>;
|
|
171
|
+
copy: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
172
|
+
local: z.ZodOptional<z.ZodObject<{
|
|
173
|
+
weekday: z.ZodOptional<z.ZodString>;
|
|
174
|
+
minute: z.ZodOptional<z.ZodString>;
|
|
175
|
+
zone: z.ZodOptional<z.ZodString>;
|
|
176
|
+
}, z.core.$strict>>;
|
|
177
|
+
movedFrom: z.ZodOptional<z.ZodObject<{
|
|
178
|
+
field: z.ZodString;
|
|
179
|
+
state: z.ZodString;
|
|
180
|
+
}, z.core.$strict>>;
|
|
181
|
+
paidBy: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
182
|
+
waived: z.ZodOptional<z.ZodString>;
|
|
183
|
+
}, z.core.$strict>;
|
|
184
|
+
}, z.core.$strict>]>;
|
|
185
|
+
export type AllowanceManifest = z.infer<typeof allowanceManifestSchema>;
|
|
186
|
+
/** The spendsOn declarations of a holder, as a list. */
|
|
187
|
+
export declare function spendsOnList(holds: Pick<AllowanceHolderSpec, "spendsOn">): readonly AllowanceSpendsOn[];
|
|
188
|
+
/** The fields a spender's host fills: what its holders' conditions may read. */
|
|
189
|
+
export declare function hostFilledFields(spends: Pick<AllowanceSpenderSpec, "copy" | "local">): string[];
|
|
190
|
+
/**
|
|
191
|
+
* Whether a spender (of extension `spenderExtension`) lets a holder object pay for it:
|
|
192
|
+
* `paidBy` names the holder's object or its extension; left out, its own extension's only.
|
|
193
|
+
*/
|
|
194
|
+
export declare function spenderAllows(spends: Pick<AllowanceSpenderSpec, "paidBy">, spenderExtension: string, holderObject: string): boolean;
|
|
195
|
+
/** The fields of `object` (local name `name`) a customer can write by any route. */
|
|
196
|
+
export declare function customerWritable(m: ExtensionManifest, name: string): Set<string>;
|
|
197
|
+
/** Reports problems with `objects.<name>.allowance` through `contract`. */
|
|
198
|
+
export declare function checkObjectAllowance(m: ExtensionManifest, name: string, object: ObjectManifest, contract: (path: string, message: string) => void): void;
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
// The `allowance` behaviour (stage 32b, ADRs 0493 and 0494): a record that holds a number of uses
|
|
2
|
+
// (a pass, a membership), and a record that spends one (a booking, a visit). The host keeps the
|
|
3
|
+
// count and refuses, in the database, a spend past it in the spender's own batch, for every actor.
|
|
4
|
+
// Its declaration, its manifest schema and its contract checks live here; the host's side is
|
|
5
|
+
// packages/core/src/allowance/.
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
/** What a holder's uses are counted over. Weeks start on Monday; both in the holder's zone. */
|
|
8
|
+
export const ALLOWANCE_PERIODS = ["total", "week", "month"];
|
|
9
|
+
/**
|
|
10
|
+
* The read-only values the host adds to every record of a holder object: uses spent in the
|
|
11
|
+
* current period, and uses left now (0 when the holder cannot be spent now; empty when
|
|
12
|
+
* unlimited). A holder object cannot have fields of these names.
|
|
13
|
+
*/
|
|
14
|
+
export const ALLOWANCE_VALUES = ["used", "left"];
|
|
15
|
+
const fieldName = z.string().max(40);
|
|
16
|
+
const path = z.string().max(81);
|
|
17
|
+
const stateValue = z.string().min(1).max(100);
|
|
18
|
+
const conditionSchema = z
|
|
19
|
+
.object({
|
|
20
|
+
field: fieldName,
|
|
21
|
+
in: z.union([fieldName, z.array(z.string().max(200)).min(1).max(100)]).optional(),
|
|
22
|
+
atLeast: z.union([fieldName, z.number()]).optional(),
|
|
23
|
+
below: z.union([fieldName, z.number()]).optional(),
|
|
24
|
+
})
|
|
25
|
+
.strict();
|
|
26
|
+
const spendsOnSchema = z
|
|
27
|
+
.object({ object: path, when: z.array(conditionSchema).max(10).optional() })
|
|
28
|
+
.strict();
|
|
29
|
+
export const allowanceManifestSchema = z.union([
|
|
30
|
+
z
|
|
31
|
+
.object({
|
|
32
|
+
holds: z
|
|
33
|
+
.object({
|
|
34
|
+
person: fieldName,
|
|
35
|
+
uses: fieldName,
|
|
36
|
+
per: z.union([z.enum(ALLOWANCE_PERIODS), z.object({ field: fieldName }).strict()]),
|
|
37
|
+
spendsOn: z.union([spendsOnSchema, z.array(spendsOnSchema).min(1).max(8)]),
|
|
38
|
+
from: fieldName.optional(),
|
|
39
|
+
until: fieldName.optional(),
|
|
40
|
+
live: z.array(stateValue).min(1).max(20).optional(),
|
|
41
|
+
zone: fieldName.optional(),
|
|
42
|
+
emptyIsUnlimited: z.boolean().optional(),
|
|
43
|
+
})
|
|
44
|
+
.strict(),
|
|
45
|
+
})
|
|
46
|
+
.strict(),
|
|
47
|
+
z
|
|
48
|
+
.object({
|
|
49
|
+
spends: z
|
|
50
|
+
.object({
|
|
51
|
+
person: fieldName,
|
|
52
|
+
holder: fieldName,
|
|
53
|
+
spendIn: z.array(stateValue).min(1).max(20),
|
|
54
|
+
returnIn: z.array(stateValue).max(20),
|
|
55
|
+
at: path.optional(),
|
|
56
|
+
copy: z.record(fieldName, path).optional(),
|
|
57
|
+
local: z
|
|
58
|
+
.object({
|
|
59
|
+
weekday: fieldName.optional(),
|
|
60
|
+
minute: fieldName.optional(),
|
|
61
|
+
zone: path.optional(),
|
|
62
|
+
})
|
|
63
|
+
.strict()
|
|
64
|
+
.optional(),
|
|
65
|
+
movedFrom: z.object({ field: fieldName, state: stateValue }).strict().optional(),
|
|
66
|
+
paidBy: z.array(path).min(1).max(20).optional(),
|
|
67
|
+
waived: fieldName.optional(),
|
|
68
|
+
})
|
|
69
|
+
.strict(),
|
|
70
|
+
})
|
|
71
|
+
.strict(),
|
|
72
|
+
]);
|
|
73
|
+
/** The spendsOn declarations of a holder, as a list. */
|
|
74
|
+
export function spendsOnList(holds) {
|
|
75
|
+
return Array.isArray(holds.spendsOn)
|
|
76
|
+
? holds.spendsOn
|
|
77
|
+
: [holds.spendsOn];
|
|
78
|
+
}
|
|
79
|
+
const DATES = new Set(["date", "datetime"]);
|
|
80
|
+
/** The fields a spender's host fills: what its holders' conditions may read. */
|
|
81
|
+
export function hostFilledFields(spends) {
|
|
82
|
+
return [
|
|
83
|
+
...Object.keys(spends.copy ?? {}),
|
|
84
|
+
...(spends.local?.weekday ? [spends.local.weekday] : []),
|
|
85
|
+
...(spends.local?.minute ? [spends.local.minute] : []),
|
|
86
|
+
];
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Whether a spender (of extension `spenderExtension`) lets a holder object pay for it:
|
|
90
|
+
* `paidBy` names the holder's object or its extension; left out, its own extension's only.
|
|
91
|
+
*/
|
|
92
|
+
export function spenderAllows(spends, spenderExtension, holderObject) {
|
|
93
|
+
const holderExtension = holderObject.split(".")[0];
|
|
94
|
+
if (holderExtension === spenderExtension)
|
|
95
|
+
return true;
|
|
96
|
+
return (spends.paidBy ?? []).some((p) => p === holderObject || p === holderExtension);
|
|
97
|
+
}
|
|
98
|
+
/** The fields of `object` (local name `name`) a customer can write by any route. */
|
|
99
|
+
export function customerWritable(m, name) {
|
|
100
|
+
const object = m.objects[name];
|
|
101
|
+
const out = new Set(object?.customer?.write ?? []);
|
|
102
|
+
if (object?.customer?.field)
|
|
103
|
+
out.add(object.customer.field);
|
|
104
|
+
for (const tool of Object.values(m.tools ?? {})) {
|
|
105
|
+
for (const field of tool.customerFields?.[`${m.id}.${name}`] ?? [])
|
|
106
|
+
out.add(field);
|
|
107
|
+
}
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
/** Reports problems with `objects.<name>.allowance` through `contract`. */
|
|
111
|
+
export function checkObjectAllowance(m, name, object, contract) {
|
|
112
|
+
const allowance = object.allowance;
|
|
113
|
+
if (!allowance)
|
|
114
|
+
return;
|
|
115
|
+
const ap = `objects.${name}.allowance`;
|
|
116
|
+
const fieldOf = (o, local) => Object.hasOwn(o.fields, local) ? o.fields[local] : undefined;
|
|
117
|
+
const customerWrites = customerWritable(m, name);
|
|
118
|
+
// A holder's Person is whose record it is: a customer sets it to themselves on create, so it
|
|
119
|
+
// is not a term they choose; it is checked against the spender's Person, never trusted.
|
|
120
|
+
if (object.customer?.field)
|
|
121
|
+
customerWrites.delete(object.customer.field);
|
|
122
|
+
const ownObject = (target) => {
|
|
123
|
+
const local = target.startsWith(`${m.id}.`)
|
|
124
|
+
? target.slice(m.id.length + 1)
|
|
125
|
+
: target.includes(".")
|
|
126
|
+
? undefined
|
|
127
|
+
: target;
|
|
128
|
+
return local !== undefined && Object.hasOwn(m.objects, local)
|
|
129
|
+
? [local, m.objects[local]]
|
|
130
|
+
: undefined;
|
|
131
|
+
};
|
|
132
|
+
const states = new Set(object.states ? (fieldOf(object, object.states.field)?.options ?? []).map((o) => o.value) : []);
|
|
133
|
+
const notCustomerWritten = (local, p, what) => {
|
|
134
|
+
if (customerWrites.has(local)) {
|
|
135
|
+
contract(p, `"${local}" is ${what}, so a customer cannot write it (customer.write or a tool's customerFields)`);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
if ("holds" in allowance) {
|
|
139
|
+
const h = allowance.holds;
|
|
140
|
+
const hp = `${ap}.holds`;
|
|
141
|
+
const person = fieldOf(object, h.person);
|
|
142
|
+
if (person?.type !== "relation" || person.many || person.to !== "person") {
|
|
143
|
+
contract(`${hp}.person`, `"${h.person}" must be a relation field (one record) to person`);
|
|
144
|
+
}
|
|
145
|
+
// The holder's Person: whose uses they are. Only the customer field itself is set to the
|
|
146
|
+
// caller by the host; any other person field must not be a customer's to write.
|
|
147
|
+
if (h.person !== object.customer?.field) {
|
|
148
|
+
notCustomerWritten(h.person, `${hp}.person`, "whose uses they are");
|
|
149
|
+
}
|
|
150
|
+
const uses = fieldOf(object, h.uses);
|
|
151
|
+
if (uses?.type !== "number" || uses.many) {
|
|
152
|
+
contract(`${hp}.uses`, `"${h.uses}" must be a number field of ${name}`);
|
|
153
|
+
}
|
|
154
|
+
notCustomerWritten(h.uses, `${hp}.uses`, "the holder's uses");
|
|
155
|
+
if (typeof h.per === "object") {
|
|
156
|
+
const per = fieldOf(object, h.per.field);
|
|
157
|
+
const options = (per?.options ?? []).map((o) => o.value);
|
|
158
|
+
if (per?.type !== "select" || per.many) {
|
|
159
|
+
contract(`${hp}.per.field`, `"${h.per.field}" must be a select field of ${name}`);
|
|
160
|
+
}
|
|
161
|
+
else if (options.some((o) => !ALLOWANCE_PERIODS.includes(o)) ||
|
|
162
|
+
per.editableOptions) {
|
|
163
|
+
contract(`${hp}.per.field`, `"${h.per.field}"'s options must be fixed and among ${ALLOWANCE_PERIODS.join(", ")}`);
|
|
164
|
+
}
|
|
165
|
+
notCustomerWritten(h.per.field, `${hp}.per.field`, "the holder's period");
|
|
166
|
+
}
|
|
167
|
+
for (const key of ["from", "until"]) {
|
|
168
|
+
const local = h[key];
|
|
169
|
+
if (local === undefined)
|
|
170
|
+
continue;
|
|
171
|
+
const field = fieldOf(object, local);
|
|
172
|
+
if (!field || !DATES.has(field.type) || field.many) {
|
|
173
|
+
contract(`${hp}.${key}`, `"${local}" must be a date or datetime field of ${name}`);
|
|
174
|
+
}
|
|
175
|
+
notCustomerWritten(local, `${hp}.${key}`, `the holder's ${key} date`);
|
|
176
|
+
}
|
|
177
|
+
if (h.zone !== undefined) {
|
|
178
|
+
const zone = fieldOf(object, h.zone);
|
|
179
|
+
if (zone?.type !== "timezone" || zone.many) {
|
|
180
|
+
contract(`${hp}.zone`, `"${h.zone}" must be a timezone field of ${name}`);
|
|
181
|
+
}
|
|
182
|
+
notCustomerWritten(h.zone, `${hp}.zone`, "the holder's zone");
|
|
183
|
+
}
|
|
184
|
+
if (object.states) {
|
|
185
|
+
if (!h.live) {
|
|
186
|
+
contract(`${hp}.live`, "A holder with states names the states it can be spent in (live)");
|
|
187
|
+
}
|
|
188
|
+
for (const state of h.live ?? []) {
|
|
189
|
+
if (!states.has(state)) {
|
|
190
|
+
contract(`${hp}.live`, `"${state}" is not an option of ${object.states.field}`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
else if (h.live) {
|
|
195
|
+
contract(`${hp}.live`, "live names states, and this object has none");
|
|
196
|
+
}
|
|
197
|
+
for (const reserved of ALLOWANCE_VALUES) {
|
|
198
|
+
if (Object.hasOwn(object.fields, reserved)) {
|
|
199
|
+
contract(`objects.${name}.fields.${reserved}`, `"${reserved}" is a value the host adds to an allowance holder; rename the field`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
spendsOnList(h).forEach((target, i) => {
|
|
203
|
+
const sp = Array.isArray(h.spendsOn) ? `${hp}.spendsOn.${i}` : `${hp}.spendsOn`;
|
|
204
|
+
const own = ownObject(target.object);
|
|
205
|
+
if (!own) {
|
|
206
|
+
if (!target.object.includes(".") || target.object.startsWith(`${m.id}.`)) {
|
|
207
|
+
contract(`${sp}.object`, `"${target.object}" is not an object of this extension`);
|
|
208
|
+
}
|
|
209
|
+
// Another extension's object: checked by the host when both are installed.
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
const [spenderName, spender] = own;
|
|
213
|
+
if (!spender.allowance || !("spends" in spender.allowance)) {
|
|
214
|
+
contract(`${sp}.object`, `${spenderName} must declare allowance.spends`);
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
const filledBy = new Set(hostFilledFields(spender.allowance.spends));
|
|
218
|
+
(target.when ?? []).forEach((clause, j) => {
|
|
219
|
+
const cp = `${sp}.when.${j}`;
|
|
220
|
+
const ops = ["in", "atLeast", "below"].filter((k) => clause[k] !== undefined);
|
|
221
|
+
if (ops.length !== 1) {
|
|
222
|
+
contract(cp, "A condition compares with exactly one of in, atLeast and below");
|
|
223
|
+
}
|
|
224
|
+
const field = fieldOf(spender, clause.field);
|
|
225
|
+
if (!field || field.type === "formula") {
|
|
226
|
+
contract(`${cp}.field`, `"${clause.field}" must be a field of ${spenderName}`);
|
|
227
|
+
}
|
|
228
|
+
else if (!filledBy.has(clause.field)) {
|
|
229
|
+
contract(`${cp}.field`, `"${clause.field}" must be a field ${spenderName}'s host fills (its copy or local), so no caller sets what pays for it`);
|
|
230
|
+
}
|
|
231
|
+
if (typeof clause.in === "string" && !fieldOf(object, clause.in)) {
|
|
232
|
+
contract(`${cp}.in`, `"${clause.in}" is not a field of ${name}`);
|
|
233
|
+
}
|
|
234
|
+
if (typeof clause.in === "string")
|
|
235
|
+
notCustomerWritten(clause.in, `${cp}.in`, "a limit");
|
|
236
|
+
for (const bound of ["atLeast", "below"]) {
|
|
237
|
+
const value = clause[bound];
|
|
238
|
+
if (typeof value !== "string")
|
|
239
|
+
continue;
|
|
240
|
+
const limit = fieldOf(object, value);
|
|
241
|
+
if (limit?.type !== "number" || limit.many) {
|
|
242
|
+
contract(`${cp}.${bound}`, `"${value}" must be a number field of ${name}`);
|
|
243
|
+
}
|
|
244
|
+
notCustomerWritten(value, `${cp}.${bound}`, "a limit");
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const s = allowance.spends;
|
|
251
|
+
const sp = `${ap}.spends`;
|
|
252
|
+
if (!object.states) {
|
|
253
|
+
contract(sp, "A spender declares states: it spends on entering a state");
|
|
254
|
+
}
|
|
255
|
+
const person = fieldOf(object, s.person);
|
|
256
|
+
if (person?.type !== "relation" || person.many || person.to !== "person") {
|
|
257
|
+
contract(`${sp}.person`, `"${s.person}" must be a relation field (one record) to person`);
|
|
258
|
+
}
|
|
259
|
+
const holder = fieldOf(object, s.holder);
|
|
260
|
+
if (holder?.type !== "text" || holder.many) {
|
|
261
|
+
contract(`${sp}.holder`, `"${s.holder}" must be a text field of ${name} (a holder's id)`);
|
|
262
|
+
}
|
|
263
|
+
const spendIn = new Set(s.spendIn);
|
|
264
|
+
for (const [key, list] of [
|
|
265
|
+
["spendIn", s.spendIn],
|
|
266
|
+
["returnIn", s.returnIn],
|
|
267
|
+
]) {
|
|
268
|
+
for (const state of list) {
|
|
269
|
+
if (object.states && !states.has(state)) {
|
|
270
|
+
contract(`${sp}.${key}`, `"${state}" is not an option of ${object.states.field}`);
|
|
271
|
+
}
|
|
272
|
+
if (key === "returnIn" && spendIn.has(state)) {
|
|
273
|
+
contract(`${sp}.returnIn`, `"${state}" cannot both spend and return a use`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
/** A path of one hop: the relation (one record) and the target field, when known. */
|
|
278
|
+
const hop = (value, p) => {
|
|
279
|
+
const [rel, target, extra] = value.split(".");
|
|
280
|
+
if (extra !== undefined || !rel || !target) {
|
|
281
|
+
contract(p, `"${value}" must be a field of ${name} or one relation away (relation.field)`);
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
const relation = fieldOf(object, rel);
|
|
285
|
+
if (relation?.type !== "relation" || relation.many || !relation.to) {
|
|
286
|
+
contract(p, `"${rel}" must be a relation field (one record) of ${name}`);
|
|
287
|
+
return undefined;
|
|
288
|
+
}
|
|
289
|
+
const other = ownObject(relation.to);
|
|
290
|
+
if (!other)
|
|
291
|
+
return null; // Another extension's object or a core one: the host checks.
|
|
292
|
+
const field = fieldOf(other[1], target);
|
|
293
|
+
if (!field)
|
|
294
|
+
contract(p, `"${target}" is not a field of ${other[0]}`);
|
|
295
|
+
return field;
|
|
296
|
+
};
|
|
297
|
+
if (s.at !== undefined) {
|
|
298
|
+
if (s.at.includes(".")) {
|
|
299
|
+
const field = hop(s.at, `${sp}.at`);
|
|
300
|
+
if (field && field.type !== "datetime") {
|
|
301
|
+
contract(`${sp}.at`, `"${s.at}" must be a datetime field`);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
const field = fieldOf(object, s.at);
|
|
306
|
+
if (field?.type !== "datetime" || field.many) {
|
|
307
|
+
contract(`${sp}.at`, `"${s.at}" must be a datetime field of ${name}`);
|
|
308
|
+
}
|
|
309
|
+
notCustomerWritten(s.at, `${sp}.at`, "the time a use is counted at");
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
const filled = (local, p, type) => {
|
|
313
|
+
const field = fieldOf(object, local);
|
|
314
|
+
if (!field || field.many || !field.readOnly || (type && field.type !== type)) {
|
|
315
|
+
contract(p, `"${local}" must be a read-only ${type ? `${type} ` : ""}field of ${name}: the host fills it`);
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
for (const [local, source] of Object.entries(s.copy ?? {})) {
|
|
319
|
+
filled(local, `${sp}.copy.${local}`);
|
|
320
|
+
if (!source.includes(".")) {
|
|
321
|
+
contract(`${sp}.copy.${local}`, `"${source}" must be one relation away (relation.field)`);
|
|
322
|
+
}
|
|
323
|
+
else
|
|
324
|
+
hop(source, `${sp}.copy.${local}`);
|
|
325
|
+
}
|
|
326
|
+
if (s.local) {
|
|
327
|
+
if (s.local.weekday !== undefined)
|
|
328
|
+
filled(s.local.weekday, `${sp}.local.weekday`, "number");
|
|
329
|
+
if (s.local.minute !== undefined)
|
|
330
|
+
filled(s.local.minute, `${sp}.local.minute`, "number");
|
|
331
|
+
if (s.local.zone !== undefined) {
|
|
332
|
+
if (s.local.zone.includes(".")) {
|
|
333
|
+
const field = hop(s.local.zone, `${sp}.local.zone`);
|
|
334
|
+
if (field && field.type !== "timezone") {
|
|
335
|
+
contract(`${sp}.local.zone`, `"${s.local.zone}" must be a timezone field`);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
if (fieldOf(object, s.local.zone)?.type !== "timezone") {
|
|
340
|
+
contract(`${sp}.local.zone`, `"${s.local.zone}" must be a timezone field of ${name}`);
|
|
341
|
+
}
|
|
342
|
+
notCustomerWritten(s.local.zone, `${sp}.local.zone`, "the zone its local time is read in");
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
for (const [i, payer] of (s.paidBy ?? []).entries()) {
|
|
347
|
+
if (!/^[a-z][a-z0-9]{1,29}(\.[a-z][a-z0-9]{0,29})?$/.test(payer)) {
|
|
348
|
+
contract(`${sp}.paidBy.${i}`, `"${payer}" must be an extension id or <extension>.<object>`);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (s.waived !== undefined) {
|
|
352
|
+
// Paid another way (ADR 0506): only the host and the extension's own code say so.
|
|
353
|
+
const waived = fieldOf(object, s.waived);
|
|
354
|
+
if (waived?.type !== "boolean" || !waived.readOnly) {
|
|
355
|
+
contract(`${sp}.waived`, `"${s.waived}" must be a read-only boolean field of ${name}`);
|
|
356
|
+
}
|
|
357
|
+
notCustomerWritten(s.waived, `${sp}.waived`, "whether it spends a use");
|
|
358
|
+
}
|
|
359
|
+
if (s.movedFrom) {
|
|
360
|
+
const rel = fieldOf(object, s.movedFrom.field);
|
|
361
|
+
if (rel?.type !== "relation" || rel.many || (rel.to !== `${m.id}.${name}` && rel.to !== name)) {
|
|
362
|
+
contract(`${sp}.movedFrom.field`, `"${s.movedFrom.field}" must be a relation field (one record) to ${m.id}.${name}`);
|
|
363
|
+
}
|
|
364
|
+
if (!s.returnIn.includes(s.movedFrom.state)) {
|
|
365
|
+
contract(`${sp}.movedFrom.state`, `"${s.movedFrom.state}" must be one of returnIn: the moved record gives its use back`);
|
|
366
|
+
}
|
|
367
|
+
if (object.states) {
|
|
368
|
+
const into = object.states.moves.filter((move) => move.to === s.movedFrom?.state);
|
|
369
|
+
if (!into.some((move) => move.by.includes("staff"))) {
|
|
370
|
+
contract(`${sp}.movedFrom.state`, `A move into "${s.movedFrom.state}" by staff must be declared: staff move a record's use`);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|