@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,698 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
OPS_BY_TYPE,
|
|
4
|
+
compileFilter,
|
|
5
|
+
holdsAccess,
|
|
6
|
+
jsonList
|
|
7
|
+
} from "./chunk-ZZFWMVJO.js";
|
|
8
|
+
import {
|
|
9
|
+
CompiledQuery,
|
|
10
|
+
ForbiddenError,
|
|
11
|
+
NotFoundError,
|
|
12
|
+
OperationError,
|
|
13
|
+
ValidationError,
|
|
14
|
+
isLiveDependant,
|
|
15
|
+
linkGuardStatement,
|
|
16
|
+
ownedSource,
|
|
17
|
+
scopeOf,
|
|
18
|
+
sql,
|
|
19
|
+
unknownObject,
|
|
20
|
+
viewColumnOf,
|
|
21
|
+
visibleSource
|
|
22
|
+
} from "./chunk-6XTU2MTP.js";
|
|
23
|
+
import {
|
|
24
|
+
getObject,
|
|
25
|
+
isCoreObject
|
|
26
|
+
} from "./chunk-YFTLIEWU.js";
|
|
27
|
+
import {
|
|
28
|
+
external_exports
|
|
29
|
+
} from "./chunk-LO6WGU5Y.js";
|
|
30
|
+
|
|
31
|
+
// ../core/src/assignment/index.ts
|
|
32
|
+
var ASSIGNMENT_OPS = ["eq", "in", "between", "is_null", "is_not_null"];
|
|
33
|
+
var assignmentConditionSchema = external_exports.object({
|
|
34
|
+
field: external_exports.string().min(1).max(80).describe("A field of the object (or of the `via` record)"),
|
|
35
|
+
op: external_exports.enum(ASSIGNMENT_OPS).describe("eq (equals), in (is one of), between [low, high], is_null, is_not_null"),
|
|
36
|
+
value: external_exports.unknown().optional(),
|
|
37
|
+
via: external_exports.string().min(1).max(40).optional().describe("A relation field (e.g. `organisation`): test the related record's field instead")
|
|
38
|
+
});
|
|
39
|
+
var ASSIGNMENT_METHODS = ["roundRobin", "user"];
|
|
40
|
+
function requireOwnedObject(name) {
|
|
41
|
+
const object = getObject(name);
|
|
42
|
+
if (!object?.ownerField) {
|
|
43
|
+
throw new ValidationError(`${name} has no owner field, so assignment rules do not apply`, {
|
|
44
|
+
field: "object"
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return object;
|
|
48
|
+
}
|
|
49
|
+
function conditionTarget(object, condition) {
|
|
50
|
+
if (!condition.via) return object;
|
|
51
|
+
const relation = object.fields.find((f) => f.name === condition.via);
|
|
52
|
+
const to = relation?.options?.to;
|
|
53
|
+
if (relation?.fieldType !== "relation" || relation.options?.many || !to) {
|
|
54
|
+
throw new ValidationError(
|
|
55
|
+
`${object.name}.${condition.via} is not a relation to one record, so it cannot be used as via`,
|
|
56
|
+
{ field: "conditions", via: condition.via }
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
const target = getObject(to);
|
|
60
|
+
if (!target) throw new ValidationError(`Unknown object ${to}`, { field: "conditions" });
|
|
61
|
+
return target;
|
|
62
|
+
}
|
|
63
|
+
function validateConditions(object, conditions) {
|
|
64
|
+
conditions.forEach((condition, i) => {
|
|
65
|
+
const target = conditionTarget(object, condition);
|
|
66
|
+
const field = target.fields.find((f) => f.name === condition.field);
|
|
67
|
+
const where = `conditions[${i}]`;
|
|
68
|
+
if (!field?.filterable || !field.column) {
|
|
69
|
+
throw new ValidationError(
|
|
70
|
+
`${where}: ${target.name} has no single-valued field "${condition.field}" to test`,
|
|
71
|
+
{ field: "conditions", index: i }
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
if (!OPS_BY_TYPE[field.type].includes(condition.op)) {
|
|
75
|
+
throw new ValidationError(`${where}: ${condition.op} does not apply to ${field.label}`, {
|
|
76
|
+
field: "conditions",
|
|
77
|
+
index: i
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
if (condition.op === "between" && field.type !== "number" && !field.type.startsWith("date")) {
|
|
81
|
+
throw new ValidationError(`${where}: between needs a number, currency or date field`, {
|
|
82
|
+
field: "conditions",
|
|
83
|
+
index: i
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
compileFilter(target, { field: field.name, op: condition.op, value: condition.value });
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function isEmpty(value) {
|
|
90
|
+
return value === null || value === void 0 || typeof value === "string" && value.trim() === "" || Array.isArray(value) && value.length === 0;
|
|
91
|
+
}
|
|
92
|
+
function comparable(field, value) {
|
|
93
|
+
if (isEmpty(value)) return null;
|
|
94
|
+
switch (field.type) {
|
|
95
|
+
case "number": {
|
|
96
|
+
const n = typeof value === "number" ? value : Number(value);
|
|
97
|
+
return Number.isFinite(n) ? n : null;
|
|
98
|
+
}
|
|
99
|
+
case "boolean":
|
|
100
|
+
return value === true || value === 1 || value === "1" || value === "true" ? 1 : 0;
|
|
101
|
+
case "text":
|
|
102
|
+
return String(value).toLowerCase();
|
|
103
|
+
default:
|
|
104
|
+
return String(value);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function conditionHolds(field, condition, value) {
|
|
108
|
+
const actual = comparable(field, value);
|
|
109
|
+
switch (condition.op) {
|
|
110
|
+
case "is_null":
|
|
111
|
+
return actual === null;
|
|
112
|
+
case "is_not_null":
|
|
113
|
+
return actual !== null;
|
|
114
|
+
case "eq":
|
|
115
|
+
return actual !== null && actual === comparable(field, condition.value);
|
|
116
|
+
case "in":
|
|
117
|
+
return actual !== null && Array.isArray(condition.value) && condition.value.some((v) => comparable(field, v) === actual);
|
|
118
|
+
case "between": {
|
|
119
|
+
if (actual === null || !Array.isArray(condition.value)) return false;
|
|
120
|
+
const [low, high] = condition.value.map((v) => comparable(field, v));
|
|
121
|
+
if (low === null || low === void 0 || high === null || high === void 0) return false;
|
|
122
|
+
return actual >= low && actual <= high;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function ruleMatches(object, rule, record, related) {
|
|
127
|
+
return rule.conditions.every((condition) => {
|
|
128
|
+
const target = getObject(
|
|
129
|
+
condition.via ? object.fields.find((f) => f.name === condition.via)?.options?.to ?? "" : object.name
|
|
130
|
+
);
|
|
131
|
+
const field = target?.fields.find((f) => f.name === condition.field);
|
|
132
|
+
if (!target || !field) return false;
|
|
133
|
+
if (!condition.via) return conditionHolds(field, condition, record.get(field.name));
|
|
134
|
+
const row = related(condition.via);
|
|
135
|
+
return conditionHolds(field, condition, row && field.column ? row[field.column] : null);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function ruleFromRow(row) {
|
|
139
|
+
return {
|
|
140
|
+
id: row.id,
|
|
141
|
+
object: row.object_type,
|
|
142
|
+
name: row.name,
|
|
143
|
+
position: row.position,
|
|
144
|
+
enabled: row.enabled === 1,
|
|
145
|
+
conditions: JSON.parse(row.conditions),
|
|
146
|
+
method: row.method,
|
|
147
|
+
userIds: JSON.parse(row.user_ids)
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
async function loadRules(db, object, options = {}) {
|
|
151
|
+
const { rows } = await sql`
|
|
152
|
+
SELECT 'rule' AS kind, id, object_type, name, position, enabled, conditions, method, user_ids
|
|
153
|
+
FROM core_assignment_rule
|
|
154
|
+
WHERE object_type = ${object} ${options.enabledOnly ? sql`AND enabled = 1` : sql``}
|
|
155
|
+
UNION ALL
|
|
156
|
+
SELECT 'fallback', '', object_type, '', 0, 1, '[]', 'user', coalesce(json_array(user_id), '[]')
|
|
157
|
+
FROM core_assignment_fallback WHERE object_type = ${object}
|
|
158
|
+
ORDER BY kind DESC, position
|
|
159
|
+
`.execute(db);
|
|
160
|
+
const fallbackRow = rows.find((r) => r.kind === "fallback");
|
|
161
|
+
const fallbackIds = fallbackRow ? JSON.parse(fallbackRow.user_ids) : [];
|
|
162
|
+
return {
|
|
163
|
+
rules: rows.filter((r) => r.kind === "rule").map(ruleFromRow),
|
|
164
|
+
fallback: fallbackRow ? fallbackIds[0] ?? null : void 0
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
var states = /* @__PURE__ */ new WeakMap();
|
|
168
|
+
function stateOf(ctx) {
|
|
169
|
+
let state = states.get(ctx);
|
|
170
|
+
if (!state) {
|
|
171
|
+
state = { rules: /* @__PURE__ */ new Map(), users: /* @__PURE__ */ new Map(), rotation: /* @__PURE__ */ new Map(), related: /* @__PURE__ */ new Map() };
|
|
172
|
+
states.set(ctx, state);
|
|
173
|
+
}
|
|
174
|
+
return state;
|
|
175
|
+
}
|
|
176
|
+
async function loadUsers(ctx, state, ids) {
|
|
177
|
+
const missing = [...new Set(ids)].filter((id) => !state.users.has(id));
|
|
178
|
+
if (!missing.length) return;
|
|
179
|
+
const rows = await ctx.db.selectFrom("core_user").select(["id", "name", "deactivated_at"]).where("id", "in", jsonList(missing)).where("kind", "=", "human").execute();
|
|
180
|
+
for (const id of missing) state.users.set(id, null);
|
|
181
|
+
for (const row of rows) {
|
|
182
|
+
state.users.set(row.id, { name: row.name, active: row.deactivated_at === null });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
async function loadRelated(ctx, state, object, via, id) {
|
|
186
|
+
if (typeof id !== "string" || !id) return null;
|
|
187
|
+
const to = object.fields.find((f) => f.name === via)?.options?.to;
|
|
188
|
+
const target = to ? getObject(to) : void 0;
|
|
189
|
+
if (!target) return null;
|
|
190
|
+
const key = `${target.name}|${id}`;
|
|
191
|
+
if (!state.related.has(key)) {
|
|
192
|
+
const { rows } = await sql`
|
|
193
|
+
SELECT * FROM ${visibleSource(ctx, target)} WHERE id = ${id}
|
|
194
|
+
`.execute(ctx.db);
|
|
195
|
+
state.related.set(key, rows[0] ?? null);
|
|
196
|
+
}
|
|
197
|
+
return state.related.get(key) ?? null;
|
|
198
|
+
}
|
|
199
|
+
function rotationWrite(ruleId, rotation, now) {
|
|
200
|
+
return {
|
|
201
|
+
compile: () => CompiledQuery.raw(
|
|
202
|
+
"INSERT INTO core_assignment_state (rule_id, next_index, updated_at) VALUES (?, ?, ?) ON CONFLICT (rule_id) DO UPDATE SET next_index = CASE WHEN core_assignment_state.next_index = ? THEN excluded.next_index ELSE -1 END, updated_at = excluded.updated_at",
|
|
203
|
+
[ruleId, rotation.base + rotation.used, now, rotation.base]
|
|
204
|
+
)
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
async function assignOwner(ctx, object, record) {
|
|
208
|
+
if (!object.ownerField) return null;
|
|
209
|
+
const state = stateOf(ctx);
|
|
210
|
+
let loaded = state.rules.get(object.name);
|
|
211
|
+
if (!loaded) {
|
|
212
|
+
loaded = await loadRules(ctx.db, object.name, { enabledOnly: true });
|
|
213
|
+
state.rules.set(object.name, loaded);
|
|
214
|
+
}
|
|
215
|
+
if (!loaded.rules.length && loaded.fallback === void 0) return null;
|
|
216
|
+
await loadUsers(ctx, state, [
|
|
217
|
+
...loaded.rules.flatMap((r) => r.userIds),
|
|
218
|
+
...loaded.fallback ? [loaded.fallback] : []
|
|
219
|
+
]);
|
|
220
|
+
const active = (ids) => ids.filter((id) => state.users.get(id)?.active);
|
|
221
|
+
const vias = [...new Set(loaded.rules.flatMap((r) => r.conditions.flatMap((c) => c.via ?? [])))];
|
|
222
|
+
const related = /* @__PURE__ */ new Map();
|
|
223
|
+
for (const via of vias) {
|
|
224
|
+
related.set(via, await loadRelated(ctx, state, object, via, record.get(via)));
|
|
225
|
+
}
|
|
226
|
+
for (const rule of loaded.rules) {
|
|
227
|
+
const users = active(rule.userIds);
|
|
228
|
+
if (!users.length) continue;
|
|
229
|
+
if (!ruleMatches(object, rule, record, (via) => related.get(via) ?? null)) continue;
|
|
230
|
+
let ownerId = users[0];
|
|
231
|
+
if (rule.method === "roundRobin") {
|
|
232
|
+
let rotation = state.rotation.get(rule.id);
|
|
233
|
+
if (!rotation) {
|
|
234
|
+
const row = await ctx.db.selectFrom("core_assignment_state").select("next_index").where("rule_id", "=", rule.id).executeTakeFirst();
|
|
235
|
+
rotation = { base: row?.next_index ?? 0, used: 0 };
|
|
236
|
+
state.rotation.set(rule.id, rotation);
|
|
237
|
+
ctx.write(rotationWrite(rule.id, rotation, ctx.now));
|
|
238
|
+
}
|
|
239
|
+
ownerId = users[(rotation.base + rotation.used) % users.length];
|
|
240
|
+
rotation.used += 1;
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
ownerId,
|
|
244
|
+
ruleId: rule.id,
|
|
245
|
+
ruleName: rule.name,
|
|
246
|
+
ownerName: state.users.get(ownerId)?.name ?? null
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
const fallback = loaded.fallback && state.users.get(loaded.fallback)?.active ? loaded.fallback : null;
|
|
250
|
+
return {
|
|
251
|
+
ownerId: fallback,
|
|
252
|
+
ruleId: null,
|
|
253
|
+
ruleName: null,
|
|
254
|
+
ownerName: fallback ? state.users.get(fallback)?.name ?? null : null
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
function assignmentSubject(assignment) {
|
|
258
|
+
const who = assignment.ownerName ?? "someone";
|
|
259
|
+
return assignment.ruleName ? `Assigned to ${who} by rule ${assignment.ruleName}` : `Assigned to ${who} (fallback owner)`;
|
|
260
|
+
}
|
|
261
|
+
function recordAssignment(ctx, object, id, assignment, options = {}) {
|
|
262
|
+
if (!assignment.ownerId || !object.ownerField) return void 0;
|
|
263
|
+
if (options.timeline !== false) {
|
|
264
|
+
ctx.postActivity({
|
|
265
|
+
subject: assignmentSubject(assignment),
|
|
266
|
+
payload: {
|
|
267
|
+
kind: "assignment",
|
|
268
|
+
object: object.name,
|
|
269
|
+
recordId: id,
|
|
270
|
+
ownerId: assignment.ownerId,
|
|
271
|
+
ruleId: assignment.ruleId,
|
|
272
|
+
rule: assignment.ruleName
|
|
273
|
+
},
|
|
274
|
+
links: [{ object: object.name, id }]
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
return { field: object.ownerField, rule: assignment.ruleName ?? "Fallback owner" };
|
|
278
|
+
}
|
|
279
|
+
function isAssignmentConflict(error) {
|
|
280
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
281
|
+
return message.includes("core_assignment_state_conflict");
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// ../core/src/diff.ts
|
|
285
|
+
function stable(value) {
|
|
286
|
+
return JSON.stringify(value, (_key, v) => {
|
|
287
|
+
if (v && typeof v === "object" && !Array.isArray(v)) {
|
|
288
|
+
return Object.fromEntries(
|
|
289
|
+
Object.entries(v).sort(([a], [b]) => a.localeCompare(b))
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
return v;
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
function valuesEqual(a, b) {
|
|
296
|
+
return stable(a ?? null) === stable(b ?? null);
|
|
297
|
+
}
|
|
298
|
+
function diffRecords(before, after, ignore = ["updatedAt"]) {
|
|
299
|
+
const keys = /* @__PURE__ */ new Set([...Object.keys(before ?? {}), ...Object.keys(after ?? {})]);
|
|
300
|
+
const diff = {};
|
|
301
|
+
for (const key of [...keys].sort()) {
|
|
302
|
+
if (ignore.includes(key)) continue;
|
|
303
|
+
const b = before?.[key] ?? null;
|
|
304
|
+
const a = after?.[key] ?? null;
|
|
305
|
+
if (!valuesEqual(b, a)) diff[key] = { before: b, after: a };
|
|
306
|
+
}
|
|
307
|
+
return diff;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// ../core/src/visibility/writes.ts
|
|
311
|
+
var SET_BY_HOST = /* @__PURE__ */ new Set(["id", "createdAt", "updatedAt", "createdBy"]);
|
|
312
|
+
async function assertVisibleWrites(ctx, all, checked = /* @__PURE__ */ new Set(), byHost = /* @__PURE__ */ new Set(), guard) {
|
|
313
|
+
if (ctx.principal.kind === "staff" || !all.length) return;
|
|
314
|
+
const changes = byHost.size ? all.filter((change) => !byHost.has(change)) : all;
|
|
315
|
+
if (!changes.length) return;
|
|
316
|
+
if (ctx.principal.kind !== "customer") {
|
|
317
|
+
throw new ForbiddenError("This call cannot change records");
|
|
318
|
+
}
|
|
319
|
+
const personId = ctx.principal.personId;
|
|
320
|
+
const existing = /* @__PURE__ */ new Map();
|
|
321
|
+
const guarded = /* @__PURE__ */ new Set();
|
|
322
|
+
const holdToLink = (dependantId) => {
|
|
323
|
+
if (!guard) return false;
|
|
324
|
+
if (!guarded.has(dependantId)) {
|
|
325
|
+
guarded.add(dependantId);
|
|
326
|
+
guard(linkGuardStatement(personId, dependantId));
|
|
327
|
+
}
|
|
328
|
+
return true;
|
|
329
|
+
};
|
|
330
|
+
const mayOwn = async (scope, owner) => owner === personId || scope.dependantsWrite && typeof owner === "string" && await isLiveDependant(ctx.db, personId, owner) && holdToLink(owner);
|
|
331
|
+
for (const change of changes) {
|
|
332
|
+
const object = getObject(change.object);
|
|
333
|
+
if (!object) throw unknownObject(change.object);
|
|
334
|
+
const scope = scopeOf(ctx.principal, object);
|
|
335
|
+
if (scope.kind !== "customer") throw unknownObject(change.object);
|
|
336
|
+
if (change.action !== "create" && change.action !== "update") {
|
|
337
|
+
throw new ForbiddenError(`A customer cannot ${change.action} a ${object.name}`);
|
|
338
|
+
}
|
|
339
|
+
const diff = change.diff ?? diffRecords(change.before, change.after);
|
|
340
|
+
const owner = scope.declaration.field;
|
|
341
|
+
for (const [name, values] of Object.entries(diff)) {
|
|
342
|
+
if (name !== owner && checked.has(change)) continue;
|
|
343
|
+
if (name === owner) {
|
|
344
|
+
if (change.action !== "create") throw new NotFoundError(object.name, change.id);
|
|
345
|
+
if (!await mayOwn(scope, values.after)) {
|
|
346
|
+
throw new ValidationError(`A new ${object.name} must belong to its caller`);
|
|
347
|
+
}
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
if (change.action !== "create" && object.customer?.createOnly?.includes(name)) {
|
|
351
|
+
throw new ValidationError(`${object.name}.${name} is set only when it is made`, {
|
|
352
|
+
field: name
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
if (scope.writable.has(name)) continue;
|
|
356
|
+
if (change.action === "create" && SET_BY_HOST.has(name)) continue;
|
|
357
|
+
if (scope.fields.some((field) => field.name === name)) {
|
|
358
|
+
throw new ValidationError(`${object.name}.${name} cannot be changed`, { field: name });
|
|
359
|
+
}
|
|
360
|
+
throw new ValidationError(`Unknown field "${name}" on ${object.name}`, { field: name });
|
|
361
|
+
}
|
|
362
|
+
if (change.action === "create") {
|
|
363
|
+
if (owner !== "id" && !await mayOwn(scope, diff[owner]?.after)) {
|
|
364
|
+
throw new ValidationError(`A new ${object.name} must belong to its caller`);
|
|
365
|
+
}
|
|
366
|
+
if (owner === "id") throw new ForbiddenError(`A customer cannot create a ${object.name}`);
|
|
367
|
+
} else {
|
|
368
|
+
existing.set(object.name, [...existing.get(object.name) ?? [], change.id]);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
for (const [name, ids] of existing) {
|
|
372
|
+
const object = getObject(name);
|
|
373
|
+
if (!object) throw unknownObject(name);
|
|
374
|
+
const rows = await ctx.db.selectFrom(ownedSource(ctx, object)).select(["id", "owner"]).where("id", "in", jsonList([...new Set(ids)])).execute();
|
|
375
|
+
const mine = new Set(rows.map((row) => String(row.id)));
|
|
376
|
+
const missing = ids.find((id) => !mine.has(id));
|
|
377
|
+
if (missing) throw new NotFoundError(name, missing);
|
|
378
|
+
for (const row of rows) {
|
|
379
|
+
const owner = row.owner === null || row.owner === void 0 ? null : String(row.owner);
|
|
380
|
+
if (owner !== null && owner !== personId && !holdToLink(owner)) {
|
|
381
|
+
throw new NotFoundError(name, String(row.id));
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
var recordWriter = null;
|
|
387
|
+
var coreWriters = /* @__PURE__ */ new Map();
|
|
388
|
+
function setRecordWriter(writer) {
|
|
389
|
+
recordWriter = writer;
|
|
390
|
+
}
|
|
391
|
+
function setCoreWriter(object, writer) {
|
|
392
|
+
coreWriters.set(object, writer);
|
|
393
|
+
}
|
|
394
|
+
function customerScopeFor(writer, name) {
|
|
395
|
+
const object = getObject(name);
|
|
396
|
+
if (!object) throw unknownObject(name);
|
|
397
|
+
const scope = scopeOf(writer.reader.principal, object);
|
|
398
|
+
if (scope.kind !== "customer") throw unknownObject(name);
|
|
399
|
+
return { object, scope };
|
|
400
|
+
}
|
|
401
|
+
function assertSettable(writer, object, scope, values) {
|
|
402
|
+
const widened = writer.widened?.get(object.name);
|
|
403
|
+
for (const name of Object.keys(values)) {
|
|
404
|
+
const reserved = name === scope.declaration.field || name === "id" || name === scope.declaration.guardians?.madeBy;
|
|
405
|
+
if (!reserved && (scope.writable.has(name) || widened?.has(name))) continue;
|
|
406
|
+
if (!reserved && scope.fields.some((field) => field.name === name)) {
|
|
407
|
+
throw new ValidationError(`${object.name}.${name} cannot be changed`, { field: name });
|
|
408
|
+
}
|
|
409
|
+
throw new ValidationError(`Unknown field "${name}" on ${object.name}`, { field: name });
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
async function assertRelationsVisible(reader, object, values) {
|
|
413
|
+
for (const [name, value] of Object.entries(values)) {
|
|
414
|
+
if (value === null || value === void 0) continue;
|
|
415
|
+
const field = object.fields.find((f) => f.name === name);
|
|
416
|
+
const to = field?.fieldType === "relation" ? field.options?.to : void 0;
|
|
417
|
+
if (!to) continue;
|
|
418
|
+
const target = getObject(to);
|
|
419
|
+
const ids = [...new Set(Array.isArray(value) ? value.map(String) : [String(value)])];
|
|
420
|
+
const missing = (wanted) => new NotFoundError(to, wanted);
|
|
421
|
+
if (!target) throw missing(ids[0] ?? "");
|
|
422
|
+
if (scopeOf(reader.principal, target).kind === "none") throw missing(ids[0] ?? "");
|
|
423
|
+
const rows = await reader.db.selectFrom(visibleSource(reader, target)).select("id").where("id", "in", jsonList(ids)).execute();
|
|
424
|
+
const seen = new Set(rows.map((row) => String(row.id)));
|
|
425
|
+
const unseen = ids.find((id) => !seen.has(id));
|
|
426
|
+
if (unseen !== void 0) throw missing(unseen);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
async function assertAccessRequired(reader, object, values, existing) {
|
|
430
|
+
const required = object.customer?.requireAccess ?? [];
|
|
431
|
+
for (const name of required) {
|
|
432
|
+
const field = object.fields.find((f) => f.name === name);
|
|
433
|
+
const to = field?.options?.to;
|
|
434
|
+
const target = to ? getObject(to) : void 0;
|
|
435
|
+
const pointed = Object.hasOwn(values, name) ? values[name] : existing?.[name];
|
|
436
|
+
const id = pointed === null || pointed === void 0 ? null : String(pointed);
|
|
437
|
+
if (!target || !id) throw new NotFoundError(to ?? name, id ?? "");
|
|
438
|
+
const held = await holdsAccess(reader, target, [id]);
|
|
439
|
+
if (!held.has(id)) throw new NotFoundError(target.name, id);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
async function recording(writer, write) {
|
|
443
|
+
const host = Object.create(writer.host, {
|
|
444
|
+
recordChange: {
|
|
445
|
+
value: (change) => {
|
|
446
|
+
writer.mark(change);
|
|
447
|
+
writer.host.recordChange(change);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
return write(host);
|
|
452
|
+
}
|
|
453
|
+
async function ownerFor(writer, scope, forPersonId) {
|
|
454
|
+
if (forPersonId === void 0 || forPersonId === scope.personId) return scope.personId;
|
|
455
|
+
if (!scope.dependantsWrite || !await isLiveDependant(writer.reader.db, scope.personId, forPersonId)) {
|
|
456
|
+
throw new NotFoundError("person", forPersonId);
|
|
457
|
+
}
|
|
458
|
+
return forPersonId;
|
|
459
|
+
}
|
|
460
|
+
async function createVisible(writer, name, values, options = {}) {
|
|
461
|
+
const { object, scope } = customerScopeFor(writer, name);
|
|
462
|
+
assertSettable(writer, object, scope, values);
|
|
463
|
+
if (isCoreObject(object.name) || !recordWriter) {
|
|
464
|
+
throw new ForbiddenError(`A customer cannot create a ${object.name}`);
|
|
465
|
+
}
|
|
466
|
+
const owner = await ownerFor(writer, scope, options.forPersonId);
|
|
467
|
+
await assertRelationsVisible(writer.reader, object, values);
|
|
468
|
+
await assertAccessRequired(writer.reader, object, values, null);
|
|
469
|
+
const madeBy = scope.declaration.guardians?.madeBy;
|
|
470
|
+
const owned = {
|
|
471
|
+
...values,
|
|
472
|
+
[scope.declaration.field]: owner,
|
|
473
|
+
...madeBy ? { [madeBy]: scope.personId } : {}
|
|
474
|
+
};
|
|
475
|
+
const create = recordWriter.create;
|
|
476
|
+
const seamSet = new Set(madeBy ? [madeBy] : []);
|
|
477
|
+
return { id: await recording(writer, (host) => create(host, object, owned, { seamSet })) };
|
|
478
|
+
}
|
|
479
|
+
async function updateVisible(writer, name, id, values, options = {}) {
|
|
480
|
+
const { object, scope } = customerScopeFor(writer, name);
|
|
481
|
+
assertSettable(writer, object, scope, values);
|
|
482
|
+
for (const field of object.customer?.createOnly ?? []) {
|
|
483
|
+
if (Object.hasOwn(values, field)) {
|
|
484
|
+
throw new ValidationError(`${object.name}.${field} is set only when it is made`, { field });
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
const row = await writer.reader.db.selectFrom(ownedSource(writer.reader, object)).select("id").where("id", "=", id).executeTakeFirst();
|
|
488
|
+
if (!row) throw new NotFoundError(object.name, id);
|
|
489
|
+
await assertRelationsVisible(writer.reader, object, values);
|
|
490
|
+
if (object.customer?.requireAccess?.length) {
|
|
491
|
+
const current = await writer.reader.db.selectFrom(visibleSource(writer.reader, object)).selectAll().where("id", "=", id).executeTakeFirst();
|
|
492
|
+
const existing = {};
|
|
493
|
+
for (const name2 of object.customer.requireAccess) {
|
|
494
|
+
const field = object.fields.find((f) => f.name === name2);
|
|
495
|
+
const column = field ? viewColumnOf(field) : null;
|
|
496
|
+
existing[name2] = column && current ? current[column] : null;
|
|
497
|
+
}
|
|
498
|
+
await assertAccessRequired(writer.reader, object, values, existing);
|
|
499
|
+
}
|
|
500
|
+
const update = coreWriters.get(object.name)?.update ?? (isCoreObject(object.name) ? null : recordWriter?.update);
|
|
501
|
+
if (!update) throw new OperationError("internal", `No writer for ${object.name}`);
|
|
502
|
+
const from = options.from === void 0 ? {} : { from: options.from };
|
|
503
|
+
await recording(writer, (host) => update(host, object, id, values, from));
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// ../core/src/automations/cron.ts
|
|
507
|
+
var WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
|
|
508
|
+
var formatters = /* @__PURE__ */ new Map();
|
|
509
|
+
function formatter(timeZone) {
|
|
510
|
+
let f = formatters.get(timeZone);
|
|
511
|
+
if (!f) {
|
|
512
|
+
f = new Intl.DateTimeFormat("en-US", {
|
|
513
|
+
timeZone,
|
|
514
|
+
hourCycle: "h23",
|
|
515
|
+
year: "numeric",
|
|
516
|
+
month: "2-digit",
|
|
517
|
+
day: "2-digit",
|
|
518
|
+
hour: "2-digit",
|
|
519
|
+
minute: "2-digit",
|
|
520
|
+
weekday: "short"
|
|
521
|
+
});
|
|
522
|
+
formatters.set(timeZone, f);
|
|
523
|
+
}
|
|
524
|
+
return f;
|
|
525
|
+
}
|
|
526
|
+
var pad = (n) => String(n).padStart(2, "0");
|
|
527
|
+
function zonedTime(at, timeZone) {
|
|
528
|
+
const parts = Object.fromEntries(
|
|
529
|
+
formatter(timeZone).formatToParts(at).map((p) => [p.type, p.value])
|
|
530
|
+
);
|
|
531
|
+
const year = Number(parts.year);
|
|
532
|
+
const month = Number(parts.month);
|
|
533
|
+
const day = Number(parts.day);
|
|
534
|
+
const hour = Number(parts.hour) % 24;
|
|
535
|
+
const minute = Number(parts.minute);
|
|
536
|
+
const weekday = WEEKDAYS.indexOf(String(parts.weekday));
|
|
537
|
+
const date = `${year}-${pad(month)}-${pad(day)}`;
|
|
538
|
+
return {
|
|
539
|
+
year,
|
|
540
|
+
month,
|
|
541
|
+
day,
|
|
542
|
+
hour,
|
|
543
|
+
minute,
|
|
544
|
+
weekday,
|
|
545
|
+
date,
|
|
546
|
+
minuteKey: `${date}T${pad(hour)}:${pad(minute)}`
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
function addDaysToDate(date, days) {
|
|
550
|
+
const t = Date.parse(`${date}T00:00:00Z`) + days * 864e5;
|
|
551
|
+
return new Date(t).toISOString().slice(0, 10);
|
|
552
|
+
}
|
|
553
|
+
var MONTHS = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"];
|
|
554
|
+
var DAYS = ["SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"];
|
|
555
|
+
var MACROS = {
|
|
556
|
+
"@yearly": "0 0 1 1 *",
|
|
557
|
+
"@annually": "0 0 1 1 *",
|
|
558
|
+
"@monthly": "0 0 1 * *",
|
|
559
|
+
"@weekly": "0 0 * * 0",
|
|
560
|
+
"@daily": "0 0 * * *",
|
|
561
|
+
"@midnight": "0 0 * * *",
|
|
562
|
+
"@hourly": "0 * * * *"
|
|
563
|
+
};
|
|
564
|
+
function parseValue(text, names, offset) {
|
|
565
|
+
const upper = text.toUpperCase();
|
|
566
|
+
if (names) {
|
|
567
|
+
const i = names.indexOf(upper);
|
|
568
|
+
if (i >= 0) return i + offset;
|
|
569
|
+
}
|
|
570
|
+
if (!/^\d+$/.test(text)) throw new Error(`"${text}" is not a number`);
|
|
571
|
+
return Number(text);
|
|
572
|
+
}
|
|
573
|
+
function parseField(text, min, max, names = null, nameOffset = 0) {
|
|
574
|
+
const values = /* @__PURE__ */ new Set();
|
|
575
|
+
for (const part of text.split(",")) {
|
|
576
|
+
const [range = "", stepText] = part.split("/");
|
|
577
|
+
const step = stepText === void 0 ? 1 : Number(stepText);
|
|
578
|
+
if (!Number.isInteger(step) || step < 1) throw new Error(`Bad step in "${part}"`);
|
|
579
|
+
let lo;
|
|
580
|
+
let hi;
|
|
581
|
+
if (range === "*") {
|
|
582
|
+
lo = min;
|
|
583
|
+
hi = max;
|
|
584
|
+
} else if (range.includes("-")) {
|
|
585
|
+
const [a = "", b = ""] = range.split("-");
|
|
586
|
+
lo = parseValue(a, names, nameOffset);
|
|
587
|
+
hi = parseValue(b, names, nameOffset);
|
|
588
|
+
} else {
|
|
589
|
+
lo = parseValue(range, names, nameOffset);
|
|
590
|
+
hi = stepText === void 0 ? lo : max;
|
|
591
|
+
}
|
|
592
|
+
if (lo < min || hi > max || lo > hi) throw new Error(`"${part}" is out of range ${min}-${max}`);
|
|
593
|
+
for (let v = lo; v <= hi; v += step) values.add(v);
|
|
594
|
+
}
|
|
595
|
+
return { values, any: text === "*" };
|
|
596
|
+
}
|
|
597
|
+
function parseCron(expression) {
|
|
598
|
+
const expanded = MACROS[expression.trim().toLowerCase()] ?? expression.trim();
|
|
599
|
+
const fields = expanded.split(/\s+/);
|
|
600
|
+
if (fields.length !== 5) {
|
|
601
|
+
throw new Error(`"${expression}" needs 5 fields: minute hour day-of-month month day-of-week`);
|
|
602
|
+
}
|
|
603
|
+
const [minute, hour, day, month, weekday] = fields;
|
|
604
|
+
const dow = parseField(weekday, 0, 7, DAYS, 0);
|
|
605
|
+
if (dow.values.has(7)) {
|
|
606
|
+
dow.values.delete(7);
|
|
607
|
+
dow.values.add(0);
|
|
608
|
+
}
|
|
609
|
+
return {
|
|
610
|
+
minute: parseField(minute, 0, 59),
|
|
611
|
+
hour: parseField(hour, 0, 23),
|
|
612
|
+
day: parseField(day, 1, 31),
|
|
613
|
+
month: parseField(month, 1, 12, MONTHS, 1),
|
|
614
|
+
weekday: dow
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
function cronMatches(expression, at) {
|
|
618
|
+
let cron;
|
|
619
|
+
try {
|
|
620
|
+
cron = typeof expression === "string" ? parseCron(expression) : expression;
|
|
621
|
+
} catch {
|
|
622
|
+
return false;
|
|
623
|
+
}
|
|
624
|
+
if (!cron.minute.values.has(at.minute) || !cron.hour.values.has(at.hour)) return false;
|
|
625
|
+
if (!cron.month.values.has(at.month)) return false;
|
|
626
|
+
const dom = cron.day.values.has(at.day);
|
|
627
|
+
const dow = cron.weekday.values.has(at.weekday);
|
|
628
|
+
if (cron.day.any && cron.weekday.any) return true;
|
|
629
|
+
if (cron.day.any) return dow;
|
|
630
|
+
if (cron.weekday.any) return dom;
|
|
631
|
+
return dom || dow;
|
|
632
|
+
}
|
|
633
|
+
function cronFires(expression, fromDate, days) {
|
|
634
|
+
const cron = typeof expression === "string" ? parseCron(expression) : expression;
|
|
635
|
+
const hours = [...cron.hour.values].sort((a, b) => a - b);
|
|
636
|
+
const minutes = [...cron.minute.values].sort((a, b) => a - b);
|
|
637
|
+
const out = [];
|
|
638
|
+
for (let i = 0; i < days; i++) {
|
|
639
|
+
const date = addDaysToDate(fromDate, i);
|
|
640
|
+
const day = /* @__PURE__ */ new Date(`${date}T00:00:00Z`);
|
|
641
|
+
const at = {
|
|
642
|
+
month: day.getUTCMonth() + 1,
|
|
643
|
+
day: day.getUTCDate(),
|
|
644
|
+
weekday: day.getUTCDay(),
|
|
645
|
+
hour: hours[0] ?? 0,
|
|
646
|
+
minute: minutes[0] ?? 0
|
|
647
|
+
};
|
|
648
|
+
if (!cronMatches(cron, at)) continue;
|
|
649
|
+
for (const hour of hours) {
|
|
650
|
+
for (const minute of minutes) out.push(`${date}T${pad(hour)}:${pad(minute)}`);
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
return out;
|
|
654
|
+
}
|
|
655
|
+
function nextCronFire(expression, after, days = 366) {
|
|
656
|
+
try {
|
|
657
|
+
return cronFires(expression, after.slice(0, 10), days).find((f) => f > after) ?? null;
|
|
658
|
+
} catch {
|
|
659
|
+
return null;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
var minuteOf = (fire) => Date.parse(`${fire}:00Z`) / 6e4;
|
|
663
|
+
function shortestCronGap(expression, fromDate) {
|
|
664
|
+
const fires = cronFires(expression, fromDate, 62);
|
|
665
|
+
let shortest = null;
|
|
666
|
+
for (let i = 1; i < fires.length; i++) {
|
|
667
|
+
const gap = minuteOf(fires[i]) - minuteOf(fires[i - 1]);
|
|
668
|
+
if (shortest === null || gap < shortest) shortest = gap;
|
|
669
|
+
}
|
|
670
|
+
return shortest;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
export {
|
|
674
|
+
assignmentConditionSchema,
|
|
675
|
+
ASSIGNMENT_METHODS,
|
|
676
|
+
requireOwnedObject,
|
|
677
|
+
validateConditions,
|
|
678
|
+
ruleMatches,
|
|
679
|
+
ruleFromRow,
|
|
680
|
+
loadRules,
|
|
681
|
+
assignOwner,
|
|
682
|
+
assignmentSubject,
|
|
683
|
+
recordAssignment,
|
|
684
|
+
isAssignmentConflict,
|
|
685
|
+
zonedTime,
|
|
686
|
+
addDaysToDate,
|
|
687
|
+
parseCron,
|
|
688
|
+
cronFires,
|
|
689
|
+
nextCronFire,
|
|
690
|
+
shortestCronGap,
|
|
691
|
+
valuesEqual,
|
|
692
|
+
diffRecords,
|
|
693
|
+
assertVisibleWrites,
|
|
694
|
+
setRecordWriter,
|
|
695
|
+
setCoreWriter,
|
|
696
|
+
createVisible,
|
|
697
|
+
updateVisible
|
|
698
|
+
};
|