@odla-ai/crm 0.1.1 → 0.1.2
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 +86 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +19 -6
- package/dist/index.d.ts +19 -6
- package/dist/index.js +86 -42
- package/dist/index.js.map +1 -1
- package/dist/ui/index.d.ts +6 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -120,7 +120,8 @@ type CrmEntityRef = string | CrmLookup;
|
|
|
120
120
|
* `transact` constrains attrs to its own `Value` type, and `any` is the one
|
|
121
121
|
* shape assignable in both directions with no cast. */
|
|
122
122
|
type CrmAttrs = Record<string, any>;
|
|
123
|
-
/** One transact operation (update = attr-merge upsert
|
|
123
|
+
/** One transact operation (update = attr-merge upsert; retract = clear the
|
|
124
|
+
* named attributes, since a typed scalar can't store null). */
|
|
124
125
|
type CrmOp = {
|
|
125
126
|
t: "update";
|
|
126
127
|
ns: string;
|
|
@@ -131,6 +132,11 @@ type CrmOp = {
|
|
|
131
132
|
ns: string;
|
|
132
133
|
id: CrmEntityRef;
|
|
133
134
|
attrs: CrmAttrs;
|
|
135
|
+
} | {
|
|
136
|
+
t: "retract";
|
|
137
|
+
ns: string;
|
|
138
|
+
id: CrmEntityRef;
|
|
139
|
+
attrs: string[];
|
|
134
140
|
} | {
|
|
135
141
|
t: "delete";
|
|
136
142
|
ns: string;
|
|
@@ -322,10 +328,15 @@ interface CrmConfigRow {
|
|
|
322
328
|
settings: Record<string, unknown>;
|
|
323
329
|
updatedAt: number;
|
|
324
330
|
}
|
|
325
|
-
/** Outcome of validating + coercing input for a type (see `prepare`).
|
|
331
|
+
/** Outcome of validating + coercing input for a type (see `prepare`). On
|
|
332
|
+
* success, `attrs` are the columns to WRITE (never null — odla-db rejects null
|
|
333
|
+
* on a typed scalar) and `retract` names the slots/promoted columns to CLEAR
|
|
334
|
+
* because their field went empty (populated only on update; empty on create,
|
|
335
|
+
* where there is nothing to clear). */
|
|
326
336
|
type PreparedWrite = {
|
|
327
337
|
ok: true;
|
|
328
338
|
attrs: CrmAttrs;
|
|
339
|
+
retract: string[];
|
|
329
340
|
} | {
|
|
330
341
|
ok: false;
|
|
331
342
|
errors: Record<string, string>;
|
|
@@ -454,10 +465,12 @@ declare function searchTextFor(def: TypeDef, name: string, email: string | null,
|
|
|
454
465
|
* Validate + coerce `input` (keyed by config field name) for one type.
|
|
455
466
|
* Create (no `existing`): required fields must be present, and the
|
|
456
467
|
* `nameField` always (a record without a name is unlistable). Update:
|
|
457
|
-
* partial — unnamed fields keep their stored values.
|
|
458
|
-
* to write
|
|
459
|
-
*
|
|
460
|
-
* rebuilt `searchText` —
|
|
468
|
+
* partial — unnamed fields keep their stored values. On success returns the
|
|
469
|
+
* columns to write — promoted `name`, `primaryEmail` and each claimed slot
|
|
470
|
+
* that has a value, the complete `fields` json (source of truth), and the
|
|
471
|
+
* rebuilt `searchText` — plus `retract`, the columns whose field went empty
|
|
472
|
+
* (odla-db can't store a scalar null, so an empty column is omitted on create
|
|
473
|
+
* and retracted on update). Otherwise `{ ok: false, errors }` keyed by field.
|
|
461
474
|
*/
|
|
462
475
|
declare function prepare(def: TypeDef, input: Record<string, unknown>, existing?: CrmRecord): PreparedWrite;
|
|
463
476
|
|
package/dist/index.d.ts
CHANGED
|
@@ -120,7 +120,8 @@ type CrmEntityRef = string | CrmLookup;
|
|
|
120
120
|
* `transact` constrains attrs to its own `Value` type, and `any` is the one
|
|
121
121
|
* shape assignable in both directions with no cast. */
|
|
122
122
|
type CrmAttrs = Record<string, any>;
|
|
123
|
-
/** One transact operation (update = attr-merge upsert
|
|
123
|
+
/** One transact operation (update = attr-merge upsert; retract = clear the
|
|
124
|
+
* named attributes, since a typed scalar can't store null). */
|
|
124
125
|
type CrmOp = {
|
|
125
126
|
t: "update";
|
|
126
127
|
ns: string;
|
|
@@ -131,6 +132,11 @@ type CrmOp = {
|
|
|
131
132
|
ns: string;
|
|
132
133
|
id: CrmEntityRef;
|
|
133
134
|
attrs: CrmAttrs;
|
|
135
|
+
} | {
|
|
136
|
+
t: "retract";
|
|
137
|
+
ns: string;
|
|
138
|
+
id: CrmEntityRef;
|
|
139
|
+
attrs: string[];
|
|
134
140
|
} | {
|
|
135
141
|
t: "delete";
|
|
136
142
|
ns: string;
|
|
@@ -322,10 +328,15 @@ interface CrmConfigRow {
|
|
|
322
328
|
settings: Record<string, unknown>;
|
|
323
329
|
updatedAt: number;
|
|
324
330
|
}
|
|
325
|
-
/** Outcome of validating + coercing input for a type (see `prepare`).
|
|
331
|
+
/** Outcome of validating + coercing input for a type (see `prepare`). On
|
|
332
|
+
* success, `attrs` are the columns to WRITE (never null — odla-db rejects null
|
|
333
|
+
* on a typed scalar) and `retract` names the slots/promoted columns to CLEAR
|
|
334
|
+
* because their field went empty (populated only on update; empty on create,
|
|
335
|
+
* where there is nothing to clear). */
|
|
326
336
|
type PreparedWrite = {
|
|
327
337
|
ok: true;
|
|
328
338
|
attrs: CrmAttrs;
|
|
339
|
+
retract: string[];
|
|
329
340
|
} | {
|
|
330
341
|
ok: false;
|
|
331
342
|
errors: Record<string, string>;
|
|
@@ -454,10 +465,12 @@ declare function searchTextFor(def: TypeDef, name: string, email: string | null,
|
|
|
454
465
|
* Validate + coerce `input` (keyed by config field name) for one type.
|
|
455
466
|
* Create (no `existing`): required fields must be present, and the
|
|
456
467
|
* `nameField` always (a record without a name is unlistable). Update:
|
|
457
|
-
* partial — unnamed fields keep their stored values.
|
|
458
|
-
* to write
|
|
459
|
-
*
|
|
460
|
-
* rebuilt `searchText` —
|
|
468
|
+
* partial — unnamed fields keep their stored values. On success returns the
|
|
469
|
+
* columns to write — promoted `name`, `primaryEmail` and each claimed slot
|
|
470
|
+
* that has a value, the complete `fields` json (source of truth), and the
|
|
471
|
+
* rebuilt `searchText` — plus `retract`, the columns whose field went empty
|
|
472
|
+
* (odla-db can't store a scalar null, so an empty column is omitted on create
|
|
473
|
+
* and retracted on update). Otherwise `{ ok: false, errors }` keyed by field.
|
|
461
474
|
*/
|
|
462
475
|
declare function prepare(def: TypeDef, input: Record<string, unknown>, existing?: CrmRecord): PreparedWrite;
|
|
463
476
|
|
package/dist/index.js
CHANGED
|
@@ -71,6 +71,10 @@ var CRM_SCHEMA = {
|
|
|
71
71
|
entities: {
|
|
72
72
|
[CRM_NS.record]: {
|
|
73
73
|
attrs: {
|
|
74
|
+
// Entity ids are not attributes in odla-db, so `where: { id }` matches
|
|
75
|
+
// nothing unless the id is mirrored as a written attr — the getRecord /
|
|
76
|
+
// tag-filter / neighbor reads all filter by id. Mirror it as unique.
|
|
77
|
+
id: uniq("string"),
|
|
74
78
|
type: idx("string"),
|
|
75
79
|
name: idx("string"),
|
|
76
80
|
primaryEmail: idx("string", true),
|
|
@@ -125,6 +129,8 @@ var CRM_SCHEMA = {
|
|
|
125
129
|
},
|
|
126
130
|
[CRM_NS.activity]: {
|
|
127
131
|
attrs: {
|
|
132
|
+
id: uniq("string"),
|
|
133
|
+
// mirrored so updateTask can read one by id
|
|
128
134
|
recordId: idx("string"),
|
|
129
135
|
kind: idx("string"),
|
|
130
136
|
body: a("string"),
|
|
@@ -174,6 +180,8 @@ var CRM_SCHEMA = {
|
|
|
174
180
|
},
|
|
175
181
|
[CRM_NS.view]: {
|
|
176
182
|
attrs: {
|
|
183
|
+
id: uniq("string"),
|
|
184
|
+
// mirrored so getView/updateView can read one by id
|
|
177
185
|
name: a("string"),
|
|
178
186
|
type: idx("string"),
|
|
179
187
|
ownerId: idx("string", true),
|
|
@@ -369,10 +377,21 @@ function prepare(def, input, existing) {
|
|
|
369
377
|
fields,
|
|
370
378
|
searchText: searchTextFor(def, name, primaryEmail, fields)
|
|
371
379
|
};
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
if (
|
|
375
|
-
|
|
380
|
+
const retract = [];
|
|
381
|
+
const clear = (column) => {
|
|
382
|
+
if (existing) retract.push(column);
|
|
383
|
+
};
|
|
384
|
+
if (def.emailField) {
|
|
385
|
+
if (primaryEmail === null) clear("primaryEmail");
|
|
386
|
+
else attrs.primaryEmail = primaryEmail;
|
|
387
|
+
}
|
|
388
|
+
for (const [fieldName, field] of Object.entries(def.fields)) {
|
|
389
|
+
if (!field.slot) continue;
|
|
390
|
+
const value = fields[fieldName];
|
|
391
|
+
if (empty(field, value)) clear(field.slot);
|
|
392
|
+
else attrs[field.slot] = value;
|
|
393
|
+
}
|
|
394
|
+
return { ok: true, attrs, retract };
|
|
376
395
|
}
|
|
377
396
|
|
|
378
397
|
// src/config.ts
|
|
@@ -682,6 +701,29 @@ async function applyConsent(deps, opts) {
|
|
|
682
701
|
return { state: outcome.state, recordId: channel.recordId, duplicate };
|
|
683
702
|
}
|
|
684
703
|
|
|
704
|
+
// src/write-ops.ts
|
|
705
|
+
function patchOps(ns, id, patch, alsoClear = []) {
|
|
706
|
+
const attrs = {};
|
|
707
|
+
const retract = [...alsoClear];
|
|
708
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
709
|
+
if (value === null) retract.push(key);
|
|
710
|
+
else if (value !== void 0) attrs[key] = value;
|
|
711
|
+
}
|
|
712
|
+
const ops = [];
|
|
713
|
+
if (Object.keys(attrs).length > 0) ops.push({ t: "update", ns, id, attrs });
|
|
714
|
+
if (retract.length > 0) ops.push({ t: "retract", ns, id, attrs: retract });
|
|
715
|
+
return ops;
|
|
716
|
+
}
|
|
717
|
+
function applyPatch(base, patch, alsoClear = []) {
|
|
718
|
+
const out = { ...base };
|
|
719
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
720
|
+
if (value === null) delete out[key];
|
|
721
|
+
else if (value !== void 0) out[key] = value;
|
|
722
|
+
}
|
|
723
|
+
for (const key of alsoClear) delete out[key];
|
|
724
|
+
return out;
|
|
725
|
+
}
|
|
726
|
+
|
|
685
727
|
// src/records.ts
|
|
686
728
|
function rowToRecord(row) {
|
|
687
729
|
const record = row;
|
|
@@ -738,6 +780,8 @@ async function createRecord(deps, opts) {
|
|
|
738
780
|
const at = now();
|
|
739
781
|
const stage = opts.stage ?? crm.initialStage(opts.type);
|
|
740
782
|
const attrs = {
|
|
783
|
+
id,
|
|
784
|
+
// mirror the entity id as an attr so `where: { id }` reads can find it
|
|
741
785
|
type: opts.type,
|
|
742
786
|
...prepared.attrs,
|
|
743
787
|
archived: false,
|
|
@@ -772,32 +816,31 @@ async function updateRecord(deps, opts) {
|
|
|
772
816
|
const existing = await mustGetRecord(deps, opts.id);
|
|
773
817
|
const prepared = crm.prepare(existing.type, opts.input, existing);
|
|
774
818
|
if (!prepared.ok) throw new CrmInputError("invalid input", prepared.errors);
|
|
775
|
-
const
|
|
776
|
-
|
|
777
|
-
const ops = [];
|
|
819
|
+
const patch = { ...prepared.attrs, updatedAt: now(), ownerId: opts.ownerId };
|
|
820
|
+
const channelOps = [];
|
|
778
821
|
if (crm.type(existing.type).emailField) {
|
|
779
822
|
const nextEmail = prepared.attrs["primaryEmail"] ?? null;
|
|
780
823
|
if (nextEmail !== (existing.primaryEmail ?? null)) {
|
|
781
824
|
const channels = await listChannels(deps, existing.id);
|
|
782
825
|
const channel = emailChannelOps(deps, existing.id, nextEmail, channels);
|
|
783
|
-
|
|
784
|
-
|
|
826
|
+
patch.emailStatus = channel.emailStatus;
|
|
827
|
+
channelOps.push(...channel.ops);
|
|
785
828
|
}
|
|
786
829
|
}
|
|
787
|
-
const candidate = { ...existing, ...attrs };
|
|
830
|
+
const candidate = { ...existing, ...prepared.attrs };
|
|
788
831
|
const score = scoreOf(deps, existing.type, candidate);
|
|
789
|
-
if (score !== void 0)
|
|
790
|
-
ops
|
|
832
|
+
if (score !== void 0) patch.score = score;
|
|
833
|
+
const ops = [...patchOps(CRM_NS.record, existing.id, patch, prepared.retract), ...channelOps];
|
|
791
834
|
const { duplicate } = await db.transact(ops, { mutationId: opts.mutationId });
|
|
792
|
-
return { record:
|
|
835
|
+
return { record: applyPatch(existing, patch, prepared.retract), duplicate };
|
|
793
836
|
}
|
|
794
837
|
async function setArchived(deps, opts) {
|
|
795
838
|
const { db, now } = resolveDeps(deps);
|
|
796
839
|
await mustGetRecord(deps, opts.id);
|
|
797
840
|
const at = now();
|
|
798
|
-
const
|
|
841
|
+
const patch = opts.archived ? { archived: true, archivedAt: at, updatedAt: at } : { archived: false, archivedAt: null, updatedAt: at };
|
|
799
842
|
const { duplicate } = await db.transact(
|
|
800
|
-
|
|
843
|
+
patchOps(CRM_NS.record, opts.id, patch),
|
|
801
844
|
{ mutationId: opts.mutationId }
|
|
802
845
|
);
|
|
803
846
|
return { duplicate };
|
|
@@ -816,13 +859,16 @@ async function setStage(deps, opts) {
|
|
|
816
859
|
stageChangedAt: at,
|
|
817
860
|
updatedAt: at
|
|
818
861
|
};
|
|
862
|
+
const activityId = newId();
|
|
819
863
|
const ops = [
|
|
820
864
|
{ t: "update", ns: CRM_NS.record, id: record.id, attrs },
|
|
821
865
|
{
|
|
822
866
|
t: "update",
|
|
823
867
|
ns: CRM_NS.activity,
|
|
824
|
-
id:
|
|
868
|
+
id: activityId,
|
|
825
869
|
attrs: {
|
|
870
|
+
id: activityId,
|
|
871
|
+
// mirror the entity id so the activity is readable by id
|
|
826
872
|
recordId: record.id,
|
|
827
873
|
kind: "stage_change",
|
|
828
874
|
body: `${record.stage ?? "(none)"} \u2192 ${opts.to}`,
|
|
@@ -1001,6 +1047,8 @@ async function addActivity(deps, opts) {
|
|
|
1001
1047
|
await mustGetRecord(deps, opts.recordId);
|
|
1002
1048
|
const id = newId();
|
|
1003
1049
|
const attrs = {
|
|
1050
|
+
id,
|
|
1051
|
+
// mirror the entity id so updateTask's `where: { id }` read can find it
|
|
1004
1052
|
recordId: opts.recordId,
|
|
1005
1053
|
kind: opts.kind,
|
|
1006
1054
|
body,
|
|
@@ -1026,25 +1074,25 @@ async function updateTask(deps, opts) {
|
|
|
1026
1074
|
if (!row) throw new CrmNotFoundError("activity");
|
|
1027
1075
|
const activity = rowToActivity(row);
|
|
1028
1076
|
if (activity.kind !== "task") throw new CrmInputError("only tasks can be updated");
|
|
1029
|
-
const
|
|
1077
|
+
const patch = {};
|
|
1030
1078
|
if (opts.status !== void 0) {
|
|
1031
1079
|
if (!TASK_STATUSES.includes(opts.status)) throw new CrmInputError(`unknown status "${opts.status}"`);
|
|
1032
|
-
|
|
1033
|
-
|
|
1080
|
+
patch.status = opts.status;
|
|
1081
|
+
patch.completedAt = opts.status === "done" ? now() : null;
|
|
1034
1082
|
}
|
|
1035
|
-
if (opts.dueAt !== void 0)
|
|
1036
|
-
if (opts.assigneeId !== void 0)
|
|
1037
|
-
if (opts.waitingOn !== void 0)
|
|
1083
|
+
if (opts.dueAt !== void 0) patch.dueAt = opts.dueAt;
|
|
1084
|
+
if (opts.assigneeId !== void 0) patch.assigneeId = opts.assigneeId;
|
|
1085
|
+
if (opts.waitingOn !== void 0) patch.waitingOn = opts.waitingOn;
|
|
1038
1086
|
if (opts.body !== void 0) {
|
|
1039
1087
|
const body = opts.body.trim();
|
|
1040
1088
|
if (!body) throw new CrmInputError("body is required");
|
|
1041
|
-
|
|
1089
|
+
patch.body = body;
|
|
1042
1090
|
}
|
|
1043
|
-
if (Object.keys(
|
|
1044
|
-
const { duplicate } = await db.transact(
|
|
1091
|
+
if (Object.keys(patch).length === 0) throw new CrmInputError("nothing to update");
|
|
1092
|
+
const { duplicate } = await db.transact(patchOps(CRM_NS.activity, opts.id, patch), {
|
|
1045
1093
|
mutationId: opts.mutationId
|
|
1046
1094
|
});
|
|
1047
|
-
return { activity:
|
|
1095
|
+
return { activity: applyPatch(activity, patch), duplicate };
|
|
1048
1096
|
}
|
|
1049
1097
|
async function listActivities(deps, opts) {
|
|
1050
1098
|
const { db } = resolveDeps(deps);
|
|
@@ -1105,6 +1153,8 @@ async function saveView(deps, opts) {
|
|
|
1105
1153
|
ns: CRM_NS.view,
|
|
1106
1154
|
id,
|
|
1107
1155
|
attrs: {
|
|
1156
|
+
id,
|
|
1157
|
+
// mirror the entity id so getView/updateView can read one by id
|
|
1108
1158
|
name: opts.spec.name.trim(),
|
|
1109
1159
|
type: opts.spec.type,
|
|
1110
1160
|
ownerId: opts.ownerId,
|
|
@@ -1231,23 +1281,17 @@ async function saveConfig(deps, patch, opts = {}) {
|
|
|
1231
1281
|
settings: patch.settings !== void 0 ? patch.settings : base.settings,
|
|
1232
1282
|
updatedAt: now()
|
|
1233
1283
|
};
|
|
1284
|
+
const write = {
|
|
1285
|
+
key: CONFIG_KEY,
|
|
1286
|
+
notificationEmail: merged.notificationEmail ?? null,
|
|
1287
|
+
replyTo: merged.replyTo ?? null,
|
|
1288
|
+
debugEmail: merged.debugEmail ?? null,
|
|
1289
|
+
emailTemplates: merged.emailTemplates,
|
|
1290
|
+
settings: merged.settings,
|
|
1291
|
+
updatedAt: merged.updatedAt
|
|
1292
|
+
};
|
|
1234
1293
|
const { duplicate } = await db.transact(
|
|
1235
|
-
|
|
1236
|
-
{
|
|
1237
|
-
t: "update",
|
|
1238
|
-
ns: CRM_NS.config,
|
|
1239
|
-
id: { ns: CRM_NS.config, attr: "key", value: CONFIG_KEY },
|
|
1240
|
-
attrs: {
|
|
1241
|
-
key: CONFIG_KEY,
|
|
1242
|
-
notificationEmail: merged.notificationEmail ?? null,
|
|
1243
|
-
replyTo: merged.replyTo ?? null,
|
|
1244
|
-
debugEmail: merged.debugEmail ?? null,
|
|
1245
|
-
emailTemplates: merged.emailTemplates,
|
|
1246
|
-
settings: merged.settings,
|
|
1247
|
-
updatedAt: merged.updatedAt
|
|
1248
|
-
}
|
|
1249
|
-
}
|
|
1250
|
-
],
|
|
1294
|
+
patchOps(CRM_NS.config, { ns: CRM_NS.config, attr: "key", value: CONFIG_KEY }, write),
|
|
1251
1295
|
{ mutationId: opts.mutationId }
|
|
1252
1296
|
);
|
|
1253
1297
|
return { config: merged, duplicate };
|