@odla-ai/crm 0.9.1 → 0.9.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
@@ -1129,7 +1129,7 @@ async function createRecord(deps, opts) {
1129
1129
  return { id, duplicate, record: { id, ...attrs } };
1130
1130
  }
1131
1131
  async function updateRecord(deps, opts) {
1132
- const { crm, db, now } = resolveDeps(deps);
1132
+ const { crm, db, now, newId } = resolveDeps(deps);
1133
1133
  const existing = await mustGetRecord(deps, opts.id);
1134
1134
  const prepared = crm.prepare(existing.type, opts.input, existing);
1135
1135
  if (!prepared.ok) throw new CrmInputError("invalid input", prepared.errors);
@@ -1148,8 +1148,33 @@ async function updateRecord(deps, opts) {
1148
1148
  const score = scoreOf(deps, existing.type, candidate);
1149
1149
  if (score !== void 0) patch.score = score;
1150
1150
  const ops = [...patchOps(CRM_NS.record, existing.id, patch, prepared.retract), ...channelOps];
1151
- const { duplicate } = await db.transact(ops, { mutationId: opts.mutationId });
1152
- return { record: applyPatch(existing, patch, prepared.retract), duplicate };
1151
+ if (opts.audit) {
1152
+ const body = opts.audit.body.trim();
1153
+ if (!body) throw new CrmInputError("audit body is required");
1154
+ const id = opts.audit.id ?? newId();
1155
+ ops.push({
1156
+ t: "update",
1157
+ ns: CRM_NS.activity,
1158
+ id,
1159
+ attrs: {
1160
+ id,
1161
+ recordId: existing.id,
1162
+ kind: "note",
1163
+ body,
1164
+ authorId: opts.audit.authorId,
1165
+ createdAt: now(),
1166
+ ...opts.audit.meta ? { meta: opts.audit.meta } : {}
1167
+ }
1168
+ });
1169
+ }
1170
+ const { duplicate } = await db.transact(ops, {
1171
+ mutationId: opts.mutationId,
1172
+ ...opts.expectedUpdatedAt === void 0 ? {} : { guards: [{ ns: CRM_NS.record, id: existing.id, exists: true, equals: { updatedAt: opts.expectedUpdatedAt } }] }
1173
+ });
1174
+ return {
1175
+ record: duplicate ? await mustGetRecord(deps, existing.id) : applyPatch(existing, patch, prepared.retract),
1176
+ duplicate
1177
+ };
1153
1178
  }
1154
1179
  async function setArchived(deps, opts) {
1155
1180
  const { db, now } = resolveDeps(deps);
@@ -1176,7 +1201,7 @@ async function setStage(deps, opts) {
1176
1201
  stageChangedAt: at,
1177
1202
  updatedAt: at
1178
1203
  };
1179
- const activityId = newId();
1204
+ const activityId = opts.activityId ?? newId();
1180
1205
  const ops = [
1181
1206
  { t: "update", ns: CRM_NS.record, id: record.id, attrs },
1182
1207
  {
@@ -1188,14 +1213,30 @@ async function setStage(deps, opts) {
1188
1213
  // mirror the entity id so the activity is readable by id
1189
1214
  recordId: record.id,
1190
1215
  kind: "stage_change",
1191
- body: `${record.stage ?? "(none)"} \u2192 ${opts.to}`,
1216
+ body: opts.activityBody ?? `${record.stage ?? "(none)"} \u2192 ${opts.to}`,
1192
1217
  authorId: opts.authorId,
1193
- createdAt: at
1218
+ createdAt: at,
1219
+ ...opts.activityMeta ? { meta: opts.activityMeta } : {}
1194
1220
  }
1195
1221
  }
1196
1222
  ];
1197
- const { duplicate } = await db.transact(ops, { mutationId: opts.mutationId });
1198
- const updated = { ...record, ...attrs };
1223
+ const equals = {};
1224
+ if (opts.expectedUpdatedAt !== void 0) equals.updatedAt = opts.expectedUpdatedAt;
1225
+ if (typeof opts.expectedStage === "string") equals.stage = opts.expectedStage;
1226
+ const guarded = opts.expectedUpdatedAt !== void 0 || opts.expectedStage !== void 0;
1227
+ const { duplicate } = await db.transact(ops, {
1228
+ mutationId: opts.mutationId,
1229
+ ...guarded ? {
1230
+ guards: [{
1231
+ ns: CRM_NS.record,
1232
+ id: record.id,
1233
+ exists: true,
1234
+ ...Object.keys(equals).length ? { equals } : {},
1235
+ ...opts.expectedStage === null ? { absent: ["stage"] } : {}
1236
+ }]
1237
+ } : {}
1238
+ });
1239
+ const updated = duplicate ? await mustGetRecord(deps, record.id) : { ...record, ...attrs };
1199
1240
  return {
1200
1241
  record: updated,
1201
1242
  from: record.stage ?? null,