@odla-ai/crm 0.9.0 → 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
@@ -803,10 +803,14 @@ function resultAsWritten(result) {
803
803
 
804
804
  // src/deps.ts
805
805
  function readingAsWritten(db) {
806
+ const batch = db.aggregateBatch;
806
807
  return {
807
808
  query: async (query) => resultAsWritten(await db.query(query)),
808
809
  transact: (ops, opts) => db.transact(ops, opts),
809
- aggregate: (ns, spec, queryOpts) => db.aggregate(ns, spec, queryOpts)
810
+ aggregate: (ns, spec, queryOpts) => db.aggregate(ns, spec, queryOpts),
811
+ ...batch ? {
812
+ aggregateBatch: (requests) => batch.call(db, requests)
813
+ } : {}
810
814
  };
811
815
  }
812
816
  function resolveDeps(deps) {
@@ -1125,7 +1129,7 @@ async function createRecord(deps, opts) {
1125
1129
  return { id, duplicate, record: { id, ...attrs } };
1126
1130
  }
1127
1131
  async function updateRecord(deps, opts) {
1128
- const { crm, db, now } = resolveDeps(deps);
1132
+ const { crm, db, now, newId } = resolveDeps(deps);
1129
1133
  const existing = await mustGetRecord(deps, opts.id);
1130
1134
  const prepared = crm.prepare(existing.type, opts.input, existing);
1131
1135
  if (!prepared.ok) throw new CrmInputError("invalid input", prepared.errors);
@@ -1144,8 +1148,33 @@ async function updateRecord(deps, opts) {
1144
1148
  const score = scoreOf(deps, existing.type, candidate);
1145
1149
  if (score !== void 0) patch.score = score;
1146
1150
  const ops = [...patchOps(CRM_NS.record, existing.id, patch, prepared.retract), ...channelOps];
1147
- const { duplicate } = await db.transact(ops, { mutationId: opts.mutationId });
1148
- 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
+ };
1149
1178
  }
1150
1179
  async function setArchived(deps, opts) {
1151
1180
  const { db, now } = resolveDeps(deps);
@@ -1172,7 +1201,7 @@ async function setStage(deps, opts) {
1172
1201
  stageChangedAt: at,
1173
1202
  updatedAt: at
1174
1203
  };
1175
- const activityId = newId();
1204
+ const activityId = opts.activityId ?? newId();
1176
1205
  const ops = [
1177
1206
  { t: "update", ns: CRM_NS.record, id: record.id, attrs },
1178
1207
  {
@@ -1184,14 +1213,30 @@ async function setStage(deps, opts) {
1184
1213
  // mirror the entity id so the activity is readable by id
1185
1214
  recordId: record.id,
1186
1215
  kind: "stage_change",
1187
- body: `${record.stage ?? "(none)"} \u2192 ${opts.to}`,
1216
+ body: opts.activityBody ?? `${record.stage ?? "(none)"} \u2192 ${opts.to}`,
1188
1217
  authorId: opts.authorId,
1189
- createdAt: at
1218
+ createdAt: at,
1219
+ ...opts.activityMeta ? { meta: opts.activityMeta } : {}
1190
1220
  }
1191
1221
  }
1192
1222
  ];
1193
- const { duplicate } = await db.transact(ops, { mutationId: opts.mutationId });
1194
- 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 };
1195
1240
  return {
1196
1241
  record: updated,
1197
1242
  from: record.stage ?? null,
@@ -1867,13 +1912,29 @@ async function typeSummary(deps, type) {
1867
1912
  where: { and: [{ type }, { archived: false }, ...extra] }
1868
1913
  });
1869
1914
  const stageIds = (def.pipeline?.stages ?? []).map((s) => s.id);
1870
- const [total, openTasks, ...stageCounts] = await Promise.all([
1871
- db.aggregate(CRM_NS.record, { count: true }, live([])),
1872
- db.aggregate(CRM_NS.activity, { count: true }, {
1873
- where: { and: [{ kind: "task" }, { status: { $in: ["open", "waiting"] } }] }
1874
- }),
1875
- ...stageIds.map((stage) => db.aggregate(CRM_NS.record, { count: true }, live([{ stage }])))
1876
- ]);
1915
+ const requests = [
1916
+ { ns: CRM_NS.record, aggregate: { count: true }, opts: live([]) },
1917
+ {
1918
+ ns: CRM_NS.activity,
1919
+ aggregate: { count: true },
1920
+ opts: {
1921
+ where: {
1922
+ and: [{ kind: "task" }, { status: { $in: ["open", "waiting"] } }]
1923
+ }
1924
+ }
1925
+ },
1926
+ ...stageIds.map((stage) => ({
1927
+ ns: CRM_NS.record,
1928
+ aggregate: { count: true },
1929
+ opts: live([{ stage }])
1930
+ }))
1931
+ ];
1932
+ const batch = db.aggregateBatch;
1933
+ const [total, openTasks, ...stageCounts] = batch ? await batch(requests) : await Promise.all(
1934
+ requests.map(
1935
+ (request) => db.aggregate(request.ns, request.aggregate, request.opts)
1936
+ )
1937
+ );
1877
1938
  const stages = {};
1878
1939
  stageIds.forEach((stage, i) => {
1879
1940
  stages[stage] = stageCounts[i]?.count ?? 0;