@wirecat/cli-core 0.722.0 → 0.724.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/CHANGELOG.md +54 -0
- package/dist/cli/documents/history.js +2 -2
- package/dist/cli/documents/words.d.ts +1 -1
- package/dist/cli/service-api-call.d.ts +6 -0
- package/dist/cli/service-api-call.js +21 -9
- package/dist/cli/service-api.d.ts +5 -0
- package/dist/cli/service-api.js +11 -0
- package/dist/content/writes.d.ts +3 -9
- package/dist/content/writes.js +3 -13
- package/dist/crm/index.d.ts +3 -2
- package/dist/crm/index.js +1 -0
- package/dist/crm/manifest/manifest.d.ts +60 -1
- package/dist/crm/manifest/manifest.js +88 -0
- package/dist/crm/manifest/source.d.ts +22 -3
- package/dist/crm/manifest/source.js +10 -3
- package/dist/crm/manifest/writer.d.ts +39 -0
- package/dist/crm/manifest/writer.js +298 -0
- package/dist/crm/testing/index.d.ts +1 -1
- package/dist/crm/testing/index.js +1 -1
- package/dist/crm/testing/manifest-fake.d.ts +14 -0
- package/dist/crm/testing/manifest-fake.js +252 -27
- package/dist/crm/writes.d.ts +4 -13
- package/dist/crm/writes.js +2 -9
- package/dist/errors/reasons.js +5 -0
- package/dist/ports/wirecat-tools.js +7 -0
- package/dist/sends/profile-writes.d.ts +22 -0
- package/dist/sends/profile-writes.js +20 -0
- package/dist/tracker/cli/index.d.ts +10 -1
- package/dist/tracker/cli/index.js +20 -1
- package/dist/tracker/cli/writes.d.ts +27 -0
- package/dist/tracker/cli/writes.js +74 -0
- package/dist/tracker/index.d.ts +5 -2
- package/dist/tracker/index.js +5 -2
- package/dist/tracker/manifest/manifest.d.ts +25 -0
- package/dist/tracker/manifest/manifest.js +59 -8
- package/dist/tracker/manifest/source.d.ts +10 -1
- package/dist/tracker/manifest/source.js +12 -9
- package/dist/tracker/manifest/writer.d.ts +20 -0
- package/dist/tracker/manifest/writer.js +72 -0
- package/dist/tracker/sync.d.ts +5 -0
- package/dist/tracker/sync.js +12 -0
- package/dist/tracker/testing/index.d.ts +3 -1
- package/dist/tracker/testing/index.js +3 -1
- package/dist/tracker/testing/manifest-fake.d.ts +2 -0
- package/dist/tracker/testing/manifest-fake.js +123 -41
- package/dist/tracker/testing/manifest-writes.d.ts +16 -0
- package/dist/tracker/testing/manifest-writes.js +70 -0
- package/dist/tracker/testing/source.d.ts +9 -1
- package/dist/tracker/testing/source.js +63 -0
- package/dist/tracker/testing/write-contract.d.ts +20 -0
- package/dist/tracker/testing/write-contract.js +94 -0
- package/dist/tracker/writer.d.ts +57 -0
- package/dist/tracker/writer.js +25 -0
- package/dist/tracker/writes.d.ts +53 -0
- package/dist/tracker/writes.js +226 -0
- package/package.json +1 -1
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
import { fakeApi, sampleOf, } from "@wirecat/cli-base/codegen";
|
|
2
|
-
import { serviceApiCall } from "../../cli/service-api-call.js";
|
|
2
|
+
import { serviceApiCall, serviceApiSend } from "../../cli/service-api-call.js";
|
|
3
3
|
import { manifestBaseUrl } from "../../cli/service-api-manifest.js";
|
|
4
4
|
import { DEAL_KINDS } from "../../domain/deals.js";
|
|
5
|
-
import { parseCrmManifest, } from "../manifest/manifest.js";
|
|
6
|
-
import { at, convert, evaluate, FIELDS, MISSING } from "../manifest/map.js";
|
|
5
|
+
import { CRM_LIST_FIELDS, parseCrmManifest, } from "../manifest/manifest.js";
|
|
6
|
+
import { at, convert, evaluate, FIELDS, MISSING, mapRecord } from "../manifest/map.js";
|
|
7
7
|
import { itemOf, resolve, step, walk } from "../manifest/paths.js";
|
|
8
8
|
import { crmSourceFromManifest } from "../manifest/source.js";
|
|
9
|
+
import { crmWriterFromManifest } from "../manifest/writer.js";
|
|
9
10
|
import { id, isRaw } from "../values.js";
|
|
10
11
|
import { crmSourceContract } from "./source-contract.js";
|
|
12
|
+
import { crmWriteContract } from "./write-contract.js";
|
|
11
13
|
export const FAKE_MANIFEST_TOKEN = "fake-manifest-token";
|
|
12
14
|
/** Three rows in pages of two: every kind pages at least once, whatever limit the manifest asks for. */
|
|
13
15
|
const ROWS = 3;
|
|
14
16
|
const PAGE = 2;
|
|
15
17
|
const SEEDED = Date.parse("2026-11-01T10:00:00Z");
|
|
18
|
+
/** The currency of every amount, and what a fixed currency reads. */
|
|
19
|
+
const CURRENCY = "EUR";
|
|
16
20
|
/** Where a rule can be written to: the first path it reads, or none for a function or a list it walks. */
|
|
17
21
|
const pathOf = (rule) => {
|
|
18
22
|
if (rule === undefined || typeof rule === "function")
|
|
@@ -29,26 +33,58 @@ const pathOf = (rule) => {
|
|
|
29
33
|
return rule.all[0];
|
|
30
34
|
return rule.path;
|
|
31
35
|
};
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
if (!Array.isArray(value) && !isRaw(value))
|
|
37
|
-
return undefined;
|
|
38
|
-
for (const [key, inner] of Object.entries(value)) {
|
|
39
|
-
const found = find(inner, [...trail, key]);
|
|
40
|
-
if (found)
|
|
41
|
-
return found;
|
|
42
|
-
}
|
|
36
|
+
const trailOf = (value, placeholder, trail = []) => {
|
|
37
|
+
if (value === placeholder)
|
|
38
|
+
return trail;
|
|
39
|
+
if (!Array.isArray(value) && !isRaw(value))
|
|
43
40
|
return undefined;
|
|
44
|
-
|
|
41
|
+
for (const [key, inner] of Object.entries(value)) {
|
|
42
|
+
const found = trailOf(inner, placeholder, [...trail, key]);
|
|
43
|
+
if (found)
|
|
44
|
+
return found;
|
|
45
|
+
}
|
|
46
|
+
return undefined;
|
|
47
|
+
};
|
|
48
|
+
const placeOf = (call, placeholder) => {
|
|
45
49
|
for (const where of ["path", "query", "body"]) {
|
|
46
|
-
const found =
|
|
50
|
+
const found = trailOf(call[where], placeholder);
|
|
47
51
|
if (found)
|
|
48
|
-
return { in: where, path: found.join(".") };
|
|
52
|
+
return { in: where, path: found.length ? found.join(".") : "." };
|
|
49
53
|
}
|
|
50
54
|
return undefined;
|
|
51
55
|
};
|
|
56
|
+
/**
|
|
57
|
+
* Our fields, in the names a read maps, that a write's values give: each rule read backwards, `"{value}"` in its
|
|
58
|
+
* `each` the place of ours. A value the service clears with is a cleared field.
|
|
59
|
+
*/
|
|
60
|
+
const givenOf = (spec, values) => {
|
|
61
|
+
const given = {};
|
|
62
|
+
if (!isRaw(values))
|
|
63
|
+
return given;
|
|
64
|
+
const clear = JSON.stringify(spec.clear === undefined ? null : spec.clear);
|
|
65
|
+
for (const [field, rule] of Object.entries(spec.fields)) {
|
|
66
|
+
if (typeof rule !== "object" || !("to" in rule) || !(rule.to in values))
|
|
67
|
+
continue;
|
|
68
|
+
const name = field === "owner" ? "assignee" : field;
|
|
69
|
+
const list = CRM_LIST_FIELDS.includes(field);
|
|
70
|
+
const raw = values[rule.to];
|
|
71
|
+
if (JSON.stringify(raw) === clear) {
|
|
72
|
+
given[name] = list ? [] : null;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
const trail = rule.each === undefined ? [] : (trailOf(rule.each, "{value}") ?? []);
|
|
76
|
+
const found = (rule.list ? (Array.isArray(raw) ? raw : []) : [raw])
|
|
77
|
+
.map((one) => (trail.length ? at(one, trail.join(".")) : one))
|
|
78
|
+
.filter((one) => one !== undefined && one !== null)
|
|
79
|
+
.map(String);
|
|
80
|
+
if (list)
|
|
81
|
+
given[name] = field === "people" ? found.map((one) => ({ externalId: one, role: "contact" })) : found;
|
|
82
|
+
// A key several rules write together is replaced whole: a part it leaves out is gone.
|
|
83
|
+
else
|
|
84
|
+
given[name] = found[0] ?? null;
|
|
85
|
+
}
|
|
86
|
+
return given;
|
|
87
|
+
};
|
|
52
88
|
const readPlace = (call, place) => {
|
|
53
89
|
if (place.in === "body")
|
|
54
90
|
return at(call.body, place.path);
|
|
@@ -288,15 +324,19 @@ export const crmManifestFake = (input, options) => {
|
|
|
288
324
|
const index = manifest.kinds.indexOf(kind);
|
|
289
325
|
return index < 0 ? undefined : String((index + 1) * 1000 + n + 1);
|
|
290
326
|
};
|
|
291
|
-
/** A
|
|
292
|
-
const
|
|
293
|
-
if (typeof rule !== "object" || !("each" in rule)
|
|
327
|
+
/** A list a rule walks, one entry for each value: a deal's people, a contact's e-mails. */
|
|
328
|
+
const putEach = (row, rule, node, values) => {
|
|
329
|
+
if (typeof rule !== "object" || !("each" in rule))
|
|
294
330
|
return;
|
|
331
|
+
const inner = "id" in rule ? rule.id : rule.value;
|
|
295
332
|
const entry = itemOf(nodeAt(node, rule.each), definitions);
|
|
296
|
-
const
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
333
|
+
const items = values.map((value) => {
|
|
334
|
+
const made = sample(entry && narrowed(entry, inner.split("."), value));
|
|
335
|
+
const item = isRaw(made) ? made : {};
|
|
336
|
+
put(item, inner, value, entry);
|
|
337
|
+
return item;
|
|
338
|
+
});
|
|
339
|
+
putList(row, rule.each, items, node);
|
|
300
340
|
};
|
|
301
341
|
const rows = {};
|
|
302
342
|
const changeAt = {};
|
|
@@ -316,12 +356,12 @@ export const crmManifestFake = (input, options) => {
|
|
|
316
356
|
externalId: String((index + 1) * 1000 + n + 1),
|
|
317
357
|
assignee: "7001",
|
|
318
358
|
...(pipeline ? { pipeline: pipeline.id, stage: pipeline.stages[n % pipeline.stages.length] } : {}),
|
|
319
|
-
...(money ? { amount: "100.50", currency:
|
|
359
|
+
...(money ? { amount: "100.50", currency: CURRENCY } : { amount: undefined, currency: undefined }),
|
|
320
360
|
...(pipeline && idAt("organization", n) ? { organization: idAt("organization", n) } : {}),
|
|
321
361
|
});
|
|
322
362
|
const person = idAt("person", n);
|
|
323
363
|
if (pipeline && person)
|
|
324
|
-
|
|
364
|
+
putEach(row, map.people, node, [person]);
|
|
325
365
|
if (keyset)
|
|
326
366
|
put(row, keyset, time, node);
|
|
327
367
|
return row;
|
|
@@ -384,7 +424,111 @@ export const crmManifestFake = (input, options) => {
|
|
|
384
424
|
}
|
|
385
425
|
if (manifest.users)
|
|
386
426
|
routes.push({ call: manifest.users.call, rows: () => users });
|
|
427
|
+
for (const kind of manifest.kinds) {
|
|
428
|
+
const spec = manifest.writes?.[kind];
|
|
429
|
+
if (!spec)
|
|
430
|
+
continue;
|
|
431
|
+
for (const verb of ["create", "update", "remove"]) {
|
|
432
|
+
const call = spec[verb];
|
|
433
|
+
if (!call)
|
|
434
|
+
continue;
|
|
435
|
+
const id = placeOf(call, "{id}");
|
|
436
|
+
const values = placeOf(call, "{values}");
|
|
437
|
+
routes.push({
|
|
438
|
+
call,
|
|
439
|
+
kind,
|
|
440
|
+
rows: () => rows[kind] ?? [],
|
|
441
|
+
write: { verb, spec, ...(id ? { id } : {}), ...(values ? { values } : {}) },
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
const currency = spec.fields.currency;
|
|
445
|
+
if (typeof currency === "object" && "fixed" in currency)
|
|
446
|
+
routes.push({ call: currency.fixed, rows: () => [], fixed: currency.at });
|
|
447
|
+
}
|
|
387
448
|
const denied = new Map();
|
|
449
|
+
let writes = 0;
|
|
450
|
+
let made = 0;
|
|
451
|
+
let lose = false;
|
|
452
|
+
/** A record, in the names a read maps, as a row the read maps back: what it does not name stays out. */
|
|
453
|
+
const rowFor = (kind, fields) => {
|
|
454
|
+
const entity = manifest.entities[kind];
|
|
455
|
+
const map = entity.map;
|
|
456
|
+
const node = rowNodeOf(entity.list);
|
|
457
|
+
const scalar = (field, value) => {
|
|
458
|
+
if (typeof value === "number" && FIELDS[kind][field] === "epoch")
|
|
459
|
+
return { ms: value };
|
|
460
|
+
return ["string", "number", "boolean"].includes(typeof value) ? value : undefined;
|
|
461
|
+
};
|
|
462
|
+
const given = Object.fromEntries(Object.keys(map).flatMap((field) => {
|
|
463
|
+
const value = scalar(field, fields[field]);
|
|
464
|
+
return value === undefined ? [] : [[field, value]];
|
|
465
|
+
}));
|
|
466
|
+
// Only the rules with a value shape the sample: one without would sample a value where the record has none.
|
|
467
|
+
const rules = Object.fromEntries(Object.entries(map).filter(([field]) => field in given || field === "pipeline"));
|
|
468
|
+
const row = rowOf(node, rules, FIELDS[kind], 0, given);
|
|
469
|
+
for (const [field, value] of Object.entries(fields)) {
|
|
470
|
+
if (!Array.isArray(value))
|
|
471
|
+
continue;
|
|
472
|
+
const ids = value.map((one) => (isRaw(one) ? String(one.externalId) : String(one)));
|
|
473
|
+
const rule = map[field];
|
|
474
|
+
const path = pathOf(rule);
|
|
475
|
+
if (typeof rule === "object" && "each" in rule)
|
|
476
|
+
putEach(row, rule, node, ids);
|
|
477
|
+
else if (path !== undefined && ids[0] !== undefined)
|
|
478
|
+
put(row, path, ids[0], node);
|
|
479
|
+
}
|
|
480
|
+
return row;
|
|
481
|
+
};
|
|
482
|
+
/** A create, update or remove of one record, as the service would make it, answered as its schema says. */
|
|
483
|
+
const written = (route, call) => {
|
|
484
|
+
writes += 1;
|
|
485
|
+
const { kind, write } = route;
|
|
486
|
+
const list = rows[kind] ?? [];
|
|
487
|
+
rows[kind] = list;
|
|
488
|
+
const root = rootOf(route.call);
|
|
489
|
+
const sampled = sample(root);
|
|
490
|
+
const answer = isRaw(sampled) ? sampled : {};
|
|
491
|
+
const externalId = write.verb === "create"
|
|
492
|
+
? String((manifest.kinds.indexOf(kind) + 1) * 1000 + 500 + ++made)
|
|
493
|
+
: String(readPlace(call, write.id) ?? "");
|
|
494
|
+
const place = list.findIndex((one) => idOf(kind, one) === externalId);
|
|
495
|
+
if (write.verb !== "create" && place < 0)
|
|
496
|
+
return refuse(`${call.operation}: no ${kind} ${externalId} in the fake`, 404);
|
|
497
|
+
if (write.verb === "remove") {
|
|
498
|
+
list.splice(place, 1);
|
|
499
|
+
return { body: answer };
|
|
500
|
+
}
|
|
501
|
+
const entity = manifest.entities[kind];
|
|
502
|
+
const before = place < 0 ? {} : (mapRecord(kind, entity, list[place]) ?? {});
|
|
503
|
+
const values = write.values ? readPlace(call, write.values) : undefined;
|
|
504
|
+
const known = [
|
|
505
|
+
...Object.values(write.spec.fields).flatMap((rule) => typeof rule === "object" && "to" in rule ? [rule.to] : []),
|
|
506
|
+
...(manifest.fields?.[kind] ? Array.from({ length: PAGE }, (_, n) => `${kind}_field_${n + 1}`) : []),
|
|
507
|
+
];
|
|
508
|
+
const unknown = Object.keys(isRaw(values) ? values : {}).filter((key) => !known.includes(key));
|
|
509
|
+
if (unknown.length)
|
|
510
|
+
return refuse(`${call.operation}: ${kind} has no field ${unknown.join(", ")} in the fake`);
|
|
511
|
+
const given = givenOf(write.spec, values);
|
|
512
|
+
const pipeline = pipelines.get(kind);
|
|
513
|
+
const fields = {
|
|
514
|
+
...(pipeline ? { pipeline: pipeline.id } : {}),
|
|
515
|
+
...before,
|
|
516
|
+
...given,
|
|
517
|
+
externalId,
|
|
518
|
+
updatedAt: Date.now(),
|
|
519
|
+
};
|
|
520
|
+
const currency = write.spec.fields.currency;
|
|
521
|
+
if (typeof currency === "object" && "fixed" in currency && fields.amount != null)
|
|
522
|
+
fields.currency = CURRENCY;
|
|
523
|
+
const row = rowFor(kind, fields);
|
|
524
|
+
if (place < 0)
|
|
525
|
+
list.push(row);
|
|
526
|
+
else
|
|
527
|
+
list[place] = row;
|
|
528
|
+
if (write.verb === "create" && write.spec.id)
|
|
529
|
+
putList(answer, write.spec.id, externalId, root);
|
|
530
|
+
return { body: answer };
|
|
531
|
+
};
|
|
388
532
|
const refuse = (message, status = 400) => {
|
|
389
533
|
own.push(message);
|
|
390
534
|
return { status, body: { message } };
|
|
@@ -464,6 +608,15 @@ export const crmManifestFake = (input, options) => {
|
|
|
464
608
|
const route = routeOf(call);
|
|
465
609
|
if (!route)
|
|
466
610
|
return refuse(`${call.operation ?? call.url.pathname}: no call of the source manifest is this one`, 404);
|
|
611
|
+
if (route.write && route.kind)
|
|
612
|
+
return written({ ...route, kind: route.kind, write: route.write }, call);
|
|
613
|
+
if (route.fixed !== undefined) {
|
|
614
|
+
const root = rootOf(route.call);
|
|
615
|
+
const sampled = sample(root);
|
|
616
|
+
const answer = isRaw(sampled) ? sampled : {};
|
|
617
|
+
putList(answer, route.fixed, CURRENCY, root);
|
|
618
|
+
return { body: answer };
|
|
619
|
+
}
|
|
467
620
|
const changes = route.since ? route : undefined;
|
|
468
621
|
if (changes?.since && readPlace(call, changes.since.place) === undefined)
|
|
469
622
|
return refuse(`${call.operation}: a read of changes without its since`);
|
|
@@ -514,7 +667,15 @@ export const crmManifestFake = (input, options) => {
|
|
|
514
667
|
answers: Object.fromEntries(api.operations.map((one) => [one.id, answerOf])),
|
|
515
668
|
});
|
|
516
669
|
return {
|
|
517
|
-
|
|
670
|
+
async fetch(input, init) {
|
|
671
|
+
const before = writes;
|
|
672
|
+
const answer = await fake.fetch(input, init);
|
|
673
|
+
if (lose && writes > before) {
|
|
674
|
+
lose = false;
|
|
675
|
+
throw new TypeError("fetch failed");
|
|
676
|
+
}
|
|
677
|
+
return answer;
|
|
678
|
+
},
|
|
518
679
|
calls: fake.calls,
|
|
519
680
|
get refusals() {
|
|
520
681
|
return [...fake.refusals, ...own];
|
|
@@ -533,6 +694,10 @@ export const crmManifestFake = (input, options) => {
|
|
|
533
694
|
if (timeOf(kind, row) === null)
|
|
534
695
|
throw new Error(`the fake cannot change the time of ${kind} ${id}`);
|
|
535
696
|
},
|
|
697
|
+
loseNextAnswer: () => {
|
|
698
|
+
lose = true;
|
|
699
|
+
},
|
|
700
|
+
requests: () => writes,
|
|
536
701
|
};
|
|
537
702
|
};
|
|
538
703
|
const externalIdOf = (manifest, kind, row) => {
|
|
@@ -616,3 +781,63 @@ export const crmManifestContract = (manifest, options) => {
|
|
|
616
781
|
},
|
|
617
782
|
}));
|
|
618
783
|
};
|
|
784
|
+
/**
|
|
785
|
+
* A `crmWriteContract` fixture for a writer built from a manifest's `writes`, over `crmManifestFake`: the writer, the
|
|
786
|
+
* first pipeline of the deal kind it writes, the service's own names of stage and owner, and the fake.
|
|
787
|
+
*/
|
|
788
|
+
export const crmManifestWriteFixture = async (manifest, options) => {
|
|
789
|
+
const fake = crmManifestFake(manifest, options);
|
|
790
|
+
const sending = {
|
|
791
|
+
manifest: options.api,
|
|
792
|
+
service: manifest.provider,
|
|
793
|
+
fetch: fake.fetch,
|
|
794
|
+
token: () => FAKE_MANIFEST_TOKEN,
|
|
795
|
+
...(options.baseUrl ? { baseUrl: options.baseUrl } : {}),
|
|
796
|
+
};
|
|
797
|
+
const source = crmSourceFromManifest(manifest, {
|
|
798
|
+
api: options.api,
|
|
799
|
+
call: serviceApiCall(sending),
|
|
800
|
+
...(options.hooks ? { hooks: options.hooks } : {}),
|
|
801
|
+
});
|
|
802
|
+
const writer = crmWriterFromManifest(manifest, {
|
|
803
|
+
api: options.api,
|
|
804
|
+
service: manifest.provider,
|
|
805
|
+
send: serviceApiSend(sending),
|
|
806
|
+
source: () => source,
|
|
807
|
+
});
|
|
808
|
+
const kind = DEAL_KINDS.find((one) => writer.capabilities.kinds.includes(one));
|
|
809
|
+
if (!kind)
|
|
810
|
+
throw new Error("the manifest writes no deal or lead, and the write contract needs one");
|
|
811
|
+
const [pipeline] = await source.pipelines(kind);
|
|
812
|
+
if (!pipeline)
|
|
813
|
+
throw new Error(`the fake holds no ${kind} pipeline`);
|
|
814
|
+
const fields = (manifest.writes?.[kind]?.fields ?? {});
|
|
815
|
+
const aliases = [fields.stage, fields.owner].flatMap((rule) => isRaw(rule) && typeof rule.to === "string" ? [rule.to] : []);
|
|
816
|
+
return {
|
|
817
|
+
writer,
|
|
818
|
+
kind,
|
|
819
|
+
pipeline,
|
|
820
|
+
aliases,
|
|
821
|
+
loseNextAnswer: fake.loseNextAnswer,
|
|
822
|
+
requests: fake.requests,
|
|
823
|
+
fake,
|
|
824
|
+
};
|
|
825
|
+
};
|
|
826
|
+
/** `crmWriteContract` over `crmManifestWriteFixture`, each case failing too when the fake refused a call. */
|
|
827
|
+
export const crmManifestWriteContract = (manifest, options) => {
|
|
828
|
+
const fakes = [];
|
|
829
|
+
return crmWriteContract(async () => {
|
|
830
|
+
const fixture = await crmManifestWriteFixture(manifest, options);
|
|
831
|
+
fakes.push(fixture.fake);
|
|
832
|
+
return fixture;
|
|
833
|
+
}).map(({ name, run }) => ({
|
|
834
|
+
name,
|
|
835
|
+
async run() {
|
|
836
|
+
fakes.length = 0;
|
|
837
|
+
await run();
|
|
838
|
+
const refused = fakes.flatMap((fake) => fake.refusals);
|
|
839
|
+
if (refused.length)
|
|
840
|
+
throw new Error(`the fake refused calls the writer made:\n${refused.join("\n")}`);
|
|
841
|
+
},
|
|
842
|
+
}));
|
|
843
|
+
};
|
package/dist/crm/writes.d.ts
CHANGED
|
@@ -1,28 +1,19 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
2
|
import type { CrmField, CrmKind, CrmOrganization, CrmPerson } from "../domain/crm.js";
|
|
3
3
|
import type { DealKind } from "../domain/deals.js";
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
6
|
-
import { type
|
|
7
|
-
import { type PermissionKey } from "../sends/permissions.js";
|
|
4
|
+
import type { Asker } from "../sends/guard.js";
|
|
5
|
+
import type { PermissionKey } from "../sends/permissions.js";
|
|
6
|
+
import { type ProfileWrites } from "../sends/profile-writes.js";
|
|
8
7
|
import type { Deal, PipelineOptions, PipelineReport } from "../store/sqlite/deals.js";
|
|
9
8
|
import type { MessageStore } from "../store/store.js";
|
|
10
9
|
import type { AccountKey } from "../store/types.js";
|
|
11
10
|
import type { CrmSource } from "./source.js";
|
|
12
11
|
import { type CrmVerb, type CrmWriter } from "./writer.js";
|
|
13
12
|
/** What a CLI hands the CRM writes for one profile. */
|
|
14
|
-
export interface CrmWrites {
|
|
13
|
+
export interface CrmWrites extends ProfileWrites {
|
|
15
14
|
writer: CrmWriter;
|
|
16
|
-
app: AppIdentity;
|
|
17
|
-
/**
|
|
18
|
-
* The profile's levels, read on every write so a config change applies at once. A CRM limits its own rate, so
|
|
19
|
-
* `sendsPerHour` is not read: the guard is there for the levels and the journal.
|
|
20
|
-
*/
|
|
21
|
-
settings: () => Omit<GuardSettings, "sendsPerHour"> & Partial<Pick<GuardSettings, "sendsPerHour">>;
|
|
22
15
|
/** The CRM user the sign-in acts as. An owner other than it checks `crm.assign-others`; left out, every owner does. */
|
|
23
16
|
self?: string;
|
|
24
|
-
env?: NodeJS.ProcessEnv;
|
|
25
|
-
warn?: Warn;
|
|
26
17
|
/**
|
|
27
18
|
* The CLI's own keys a write also checks, such as Bitrix24's `api.crm.deal.update`; pass the same function to
|
|
28
19
|
* `crmAlsoChecks` so `--dry-run` names them.
|
package/dist/crm/writes.js
CHANGED
|
@@ -2,10 +2,8 @@ import { CliError, isCliError } from "@wirecat/cli-base";
|
|
|
2
2
|
import { currencyOf, minorUnits } from "../domain/money.js";
|
|
3
3
|
import { e164Of } from "../domain/phones.js";
|
|
4
4
|
import { foldName } from "../resolve.js";
|
|
5
|
-
import { guardFor } from "../sends/guard.js";
|
|
6
5
|
import { guardedWrite } from "../sends/guarded.js";
|
|
7
|
-
import {
|
|
8
|
-
import { DEFAULT_PERMISSIONS, levelFor } from "../sends/permissions.js";
|
|
6
|
+
import { profileWriteGuard } from "../sends/profile-writes.js";
|
|
9
7
|
import { newOperationId } from "../sends/send-id.js";
|
|
10
8
|
import { foreignContacts } from "./foreign-contacts.js";
|
|
11
9
|
import { CRM_ASSIGN_OTHERS_KEY, CRM_WRITE_DEFAULTS, } from "./writer.js";
|
|
@@ -250,12 +248,7 @@ const keysOf = (context, kind, verb, { stage, owner }) => {
|
|
|
250
248
|
extra.push(CRM_ASSIGN_OTHERS_KEY);
|
|
251
249
|
return { key: keyOf(kind, verb), extra: [...extra, ...(context.writes.alsoChecks?.(kind, verb) ?? [])] };
|
|
252
250
|
};
|
|
253
|
-
|
|
254
|
-
const guardOf = (context, extra) => {
|
|
255
|
-
const settings = context.writes.settings();
|
|
256
|
-
const defaults = { ...DEFAULT_PERMISSIONS, ...CRM_WRITE_DEFAULTS, ...settings.permissionDefaults };
|
|
257
|
-
return keyedGuard((ask) => guardFor(context.writes.app, { ...settings, sendsPerHour: Number.MAX_SAFE_INTEGER, permissionDefaults: defaults }, context.writes.warn ?? (() => { }), context.writes.env, ask), context.ask, (key) => levelFor(settings.permissions, key, defaults).level, extra);
|
|
258
|
-
};
|
|
251
|
+
const guardOf = (context, extra) => profileWriteGuard(context.writes, context.ask, CRM_WRITE_DEFAULTS, extra);
|
|
259
252
|
/** Sends the write once, never again: a lost answer is `outcome_unknown`, with what the owner may do about it. */
|
|
260
253
|
const written = async (context, { key, extra }, operationId, act) => {
|
|
261
254
|
try {
|
package/dist/errors/reasons.js
CHANGED
|
@@ -540,6 +540,11 @@ const OTHERS = [
|
|
|
540
540
|
validation("crm.write-unsupported", "the CRM does not take this write, or does not recognise a repeated one"),
|
|
541
541
|
validation("crm.unlisted-held", "a list of every id would mark most of a kind deleted; nothing was, until --rescan"),
|
|
542
542
|
validation("tracker.unlisted-held", "a tracker's id list would mark too much deleted, or the sign-in read only part; nothing was, until --rescan"),
|
|
543
|
+
validation("tracker.write-unsupported", "the tracker does not take this write, or does not recognise a repeated one"),
|
|
544
|
+
validation("tracker.field", "a field of an issue or a comment the tracker's write does not take as given"),
|
|
545
|
+
validation("tracker.reference", "a team, state, label, project or user no single row of the copy has by that name"),
|
|
546
|
+
validation("tracker.team-required", "a new issue needs its team: name one, or sync a copy that holds one"),
|
|
547
|
+
["tracker.no-record", "invalid_response", "the tracker answered a write with no record and no error; sync and look"],
|
|
543
548
|
validation("content.unlisted-held", "a content id list would mark too much deleted, the sign-in read only part, or the site moved; nothing was"),
|
|
544
549
|
validation("content.conflict", "the post changed at the source since the last sync; sync, then write again"),
|
|
545
550
|
validation("content.lossy-body", "the post's text holds blocks or embeds Markdown does not keep; --replace-body"),
|
|
@@ -71,6 +71,13 @@ export const WIRECAT_TOOLS = [
|
|
|
71
71
|
package: "@wirecat/amo-cli",
|
|
72
72
|
does: "amoCRM and Kommo: deals, contacts and companies, searchable with your other WireCat data",
|
|
73
73
|
},
|
|
74
|
+
{
|
|
75
|
+
command: "attio",
|
|
76
|
+
appName: "attio-cli",
|
|
77
|
+
envPrefix: "WCATTIO",
|
|
78
|
+
package: "@wirecat/attio-cli",
|
|
79
|
+
does: "Attio CRM: deals, people and companies, searchable with your other WireCat data",
|
|
80
|
+
},
|
|
74
81
|
{
|
|
75
82
|
command: "pbx",
|
|
76
83
|
appName: "asterisk-cli",
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { AppIdentity } from "../ports/app.js";
|
|
2
|
+
import type { Warn } from "../runs/events.js";
|
|
3
|
+
import { type Asker, type GuardSettings, type SendGuard } from "./guard.js";
|
|
4
|
+
import { type Level, type PermissionKey } from "./permissions.js";
|
|
5
|
+
/** What a CLI hands a service's writes (a CRM's, a site's, a tracker's) for one profile. */
|
|
6
|
+
export interface ProfileWrites {
|
|
7
|
+
app: AppIdentity;
|
|
8
|
+
/**
|
|
9
|
+
* The profile's levels, read on every write so a config change applies at once. A service limits its own rate, so
|
|
10
|
+
* `sendsPerHour` is not read: the guard is there for the levels and the journal.
|
|
11
|
+
*/
|
|
12
|
+
settings: () => Omit<GuardSettings, "sendsPerHour"> & Partial<Pick<GuardSettings, "sendsPerHour">>;
|
|
13
|
+
env?: NodeJS.ProcessEnv;
|
|
14
|
+
warn?: Warn;
|
|
15
|
+
}
|
|
16
|
+
/** The profile's guard over a write's own key and the keys its fields imply (`keyedGuard`). */
|
|
17
|
+
export declare const profileWriteGuard: (writes: ProfileWrites, ask: Asker, builtIn: Readonly<Record<PermissionKey, Level>>, extra: readonly PermissionKey[]) => SendGuard;
|
|
18
|
+
/**
|
|
19
|
+
* The keys at level `ask`: the owner is asked about them here, so a writer over the `api` group need not ask again. A
|
|
20
|
+
* key at `allow` was asked about by nobody and confirms nothing.
|
|
21
|
+
*/
|
|
22
|
+
export declare const askedKeys: (writes: ProfileWrites, builtIn: Readonly<Record<PermissionKey, Level>>, keys: readonly PermissionKey[]) => PermissionKey[];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { guardFor } from "./guard.js";
|
|
2
|
+
import { keyedGuard } from "./keyed-guard.js";
|
|
3
|
+
import { DEFAULT_PERMISSIONS, levelFor } from "./permissions.js";
|
|
4
|
+
const levelsOf = (writes, defaults) => {
|
|
5
|
+
const settings = writes.settings();
|
|
6
|
+
return { settings, defaults: { ...DEFAULT_PERMISSIONS, ...defaults, ...settings.permissionDefaults } };
|
|
7
|
+
};
|
|
8
|
+
/** The profile's guard over a write's own key and the keys its fields imply (`keyedGuard`). */
|
|
9
|
+
export const profileWriteGuard = (writes, ask, builtIn, extra) => {
|
|
10
|
+
const { settings, defaults } = levelsOf(writes, builtIn);
|
|
11
|
+
return keyedGuard((asker) => guardFor(writes.app, { ...settings, sendsPerHour: Number.MAX_SAFE_INTEGER, permissionDefaults: defaults }, writes.warn ?? (() => { }), writes.env, asker), ask, (key) => levelFor(settings.permissions, key, defaults).level, extra);
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* The keys at level `ask`: the owner is asked about them here, so a writer over the `api` group need not ask again. A
|
|
15
|
+
* key at `allow` was asked about by nobody and confirms nothing.
|
|
16
|
+
*/
|
|
17
|
+
export const askedKeys = (writes, builtIn, keys) => {
|
|
18
|
+
const { settings, defaults } = levelsOf(writes, builtIn);
|
|
19
|
+
return keys.filter((key) => levelFor(settings.permissions, key, defaults).level === "ask");
|
|
20
|
+
};
|
|
@@ -3,12 +3,16 @@ import type { AppIdentity } from "../../ports/app.js";
|
|
|
3
3
|
import type { MessageStore } from "../../store/store.js";
|
|
4
4
|
import type { AccountKey } from "../../store/types.js";
|
|
5
5
|
import type { TrackerSource } from "../source.js";
|
|
6
|
+
import type { TrackerWrite } from "../writer.js";
|
|
7
|
+
import type { TrackerWrites } from "../writes.js";
|
|
6
8
|
/** The selected profile's tracker account, opened for one command and closed before its answer is printed. */
|
|
7
9
|
export interface TrackerRun {
|
|
8
10
|
account: AccountKey;
|
|
9
11
|
store: MessageStore;
|
|
10
12
|
/** The tracker, built on first use: a command that only reads the store never reads the sign-in. */
|
|
11
13
|
source: () => TrackerSource;
|
|
14
|
+
/** The tracker's writer and the profile's levels, built for the command run; needed by every write command. */
|
|
15
|
+
writes?: (command: Command) => TrackerWrites;
|
|
12
16
|
close?: () => Promise<void>;
|
|
13
17
|
}
|
|
14
18
|
export interface TrackerCommandOptions {
|
|
@@ -18,7 +22,12 @@ export interface TrackerCommandOptions {
|
|
|
18
22
|
open: (command: Command, access: {
|
|
19
23
|
readOnly: boolean;
|
|
20
24
|
}) => Promise<TrackerRun>;
|
|
25
|
+
/** What the tracker's writer takes (`TrackerWriter.writes`): mounts `issues create`, `update` and `comment`. */
|
|
26
|
+
writes?: readonly TrackerWrite[];
|
|
21
27
|
now?: () => number;
|
|
22
28
|
}
|
|
23
|
-
/**
|
|
29
|
+
/**
|
|
30
|
+
* The tracker's commands: `sync`, `issues list|show|search|assign`, `issues create|update|comment` for the writes in
|
|
31
|
+
* `writes`, and a `list` of each other kind.
|
|
32
|
+
*/
|
|
24
33
|
export declare const addTrackerCommands: (program: Command, options: TrackerCommandOptions) => void;
|
|
@@ -12,6 +12,7 @@ import { STATE_TYPES, TRACKER_KINDS, } from "../../domain/tracker.js";
|
|
|
12
12
|
import { count, text } from "../../entities/fields.js";
|
|
13
13
|
import { syncTracker } from "../sync.js";
|
|
14
14
|
import { trackerIssueTask } from "../tasks.js";
|
|
15
|
+
import { addTrackerWriteCommands } from "./writes.js";
|
|
15
16
|
const MOST = 500;
|
|
16
17
|
const integer = (most) => (value) => {
|
|
17
18
|
const number = Number(value);
|
|
@@ -89,6 +90,12 @@ const trackerSync = v.strictObject({
|
|
|
89
90
|
v.strictObject({ skipped: text }),
|
|
90
91
|
])),
|
|
91
92
|
});
|
|
93
|
+
const trackerWrite = v.strictObject({
|
|
94
|
+
operation_id: text,
|
|
95
|
+
issue: v.optional(trackerView),
|
|
96
|
+
comment: v.optional(trackerView),
|
|
97
|
+
untrusted: v.array(text),
|
|
98
|
+
});
|
|
92
99
|
const trackerTask = v.strictObject({ task_id: text, key: v.optional(text), issue: text, created: v.boolean() });
|
|
93
100
|
/** Each issue with its state's name and type, and its assignee's name and person, read from the same copy. */
|
|
94
101
|
const withNames = async (run, items) => {
|
|
@@ -263,6 +270,15 @@ const addTrackerCommandsDefinition = (program, options) => {
|
|
|
263
270
|
return itemOf({ task_id: task.id, ...(task.key ? { key: task.key } : {}), issue: task.source, created });
|
|
264
271
|
});
|
|
265
272
|
});
|
|
273
|
+
if (options.writes?.length)
|
|
274
|
+
addTrackerWriteCommands(issues, {
|
|
275
|
+
writes: options.writes,
|
|
276
|
+
within,
|
|
277
|
+
example,
|
|
278
|
+
view: async (run, item) => item.kind === "issue" ? (await withNames(run, [item]))[0] : viewOf(item),
|
|
279
|
+
answer: trackerWrite,
|
|
280
|
+
now,
|
|
281
|
+
});
|
|
266
282
|
for (const [kind, plural, what] of [
|
|
267
283
|
["project", "projects", "the tracker's projects: status, lead, teams and target date"],
|
|
268
284
|
["team", "teams", "the tracker's teams"],
|
|
@@ -294,5 +310,8 @@ const addTrackerCommandsDefinition = (program, options) => {
|
|
|
294
310
|
});
|
|
295
311
|
}
|
|
296
312
|
};
|
|
297
|
-
/**
|
|
313
|
+
/**
|
|
314
|
+
* The tracker's commands: `sync`, `issues list|show|search|assign`, `issues create|update|comment` for the writes in
|
|
315
|
+
* `writes`, and a `list` of each other kind.
|
|
316
|
+
*/
|
|
298
317
|
export const addTrackerCommands = honoursDryRun(sharedCommand(addTrackerCommandsDefinition));
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Command } from "commander";
|
|
2
|
+
import type * as v from "valibot";
|
|
3
|
+
import { type Body } from "../../cli/envelope.js";
|
|
4
|
+
import type { TrackerItem } from "../../store/sqlite/tracker.js";
|
|
5
|
+
import type { TrackerWrite } from "../writer.js";
|
|
6
|
+
import { type TrackerWriteContext, type TrackerWrites } from "../writes.js";
|
|
7
|
+
/** What the writes need of the open profile: the shared `TrackerRun` and its writes, built for the command run. */
|
|
8
|
+
interface WriteRun {
|
|
9
|
+
account: TrackerWriteContext["account"];
|
|
10
|
+
store: TrackerWriteContext["store"];
|
|
11
|
+
writes?: (command: Command) => TrackerWrites;
|
|
12
|
+
source?: TrackerWriteContext["source"];
|
|
13
|
+
}
|
|
14
|
+
export interface TrackerWriteCommandOptions<R extends WriteRun> {
|
|
15
|
+
writes: readonly TrackerWrite[];
|
|
16
|
+
within: (command: Command, readOnly: boolean, work: (run: R) => Promise<Body>) => Promise<void>;
|
|
17
|
+
example: (command: Command, ...lines: string[]) => Command;
|
|
18
|
+
/** An issue or comment as the read commands show it, with its `untrusted` fields. */
|
|
19
|
+
view: (run: R, item: TrackerItem) => Promise<Record<string, unknown> & {
|
|
20
|
+
untrusted: string[];
|
|
21
|
+
}>;
|
|
22
|
+
answer: v.GenericSchema;
|
|
23
|
+
now: () => number;
|
|
24
|
+
}
|
|
25
|
+
/** `issues create|update|comment` for each write the tracker takes, under the `issues` group. */
|
|
26
|
+
export declare const addTrackerWriteCommands: <R extends WriteRun>(issues: Command, options: TrackerWriteCommandOptions<R>) => void;
|
|
27
|
+
export {};
|