@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 CHANGED
@@ -172,6 +172,10 @@ var CRM_SCHEMA = {
172
172
  entities: {
173
173
  [CRM_NS.record]: {
174
174
  attrs: {
175
+ // Entity ids are not attributes in odla-db, so `where: { id }` matches
176
+ // nothing unless the id is mirrored as a written attr — the getRecord /
177
+ // tag-filter / neighbor reads all filter by id. Mirror it as unique.
178
+ id: uniq("string"),
175
179
  type: idx("string"),
176
180
  name: idx("string"),
177
181
  primaryEmail: idx("string", true),
@@ -226,6 +230,8 @@ var CRM_SCHEMA = {
226
230
  },
227
231
  [CRM_NS.activity]: {
228
232
  attrs: {
233
+ id: uniq("string"),
234
+ // mirrored so updateTask can read one by id
229
235
  recordId: idx("string"),
230
236
  kind: idx("string"),
231
237
  body: a("string"),
@@ -275,6 +281,8 @@ var CRM_SCHEMA = {
275
281
  },
276
282
  [CRM_NS.view]: {
277
283
  attrs: {
284
+ id: uniq("string"),
285
+ // mirrored so getView/updateView can read one by id
278
286
  name: a("string"),
279
287
  type: idx("string"),
280
288
  ownerId: idx("string", true),
@@ -470,10 +478,21 @@ function prepare(def, input, existing) {
470
478
  fields,
471
479
  searchText: searchTextFor(def, name, primaryEmail, fields)
472
480
  };
473
- if (def.emailField) attrs.primaryEmail = primaryEmail;
474
- for (const [fieldName, field] of Object.entries(def.fields))
475
- if (field.slot) attrs[field.slot] = fields[fieldName] ?? null;
476
- return { ok: true, attrs };
481
+ const retract = [];
482
+ const clear = (column) => {
483
+ if (existing) retract.push(column);
484
+ };
485
+ if (def.emailField) {
486
+ if (primaryEmail === null) clear("primaryEmail");
487
+ else attrs.primaryEmail = primaryEmail;
488
+ }
489
+ for (const [fieldName, field] of Object.entries(def.fields)) {
490
+ if (!field.slot) continue;
491
+ const value = fields[fieldName];
492
+ if (empty(field, value)) clear(field.slot);
493
+ else attrs[field.slot] = value;
494
+ }
495
+ return { ok: true, attrs, retract };
477
496
  }
478
497
 
479
498
  // src/config.ts
@@ -783,6 +802,29 @@ async function applyConsent(deps, opts) {
783
802
  return { state: outcome.state, recordId: channel.recordId, duplicate };
784
803
  }
785
804
 
805
+ // src/write-ops.ts
806
+ function patchOps(ns, id, patch, alsoClear = []) {
807
+ const attrs = {};
808
+ const retract = [...alsoClear];
809
+ for (const [key, value] of Object.entries(patch)) {
810
+ if (value === null) retract.push(key);
811
+ else if (value !== void 0) attrs[key] = value;
812
+ }
813
+ const ops = [];
814
+ if (Object.keys(attrs).length > 0) ops.push({ t: "update", ns, id, attrs });
815
+ if (retract.length > 0) ops.push({ t: "retract", ns, id, attrs: retract });
816
+ return ops;
817
+ }
818
+ function applyPatch(base, patch, alsoClear = []) {
819
+ const out = { ...base };
820
+ for (const [key, value] of Object.entries(patch)) {
821
+ if (value === null) delete out[key];
822
+ else if (value !== void 0) out[key] = value;
823
+ }
824
+ for (const key of alsoClear) delete out[key];
825
+ return out;
826
+ }
827
+
786
828
  // src/records.ts
787
829
  function rowToRecord(row) {
788
830
  const record = row;
@@ -839,6 +881,8 @@ async function createRecord(deps, opts) {
839
881
  const at = now();
840
882
  const stage = opts.stage ?? crm.initialStage(opts.type);
841
883
  const attrs = {
884
+ id,
885
+ // mirror the entity id as an attr so `where: { id }` reads can find it
842
886
  type: opts.type,
843
887
  ...prepared.attrs,
844
888
  archived: false,
@@ -873,32 +917,31 @@ async function updateRecord(deps, opts) {
873
917
  const existing = await mustGetRecord(deps, opts.id);
874
918
  const prepared = crm.prepare(existing.type, opts.input, existing);
875
919
  if (!prepared.ok) throw new CrmInputError("invalid input", prepared.errors);
876
- const attrs = { ...prepared.attrs, updatedAt: now() };
877
- if (opts.ownerId !== void 0) attrs.ownerId = opts.ownerId;
878
- const ops = [];
920
+ const patch = { ...prepared.attrs, updatedAt: now(), ownerId: opts.ownerId };
921
+ const channelOps = [];
879
922
  if (crm.type(existing.type).emailField) {
880
923
  const nextEmail = prepared.attrs["primaryEmail"] ?? null;
881
924
  if (nextEmail !== (existing.primaryEmail ?? null)) {
882
925
  const channels = await listChannels(deps, existing.id);
883
926
  const channel = emailChannelOps(deps, existing.id, nextEmail, channels);
884
- attrs.emailStatus = channel.emailStatus;
885
- ops.push(...channel.ops);
927
+ patch.emailStatus = channel.emailStatus;
928
+ channelOps.push(...channel.ops);
886
929
  }
887
930
  }
888
- const candidate = { ...existing, ...attrs };
931
+ const candidate = { ...existing, ...prepared.attrs };
889
932
  const score = scoreOf(deps, existing.type, candidate);
890
- if (score !== void 0) attrs.score = score;
891
- ops.unshift({ t: "update", ns: CRM_NS.record, id: existing.id, attrs });
933
+ if (score !== void 0) patch.score = score;
934
+ const ops = [...patchOps(CRM_NS.record, existing.id, patch, prepared.retract), ...channelOps];
892
935
  const { duplicate } = await db.transact(ops, { mutationId: opts.mutationId });
893
- return { record: { ...existing, ...attrs }, duplicate };
936
+ return { record: applyPatch(existing, patch, prepared.retract), duplicate };
894
937
  }
895
938
  async function setArchived(deps, opts) {
896
939
  const { db, now } = resolveDeps(deps);
897
940
  await mustGetRecord(deps, opts.id);
898
941
  const at = now();
899
- const attrs = opts.archived ? { archived: true, archivedAt: at, updatedAt: at } : { archived: false, archivedAt: null, updatedAt: at };
942
+ const patch = opts.archived ? { archived: true, archivedAt: at, updatedAt: at } : { archived: false, archivedAt: null, updatedAt: at };
900
943
  const { duplicate } = await db.transact(
901
- [{ t: "update", ns: CRM_NS.record, id: opts.id, attrs }],
944
+ patchOps(CRM_NS.record, opts.id, patch),
902
945
  { mutationId: opts.mutationId }
903
946
  );
904
947
  return { duplicate };
@@ -917,13 +960,16 @@ async function setStage(deps, opts) {
917
960
  stageChangedAt: at,
918
961
  updatedAt: at
919
962
  };
963
+ const activityId = newId();
920
964
  const ops = [
921
965
  { t: "update", ns: CRM_NS.record, id: record.id, attrs },
922
966
  {
923
967
  t: "update",
924
968
  ns: CRM_NS.activity,
925
- id: newId(),
969
+ id: activityId,
926
970
  attrs: {
971
+ id: activityId,
972
+ // mirror the entity id so the activity is readable by id
927
973
  recordId: record.id,
928
974
  kind: "stage_change",
929
975
  body: `${record.stage ?? "(none)"} \u2192 ${opts.to}`,
@@ -1102,6 +1148,8 @@ async function addActivity(deps, opts) {
1102
1148
  await mustGetRecord(deps, opts.recordId);
1103
1149
  const id = newId();
1104
1150
  const attrs = {
1151
+ id,
1152
+ // mirror the entity id so updateTask's `where: { id }` read can find it
1105
1153
  recordId: opts.recordId,
1106
1154
  kind: opts.kind,
1107
1155
  body,
@@ -1127,25 +1175,25 @@ async function updateTask(deps, opts) {
1127
1175
  if (!row) throw new CrmNotFoundError("activity");
1128
1176
  const activity = rowToActivity(row);
1129
1177
  if (activity.kind !== "task") throw new CrmInputError("only tasks can be updated");
1130
- const attrs = {};
1178
+ const patch = {};
1131
1179
  if (opts.status !== void 0) {
1132
1180
  if (!TASK_STATUSES.includes(opts.status)) throw new CrmInputError(`unknown status "${opts.status}"`);
1133
- attrs.status = opts.status;
1134
- attrs.completedAt = opts.status === "done" ? now() : null;
1181
+ patch.status = opts.status;
1182
+ patch.completedAt = opts.status === "done" ? now() : null;
1135
1183
  }
1136
- if (opts.dueAt !== void 0) attrs.dueAt = opts.dueAt;
1137
- if (opts.assigneeId !== void 0) attrs.assigneeId = opts.assigneeId;
1138
- if (opts.waitingOn !== void 0) attrs.waitingOn = opts.waitingOn;
1184
+ if (opts.dueAt !== void 0) patch.dueAt = opts.dueAt;
1185
+ if (opts.assigneeId !== void 0) patch.assigneeId = opts.assigneeId;
1186
+ if (opts.waitingOn !== void 0) patch.waitingOn = opts.waitingOn;
1139
1187
  if (opts.body !== void 0) {
1140
1188
  const body = opts.body.trim();
1141
1189
  if (!body) throw new CrmInputError("body is required");
1142
- attrs.body = body;
1190
+ patch.body = body;
1143
1191
  }
1144
- if (Object.keys(attrs).length === 0) throw new CrmInputError("nothing to update");
1145
- const { duplicate } = await db.transact([{ t: "update", ns: CRM_NS.activity, id: opts.id, attrs }], {
1192
+ if (Object.keys(patch).length === 0) throw new CrmInputError("nothing to update");
1193
+ const { duplicate } = await db.transact(patchOps(CRM_NS.activity, opts.id, patch), {
1146
1194
  mutationId: opts.mutationId
1147
1195
  });
1148
- return { activity: { ...activity, ...attrs }, duplicate };
1196
+ return { activity: applyPatch(activity, patch), duplicate };
1149
1197
  }
1150
1198
  async function listActivities(deps, opts) {
1151
1199
  const { db } = resolveDeps(deps);
@@ -1206,6 +1254,8 @@ async function saveView(deps, opts) {
1206
1254
  ns: CRM_NS.view,
1207
1255
  id,
1208
1256
  attrs: {
1257
+ id,
1258
+ // mirror the entity id so getView/updateView can read one by id
1209
1259
  name: opts.spec.name.trim(),
1210
1260
  type: opts.spec.type,
1211
1261
  ownerId: opts.ownerId,
@@ -1332,23 +1382,17 @@ async function saveConfig(deps, patch, opts = {}) {
1332
1382
  settings: patch.settings !== void 0 ? patch.settings : base.settings,
1333
1383
  updatedAt: now()
1334
1384
  };
1385
+ const write = {
1386
+ key: CONFIG_KEY,
1387
+ notificationEmail: merged.notificationEmail ?? null,
1388
+ replyTo: merged.replyTo ?? null,
1389
+ debugEmail: merged.debugEmail ?? null,
1390
+ emailTemplates: merged.emailTemplates,
1391
+ settings: merged.settings,
1392
+ updatedAt: merged.updatedAt
1393
+ };
1335
1394
  const { duplicate } = await db.transact(
1336
- [
1337
- {
1338
- t: "update",
1339
- ns: CRM_NS.config,
1340
- id: { ns: CRM_NS.config, attr: "key", value: CONFIG_KEY },
1341
- attrs: {
1342
- key: CONFIG_KEY,
1343
- notificationEmail: merged.notificationEmail ?? null,
1344
- replyTo: merged.replyTo ?? null,
1345
- debugEmail: merged.debugEmail ?? null,
1346
- emailTemplates: merged.emailTemplates,
1347
- settings: merged.settings,
1348
- updatedAt: merged.updatedAt
1349
- }
1350
- }
1351
- ],
1395
+ patchOps(CRM_NS.config, { ns: CRM_NS.config, attr: "key", value: CONFIG_KEY }, write),
1352
1396
  { mutationId: opts.mutationId }
1353
1397
  );
1354
1398
  return { config: merged, duplicate };