@composable-os/cli 0.0.0-stage → 0.1.0-beta.11
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/README.md +24 -2
- package/dist/accounts-UHYLCLEY.js +45 -0
- package/dist/blocks-5IFU25J6.js +233 -0
- package/dist/chunk-2ABSVMGB.js +13 -0
- package/dist/chunk-2IVEVPMY.js +669 -0
- package/dist/chunk-2TWV3SVB.js +349 -0
- package/dist/chunk-2XC5JLBO.js +1931 -0
- package/dist/chunk-324WJXNP.js +320 -0
- package/dist/chunk-5KSXSK7Y.js +47 -0
- package/dist/chunk-6ELZRNUU.js +116 -0
- package/dist/chunk-6UWRT4VM.js +220 -0
- package/dist/chunk-6WGFA2IV.js +643 -0
- package/dist/chunk-6XTU2MTP.js +11278 -0
- package/dist/chunk-7CQGMVDO.js +63 -0
- package/dist/chunk-7D4GKZOX.js +615 -0
- package/dist/chunk-7OGCMQXG.js +284 -0
- package/dist/chunk-DHS2CV2M.js +139 -0
- package/dist/chunk-DU6OIFOU.js +2358 -0
- package/dist/chunk-E4WCYTCT.js +347 -0
- package/dist/chunk-E64SG24J.js +88 -0
- package/dist/chunk-FJIZGS5Q.js +698 -0
- package/dist/chunk-GYTEW66E.js +50 -0
- package/dist/chunk-HDIOVFAZ.js +544 -0
- package/dist/chunk-K34UKWEI.js +298 -0
- package/dist/chunk-KGHA67HG.js +765 -0
- package/dist/chunk-LAAL4YGR.js +365 -0
- package/dist/chunk-LO6WGU5Y.js +26189 -0
- package/dist/chunk-MBIKVRAC.js +1425 -0
- package/dist/chunk-P6UWZ6QQ.js +426 -0
- package/dist/chunk-PDD2ETVF.js +1302 -0
- package/dist/chunk-UB4I2QKR.js +31575 -0
- package/dist/chunk-WHKJGF36.js +2418 -0
- package/dist/chunk-XIDLW3EP.js +15471 -0
- package/dist/chunk-YFTLIEWU.js +504 -0
- package/dist/chunk-YNUZRTPH.js +182 -0
- package/dist/chunk-ZAWES2UD.js +9364 -0
- package/dist/chunk-ZZFWMVJO.js +704 -0
- package/dist/cli-5UNY5G5L.js +136 -0
- package/dist/cli-UYZMVQMR.js +24534 -0
- package/dist/cli.js +25 -0
- package/dist/client-repo-SE4EKG6W.js +55 -0
- package/dist/create.js +25 -0
- package/dist/fields-FTWWMD7N.js +50 -0
- package/dist/grants-LOW6TSWD.js +32 -0
- package/dist/invoke-6KVT6WJI.js +41 -0
- package/dist/moves-E2HKYCTK.js +41 -0
- package/dist/order-U4KZLLSJ.js +19 -0
- package/dist/person-UHLVBVLR.js +67 -0
- package/dist/plan-on-sale-TJPFCNL7.js +26 -0
- package/dist/platform/migrations/0001_init.sql +59 -0
- package/dist/platform/migrations/0002_users_settings.sql +34 -0
- package/dist/platform/migrations/0003_people_organisations.sql +225 -0
- package/dist/platform/migrations/0004_activities_tasks.sql +88 -0
- package/dist/platform/migrations/0005_views_audit.sql +79 -0
- package/dist/platform/migrations/0006_search.sql +223 -0
- package/dist/platform/migrations/0007_crm_views.sql +141 -0
- package/dist/platform/migrations/0008_audit_redaction.sql +116 -0
- package/dist/platform/migrations/0010_approval_queue.sql +90 -0
- package/dist/platform/migrations/0011_connected_apps.sql +36 -0
- package/dist/platform/migrations/0020_extensions.sql +373 -0
- package/dist/platform/migrations/0030_extension_proposals.sql +29 -0
- package/dist/platform/migrations/0050_live_row_indexes.sql +32 -0
- package/dist/platform/migrations/0060_audit_read_counts.sql +31 -0
- package/dist/platform/migrations/0061_audit_retention.sql +95 -0
- package/dist/platform/migrations/0062_run_history_retention.sql +6 -0
- package/dist/platform/migrations/0063_pending_action_compaction.sql +18 -0
- package/dist/platform/migrations/0064_drop_rate_limit.sql +6 -0
- package/dist/platform/migrations/0065_storage_stats.sql +13 -0
- package/dist/platform/migrations/0070_drop_proposals_ext_idx.sql +9 -0
- package/dist/platform/migrations/0080_views_dashboards.sql +46 -0
- package/dist/platform/migrations/0100_stage_history.sql +54 -0
- package/dist/platform/migrations/0110_integrations.sql +103 -0
- package/dist/platform/migrations/0111_email_calendar_sync.sql +137 -0
- package/dist/platform/migrations/0112_sync_item_occurred_at.sql +8 -0
- package/dist/platform/migrations/0130_automations.sql +113 -0
- package/dist/platform/migrations/0131_notifications.sql +29 -0
- package/dist/platform/migrations/0132_automation_runtime.sql +14 -0
- package/dist/platform/migrations/0150_ai_summaries_embeddings.sql +48 -0
- package/dist/platform/migrations/0160_releases.sql +41 -0
- package/dist/platform/migrations/0161_setup_wizard.sql +30 -0
- package/dist/platform/migrations/0170_automation_connections.sql +32 -0
- package/dist/platform/migrations/0171_automation_outbox.sql +23 -0
- package/dist/platform/migrations/0172_notification_delivery.sql +46 -0
- package/dist/platform/migrations/0200_assistant_conversations.sql +25 -0
- package/dist/platform/migrations/0205_policy_adjudicator.sql +61 -0
- package/dist/platform/migrations/0210_daily_digest.sql +33 -0
- package/dist/platform/migrations/0220_entitlements.sql +13 -0
- package/dist/platform/migrations/0221_support_grants.sql +33 -0
- package/dist/platform/migrations/0250_judge.sql +11 -0
- package/dist/platform/migrations/0251_adjudication_judgement.sql +8 -0
- package/dist/platform/migrations/0252_judge_usage.sql +14 -0
- package/dist/platform/migrations/0255_summary_checks.sql +9 -0
- package/dist/platform/migrations/0256_outbound_check.sql +9 -0
- package/dist/platform/migrations/0260_ai_usage.sql +20 -0
- package/dist/platform/migrations/0270_assistant_conversation_refs.sql +29 -0
- package/dist/platform/migrations/0310_assignment_rules.sql +54 -0
- package/dist/platform/migrations/0320_duplicates.sql +41 -0
- package/dist/platform/migrations/0330_erase_completeness.sql +54 -0
- package/dist/platform/migrations/0350_assistant_thread.sql +38 -0
- package/dist/platform/migrations/0351_assistant_model_roles.sql +26 -0
- package/dist/platform/migrations/0352_assistant_memory.sql +74 -0
- package/dist/platform/migrations/0358_ai_cache_observation.sql +24 -0
- package/dist/platform/migrations/0359_ai_compaction_decision.sql +25 -0
- package/dist/platform/migrations/0360_ai_cache_estimate.sql +35 -0
- package/dist/platform/migrations/0361_voice_preferences.sql +27 -0
- package/dist/platform/migrations/0362_voice_usage.sql +17 -0
- package/dist/platform/migrations/0365_routines.sql +38 -0
- package/dist/platform/migrations/0370_approval_execution.sql +20 -0
- package/dist/platform/migrations/0371_agent_jobs.sql +65 -0
- package/dist/platform/migrations/0380_rec_views.sql +94 -0
- package/dist/platform/migrations/0400_customer_audit_actor.sql +142 -0
- package/dist/platform/migrations/0401_customer_accounts.sql +61 -0
- package/dist/platform/migrations/0402_erase_requests.sql +34 -0
- package/dist/platform/migrations/0410_idempotency_keys.sql +45 -0
- package/dist/platform/migrations/0411_device_sessions.sql +33 -0
- package/dist/platform/migrations/0420_transition_history.sql +100 -0
- package/dist/platform/migrations/0421_orders.sql +134 -0
- package/dist/platform/migrations/0422_order_payments.sql +109 -0
- package/dist/platform/migrations/0440_capacity.sql +100 -0
- package/dist/platform/migrations/0441_capacity_views.sql +40 -0
- package/dist/platform/migrations/0450_order_grants.sql +21 -0
- package/dist/platform/migrations/0451_pack_roles.sql +18 -0
- package/dist/platform/migrations/0460_content.sql +59 -0
- package/dist/platform/migrations/0461_media.sql +52 -0
- package/dist/platform/migrations/0470_publish_versions.sql +36 -0
- package/dist/platform/migrations/0471_permission_grants.sql +18 -0
- package/dist/platform/migrations/0490_staff_invites.sql +40 -0
- package/dist/platform/migrations/0491_sign_in_mail_kind.sql +10 -0
- package/dist/platform/migrations/0492_email_confirmation_and_sign_in_confirm.sql +30 -0
- package/dist/platform/migrations/0500_published_media_field.sql +25 -0
- package/dist/platform/migrations/0501_import_keys.sql +23 -0
- package/dist/platform/migrations/0510_video_and_file_media.sql +109 -0
- package/dist/platform/migrations/0516_order_tax.sql +63 -0
- package/dist/platform/migrations/0520_publish_requests.sql +20 -0
- package/dist/platform/migrations/0530_subscriptions.sql +173 -0
- package/dist/platform/migrations/0531_subscription_trials.sql +56 -0
- package/dist/platform/migrations/0532_subscription_offers.sql +80 -0
- package/dist/platform/migrations/0533_allowances.sql +262 -0
- package/dist/platform/migrations/0560_recurring.sql +75 -0
- package/dist/platform/migrations/0580_subscription_grants.sql +21 -0
- package/dist/platform/migrations/0581_once_per_person.sql +34 -0
- package/dist/platform/migrations/0600_tool_actions_ran_at_queue.sql +22 -0
- package/dist/platform/migrations/0610_waitlists.sql +54 -0
- package/dist/platform/migrations/0630_subscription_changes.sql +122 -0
- package/dist/platform/migrations/0650_person_links.sql +90 -0
- package/dist/platform/migrations/0670_unmoved_guard.sql +52 -0
- package/dist/platform/migrations/0690_sellable_refund_due.sql +98 -0
- package/dist/platform/sdk/allowance.ts +522 -0
- package/dist/platform/sdk/automation-check.ts +534 -0
- package/dist/platform/sdk/automation.ts +743 -0
- package/dist/platform/sdk/behaviour.ts +215 -0
- package/dist/platform/sdk/capacity.ts +303 -0
- package/dist/platform/sdk/contract.ts +909 -0
- package/dist/platform/sdk/document.ts +935 -0
- package/dist/platform/sdk/field-types.ts +111 -0
- package/dist/platform/sdk/formula/ast.ts +72 -0
- package/dist/platform/sdk/formula/compile.ts +454 -0
- package/dist/platform/sdk/formula/index.ts +4 -0
- package/dist/platform/sdk/formula/parser.ts +417 -0
- package/dist/platform/sdk/index.ts +36 -0
- package/dist/platform/sdk/manifest.ts +1785 -0
- package/dist/platform/sdk/media.ts +108 -0
- package/dist/platform/sdk/permissions.ts +60 -0
- package/dist/platform/sdk/recurring.ts +321 -0
- package/dist/platform/sdk/renamed.ts +63 -0
- package/dist/platform/sdk/rich-text-fixture.ts +68 -0
- package/dist/platform/sdk/rich-text.ts +221 -0
- package/dist/platform/sdk/runtime.ts +410 -0
- package/dist/platform/sdk/sellable.ts +1138 -0
- package/dist/platform/sdk/slug.ts +25 -0
- package/dist/platform/sdk/states.ts +598 -0
- package/dist/platform/sdk/testing.ts +847 -0
- package/dist/platform/sdk/unique.ts +81 -0
- package/dist/products-2H5DR232.js +11 -0
- package/dist/run-7P4RQALH.js +648 -0
- package/dist/run-PO3W5BFA.js +508 -0
- package/dist/run-ZCGFMEZF.js +309 -0
- package/dist/src-CRO35DYY.js +46 -0
- package/dist/src-I3IJ4NKC.js +354 -0
- package/dist/token-MOJUA2U6.js +70 -0
- package/dist/token-util-TENRN4V7.js +6 -0
- package/dist/visibility-TVXZ67FD.js +41 -0
- package/package.json +33 -4
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// The `slug` field type (stage 28, ADR 0442): a record's address in its object, such as
|
|
2
|
+
// `summer-launch` in `/blog/summer-launch`. Lowercase letters and digits in words joined by single
|
|
3
|
+
// hyphens. The host keeps it unique among the object's live records and makes one from the title
|
|
4
|
+
// when a record is created without one.
|
|
5
|
+
|
|
6
|
+
/** What a slug may be. */
|
|
7
|
+
export const SLUG_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
8
|
+
/** The longest a slug may be. */
|
|
9
|
+
export const SLUG_MAX = 120;
|
|
10
|
+
/** The longest slug made from a title, leaving room for a `-<n>` that tells two apart. */
|
|
11
|
+
export const SLUG_FROM_TITLE_MAX = 80;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A slug made from a title: accents dropped, every run of anything that is not a letter or digit
|
|
15
|
+
* a hyphen, at most SLUG_FROM_TITLE_MAX characters. Empty when nothing is left.
|
|
16
|
+
*/
|
|
17
|
+
export function slugify(title: string): string {
|
|
18
|
+
const plain = title
|
|
19
|
+
.normalize("NFKD")
|
|
20
|
+
.replace(/[̀-ͯ]/g, "")
|
|
21
|
+
.toLowerCase()
|
|
22
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
23
|
+
.replace(/^-+|-+$/g, "");
|
|
24
|
+
return plain.slice(0, SLUG_FROM_TITLE_MAX).replace(/-+$/g, "");
|
|
25
|
+
}
|
|
@@ -0,0 +1,598 @@
|
|
|
1
|
+
// The `states` behaviour (ADR 0425): an object declares a small, fixed state machine and the host
|
|
2
|
+
// holds every write to it. Its declaration, its manifest schema and its contract checks live
|
|
3
|
+
// here; the host's side is packages/core/src/records/states.ts, on the transition engine it
|
|
4
|
+
// shares with `stages`.
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import type { ExtensionManifest, FieldManifest, ObjectManifest } from "./manifest.ts";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Who may make a move. `staff`: anyone working for the organisation (people, their assistants,
|
|
10
|
+
* MCP clients, automations and extensions acting for them). `customer`: a signed-in customer, on
|
|
11
|
+
* their own record. `host`: platform code only (a payment confirmation, an expiry); no caller of
|
|
12
|
+
* an operation can make it.
|
|
13
|
+
*/
|
|
14
|
+
export const MOVERS = ["staff", "customer", "host"] as const;
|
|
15
|
+
export type Mover = (typeof MOVERS)[number];
|
|
16
|
+
|
|
17
|
+
/** One allowed move of a state machine. */
|
|
18
|
+
export interface StateMove {
|
|
19
|
+
/** The state, or states, the record moves from. */
|
|
20
|
+
from: string | readonly string[];
|
|
21
|
+
/** The state it moves to. */
|
|
22
|
+
to: string;
|
|
23
|
+
/** Who may make it. */
|
|
24
|
+
by: readonly Mover[];
|
|
25
|
+
/** Fields that must have a value to make the move, and cannot be emptied while in `to`. */
|
|
26
|
+
requires?: readonly string[];
|
|
27
|
+
/** The move ends the lifecycle: nothing moves out of `to`. */
|
|
28
|
+
final?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* When a customer may make it (stage 31, ADR 0503): from `from` (inclusive) until `until`
|
|
31
|
+
* (exclusive), by the host's clock in the write. Staff and the host are not bound.
|
|
32
|
+
*/
|
|
33
|
+
window?: MoveWindow;
|
|
34
|
+
/**
|
|
35
|
+
* A boolean field (named as a window's `at` is), or several: while any is true, only staff and
|
|
36
|
+
* the host may make the move ("this class type is booked by the gym"). A boolean formula counts
|
|
37
|
+
* too (a class that is not scheduled).
|
|
38
|
+
*/
|
|
39
|
+
staffOnly?: string | readonly string[];
|
|
40
|
+
/**
|
|
41
|
+
* Tools of this extension that may also make the move for whoever called them (stage 32b,
|
|
42
|
+
* ADR 0513): a move by `["host"]` with `tools: ["voidPass"]` is made by the host and by that
|
|
43
|
+
* tool, and by no other staff write (not `record.update`, not the assistant, not an automation).
|
|
44
|
+
* Each tool must be `humanOnly`, or `personApproves` (ADR 0552: anyone else's call waits for a
|
|
45
|
+
* person's approval), and declare a `permission`, so the move is a person's act with that
|
|
46
|
+
* permission.
|
|
47
|
+
*/
|
|
48
|
+
tools?: readonly string[];
|
|
49
|
+
/**
|
|
50
|
+
* Its window binds staff too (ADR 0507), every staff caller, a person included: a cancel that
|
|
51
|
+
* gives a use back is in time for everyone. The host is not bound. A person who must make the
|
|
52
|
+
* move outside it does so through another move only a person's tool makes (`tools`). A move
|
|
53
|
+
* staff alone make may have a window with it (ADR 0551): a coach ticks a name from an hour
|
|
54
|
+
* before the class, not days ahead.
|
|
55
|
+
*/
|
|
56
|
+
bindsStaff?: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A move the host makes by itself once a moment has passed (ADR 0550): a booking still `booked`
|
|
61
|
+
* 30 minutes after its class ends becomes a `no_show`, if anyone on the class was ticked. The
|
|
62
|
+
* runner's minute cron finds the records due and moves each as the host, from where it was or not
|
|
63
|
+
* at all. Each needs a declared `host` move from each of `from` to `to`.
|
|
64
|
+
*/
|
|
65
|
+
export interface TimedMove {
|
|
66
|
+
/** The state, or states, the record moves from. */
|
|
67
|
+
from: string | readonly string[];
|
|
68
|
+
to: string;
|
|
69
|
+
/** A datetime field of the record, or `<relation>.<field>` one relation away (`class.endsAt`). */
|
|
70
|
+
at: string;
|
|
71
|
+
/** Minutes after `at` the move is due (0 to 10,080). */
|
|
72
|
+
minutesAfter?: number;
|
|
73
|
+
/**
|
|
74
|
+
* Only while another live record of this object pointing at the same record (through `at`'s
|
|
75
|
+
* relation) is in one of these states: a class whose attendance was taken has someone ticked.
|
|
76
|
+
*/
|
|
77
|
+
ifAnyIn?: readonly string[];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A number a window is worked out from (ADR 0505): a number; a number field named as a window's
|
|
82
|
+
* `at` is; or `{ field?, setting? }`, the field's value and, when it is empty, this extension's
|
|
83
|
+
* setting of that name (its value, or the setting's default). "A class type's own late-cancel
|
|
84
|
+
* time, else the organisation's": `{ field: "class.classType.lateMinutes", setting:
|
|
85
|
+
* "lateMinutes" }`.
|
|
86
|
+
*/
|
|
87
|
+
export type WindowNumber = number | string | { field?: string; setting?: string };
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* One end of a window (ADR 0503): the moment in the datetime field `at` (a field of the record,
|
|
91
|
+
* or `<relation>.<field>` on the record a relation of one record points to, or two relations
|
|
92
|
+
* away, `<relation>.<relation>.<field>`: ADR 0505), less `daysBefore` whole days at the same
|
|
93
|
+
* local time in `zone` (a timezone field named the same way; the organisation's zone when left
|
|
94
|
+
* out) and then `minutesBefore` minutes. Each of the two is a `WindowNumber`. A string is short
|
|
95
|
+
* for `{ at }`. When any of them has no value the window cannot be worked out, and a customer's
|
|
96
|
+
* move is refused. Every field a path passes through is this extension's own: a field of its
|
|
97
|
+
* objects, or one it adds to another extension's object (`fields: { "programs.classtype": … }`).
|
|
98
|
+
*/
|
|
99
|
+
export interface WindowBound {
|
|
100
|
+
at: string;
|
|
101
|
+
daysBefore?: WindowNumber;
|
|
102
|
+
minutesBefore?: WindowNumber;
|
|
103
|
+
zone?: string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** A window on a move or a create (ADR 0503). */
|
|
107
|
+
export interface MoveWindow {
|
|
108
|
+
from?: string | WindowBound;
|
|
109
|
+
until?: string | WindowBound;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* A state machine (ADR 0425). The host enforces it in the record operations and the record CSV
|
|
114
|
+
* import, for every actor, and in the approval queue's dry run:
|
|
115
|
+
*
|
|
116
|
+
* - A record is created in `initial`. Creating it in another state is judged as the move from
|
|
117
|
+
* `initial` to that state.
|
|
118
|
+
* - A move that is not declared is refused, with details naming the rule, the state and the
|
|
119
|
+
* field to fix. So is a declared move made by someone its `by` does not list, and a move while
|
|
120
|
+
* one of its `requires` fields is empty.
|
|
121
|
+
* - Every move is recorded in state history (entered and left times, who) in the same batch as
|
|
122
|
+
* the write and, when the state field is tracked, posted on the timeline as "Moved to <state>".
|
|
123
|
+
*/
|
|
124
|
+
export interface StatesSpec {
|
|
125
|
+
/** This object's select field holding the state. Its options are the states. */
|
|
126
|
+
field: string;
|
|
127
|
+
/** The state a new record starts in. */
|
|
128
|
+
initial: string;
|
|
129
|
+
moves: readonly StateMove[];
|
|
130
|
+
/**
|
|
131
|
+
* Rules on every create, whatever state the record is created in (ADR 0503): `in`, the states
|
|
132
|
+
* a record may be created in besides `initial`, each with who may create it there (it is then
|
|
133
|
+
* not judged as a move from `initial`: a booking is created booked or waiting by a customer,
|
|
134
|
+
* pending only by the host); the `window` in which a customer
|
|
135
|
+
* may create one; and `staffOnly`, a boolean field while true only staff and the host create.
|
|
136
|
+
* Named as a move's are.
|
|
137
|
+
*/
|
|
138
|
+
create?: {
|
|
139
|
+
in?: Readonly<Record<string, readonly Mover[]>>;
|
|
140
|
+
window?: MoveWindow;
|
|
141
|
+
staffOnly?: string | readonly string[];
|
|
142
|
+
/**
|
|
143
|
+
* Booleans named as `staffOnly` is (ADR 0507): while any is true, nobody but the host creates
|
|
144
|
+
* one, staff included (no booking into a class that was cancelled or is over).
|
|
145
|
+
*/
|
|
146
|
+
closed?: string | readonly string[];
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* Moves a caller asks for by name, resolved by the host (ADR 0503): writing the name (`cancel`)
|
|
150
|
+
* into the state field moves the record to the first of the listed states whose move from where
|
|
151
|
+
* it is the caller may make now, windows included (`["cancelled", "late_cancelled"]`). A name
|
|
152
|
+
* is not a state. A person on the staff is not bound; every other caller (a customer, the
|
|
153
|
+
* assistant, an MCP client, an automation, extension code) is resolved by the clock (ADR 0507).
|
|
154
|
+
*/
|
|
155
|
+
requests?: Readonly<Record<string, readonly string[]>>;
|
|
156
|
+
/**
|
|
157
|
+
* A cascade from the record a relation points to (ADR 0507): when that record moves into a
|
|
158
|
+
* state named in `when`, the host moves every live record of this object pointing at it, and in
|
|
159
|
+
* one of `from`, to the state `when` gives, in the same batch, as the host (a class cancelled
|
|
160
|
+
* moves its bookings to `cancelled_by_us`: their places released, their uses given back). Each
|
|
161
|
+
* target needs a `host` move from each of `from`.
|
|
162
|
+
*/
|
|
163
|
+
follows?: {
|
|
164
|
+
relation: string;
|
|
165
|
+
when: Readonly<Record<string, string>>;
|
|
166
|
+
from: readonly string[];
|
|
167
|
+
};
|
|
168
|
+
/** Moves the host makes once a moment has passed (ADR 0550), at most four. */
|
|
169
|
+
after?: readonly TimedMove[];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const fieldName = z.string().max(40);
|
|
173
|
+
const stateValue = z.string().min(1).max(100);
|
|
174
|
+
const fieldPath = z.string().min(1).max(122);
|
|
175
|
+
const windowNumber = (number: z.ZodNumber) =>
|
|
176
|
+
z.union([
|
|
177
|
+
number,
|
|
178
|
+
fieldPath,
|
|
179
|
+
z
|
|
180
|
+
.object({ field: fieldPath.optional(), setting: z.string().min(1).max(40).optional() })
|
|
181
|
+
.strict()
|
|
182
|
+
.refine((n) => n.field !== undefined || n.setting !== undefined, "Name a field or a setting"),
|
|
183
|
+
]);
|
|
184
|
+
export const windowBoundSchema = z.union([
|
|
185
|
+
fieldPath,
|
|
186
|
+
z
|
|
187
|
+
.object({
|
|
188
|
+
at: fieldPath,
|
|
189
|
+
daysBefore: windowNumber(z.number().int().min(-366).max(366)).optional(),
|
|
190
|
+
minutesBefore: windowNumber(z.number().int().min(-527_040).max(527_040)).optional(),
|
|
191
|
+
zone: fieldPath.optional(),
|
|
192
|
+
})
|
|
193
|
+
.strict(),
|
|
194
|
+
]);
|
|
195
|
+
const staffOnlyPaths = z.union([fieldPath, z.array(fieldPath).min(1).max(4)]);
|
|
196
|
+
const moveWindow = z
|
|
197
|
+
.object({ from: windowBoundSchema.optional(), until: windowBoundSchema.optional() })
|
|
198
|
+
.strict()
|
|
199
|
+
.refine((w) => w.from !== undefined || w.until !== undefined, "A window needs from or until");
|
|
200
|
+
|
|
201
|
+
export const statesManifestSchema = z.object({
|
|
202
|
+
field: fieldName,
|
|
203
|
+
initial: stateValue,
|
|
204
|
+
moves: z
|
|
205
|
+
.array(
|
|
206
|
+
z.object({
|
|
207
|
+
from: z.union([stateValue, z.array(stateValue).min(1).max(50)]),
|
|
208
|
+
to: stateValue,
|
|
209
|
+
by: z.array(z.enum(MOVERS)).min(1).max(MOVERS.length),
|
|
210
|
+
requires: z.array(fieldName).max(50).optional(),
|
|
211
|
+
final: z.boolean().optional(),
|
|
212
|
+
window: moveWindow.optional(),
|
|
213
|
+
staffOnly: staffOnlyPaths.optional(),
|
|
214
|
+
tools: z.array(z.string().min(1).max(40)).min(1).max(10).optional(),
|
|
215
|
+
bindsStaff: z.boolean().optional(),
|
|
216
|
+
}),
|
|
217
|
+
)
|
|
218
|
+
.max(200),
|
|
219
|
+
create: z
|
|
220
|
+
.object({
|
|
221
|
+
in: z
|
|
222
|
+
.record(stateValue, z.array(z.enum(MOVERS)).min(1).max(MOVERS.length))
|
|
223
|
+
.refine((r) => Object.keys(r).length <= 20, "At most 20 states")
|
|
224
|
+
.optional(),
|
|
225
|
+
window: moveWindow.optional(),
|
|
226
|
+
staffOnly: staffOnlyPaths.optional(),
|
|
227
|
+
closed: staffOnlyPaths.optional(),
|
|
228
|
+
})
|
|
229
|
+
.strict()
|
|
230
|
+
.optional(),
|
|
231
|
+
requests: z
|
|
232
|
+
.record(z.string().regex(/^[a-z][a-z0-9_]{0,39}$/), z.array(stateValue).min(1).max(10))
|
|
233
|
+
.refine((r) => Object.keys(r).length <= 10, "At most 10 requests")
|
|
234
|
+
.optional(),
|
|
235
|
+
follows: z
|
|
236
|
+
.object({
|
|
237
|
+
relation: fieldName,
|
|
238
|
+
when: z
|
|
239
|
+
.record(stateValue, stateValue)
|
|
240
|
+
.refine((r) => Object.keys(r).length >= 1 && Object.keys(r).length <= 10, "1 to 10 states"),
|
|
241
|
+
from: z.array(stateValue).min(1).max(20),
|
|
242
|
+
})
|
|
243
|
+
.strict()
|
|
244
|
+
.optional(),
|
|
245
|
+
after: z
|
|
246
|
+
.array(
|
|
247
|
+
z
|
|
248
|
+
.object({
|
|
249
|
+
from: z.union([stateValue, z.array(stateValue).min(1).max(20)]),
|
|
250
|
+
to: stateValue,
|
|
251
|
+
at: fieldPath,
|
|
252
|
+
minutesAfter: z.number().int().min(0).max(10_080).optional(),
|
|
253
|
+
ifAnyIn: z.array(stateValue).min(1).max(20).optional(),
|
|
254
|
+
})
|
|
255
|
+
.strict(),
|
|
256
|
+
)
|
|
257
|
+
.min(1)
|
|
258
|
+
.max(4)
|
|
259
|
+
.optional(),
|
|
260
|
+
});
|
|
261
|
+
export type StatesManifest = z.infer<typeof statesManifestSchema>;
|
|
262
|
+
|
|
263
|
+
/** A staff-only rule's paths, as a list (ADR 0505). */
|
|
264
|
+
export function staffOnlyPathsOf(value: string | readonly string[] | undefined): readonly string[] {
|
|
265
|
+
return value === undefined ? [] : typeof value === "string" ? [value] : value;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** The states a move leaves, as a list. */
|
|
269
|
+
export function moveOrigins(move: { from: string | readonly string[] }): readonly string[] {
|
|
270
|
+
return typeof move.from === "string" ? [move.from] : move.from;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Reports problems with `objects.<name>.states` through `contract`. */
|
|
274
|
+
export function checkObjectStates(
|
|
275
|
+
m: ExtensionManifest,
|
|
276
|
+
name: string,
|
|
277
|
+
object: ObjectManifest,
|
|
278
|
+
contract: (path: string, message: string) => void,
|
|
279
|
+
): void {
|
|
280
|
+
const states = object.states;
|
|
281
|
+
if (!states) return;
|
|
282
|
+
const sp = `objects.${name}.states`;
|
|
283
|
+
if (object.stages) {
|
|
284
|
+
contract(sp, "An object has one lifecycle: declare stages or states, not both");
|
|
285
|
+
}
|
|
286
|
+
const fieldOf = (local: string): FieldManifest | undefined =>
|
|
287
|
+
Object.hasOwn(object.fields, local) ? object.fields[local] : undefined;
|
|
288
|
+
const field = fieldOf(states.field);
|
|
289
|
+
if (field?.type !== "select") {
|
|
290
|
+
contract(`${sp}.field`, `"${states.field}" must be a select field of ${name}`);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
if (field.readOnly) {
|
|
294
|
+
contract(`${sp}.field`, "The state field cannot be read-only: the moves say who may change it");
|
|
295
|
+
}
|
|
296
|
+
if (field.editableOptions) {
|
|
297
|
+
contract(
|
|
298
|
+
`${sp}.field`,
|
|
299
|
+
"The state field cannot have editableOptions: states are fixed in the declaration",
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
const values = new Set((field.options ?? []).map((o) => o.value));
|
|
303
|
+
const isState = (value: string) => values.has(value);
|
|
304
|
+
if (!isState(states.initial)) {
|
|
305
|
+
contract(`${sp}.initial`, `"${states.initial}" is not an option of ${states.field}`);
|
|
306
|
+
}
|
|
307
|
+
if (field.default !== undefined && field.default !== states.initial) {
|
|
308
|
+
contract(
|
|
309
|
+
`${sp}.initial`,
|
|
310
|
+
`${states.field}'s default must be the initial state "${states.initial}", or be left out`,
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const seen = new Set<string>();
|
|
315
|
+
const finals = new Set(states.moves.filter((move) => move.final).map((move) => move.to));
|
|
316
|
+
states.moves.forEach((move, i) => {
|
|
317
|
+
const mp = `${sp}.moves.${i}`;
|
|
318
|
+
if (!isState(move.to)) contract(`${mp}.to`, `"${move.to}" is not an option of ${states.field}`);
|
|
319
|
+
if (new Set(move.by).size !== move.by.length) contract(`${mp}.by`, "List each mover once");
|
|
320
|
+
if (finals.has(move.to) && !move.final) {
|
|
321
|
+
contract(
|
|
322
|
+
`${mp}.final`,
|
|
323
|
+
`Another move into "${move.to}" is final; every move into a state must agree`,
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
if (move.final && move.to === states.initial) {
|
|
327
|
+
contract(`${mp}.final`, "The initial state cannot be final");
|
|
328
|
+
}
|
|
329
|
+
for (const from of moveOrigins(move)) {
|
|
330
|
+
if (!isState(from)) {
|
|
331
|
+
contract(`${mp}.from`, `"${from}" is not an option of ${states.field}`);
|
|
332
|
+
}
|
|
333
|
+
if (from === move.to) contract(`${mp}.from`, `A move cannot go from "${from}" to itself`);
|
|
334
|
+
if (finals.has(from)) {
|
|
335
|
+
contract(`${mp}.from`, `"${from}" is final: nothing moves out of it`);
|
|
336
|
+
}
|
|
337
|
+
const key = JSON.stringify([from, move.to]);
|
|
338
|
+
if (seen.has(key)) {
|
|
339
|
+
contract(mp, `The move from "${from}" to "${move.to}" is declared twice`);
|
|
340
|
+
}
|
|
341
|
+
seen.add(key);
|
|
342
|
+
}
|
|
343
|
+
for (const local of move.requires ?? []) {
|
|
344
|
+
const required = fieldOf(local);
|
|
345
|
+
if (!required || required.type === "formula" || local === states.field) {
|
|
346
|
+
contract(
|
|
347
|
+
`${mp}.requires`,
|
|
348
|
+
`"${local}" must be a field of ${name} other than a formula or the state field`,
|
|
349
|
+
);
|
|
350
|
+
} else if (required.readOnly && move.by.some((mover) => mover !== "host")) {
|
|
351
|
+
// Only the host writes a read-only field, so only a host move can fill it.
|
|
352
|
+
contract(
|
|
353
|
+
`${mp}.requires`,
|
|
354
|
+
`"${local}" is read-only, so only a move made by the host alone can require it`,
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
// Windows, staff-only rules and requests (ADR 0503; two relations and settings, ADR 0505).
|
|
361
|
+
const { checkPath, checkWindow } = windowChecks(m, object, name, contract);
|
|
362
|
+
states.moves.forEach((move, i) => {
|
|
363
|
+
const mp = `${sp}.moves.${i}`;
|
|
364
|
+
// A window binds customers, or staff too with bindsStaff (ADR 0551); staffOnly customers.
|
|
365
|
+
if (move.staffOnly && !move.by.includes("customer")) {
|
|
366
|
+
contract(mp, "staffOnly binds customers: the move must list customer in by");
|
|
367
|
+
}
|
|
368
|
+
if (move.window && !move.by.includes("customer") && !move.bindsStaff) {
|
|
369
|
+
contract(
|
|
370
|
+
mp,
|
|
371
|
+
"A window binds customers: the move must list customer in by, or bind staff (bindsStaff)",
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
checkWindow(move.window, `${mp}.window`);
|
|
375
|
+
for (const p of staffOnlyPathsOf(move.staffOnly)) checkPath(p, `${mp}.staffOnly`, "boolean");
|
|
376
|
+
// A move only named tools may make (ADR 0513): each a person's, with a permission.
|
|
377
|
+
for (const tool of move.tools ?? []) {
|
|
378
|
+
const spec = m.tools?.[tool];
|
|
379
|
+
if (!spec) {
|
|
380
|
+
contract(`${mp}.tools`, `"${tool}" is not a tool of this extension`);
|
|
381
|
+
} else if ((!spec.humanOnly && !spec.personApproves) || !spec.permission) {
|
|
382
|
+
contract(
|
|
383
|
+
`${mp}.tools`,
|
|
384
|
+
`"${tool}" must be humanOnly or personApproves and declare a permission: the move is a person's act with it`,
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
if (move.bindsStaff && (!move.window || (!move.by.includes("staff") && !move.tools))) {
|
|
389
|
+
contract(`${mp}.bindsStaff`, "bindsStaff needs a window, and staff in by or named tools");
|
|
390
|
+
}
|
|
391
|
+
if (move.tools && move.by.includes("staff")) {
|
|
392
|
+
contract(
|
|
393
|
+
`${mp}.tools`,
|
|
394
|
+
"A move named tools may make is not also open to all staff: drop staff from by",
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
if (states.create) {
|
|
399
|
+
for (const state of Object.keys(states.create.in ?? {})) {
|
|
400
|
+
if (!isState(state))
|
|
401
|
+
contract(`${sp}.create.in`, `"${state}" is not an option of ${states.field}`);
|
|
402
|
+
if (finals.has(state))
|
|
403
|
+
contract(`${sp}.create.in`, `"${state}" is final: nothing is created in it`);
|
|
404
|
+
}
|
|
405
|
+
checkWindow(states.create.window, `${sp}.create.window`);
|
|
406
|
+
for (const p of staffOnlyPathsOf(states.create.staffOnly)) {
|
|
407
|
+
checkPath(p, `${sp}.create.staffOnly`, "boolean");
|
|
408
|
+
}
|
|
409
|
+
for (const p of staffOnlyPathsOf(states.create.closed)) {
|
|
410
|
+
checkPath(p, `${sp}.create.closed`, "boolean");
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
if (states.follows) {
|
|
414
|
+
const fp = `${sp}.follows`;
|
|
415
|
+
const f = states.follows;
|
|
416
|
+
const rel = fieldOf(f.relation);
|
|
417
|
+
const target =
|
|
418
|
+
rel?.type === "relation" && !rel.many && rel.to?.startsWith(`${m.id}.`)
|
|
419
|
+
? m.objects[rel.to.slice(m.id.length + 1)]
|
|
420
|
+
: undefined;
|
|
421
|
+
if (!target?.states) {
|
|
422
|
+
contract(
|
|
423
|
+
`${fp}.relation`,
|
|
424
|
+
`"${f.relation}" must be a relation (one record) of ${name} to another of this extension's objects that declares states`,
|
|
425
|
+
);
|
|
426
|
+
} else {
|
|
427
|
+
// A parent whose places other records draw on as a pool: its counter counts claims that
|
|
428
|
+
// are not its own, which the cascade's check at commit cannot read (ADR 0550).
|
|
429
|
+
const pooled = Object.values(m.objects).some((other) => {
|
|
430
|
+
const poolField = other.capacity?.pool;
|
|
431
|
+
const relTo = poolField ? other.fields[poolField]?.to : undefined;
|
|
432
|
+
return relTo === rel?.to || relTo === rel?.to?.slice(m.id.length + 1);
|
|
433
|
+
});
|
|
434
|
+
if (pooled) {
|
|
435
|
+
contract(
|
|
436
|
+
`${fp}.relation`,
|
|
437
|
+
`${rel?.to ?? f.relation} is a pool other records draw places from: a cascade cannot follow it`,
|
|
438
|
+
);
|
|
439
|
+
}
|
|
440
|
+
const parentStates = new Set(
|
|
441
|
+
(target.fields[target.states.field]?.options ?? []).map((o) => o.value),
|
|
442
|
+
);
|
|
443
|
+
for (const [when, to] of Object.entries(f.when)) {
|
|
444
|
+
if (!parentStates.has(when)) {
|
|
445
|
+
contract(`${fp}.when`, `"${when}" is not a state of ${rel?.to ?? f.relation}`);
|
|
446
|
+
}
|
|
447
|
+
for (const from of f.from) {
|
|
448
|
+
if (!isState(from))
|
|
449
|
+
contract(`${fp}.from`, `"${from}" is not an option of ${states.field}`);
|
|
450
|
+
else if (
|
|
451
|
+
!states.moves.some(
|
|
452
|
+
(move) =>
|
|
453
|
+
move.to === to && move.by.includes("host") && moveOrigins(move).includes(from),
|
|
454
|
+
)
|
|
455
|
+
) {
|
|
456
|
+
contract(`${fp}.when`, `${name} needs a host move from "${from}" to "${to}"`);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
// Timed moves (ADR 0550): a declared host move each, and a moment the sweep reads in SQL.
|
|
463
|
+
(states.after ?? []).forEach((rule, i) => {
|
|
464
|
+
const tp = `${sp}.after.${i}`;
|
|
465
|
+
if (!isState(rule.to)) contract(`${tp}.to`, `"${rule.to}" is not an option of ${states.field}`);
|
|
466
|
+
for (const from of moveOrigins(rule)) {
|
|
467
|
+
if (!isState(from)) {
|
|
468
|
+
contract(`${tp}.from`, `"${from}" is not an option of ${states.field}`);
|
|
469
|
+
} else if (
|
|
470
|
+
!states.moves.some(
|
|
471
|
+
(move) =>
|
|
472
|
+
move.to === rule.to && move.by.includes("host") && moveOrigins(move).includes(from),
|
|
473
|
+
)
|
|
474
|
+
) {
|
|
475
|
+
contract(`${tp}.from`, `${name} needs a host move from "${from}" to "${rule.to}"`);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
const parts = rule.at.split(".");
|
|
479
|
+
const at = parts.length <= 2 ? resolveFieldPath(m, object, rule.at) : undefined;
|
|
480
|
+
const via = parts.length === 2 ? fieldOf(parts[0] as string) : undefined;
|
|
481
|
+
const ownTarget = via?.type === "relation" && !via.many && via.to?.startsWith(`${m.id}.`);
|
|
482
|
+
if (at?.type !== "datetime" || at.many || (parts.length === 2 && !ownTarget)) {
|
|
483
|
+
contract(
|
|
484
|
+
`${tp}.at`,
|
|
485
|
+
`"${rule.at}" must be a datetime field of ${name}, or <relation>.<field> on another of this extension's objects`,
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
if (rule.ifAnyIn) {
|
|
489
|
+
if (parts.length !== 2) {
|
|
490
|
+
contract(`${tp}.ifAnyIn`, "ifAnyIn needs at one relation away: the records it compares");
|
|
491
|
+
}
|
|
492
|
+
for (const state of rule.ifAnyIn) {
|
|
493
|
+
if (!isState(state)) {
|
|
494
|
+
contract(`${tp}.ifAnyIn`, `"${state}" is not an option of ${states.field}`);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
for (const [request, targets] of Object.entries(states.requests ?? {})) {
|
|
500
|
+
const rp = `${sp}.requests.${request}`;
|
|
501
|
+
if (isState(request)) contract(rp, `"${request}" is a state; a request needs its own name`);
|
|
502
|
+
if (new Set(targets).size !== targets.length) contract(rp, "List each state once");
|
|
503
|
+
for (const to of targets) {
|
|
504
|
+
if (!isState(to)) contract(rp, `"${to}" is not an option of ${states.field}`);
|
|
505
|
+
else if (!states.moves.some((move) => move.to === to)) {
|
|
506
|
+
contract(rp, `No move goes to "${to}"`);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* The checks of window paths and numbers (ADRs 0503 and 0505): for a `states` declaration, and
|
|
514
|
+
* for any other declaration that names a moment the same way (a waitlist's cut-off, ADR 0520).
|
|
515
|
+
*/
|
|
516
|
+
export function windowChecks(
|
|
517
|
+
m: ExtensionManifest,
|
|
518
|
+
object: ObjectManifest,
|
|
519
|
+
name: string,
|
|
520
|
+
contract: (path: string, message: string) => void,
|
|
521
|
+
) {
|
|
522
|
+
const checkPath = (at: string, path: string, type: string) => {
|
|
523
|
+
const field = resolveFieldPath(m, object, at);
|
|
524
|
+
// A staff-only rule may read a formula: the host computes it in the view (a boolean one).
|
|
525
|
+
const formulaOk = type === "boolean" && field?.type === "formula";
|
|
526
|
+
if ((field?.type !== type && !formulaOk) || field?.many) {
|
|
527
|
+
contract(
|
|
528
|
+
path,
|
|
529
|
+
`"${at}" must be a ${type} field of ${name}, or <relation>.<field> through up to two relations (one record each), every field on the way declared by this extension`,
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
const checkNumber = (value: WindowNumber | undefined, path: string) => {
|
|
534
|
+
if (typeof value === "string") checkPath(value, path, "number");
|
|
535
|
+
else if (typeof value === "object") {
|
|
536
|
+
if (value.field !== undefined) checkPath(value.field, `${path}.field`, "number");
|
|
537
|
+
if (value.setting !== undefined) {
|
|
538
|
+
const settings = m.settings ?? {};
|
|
539
|
+
const setting = Object.hasOwn(settings, value.setting)
|
|
540
|
+
? settings[value.setting]
|
|
541
|
+
: undefined;
|
|
542
|
+
if (setting?.type !== "number") {
|
|
543
|
+
contract(`${path}.setting`, `"${value.setting}" must be a number setting of ${m.id}`);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
const checkBound = (bound: string | WindowBound, path: string) => {
|
|
549
|
+
const b = typeof bound === "string" ? { at: bound } : bound;
|
|
550
|
+
checkPath(b.at, `${path}.at`, "datetime");
|
|
551
|
+
for (const key of ["daysBefore", "minutesBefore"] as const) {
|
|
552
|
+
checkNumber(b[key], `${path}.${key}`);
|
|
553
|
+
}
|
|
554
|
+
if (b.zone !== undefined) checkPath(b.zone, `${path}.zone`, "timezone");
|
|
555
|
+
};
|
|
556
|
+
const checkWindow = (window: MoveWindow | undefined, path: string) => {
|
|
557
|
+
for (const end of ["from", "until"] as const) {
|
|
558
|
+
const bound = window?.[end];
|
|
559
|
+
if (bound !== undefined) checkBound(bound, `${path}.${end}`);
|
|
560
|
+
}
|
|
561
|
+
};
|
|
562
|
+
return { checkPath, checkNumber, checkBound, checkWindow };
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* The field a path names (ADR 0503; two relations from ADR 0505): `field` on the object, or
|
|
567
|
+
* `relation.field` / `relation.relation.field` through relations of one record. Every field on
|
|
568
|
+
* the way is this extension's: a field of one of its objects, or one it adds to another object
|
|
569
|
+
* (`fields: { "programs.classtype": { … } }`). Undefined when the path names no such field.
|
|
570
|
+
*/
|
|
571
|
+
export function resolveFieldPath(
|
|
572
|
+
m: ExtensionManifest,
|
|
573
|
+
object: ObjectManifest,
|
|
574
|
+
path: string,
|
|
575
|
+
): FieldManifest | undefined {
|
|
576
|
+
const parts = path.split(".");
|
|
577
|
+
if (parts.length > 3) return undefined;
|
|
578
|
+
const added = m.fields ?? {};
|
|
579
|
+
/** The fields this extension declares on an object (by its full name), or on `object`. */
|
|
580
|
+
const fieldsOn = (target: string | null): Readonly<Record<string, FieldManifest>> | undefined => {
|
|
581
|
+
if (target === null) return object.fields;
|
|
582
|
+
if (target.startsWith(`${m.id}.`)) {
|
|
583
|
+
const local = target.slice(m.id.length + 1);
|
|
584
|
+
return Object.hasOwn(m.objects, local) ? m.objects[local]?.fields : undefined;
|
|
585
|
+
}
|
|
586
|
+
return Object.hasOwn(added, target) ? added[target] : undefined;
|
|
587
|
+
};
|
|
588
|
+
let on: string | null = null;
|
|
589
|
+
for (const [i, part] of parts.entries()) {
|
|
590
|
+
const fields = fieldsOn(on);
|
|
591
|
+
const field = fields && Object.hasOwn(fields, part) ? fields[part] : undefined;
|
|
592
|
+
if (!field) return undefined;
|
|
593
|
+
if (i === parts.length - 1) return field;
|
|
594
|
+
if (field.type !== "relation" || field.many || !field.to) return undefined;
|
|
595
|
+
on = field.to.includes(".") ? field.to : `${m.id}.${field.to}`;
|
|
596
|
+
}
|
|
597
|
+
return undefined;
|
|
598
|
+
}
|