@odla-ai/crm 0.3.2 → 0.5.0
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/README.md +41 -1
- package/dist/index.cjs +390 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +136 -2
- package/dist/index.d.ts +136 -2
- package/dist/index.js +390 -12
- package/dist/index.js.map +1 -1
- package/dist/ui/index.d.ts +37 -1
- package/dist/ui/index.js +82 -28
- package/dist/ui/index.js.map +1 -1
- package/package.json +1 -1
- package/ui.css +4 -0
package/dist/index.js
CHANGED
|
@@ -7,7 +7,9 @@ var CRM_NS = {
|
|
|
7
7
|
channel: "crm_contact_channel",
|
|
8
8
|
emailLog: "crm_email_log",
|
|
9
9
|
view: "crm_view",
|
|
10
|
-
config: "crm_config"
|
|
10
|
+
config: "crm_config",
|
|
11
|
+
origin: "crm_record_origin",
|
|
12
|
+
delivery: "crm_record_delivery"
|
|
11
13
|
};
|
|
12
14
|
var CRM_SLOTS = ["s1", "s2", "s3", "s4", "n1", "n2", "n3", "d1", "d2"];
|
|
13
15
|
var CONSENT_STATES = ["subscribed", "unsubscribed", "bounced", "suppressed"];
|
|
@@ -205,6 +207,37 @@ var CRM_SCHEMA = {
|
|
|
205
207
|
settings: a("json"),
|
|
206
208
|
updatedAt: idx("date")
|
|
207
209
|
}
|
|
210
|
+
},
|
|
211
|
+
[CRM_NS.origin]: {
|
|
212
|
+
attrs: {
|
|
213
|
+
// One upstream record maps to one local record. The structured source
|
|
214
|
+
// survives tag edits and supports more than one upstream site.
|
|
215
|
+
key: uniq("string"),
|
|
216
|
+
// `${sourceId}:${sourceRecordId}`
|
|
217
|
+
recordId: idx("string"),
|
|
218
|
+
sourceId: idx("string"),
|
|
219
|
+
sourceRecordId: idx("string"),
|
|
220
|
+
sourceUrl: opt("string"),
|
|
221
|
+
payloadVersion: idx("number"),
|
|
222
|
+
firstReceivedAt: idx("date"),
|
|
223
|
+
lastReceivedAt: idx("date")
|
|
224
|
+
}
|
|
225
|
+
},
|
|
226
|
+
[CRM_NS.delivery]: {
|
|
227
|
+
attrs: {
|
|
228
|
+
// The latest delivery state for one local record → target edge.
|
|
229
|
+
key: uniq("string"),
|
|
230
|
+
// `${recordId}:${targetId}`
|
|
231
|
+
recordId: idx("string"),
|
|
232
|
+
targetId: idx("string"),
|
|
233
|
+
remoteRecordId: idx("string", true),
|
|
234
|
+
status: idx("string"),
|
|
235
|
+
attempts: idx("number"),
|
|
236
|
+
payloadVersion: idx("number"),
|
|
237
|
+
lastAttemptAt: idx("date"),
|
|
238
|
+
deliveredAt: idx("date", true),
|
|
239
|
+
error: opt("string")
|
|
240
|
+
}
|
|
208
241
|
}
|
|
209
242
|
},
|
|
210
243
|
links: {}
|
|
@@ -414,6 +447,9 @@ function fail(path, message) {
|
|
|
414
447
|
function checkType(typeName, def) {
|
|
415
448
|
const at = `types.${typeName}`;
|
|
416
449
|
if (!NAME_RE.test(typeName)) fail(at, "must match /^[a-z][a-z0-9_-]*$/");
|
|
450
|
+
if (def.workspace != null && !NAME_RE.test(def.workspace)) {
|
|
451
|
+
fail(`${at}.workspace`, "must match /^[a-z][a-z0-9_-]*$/");
|
|
452
|
+
}
|
|
417
453
|
const claimed = /* @__PURE__ */ new Map();
|
|
418
454
|
for (const [fieldName, field] of Object.entries(def.fields)) {
|
|
419
455
|
const fat = `${at}.fields.${fieldName}`;
|
|
@@ -494,6 +530,71 @@ ${tpl.defaults.text}`, tpl.vars);
|
|
|
494
530
|
stageIndex: (name, stage) => stageIndexOf(type(name), stage)
|
|
495
531
|
};
|
|
496
532
|
}
|
|
533
|
+
function composeCrmConfigs(...configs) {
|
|
534
|
+
if (configs.length === 0) throw new Error("composeCrmConfigs: at least one config is required");
|
|
535
|
+
const result = { types: {} };
|
|
536
|
+
const merge = (kind, target, source) => {
|
|
537
|
+
for (const [id, value] of Object.entries(source ?? {})) {
|
|
538
|
+
if (Object.hasOwn(target, id)) throw new Error(`composeCrmConfigs: duplicate ${kind} "${id}"`);
|
|
539
|
+
target[id] = value;
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
for (const config of configs) {
|
|
543
|
+
merge("type", result.types, config.types);
|
|
544
|
+
if (config.relations) {
|
|
545
|
+
result.relations ??= {};
|
|
546
|
+
merge("relation", result.relations, config.relations);
|
|
547
|
+
}
|
|
548
|
+
if (config.templates) {
|
|
549
|
+
result.templates ??= {};
|
|
550
|
+
merge("template", result.templates, config.templates);
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
return result;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// src/discussion-reference.ts
|
|
557
|
+
var CRM_DISCUSSION_REFERENCE_KINDS = [
|
|
558
|
+
"crm:record",
|
|
559
|
+
"crm:activity"
|
|
560
|
+
];
|
|
561
|
+
var SEGMENT = /^[A-Za-z0-9][A-Za-z0-9._~-]{0,199}$/;
|
|
562
|
+
function crmDiscussionReferenceId(target) {
|
|
563
|
+
const root = `${target.type}/${target.recordId}`;
|
|
564
|
+
return target.activityId ? `${root}/${target.activityId}` : root;
|
|
565
|
+
}
|
|
566
|
+
function formatCrmDiscussionReference(target) {
|
|
567
|
+
return `${target.kind}/${crmDiscussionReferenceId(target)}`;
|
|
568
|
+
}
|
|
569
|
+
function parseCrmDiscussionReference(input) {
|
|
570
|
+
let raw;
|
|
571
|
+
try {
|
|
572
|
+
raw = input instanceof URL ? input.searchParams.get("odla-ref") : new URL(input, "https://crm.invalid").searchParams.get("odla-ref");
|
|
573
|
+
} catch {
|
|
574
|
+
return null;
|
|
575
|
+
}
|
|
576
|
+
if (!raw) return null;
|
|
577
|
+
const [kind, type, recordId, activityId, extra] = raw.split("/");
|
|
578
|
+
if (extra !== void 0 || !CRM_DISCUSSION_REFERENCE_KINDS.includes(
|
|
579
|
+
kind
|
|
580
|
+
) || !type || !recordId || !SEGMENT.test(type) || !SEGMENT.test(recordId)) return null;
|
|
581
|
+
if (kind === "crm:record") {
|
|
582
|
+
return activityId === void 0 ? { kind, type, recordId } : null;
|
|
583
|
+
}
|
|
584
|
+
return activityId && SEGMENT.test(activityId) ? { kind: "crm:activity", type, recordId, activityId } : null;
|
|
585
|
+
}
|
|
586
|
+
function crmDiscussionReferenceHref(current, target, basePath = "/admin/") {
|
|
587
|
+
const next = new URL(current.origin);
|
|
588
|
+
next.pathname = basePath.startsWith("/") ? basePath : `/${basePath}`;
|
|
589
|
+
next.searchParams.set("odla-ref", formatCrmDiscussionReference(target));
|
|
590
|
+
next.hash = [
|
|
591
|
+
"people",
|
|
592
|
+
target.type,
|
|
593
|
+
target.recordId,
|
|
594
|
+
target.kind === "crm:activity" ? "notes" : "profile"
|
|
595
|
+
].map(encodeURIComponent).join("/");
|
|
596
|
+
return next.toString();
|
|
597
|
+
}
|
|
497
598
|
|
|
498
599
|
// src/deps.ts
|
|
499
600
|
function resolveDeps(deps) {
|
|
@@ -543,14 +644,14 @@ function compileList(crm, params, tagRecordIds) {
|
|
|
543
644
|
const column = resolveSortColumn(crm, params.type, sort.field);
|
|
544
645
|
const limit = Math.min(Math.max(params.limit ?? DEFAULT_LIMIT, 1), MAX_LIMIT);
|
|
545
646
|
const offset = Math.max(params.offset ?? 0, 0);
|
|
546
|
-
const
|
|
647
|
+
const search2 = params.search?.trim();
|
|
547
648
|
return {
|
|
548
649
|
opts: {
|
|
549
650
|
where: { and },
|
|
550
651
|
order: { [column]: sort.dir },
|
|
551
652
|
limit,
|
|
552
653
|
offset,
|
|
553
|
-
...
|
|
654
|
+
...search2 ? { search: search2 } : {}
|
|
554
655
|
},
|
|
555
656
|
limit,
|
|
556
657
|
offset
|
|
@@ -1037,6 +1138,129 @@ async function listLinks(deps, recordId) {
|
|
|
1037
1138
|
return (result[CRM_NS.link] ?? []).map((row) => row);
|
|
1038
1139
|
}
|
|
1039
1140
|
|
|
1141
|
+
// src/provenance.ts
|
|
1142
|
+
var SOURCE_ID = /^[a-z0-9][a-z0-9-]{1,127}$/;
|
|
1143
|
+
var TARGET_ID = SOURCE_ID;
|
|
1144
|
+
function required(value, label, max = 512) {
|
|
1145
|
+
const normalized = value.trim();
|
|
1146
|
+
if (!normalized || normalized.length > max) throw new CrmInputError(`${label} must be 1\u2013${max} characters`);
|
|
1147
|
+
return normalized;
|
|
1148
|
+
}
|
|
1149
|
+
function recordOriginKey(sourceId, sourceRecordId) {
|
|
1150
|
+
const source = required(sourceId, "sourceId", 128);
|
|
1151
|
+
const record = required(sourceRecordId, "sourceRecordId");
|
|
1152
|
+
if (!SOURCE_ID.test(source)) throw new CrmInputError("sourceId must be a lowercase app slug");
|
|
1153
|
+
return `${source}:${record}`;
|
|
1154
|
+
}
|
|
1155
|
+
async function getRecordOrigin(deps, sourceId, sourceRecordId) {
|
|
1156
|
+
const { db } = resolveDeps(deps);
|
|
1157
|
+
const key = recordOriginKey(sourceId, sourceRecordId);
|
|
1158
|
+
const result = await db.query({ [CRM_NS.origin]: { $: { where: { key }, limit: 1 } } });
|
|
1159
|
+
return (result[CRM_NS.origin] ?? [])[0] ?? null;
|
|
1160
|
+
}
|
|
1161
|
+
async function getRecordByOrigin(deps, sourceId, sourceRecordId) {
|
|
1162
|
+
const origin = await getRecordOrigin(deps, sourceId, sourceRecordId);
|
|
1163
|
+
return origin ? getRecord(deps, origin.recordId) : null;
|
|
1164
|
+
}
|
|
1165
|
+
async function upsertRecordOrigin(deps, opts) {
|
|
1166
|
+
const { db, now } = resolveDeps(deps);
|
|
1167
|
+
await mustGetRecord(deps, opts.recordId);
|
|
1168
|
+
const key = recordOriginKey(opts.sourceId, opts.sourceRecordId);
|
|
1169
|
+
if (!Number.isSafeInteger(opts.payloadVersion) || opts.payloadVersion <= 0) {
|
|
1170
|
+
throw new CrmInputError("payloadVersion must be a positive integer");
|
|
1171
|
+
}
|
|
1172
|
+
const sourceUrl = opts.sourceUrl == null ? void 0 : required(opts.sourceUrl, "sourceUrl", 2048);
|
|
1173
|
+
const existing = await getRecordOrigin(deps, opts.sourceId, opts.sourceRecordId);
|
|
1174
|
+
if (existing && existing.recordId !== opts.recordId) {
|
|
1175
|
+
throw new CrmInputError("upstream record is already bound to another local record");
|
|
1176
|
+
}
|
|
1177
|
+
const at = now();
|
|
1178
|
+
const attrs = {
|
|
1179
|
+
key,
|
|
1180
|
+
recordId: opts.recordId,
|
|
1181
|
+
sourceId: opts.sourceId,
|
|
1182
|
+
sourceRecordId: opts.sourceRecordId,
|
|
1183
|
+
payloadVersion: opts.payloadVersion,
|
|
1184
|
+
firstReceivedAt: existing?.firstReceivedAt ?? at,
|
|
1185
|
+
lastReceivedAt: at,
|
|
1186
|
+
...sourceUrl ? { sourceUrl } : {}
|
|
1187
|
+
};
|
|
1188
|
+
const { duplicate } = await db.transact(
|
|
1189
|
+
[{ t: "update", ns: CRM_NS.origin, id: { ns: CRM_NS.origin, attr: "key", value: key }, attrs }],
|
|
1190
|
+
{ mutationId: opts.mutationId }
|
|
1191
|
+
);
|
|
1192
|
+
return {
|
|
1193
|
+
duplicate,
|
|
1194
|
+
origin: {
|
|
1195
|
+
id: existing?.id ?? key,
|
|
1196
|
+
...attrs
|
|
1197
|
+
}
|
|
1198
|
+
};
|
|
1199
|
+
}
|
|
1200
|
+
async function listRecordOrigins(deps, recordId) {
|
|
1201
|
+
const { db } = resolveDeps(deps);
|
|
1202
|
+
const result = await db.query({
|
|
1203
|
+
[CRM_NS.origin]: { $: { where: { recordId }, order: { firstReceivedAt: "asc" }, limit: 200 } }
|
|
1204
|
+
});
|
|
1205
|
+
return result[CRM_NS.origin] ?? [];
|
|
1206
|
+
}
|
|
1207
|
+
function recordDeliveryKey(recordId, targetId) {
|
|
1208
|
+
const record = required(recordId, "recordId");
|
|
1209
|
+
const target = required(targetId, "targetId", 128);
|
|
1210
|
+
if (!TARGET_ID.test(target)) throw new CrmInputError("targetId must be a lowercase app slug");
|
|
1211
|
+
return `${record}:${target}`;
|
|
1212
|
+
}
|
|
1213
|
+
async function recordDeliveryAttempt(deps, opts) {
|
|
1214
|
+
const { db, now } = resolveDeps(deps);
|
|
1215
|
+
await mustGetRecord(deps, opts.recordId);
|
|
1216
|
+
const key = recordDeliveryKey(opts.recordId, opts.targetId);
|
|
1217
|
+
if (!Number.isSafeInteger(opts.payloadVersion) || opts.payloadVersion <= 0) {
|
|
1218
|
+
throw new CrmInputError("payloadVersion must be a positive integer");
|
|
1219
|
+
}
|
|
1220
|
+
const found = await db.query({ [CRM_NS.delivery]: { $: { where: { key }, limit: 1 } } });
|
|
1221
|
+
const existing = (found[CRM_NS.delivery] ?? [])[0];
|
|
1222
|
+
const at = now();
|
|
1223
|
+
const remoteRecordId = opts.remoteRecordId == null ? existing?.remoteRecordId : required(opts.remoteRecordId, "remoteRecordId");
|
|
1224
|
+
const error = opts.error == null ? void 0 : required(opts.error, "error", 1e3);
|
|
1225
|
+
const attrs = {
|
|
1226
|
+
key,
|
|
1227
|
+
recordId: opts.recordId,
|
|
1228
|
+
targetId: opts.targetId,
|
|
1229
|
+
status: opts.status,
|
|
1230
|
+
attempts: (existing?.attempts ?? 0) + 1,
|
|
1231
|
+
payloadVersion: opts.payloadVersion,
|
|
1232
|
+
lastAttemptAt: at,
|
|
1233
|
+
...remoteRecordId ? { remoteRecordId } : {},
|
|
1234
|
+
...opts.status === "delivered" ? { deliveredAt: at } : existing?.deliveredAt ? { deliveredAt: existing.deliveredAt } : {},
|
|
1235
|
+
...error ? { error } : {}
|
|
1236
|
+
};
|
|
1237
|
+
const ref = { ns: CRM_NS.delivery, attr: "key", value: key };
|
|
1238
|
+
const ops = [{ t: "update", ns: CRM_NS.delivery, id: ref, attrs }];
|
|
1239
|
+
if (opts.status === "delivered" && existing?.error) {
|
|
1240
|
+
ops.push({ t: "retract", ns: CRM_NS.delivery, id: ref, attrs: ["error"] });
|
|
1241
|
+
}
|
|
1242
|
+
const { duplicate } = await db.transact(ops, { mutationId: opts.mutationId });
|
|
1243
|
+
return {
|
|
1244
|
+
duplicate,
|
|
1245
|
+
delivery: {
|
|
1246
|
+
id: existing?.id ?? key,
|
|
1247
|
+
...attrs
|
|
1248
|
+
}
|
|
1249
|
+
};
|
|
1250
|
+
}
|
|
1251
|
+
async function listRecordDeliveries(deps, filter) {
|
|
1252
|
+
if (!filter.recordId && !filter.targetId) throw new CrmInputError("recordId or targetId is required");
|
|
1253
|
+
const { db } = resolveDeps(deps);
|
|
1254
|
+
const where = {
|
|
1255
|
+
...filter.recordId ? { recordId: required(filter.recordId, "recordId") } : {},
|
|
1256
|
+
...filter.targetId ? { targetId: required(filter.targetId, "targetId", 128) } : {}
|
|
1257
|
+
};
|
|
1258
|
+
const result = await db.query({
|
|
1259
|
+
[CRM_NS.delivery]: { $: { where, order: { lastAttemptAt: "desc" }, limit: 500 } }
|
|
1260
|
+
});
|
|
1261
|
+
return result[CRM_NS.delivery] ?? [];
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1040
1264
|
// src/activities.ts
|
|
1041
1265
|
var rowToActivity = (row) => row;
|
|
1042
1266
|
async function addActivity(deps, opts) {
|
|
@@ -1479,8 +1703,8 @@ function parseListParams(url) {
|
|
|
1479
1703
|
const type = q.get("type");
|
|
1480
1704
|
if (!type) throw new CrmInputError("type is required");
|
|
1481
1705
|
const params = { type };
|
|
1482
|
-
const
|
|
1483
|
-
if (
|
|
1706
|
+
const search2 = q.get("search");
|
|
1707
|
+
if (search2) params.search = search2;
|
|
1484
1708
|
params.stage = csv(q.get("stage"));
|
|
1485
1709
|
params.emailStatus = csv(q.get("emailStatus"));
|
|
1486
1710
|
params.billingStatus = csv(q.get("billingStatus"));
|
|
@@ -1754,12 +1978,14 @@ async function handleRecordItem(ctx, req, id) {
|
|
|
1754
1978
|
if (req.method === "GET") {
|
|
1755
1979
|
const record = await getRecord(ctx.deps, id);
|
|
1756
1980
|
if (!record) throw new CrmNotFoundError("record");
|
|
1757
|
-
const [tags, channels, links] = await Promise.all([
|
|
1981
|
+
const [tags, channels, links, origins, deliveries] = await Promise.all([
|
|
1758
1982
|
listTags(ctx.deps, id),
|
|
1759
1983
|
listChannels(ctx.deps, id),
|
|
1760
|
-
listLinks(ctx.deps, id)
|
|
1984
|
+
listLinks(ctx.deps, id),
|
|
1985
|
+
listRecordOrigins(ctx.deps, id),
|
|
1986
|
+
listRecordDeliveries(ctx.deps, { recordId: id })
|
|
1761
1987
|
]);
|
|
1762
|
-
return json({ record, tags, channels, links });
|
|
1988
|
+
return json({ record, tags, channels, links, origins, deliveries });
|
|
1763
1989
|
}
|
|
1764
1990
|
if (req.method === "PATCH") {
|
|
1765
1991
|
const body = await readJson(req);
|
|
@@ -1893,9 +2119,141 @@ async function handleUnsubscribe(deps, req, url) {
|
|
|
1893
2119
|
return oneClick ? json({ ok: true }) : unsubscribePage(token, true);
|
|
1894
2120
|
}
|
|
1895
2121
|
|
|
2122
|
+
// src/routes/discussion-references.ts
|
|
2123
|
+
var capLimit = (raw) => {
|
|
2124
|
+
const parsed = Number(raw ?? 20);
|
|
2125
|
+
return Number.isInteger(parsed) ? Math.max(1, Math.min(parsed, 20)) : 20;
|
|
2126
|
+
};
|
|
2127
|
+
var targetFromQuery = (url) => {
|
|
2128
|
+
const kind = url.searchParams.get("kind");
|
|
2129
|
+
const id = url.searchParams.get("id");
|
|
2130
|
+
if (!kind || !id) return null;
|
|
2131
|
+
const value = new URL("https://crm.invalid");
|
|
2132
|
+
value.searchParams.set("odla-ref", `${kind}/${id}`);
|
|
2133
|
+
return parseCrmDiscussionReference(value);
|
|
2134
|
+
};
|
|
2135
|
+
var recordItem = (ctx, req, record) => {
|
|
2136
|
+
const typeLabel = ctx.deps.crm.type(record.type).label;
|
|
2137
|
+
const status = record.archived ? "archived" : record.stage ?? "record";
|
|
2138
|
+
const target = {
|
|
2139
|
+
kind: "crm:record",
|
|
2140
|
+
type: record.type,
|
|
2141
|
+
recordId: record.id
|
|
2142
|
+
};
|
|
2143
|
+
return {
|
|
2144
|
+
kind: target.kind,
|
|
2145
|
+
id: crmDiscussionReferenceId(target),
|
|
2146
|
+
label: record.name,
|
|
2147
|
+
hint: `${typeLabel} \xB7 ${status}`,
|
|
2148
|
+
summary: `${typeLabel} record for ${record.name}`,
|
|
2149
|
+
status,
|
|
2150
|
+
destination: `CRM \xB7 ${typeLabel} profile`,
|
|
2151
|
+
href: crmDiscussionReferenceHref(
|
|
2152
|
+
new URL(req.url),
|
|
2153
|
+
target,
|
|
2154
|
+
ctx.opts.discussionBasePath
|
|
2155
|
+
)
|
|
2156
|
+
};
|
|
2157
|
+
};
|
|
2158
|
+
var activityItem = (ctx, req, record, activity) => {
|
|
2159
|
+
const target = {
|
|
2160
|
+
kind: "crm:activity",
|
|
2161
|
+
type: record.type,
|
|
2162
|
+
recordId: record.id,
|
|
2163
|
+
activityId: activity.id
|
|
2164
|
+
};
|
|
2165
|
+
return {
|
|
2166
|
+
kind: target.kind,
|
|
2167
|
+
id: crmDiscussionReferenceId(target),
|
|
2168
|
+
label: activity.body,
|
|
2169
|
+
hint: `${record.name} \xB7 ${activity.kind}${activity.status ? ` \xB7 ${activity.status}` : ""}`,
|
|
2170
|
+
summary: `${activity.kind} on ${record.name}: ${activity.body}`,
|
|
2171
|
+
status: activity.status ?? activity.kind,
|
|
2172
|
+
destination: `CRM \xB7 ${record.name} notes`,
|
|
2173
|
+
href: crmDiscussionReferenceHref(
|
|
2174
|
+
new URL(req.url),
|
|
2175
|
+
target,
|
|
2176
|
+
ctx.opts.discussionBasePath
|
|
2177
|
+
)
|
|
2178
|
+
};
|
|
2179
|
+
};
|
|
2180
|
+
async function exact(ctx, req, target) {
|
|
2181
|
+
const record = await getRecord(ctx.deps, target.recordId);
|
|
2182
|
+
if (!record || record.type !== target.type) return [];
|
|
2183
|
+
if (target.kind === "crm:record") return [recordItem(ctx, req, record)];
|
|
2184
|
+
const result = await ctx.deps.db.query({
|
|
2185
|
+
[CRM_NS.activity]: {
|
|
2186
|
+
$: { where: { id: target.activityId }, limit: 1 }
|
|
2187
|
+
}
|
|
2188
|
+
});
|
|
2189
|
+
const activity = (result[CRM_NS.activity] ?? [])[0];
|
|
2190
|
+
return activity && activity.recordId === record.id ? [activityItem(ctx, req, record, activity)] : [];
|
|
2191
|
+
}
|
|
2192
|
+
var matches = (item, needle) => !needle || `${item.label}
|
|
2193
|
+
${item.hint}
|
|
2194
|
+
${item.id}`.toLowerCase().includes(needle);
|
|
2195
|
+
async function searchableActivities(ctx, req, needle) {
|
|
2196
|
+
const result = await ctx.deps.db.query({
|
|
2197
|
+
[CRM_NS.activity]: {
|
|
2198
|
+
$: { order: { createdAt: "desc" }, limit: 100 }
|
|
2199
|
+
}
|
|
2200
|
+
});
|
|
2201
|
+
const rows = result[CRM_NS.activity] ?? [];
|
|
2202
|
+
const matching = rows.filter(
|
|
2203
|
+
(activity) => !needle || activity.body.toLowerCase().includes(needle)
|
|
2204
|
+
);
|
|
2205
|
+
const records = await Promise.all(
|
|
2206
|
+
[...new Set(matching.map((activity) => activity.recordId))].map((id) => getRecord(ctx.deps, id))
|
|
2207
|
+
);
|
|
2208
|
+
const byId = new Map(
|
|
2209
|
+
records.filter((record) => record !== null).map((record) => [record.id, record])
|
|
2210
|
+
);
|
|
2211
|
+
return matching.flatMap((activity) => {
|
|
2212
|
+
const record = byId.get(activity.recordId);
|
|
2213
|
+
return record ? [activityItem(ctx, req, record, activity)] : [];
|
|
2214
|
+
});
|
|
2215
|
+
}
|
|
2216
|
+
async function search(ctx, req, url) {
|
|
2217
|
+
const limit = capLimit(url.searchParams.get("limit"));
|
|
2218
|
+
const needle = (url.searchParams.get("q") ?? "").trim().toLowerCase().slice(0, 120);
|
|
2219
|
+
const recordPages = await Promise.all(
|
|
2220
|
+
Object.keys(ctx.deps.crm.config.types).map(
|
|
2221
|
+
(type) => listRecords(ctx.deps, {
|
|
2222
|
+
type,
|
|
2223
|
+
...needle ? { search: needle } : {},
|
|
2224
|
+
limit,
|
|
2225
|
+
offset: 0
|
|
2226
|
+
})
|
|
2227
|
+
)
|
|
2228
|
+
);
|
|
2229
|
+
const records = recordPages.flatMap(
|
|
2230
|
+
(page) => page.records.map((record) => recordItem(ctx, req, record))
|
|
2231
|
+
);
|
|
2232
|
+
const activities = await searchableActivities(ctx, req, needle);
|
|
2233
|
+
return [...records, ...activities].filter((item) => matches(item, needle)).slice(0, limit);
|
|
2234
|
+
}
|
|
2235
|
+
async function handleCrmDiscussionReferences(ctx, req, url) {
|
|
2236
|
+
if (req.method !== "GET") return json({ error: "method not allowed" }, 405);
|
|
2237
|
+
if (ctx.actor.discussionReferenceScope !== "all") {
|
|
2238
|
+
return json({ error: "forbidden" }, 403);
|
|
2239
|
+
}
|
|
2240
|
+
const kind = url.searchParams.get("kind");
|
|
2241
|
+
const id = url.searchParams.get("id");
|
|
2242
|
+
if (kind && !id || !kind && id) {
|
|
2243
|
+
return json({ error: "kind and id must be paired" }, 400);
|
|
2244
|
+
}
|
|
2245
|
+
const target = targetFromQuery(url);
|
|
2246
|
+
if ((kind || id) && !target) return json({ items: [] });
|
|
2247
|
+
return json({
|
|
2248
|
+
items: target ? await exact(ctx, req, target) : await search(ctx, req, url)
|
|
2249
|
+
});
|
|
2250
|
+
}
|
|
2251
|
+
|
|
1896
2252
|
// src/routes/index.ts
|
|
1897
2253
|
async function route(ctx, req, url, seg) {
|
|
1898
2254
|
const [head, id, action, ...rest] = seg;
|
|
2255
|
+
if (head === "discussion-references" && id === void 0)
|
|
2256
|
+
return handleCrmDiscussionReferences(ctx, req, url);
|
|
1899
2257
|
if (head === "records") {
|
|
1900
2258
|
if (id === void 0) return handleRecordsRoot(ctx, req, url);
|
|
1901
2259
|
if (action === void 0) return handleRecordItem(ctx, req, id);
|
|
@@ -1926,7 +2284,8 @@ function createCrmRoutes(options) {
|
|
|
1926
2284
|
...options,
|
|
1927
2285
|
basePath,
|
|
1928
2286
|
envName: options.envName ?? "development",
|
|
1929
|
-
baseUrl: options.baseUrl ?? ""
|
|
2287
|
+
baseUrl: options.baseUrl ?? "",
|
|
2288
|
+
discussionBasePath: options.discussionBasePath ?? "/admin/"
|
|
1930
2289
|
};
|
|
1931
2290
|
const deps = {
|
|
1932
2291
|
crm: options.crm,
|
|
@@ -1942,9 +2301,14 @@ function createCrmRoutes(options) {
|
|
|
1942
2301
|
try {
|
|
1943
2302
|
if (segments[0] === "unsubscribe" && segments.length === 1)
|
|
1944
2303
|
return await handleUnsubscribe(deps, req, url);
|
|
1945
|
-
const
|
|
2304
|
+
const discussionReferences = segments[0] === "discussion-references";
|
|
2305
|
+
const actor = discussionReferences && options.authorizeDiscussionReferences ? await options.authorizeDiscussionReferences(req) : await options.authorize(req);
|
|
1946
2306
|
if (!actor) return json({ error: "unauthorized" }, 401);
|
|
1947
|
-
const ctx = {
|
|
2307
|
+
const ctx = {
|
|
2308
|
+
deps,
|
|
2309
|
+
opts,
|
|
2310
|
+
actor: discussionReferences && !options.authorizeDiscussionReferences ? { ...actor, discussionReferenceScope: "all" } : actor
|
|
2311
|
+
};
|
|
1948
2312
|
return await route(ctx, req, url, segments) ?? json({ error: "not found" }, 404);
|
|
1949
2313
|
} catch (error) {
|
|
1950
2314
|
return errorResponse(error);
|
|
@@ -2163,6 +2527,7 @@ export {
|
|
|
2163
2527
|
ACTIVITY_KINDS,
|
|
2164
2528
|
CONFIG_KEY,
|
|
2165
2529
|
CONSENT_STATES,
|
|
2530
|
+
CRM_DISCUSSION_REFERENCE_KINDS,
|
|
2166
2531
|
CRM_NS,
|
|
2167
2532
|
CRM_RULES,
|
|
2168
2533
|
CRM_SCHEMA,
|
|
@@ -2186,9 +2551,12 @@ export {
|
|
|
2186
2551
|
canTransition,
|
|
2187
2552
|
channelKey,
|
|
2188
2553
|
compileList,
|
|
2554
|
+
composeCrmConfigs,
|
|
2189
2555
|
createCrmIntegration,
|
|
2190
2556
|
createCrmRoutes,
|
|
2191
2557
|
createRecord,
|
|
2558
|
+
crmDiscussionReferenceHref,
|
|
2559
|
+
crmDiscussionReferenceId,
|
|
2192
2560
|
crmIntegration,
|
|
2193
2561
|
crmRules,
|
|
2194
2562
|
defaultTemplates,
|
|
@@ -2196,8 +2564,11 @@ export {
|
|
|
2196
2564
|
deleteView,
|
|
2197
2565
|
emailChannelOps,
|
|
2198
2566
|
foldEmailStatus,
|
|
2567
|
+
formatCrmDiscussionReference,
|
|
2199
2568
|
getConfig,
|
|
2200
2569
|
getRecord,
|
|
2570
|
+
getRecordByOrigin,
|
|
2571
|
+
getRecordOrigin,
|
|
2201
2572
|
initialStage,
|
|
2202
2573
|
linkIdentity,
|
|
2203
2574
|
lintTemplate,
|
|
@@ -2205,6 +2576,8 @@ export {
|
|
|
2205
2576
|
listChannels,
|
|
2206
2577
|
listEmailLog,
|
|
2207
2578
|
listLinks,
|
|
2579
|
+
listRecordDeliveries,
|
|
2580
|
+
listRecordOrigins,
|
|
2208
2581
|
listRecords,
|
|
2209
2582
|
listTags,
|
|
2210
2583
|
listTasks,
|
|
@@ -2212,11 +2585,15 @@ export {
|
|
|
2212
2585
|
mustGetRecord,
|
|
2213
2586
|
needsRebalance,
|
|
2214
2587
|
normalizeTag,
|
|
2588
|
+
parseCrmDiscussionReference,
|
|
2215
2589
|
prepare,
|
|
2216
2590
|
rankBetween,
|
|
2217
2591
|
rebalancePlan,
|
|
2218
2592
|
rebalanceRanks,
|
|
2219
2593
|
recordCanEmail,
|
|
2594
|
+
recordDeliveryAttempt,
|
|
2595
|
+
recordDeliveryKey,
|
|
2596
|
+
recordOriginKey,
|
|
2220
2597
|
removeLink,
|
|
2221
2598
|
removeTag,
|
|
2222
2599
|
renderTemplate,
|
|
@@ -2235,6 +2612,7 @@ export {
|
|
|
2235
2612
|
typeSummary,
|
|
2236
2613
|
updateRecord,
|
|
2237
2614
|
updateTask,
|
|
2238
|
-
updateView
|
|
2615
|
+
updateView,
|
|
2616
|
+
upsertRecordOrigin
|
|
2239
2617
|
};
|
|
2240
2618
|
//# sourceMappingURL=index.js.map
|