@cherwell/emdash-crm 0.0.0-stage → 0.1.1
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/CHANGELOG.md +25 -0
- package/LICENSE +21 -0
- package/README.md +205 -2
- package/assets/banner.png +0 -0
- package/assets/icon.png +0 -0
- package/assets/icon.svg +6 -0
- package/dist/helpers/testing.d.ts +95 -0
- package/dist/helpers/testing.js +365 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +924 -0
- package/dist/manifest.json +921 -0
- package/dist/plugin.d.mts +6 -0
- package/dist/plugin.mjs +58 -0
- package/docs/faq.md +23 -0
- package/docs/install.md +38 -0
- package/docs/overview.md +10 -0
- package/docs/reference.md +332 -0
- package/docs/security.md +11 -0
- package/emdash-plugin.jsonc +74 -0
- package/llms.txt +20 -0
- package/package.json +78 -3
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
//#region src/testing.ts
|
|
2
|
+
/**
|
|
3
|
+
* A fake CRM service API (emdash-plugins/docs/crm-service.md), answering
|
|
4
|
+
* `POST {site}/_emdash/api/plugins/crm/service/<route>` from memory. Give
|
|
5
|
+
* `crm.fetch` to `createTestContext({ fetch })` and a plugin built on the
|
|
6
|
+
* CRM (claims, members) talks to it as it would to the real one: the same
|
|
7
|
+
* key header, the same `{ data: { ok, ... } }` answers and the same refusals
|
|
8
|
+
* (SERVICE_KEY, NOT_FOUND, ENTRY_TAKEN, LAST_OWNER, ROLE_UNKNOWN, ALREADY_MEMBER).
|
|
9
|
+
*
|
|
10
|
+
* import { fakeCrm } from "@cherwell/emdash-crm/testing";
|
|
11
|
+
*/
|
|
12
|
+
const CRM_KEY = "crm-service-key-0123456789";
|
|
13
|
+
const HEADER = "x-crm-service-key";
|
|
14
|
+
const ENTRY = /^[a-z][a-z0-9_]*:[A-Za-z0-9_-]{1,64}$/;
|
|
15
|
+
const DEFAULT_ROLES = {
|
|
16
|
+
owner: ["*"],
|
|
17
|
+
manager: [
|
|
18
|
+
"profile.update",
|
|
19
|
+
"organisation.update",
|
|
20
|
+
"entries.update",
|
|
21
|
+
"people.invite",
|
|
22
|
+
"membership.view",
|
|
23
|
+
"report.view",
|
|
24
|
+
"notify"
|
|
25
|
+
],
|
|
26
|
+
billing: [
|
|
27
|
+
"profile.update",
|
|
28
|
+
"membership.view",
|
|
29
|
+
"billing.view"
|
|
30
|
+
],
|
|
31
|
+
contact: ["profile.update", "report.view"]
|
|
32
|
+
};
|
|
33
|
+
const BASE_PERMISSIONS = [
|
|
34
|
+
"profile.update",
|
|
35
|
+
"organisation.update",
|
|
36
|
+
"entries.update",
|
|
37
|
+
"people.invite",
|
|
38
|
+
"people.manage",
|
|
39
|
+
"membership.view",
|
|
40
|
+
"billing.view",
|
|
41
|
+
"report.view",
|
|
42
|
+
"notify"
|
|
43
|
+
];
|
|
44
|
+
var Refused = class extends Error {
|
|
45
|
+
constructor(code) {
|
|
46
|
+
super(code);
|
|
47
|
+
this.code = code;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
function fakeCrm(opts = {}) {
|
|
51
|
+
const key = opts.key ?? CRM_KEY;
|
|
52
|
+
const prefix = `${(opts.site ?? "https://example.com").replace(/\/$/, "")}/_emdash/api/plugins/crm/service/`;
|
|
53
|
+
const roles = opts.roles ?? DEFAULT_ROLES;
|
|
54
|
+
const ownerRole = opts.ownerRole ?? "owner";
|
|
55
|
+
const notifyPermission = opts.notifyPermission ?? "notify";
|
|
56
|
+
const orgs = /* @__PURE__ */ new Map();
|
|
57
|
+
const people = /* @__PURE__ */ new Map();
|
|
58
|
+
const links = [];
|
|
59
|
+
const activities = [];
|
|
60
|
+
const records = [];
|
|
61
|
+
const calls = [];
|
|
62
|
+
let seq = 0;
|
|
63
|
+
let down = false;
|
|
64
|
+
/** One-shot refusals by route, for testing how a caller passes them on. */
|
|
65
|
+
const refusals = /* @__PURE__ */ new Map();
|
|
66
|
+
const permsFor = (role) => {
|
|
67
|
+
const p = roles[role] ?? [];
|
|
68
|
+
return p.includes("*") ? [...new Set([...BASE_PERMISSIONS, ...Object.values(roles).flat().filter((x) => x !== "*")])] : p;
|
|
69
|
+
};
|
|
70
|
+
const current = (where) => links.filter((l) => !l.ended && (!where.orgId || l.orgId === where.orgId) && (!where.personId || l.personId === where.personId));
|
|
71
|
+
const ownerOf = (entry) => [...orgs.values()].find((o) => o.entries.includes(entry))?.id ?? null;
|
|
72
|
+
const byEmail = (email) => [...people.values()].find((p) => p.email === email.trim().toLowerCase()) ?? null;
|
|
73
|
+
const actor = (b) => {
|
|
74
|
+
if (typeof b.actor !== "string" || !b.actor || b.actor.length > 80) throw new Refused("INVALID_INPUT");
|
|
75
|
+
};
|
|
76
|
+
const orgOut = (o) => structuredClone(o);
|
|
77
|
+
const api = {
|
|
78
|
+
orgs,
|
|
79
|
+
people,
|
|
80
|
+
links,
|
|
81
|
+
activities,
|
|
82
|
+
records,
|
|
83
|
+
calls,
|
|
84
|
+
key,
|
|
85
|
+
set down(v) {
|
|
86
|
+
down = v;
|
|
87
|
+
},
|
|
88
|
+
addOrg(o) {
|
|
89
|
+
const id = o.id ?? `org_${++seq}`;
|
|
90
|
+
orgs.set(id, {
|
|
91
|
+
id,
|
|
92
|
+
status: "active",
|
|
93
|
+
email: null,
|
|
94
|
+
phone: null,
|
|
95
|
+
website: null,
|
|
96
|
+
address: null,
|
|
97
|
+
entries: [],
|
|
98
|
+
notifyEmails: [],
|
|
99
|
+
plans: {},
|
|
100
|
+
renewals: {},
|
|
101
|
+
...o
|
|
102
|
+
});
|
|
103
|
+
return id;
|
|
104
|
+
},
|
|
105
|
+
addPerson(email, name = email.split("@")[0]) {
|
|
106
|
+
const found = byEmail(email);
|
|
107
|
+
if (found) return found.id;
|
|
108
|
+
const id = `per_${++seq}`;
|
|
109
|
+
people.set(id, {
|
|
110
|
+
id,
|
|
111
|
+
name,
|
|
112
|
+
email: email.toLowerCase()
|
|
113
|
+
});
|
|
114
|
+
return id;
|
|
115
|
+
},
|
|
116
|
+
link(orgId, personId, role, extra = {}) {
|
|
117
|
+
links.push({
|
|
118
|
+
orgId,
|
|
119
|
+
personId,
|
|
120
|
+
role,
|
|
121
|
+
jobTitle: null,
|
|
122
|
+
primary: false,
|
|
123
|
+
ended: false,
|
|
124
|
+
...extra
|
|
125
|
+
});
|
|
126
|
+
},
|
|
127
|
+
orgWithOwner(name, ownerEmail, extra = {}, ownerName = "Sam") {
|
|
128
|
+
const id = api.addOrg({
|
|
129
|
+
name,
|
|
130
|
+
...extra
|
|
131
|
+
});
|
|
132
|
+
api.link(id, api.addPerson(ownerEmail, ownerName), ownerRole, { primary: true });
|
|
133
|
+
return id;
|
|
134
|
+
},
|
|
135
|
+
roleOf(orgId, email) {
|
|
136
|
+
const p = byEmail(email);
|
|
137
|
+
return p ? current({
|
|
138
|
+
orgId,
|
|
139
|
+
personId: p.id
|
|
140
|
+
})[0]?.role ?? null : null;
|
|
141
|
+
},
|
|
142
|
+
routes: () => calls.map((c) => c.route),
|
|
143
|
+
refuseNext(route, code) {
|
|
144
|
+
refusals.set(route, code);
|
|
145
|
+
},
|
|
146
|
+
answer(route, b) {
|
|
147
|
+
switch (route) {
|
|
148
|
+
case "config": return {
|
|
149
|
+
roles: Object.fromEntries(Object.keys(roles).map((r) => [r, permsFor(r)])),
|
|
150
|
+
order: Object.keys(roles),
|
|
151
|
+
ownerRole,
|
|
152
|
+
billingRole: opts.billingRole ?? "billing",
|
|
153
|
+
notifyPermission,
|
|
154
|
+
contactRole: opts.contactRole ?? "contact",
|
|
155
|
+
timeZone: "UTC"
|
|
156
|
+
};
|
|
157
|
+
case "person": {
|
|
158
|
+
const p = typeof b.email === "string" ? byEmail(b.email) : typeof b.id === "string" ? people.get(b.id) ?? null : null;
|
|
159
|
+
if (!p) return {
|
|
160
|
+
person: null,
|
|
161
|
+
links: []
|
|
162
|
+
};
|
|
163
|
+
return {
|
|
164
|
+
person: { ...p },
|
|
165
|
+
links: current({ personId: p.id }).filter((l) => orgs.has(l.orgId) && orgs.get(l.orgId).status !== "archived").map((l) => {
|
|
166
|
+
const o = orgs.get(l.orgId);
|
|
167
|
+
const link = {
|
|
168
|
+
orgId: l.orgId,
|
|
169
|
+
orgName: o.name,
|
|
170
|
+
orgStatus: o.status,
|
|
171
|
+
role: l.role,
|
|
172
|
+
perms: permsFor(l.role),
|
|
173
|
+
jobTitle: l.jobTitle,
|
|
174
|
+
primary: l.primary
|
|
175
|
+
};
|
|
176
|
+
return b.withOrgs ? {
|
|
177
|
+
...link,
|
|
178
|
+
entries: [...o.entries],
|
|
179
|
+
plans: { ...o.plans },
|
|
180
|
+
renewals: { ...o.renewals },
|
|
181
|
+
notifyEmails: [...o.notifyEmails]
|
|
182
|
+
} : link;
|
|
183
|
+
})
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
case "organisation": {
|
|
187
|
+
const o = orgs.get(b.id);
|
|
188
|
+
if (!o) return { organisation: null };
|
|
189
|
+
if (!b.people) return { organisation: orgOut(o) };
|
|
190
|
+
return {
|
|
191
|
+
organisation: orgOut(o),
|
|
192
|
+
people: current({ orgId: o.id }).map((l) => ({
|
|
193
|
+
personId: l.personId,
|
|
194
|
+
name: people.get(l.personId)?.name ?? "",
|
|
195
|
+
email: people.get(l.personId)?.email ?? "",
|
|
196
|
+
role: l.role,
|
|
197
|
+
perms: permsFor(l.role),
|
|
198
|
+
jobTitle: l.jobTitle,
|
|
199
|
+
primary: l.primary
|
|
200
|
+
}))
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
case "entry-owner": {
|
|
204
|
+
if (!ENTRY.test(String(b.entry))) throw new Refused("INVALID_INPUT");
|
|
205
|
+
const orgId = ownerOf(b.entry);
|
|
206
|
+
return {
|
|
207
|
+
orgId,
|
|
208
|
+
status: orgId ? orgs.get(orgId).status : null
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
case "timeline": {
|
|
212
|
+
const limit = b.limit ?? 30;
|
|
213
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 50) throw new Refused("INVALID_INPUT");
|
|
214
|
+
const memberOnly = b.memberOnly ?? true;
|
|
215
|
+
return { items: activities.filter((a) => a.orgId === b.orgId && (!memberOnly || a.visibleToMember)).reverse().slice(0, limit).map((a) => ({
|
|
216
|
+
kind: a.kind,
|
|
217
|
+
text: a.text,
|
|
218
|
+
byName: a.byName,
|
|
219
|
+
at: a.at
|
|
220
|
+
})) };
|
|
221
|
+
}
|
|
222
|
+
case "organisations": return {
|
|
223
|
+
organisations: [...orgs.values()].filter((o) => !b.q || o.name.toLowerCase().startsWith(String(b.q).toLowerCase())).map((o) => ({
|
|
224
|
+
id: o.id,
|
|
225
|
+
name: o.name,
|
|
226
|
+
status: o.status
|
|
227
|
+
})),
|
|
228
|
+
cursor: null
|
|
229
|
+
};
|
|
230
|
+
case "organisation/create": {
|
|
231
|
+
actor(b);
|
|
232
|
+
const entries = b.entries ?? [];
|
|
233
|
+
if (entries.some((e) => ownerOf(e))) throw new Refused("ENTRY_TAKEN");
|
|
234
|
+
return { id: api.addOrg({
|
|
235
|
+
name: b.name,
|
|
236
|
+
status: "prospect",
|
|
237
|
+
entries
|
|
238
|
+
}) };
|
|
239
|
+
}
|
|
240
|
+
case "organisation/update": {
|
|
241
|
+
actor(b);
|
|
242
|
+
const o = orgs.get(b.id);
|
|
243
|
+
if (!o) throw new Refused("NOT_FOUND");
|
|
244
|
+
const added = (b.addEntries ?? []).filter((e) => !o.entries.includes(e));
|
|
245
|
+
if (added.some((e) => ownerOf(e))) throw new Refused("ENTRY_TAKEN");
|
|
246
|
+
if (b.website !== void 0 && b.website !== "" && !/^https?:\/\/[^\s]+$/i.test(b.website)) throw new Refused("INVALID_INPUT");
|
|
247
|
+
for (const k of [
|
|
248
|
+
"email",
|
|
249
|
+
"phone",
|
|
250
|
+
"website"
|
|
251
|
+
]) if (b[k] !== void 0) o[k] = b[k] || null;
|
|
252
|
+
if (b.notifyEmails) o.notifyEmails = b.notifyEmails.map((x) => x.toLowerCase());
|
|
253
|
+
if (b.plans) o.plans = b.plans;
|
|
254
|
+
if (b.renewals) o.renewals = b.renewals;
|
|
255
|
+
o.entries.push(...added);
|
|
256
|
+
if (b.status && (!b.statusFrom || b.statusFrom.includes(o.status))) o.status = b.status;
|
|
257
|
+
return { status: o.status };
|
|
258
|
+
}
|
|
259
|
+
case "person/ensure": {
|
|
260
|
+
actor(b);
|
|
261
|
+
const found = byEmail(b.email);
|
|
262
|
+
if (found) return {
|
|
263
|
+
id: found.id,
|
|
264
|
+
created: false
|
|
265
|
+
};
|
|
266
|
+
return {
|
|
267
|
+
id: api.addPerson(b.email, b.name || String(b.email).split("@")[0]),
|
|
268
|
+
created: true
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
case "link": {
|
|
272
|
+
actor(b);
|
|
273
|
+
if (b.role !== null && !(b.role in roles)) throw new Refused("ROLE_UNKNOWN");
|
|
274
|
+
if (!orgs.has(b.orgId)) throw new Refused("NOT_FOUND");
|
|
275
|
+
const l = current({
|
|
276
|
+
orgId: b.orgId,
|
|
277
|
+
personId: b.personId
|
|
278
|
+
})[0];
|
|
279
|
+
if (!l) {
|
|
280
|
+
if (b.role === null || !people.has(b.personId)) throw new Refused("NOT_FOUND");
|
|
281
|
+
const primary = b.primary === "ifNone" ? !current({ orgId: b.orgId }).some((x) => x.primary) : b.primary ?? false;
|
|
282
|
+
api.link(b.orgId, b.personId, b.role, {
|
|
283
|
+
jobTitle: b.jobTitle ?? null,
|
|
284
|
+
primary
|
|
285
|
+
});
|
|
286
|
+
return {};
|
|
287
|
+
}
|
|
288
|
+
if (b.mode === "add") throw new Refused("ALREADY_MEMBER");
|
|
289
|
+
if (b.role === l.role) return {};
|
|
290
|
+
if (l.role === ownerRole && current({ orgId: b.orgId }).filter((x) => x.role === ownerRole).length <= 1) throw new Refused("LAST_OWNER");
|
|
291
|
+
if (b.role === null) {
|
|
292
|
+
l.ended = true;
|
|
293
|
+
l.primary = false;
|
|
294
|
+
} else l.role = b.role;
|
|
295
|
+
return {};
|
|
296
|
+
}
|
|
297
|
+
case "activity":
|
|
298
|
+
if (![
|
|
299
|
+
"note",
|
|
300
|
+
"call",
|
|
301
|
+
"email",
|
|
302
|
+
"meeting",
|
|
303
|
+
"system",
|
|
304
|
+
"member"
|
|
305
|
+
].includes(b.kind) || typeof b.text !== "string" || !b.text.trim() || b.text.length > 4e3 || b.by !== null && String(b.by).length > 80) throw new Refused("INVALID_INPUT");
|
|
306
|
+
if (!orgs.has(b.orgId)) throw new Refused("NOT_FOUND");
|
|
307
|
+
activities.push({
|
|
308
|
+
orgId: b.orgId,
|
|
309
|
+
kind: b.kind,
|
|
310
|
+
text: b.text,
|
|
311
|
+
by: b.by,
|
|
312
|
+
byName: b.byName ?? null,
|
|
313
|
+
visibleToMember: b.visibleToMember ?? false,
|
|
314
|
+
at: new Date(Date.UTC(2026, 0, 1, 0, 0, activities.length)).toISOString()
|
|
315
|
+
});
|
|
316
|
+
return {};
|
|
317
|
+
case "record":
|
|
318
|
+
actor(b);
|
|
319
|
+
if (!/^[a-z][a-z0-9_.]{0,79}$/.test(b.action) || b.event && (String(b.event).startsWith("crm.") || !/^[a-z][a-z0-9_]*(\.[a-z0-9_]+)+$/.test(b.event))) throw new Refused("INVALID_INPUT");
|
|
320
|
+
records.push(structuredClone(b));
|
|
321
|
+
return {};
|
|
322
|
+
}
|
|
323
|
+
throw new Refused("NOT_FOUND");
|
|
324
|
+
},
|
|
325
|
+
async fetch(url, init) {
|
|
326
|
+
if (down) throw new Error("CRM down");
|
|
327
|
+
if (!url.startsWith(prefix)) return new Response("Not found", { status: 404 });
|
|
328
|
+
const route = url.slice(prefix.length);
|
|
329
|
+
const body = JSON.parse(String(init?.body ?? "{}"));
|
|
330
|
+
calls.push({
|
|
331
|
+
route,
|
|
332
|
+
body
|
|
333
|
+
});
|
|
334
|
+
const given = (init?.headers)?.[HEADER];
|
|
335
|
+
let data;
|
|
336
|
+
if (!key || given !== key) data = {
|
|
337
|
+
ok: false,
|
|
338
|
+
error: "SERVICE_KEY"
|
|
339
|
+
};
|
|
340
|
+
else if (refusals.has(route)) {
|
|
341
|
+
data = {
|
|
342
|
+
ok: false,
|
|
343
|
+
error: refusals.get(route)
|
|
344
|
+
};
|
|
345
|
+
refusals.delete(route);
|
|
346
|
+
} else try {
|
|
347
|
+
data = {
|
|
348
|
+
ok: true,
|
|
349
|
+
...api.answer(route, body)
|
|
350
|
+
};
|
|
351
|
+
} catch (e) {
|
|
352
|
+
if (!(e instanceof Refused)) throw e;
|
|
353
|
+
data = {
|
|
354
|
+
ok: false,
|
|
355
|
+
error: e.code
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
return Response.json({ data });
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
return api;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
//#endregion
|
|
365
|
+
export { CRM_KEY, DEFAULT_ROLES, fakeCrm };
|