@odla-ai/crm 0.6.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +74 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +56 -1
- package/dist/index.d.ts +56 -1
- package/dist/index.js +74 -2
- package/dist/index.js.map +1 -1
- package/dist/ui/index.d.ts +9 -0
- package/dist/ui/index.js +16 -2
- package/dist/ui/index.js.map +1 -1
- package/package.json +4 -1
package/dist/index.cjs
CHANGED
|
@@ -43,6 +43,7 @@ __export(index_exports, {
|
|
|
43
43
|
addTag: () => addTag,
|
|
44
44
|
applyConsent: () => applyConsent,
|
|
45
45
|
applyConsentEvent: () => applyConsentEvent,
|
|
46
|
+
assertFieldCondition: () => assertFieldCondition,
|
|
46
47
|
canEmail: () => canEmail,
|
|
47
48
|
canTransition: () => canTransition,
|
|
48
49
|
channelKey: () => channelKey,
|
|
@@ -59,6 +60,7 @@ __export(index_exports, {
|
|
|
59
60
|
defineCrm: () => defineCrm,
|
|
60
61
|
deleteView: () => deleteView,
|
|
61
62
|
emailChannelOps: () => emailChannelOps,
|
|
63
|
+
evaluateFieldCondition: () => evaluateFieldCondition,
|
|
62
64
|
foldEmailStatus: () => foldEmailStatus,
|
|
63
65
|
formatCrmDiscussionReference: () => formatCrmDiscussionReference,
|
|
64
66
|
getConfig: () => getConfig,
|
|
@@ -94,6 +96,8 @@ __export(index_exports, {
|
|
|
94
96
|
removeTag: () => removeTag,
|
|
95
97
|
renderTemplate: () => renderTemplate,
|
|
96
98
|
resolveDeps: () => resolveDeps,
|
|
99
|
+
resolveFieldState: () => resolveFieldState,
|
|
100
|
+
resolveFieldStates: () => resolveFieldStates,
|
|
97
101
|
resolveSortColumn: () => resolveSortColumn,
|
|
98
102
|
rowToRecord: () => rowToRecord,
|
|
99
103
|
saveConfig: () => saveConfig,
|
|
@@ -435,6 +439,45 @@ function lintTemplate(template, knownVars) {
|
|
|
435
439
|
};
|
|
436
440
|
}
|
|
437
441
|
|
|
442
|
+
// src/field-conditions.ts
|
|
443
|
+
var import_cel_js = require("@marcbachmann/cel-js");
|
|
444
|
+
var env = new import_cel_js.Environment({ unlistedVariablesAreDyn: true });
|
|
445
|
+
function assertFieldCondition(expression, label) {
|
|
446
|
+
try {
|
|
447
|
+
env.evaluate(expression, { values: {} });
|
|
448
|
+
} catch (error) {
|
|
449
|
+
if (error instanceof import_cel_js.ParseError) {
|
|
450
|
+
throw new Error(`${label}: not a valid CEL condition \u2014 ${error.message}`);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
var REFERENCED = /\bvalues\.([A-Za-z_][A-Za-z0-9_]*)/g;
|
|
455
|
+
function evaluateFieldCondition(expression, values, fallback) {
|
|
456
|
+
const unanswered = {};
|
|
457
|
+
for (const [, name] of expression.matchAll(REFERENCED)) {
|
|
458
|
+
if (name !== void 0 && !(name in values)) unanswered[name] = "";
|
|
459
|
+
}
|
|
460
|
+
try {
|
|
461
|
+
return env.evaluate(expression, { values: { ...unanswered, ...values } }) === true;
|
|
462
|
+
} catch {
|
|
463
|
+
return fallback;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
function resolveFieldState(conditions, values, staticRequired = false) {
|
|
467
|
+
const visible = conditions?.visibleWhen === void 0 ? true : evaluateFieldCondition(conditions.visibleWhen, values, true);
|
|
468
|
+
if (!visible) return { visible: false, required: false };
|
|
469
|
+
const required2 = conditions?.requiredWhen === void 0 ? staticRequired : evaluateFieldCondition(conditions.requiredWhen, values, false);
|
|
470
|
+
return { visible, required: required2 };
|
|
471
|
+
}
|
|
472
|
+
function resolveFieldStates(conditions, values, staticRequired = []) {
|
|
473
|
+
const required2 = new Set(staticRequired);
|
|
474
|
+
const out = {};
|
|
475
|
+
for (const field of /* @__PURE__ */ new Set([...Object.keys(conditions), ...required2])) {
|
|
476
|
+
out[field] = resolveFieldState(conditions[field], values, required2.has(field));
|
|
477
|
+
}
|
|
478
|
+
return out;
|
|
479
|
+
}
|
|
480
|
+
|
|
438
481
|
// src/validate.ts
|
|
439
482
|
var EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
440
483
|
var STRINGISH = /* @__PURE__ */ new Set(["string", "email", "url", "enum"]);
|
|
@@ -573,6 +616,18 @@ function checkType(typeName, def) {
|
|
|
573
616
|
if (field.type === "enum" && !(field.options && field.options.length > 0))
|
|
574
617
|
fail(fat, "enum fields need non-empty options");
|
|
575
618
|
if (field.type !== "enum" && field.options) fail(fat, "options are for enum fields only");
|
|
619
|
+
for (const key of ["visibleWhen", "requiredWhen"]) {
|
|
620
|
+
const expression = field[key];
|
|
621
|
+
if (expression === void 0) continue;
|
|
622
|
+
if (typeof expression !== "string" || !expression.trim()) {
|
|
623
|
+
fail(`${fat}.${key}`, "must be a non-empty CEL condition");
|
|
624
|
+
}
|
|
625
|
+
try {
|
|
626
|
+
assertFieldCondition(expression, `${fat}.${key}`);
|
|
627
|
+
} catch (error) {
|
|
628
|
+
fail(`${fat}.${key}`, error instanceof Error ? error.message : "invalid condition");
|
|
629
|
+
}
|
|
630
|
+
}
|
|
576
631
|
if (field.slot) {
|
|
577
632
|
if (!CRM_SLOTS.includes(field.slot)) fail(fat, `unknown slot ${field.slot}`);
|
|
578
633
|
if (!SLOT_KIND[field.slot].includes(field.type))
|
|
@@ -1253,6 +1308,14 @@ async function listLinks(deps, recordId) {
|
|
|
1253
1308
|
return (result[CRM_NS.link] ?? []).map((row) => row);
|
|
1254
1309
|
}
|
|
1255
1310
|
|
|
1311
|
+
// src/time.ts
|
|
1312
|
+
function epochOf(value) {
|
|
1313
|
+
if (typeof value === "number") return Number.isFinite(value) ? value : void 0;
|
|
1314
|
+
if (typeof value !== "string") return void 0;
|
|
1315
|
+
const parsed = Date.parse(value);
|
|
1316
|
+
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1256
1319
|
// src/provenance.ts
|
|
1257
1320
|
var SOURCE_ID = /^[a-z0-9][a-z0-9-]{1,127}$/;
|
|
1258
1321
|
var TARGET_ID = SOURCE_ID;
|
|
@@ -1267,11 +1330,20 @@ function recordOriginKey(sourceId, sourceRecordId) {
|
|
|
1267
1330
|
if (!SOURCE_ID.test(source)) throw new CrmInputError("sourceId must be a lowercase app slug");
|
|
1268
1331
|
return `${source}:${record}`;
|
|
1269
1332
|
}
|
|
1333
|
+
function originRow(row) {
|
|
1334
|
+
const out = { ...row };
|
|
1335
|
+
for (const field of ["firstReceivedAt", "lastReceivedAt"]) {
|
|
1336
|
+
const parsed = epochOf(out[field]);
|
|
1337
|
+
if (parsed !== void 0) out[field] = parsed;
|
|
1338
|
+
}
|
|
1339
|
+
return out;
|
|
1340
|
+
}
|
|
1270
1341
|
async function getRecordOrigin(deps, sourceId, sourceRecordId) {
|
|
1271
1342
|
const { db } = resolveDeps(deps);
|
|
1272
1343
|
const key = recordOriginKey(sourceId, sourceRecordId);
|
|
1273
1344
|
const result = await db.query({ [CRM_NS.origin]: { $: { where: { key }, limit: 1 } } });
|
|
1274
|
-
|
|
1345
|
+
const row = (result[CRM_NS.origin] ?? [])[0];
|
|
1346
|
+
return row ? originRow(row) : null;
|
|
1275
1347
|
}
|
|
1276
1348
|
async function getRecordByOrigin(deps, sourceId, sourceRecordId) {
|
|
1277
1349
|
const origin = await getRecordOrigin(deps, sourceId, sourceRecordId);
|
|
@@ -1317,7 +1389,7 @@ async function listRecordOrigins(deps, recordId) {
|
|
|
1317
1389
|
const result = await db.query({
|
|
1318
1390
|
[CRM_NS.origin]: { $: { where: { recordId }, order: { firstReceivedAt: "asc" }, limit: 200 } }
|
|
1319
1391
|
});
|
|
1320
|
-
return result[CRM_NS.origin] ?? [];
|
|
1392
|
+
return (result[CRM_NS.origin] ?? []).map(originRow);
|
|
1321
1393
|
}
|
|
1322
1394
|
function recordDeliveryKey(recordId, targetId) {
|
|
1323
1395
|
const record = required(recordId, "recordId");
|