@wirecat/cli-core 0.675.0 → 0.676.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 +88 -0
- package/dist/background/daily-archive-timer.d.ts +1 -1
- package/dist/calendar/mcp/index.d.ts +12 -12
- package/dist/cli/documents/words.d.ts +1 -1
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/list-query.d.ts +3 -2
- package/dist/cli/list-query.js +5 -2
- package/dist/cli/setup-command.js +9 -3
- package/dist/crm/cli/index.d.ts +7 -1
- package/dist/crm/cli/index.js +104 -20
- package/dist/crm/cli/pipeline-view.js +15 -1
- package/dist/crm/cli/records.js +11 -3
- package/dist/crm/cli/search-all.d.ts +5 -2
- package/dist/crm/cli/search-all.js +15 -3
- package/dist/crm/source.d.ts +3 -2
- package/dist/crm/stage-tasks.js +1 -1
- package/dist/crm/sync.js +13 -11
- package/dist/crm/testing/write-contract.js +5 -1
- package/dist/crm/viewer.d.ts +28 -0
- package/dist/crm/viewer.js +63 -0
- package/dist/crm/writer.d.ts +3 -0
- package/dist/crm/writes.d.ts +7 -2
- package/dist/crm/writes.js +24 -14
- package/dist/domain/crm.d.ts +9 -9
- package/dist/domain/moment.d.ts +2 -0
- package/dist/domain/moment.js +2 -0
- package/dist/entities/business-context.js +1 -1
- package/dist/entities/deal.d.ts +86 -39
- package/dist/entities/deal.js +45 -4
- package/dist/entities/index.d.ts +20 -32
- package/dist/errors/reasons.js +1 -0
- package/dist/meetings/mcp/index.d.ts +15 -15
- package/dist/services/search-all.d.ts +3 -1
- package/dist/services/search-all.js +1 -0
- package/dist/setup/define.d.ts +17 -2
- package/dist/store/exports.d.ts +1 -0
- package/dist/store/exports.js +1 -0
- package/dist/store/sqlite/copy-viewer.d.ts +16 -0
- package/dist/store/sqlite/copy-viewer.js +11 -0
- package/dist/store/sqlite/crm-activities.d.ts +7 -1
- package/dist/store/sqlite/crm-activities.js +32 -5
- package/dist/store/sqlite/crm-contacts.d.ts +5 -2
- package/dist/store/sqlite/crm-contacts.js +30 -14
- package/dist/store/sqlite/crm-rows.d.ts +7 -0
- package/dist/store/sqlite/crm-rows.js +37 -5
- package/dist/store/sqlite/crm-settings.d.ts +2 -1
- package/dist/store/sqlite/crm-settings.js +3 -2
- package/dist/store/sqlite/crm-stage-entries.d.ts +24 -4
- package/dist/store/sqlite/crm-stage-entries.js +77 -18
- package/dist/store/sqlite/crm.d.ts +14 -4
- package/dist/store/sqlite/crm.js +59 -7
- package/dist/store/sqlite/deals.d.ts +47 -29
- package/dist/store/sqlite/deals.js +64 -76
- package/dist/store/sqlite/pipeline-period.d.ts +85 -0
- package/dist/store/sqlite/pipeline-period.js +199 -0
- package/dist/store/sqlite/stage-amounts.d.ts +6 -2
- package/dist/store/sqlite/stage-amounts.js +8 -5
- package/package.json +3 -3
package/dist/crm/cli/records.js
CHANGED
|
@@ -340,6 +340,7 @@ const users = (program, { within, example }) => {
|
|
|
340
340
|
const crmRecordWrite = v.strictObject({
|
|
341
341
|
operation_id: v.string(),
|
|
342
342
|
stored: v.boolean(),
|
|
343
|
+
removed: v.optional(v.boolean()),
|
|
343
344
|
person: v.optional(crmPersonSchema),
|
|
344
345
|
organization: v.optional(crmOrganizationSchema),
|
|
345
346
|
untrusted,
|
|
@@ -356,7 +357,11 @@ const addRecordWrites = (group, noun, { within, example, writes }) => {
|
|
|
356
357
|
.option("--first-name <name>", "the first name; empty clears it")
|
|
357
358
|
.option("--last-name <name>", "the last name; empty clears it")
|
|
358
359
|
.option("--position <title>", "the job title; empty clears it");
|
|
360
|
+
const repeated = (value, previous = []) => [...previous, value];
|
|
359
361
|
command
|
|
362
|
+
.option("--email <address>", "an e-mail address to give it; repeat it for more; replaces its list, and an empty one clears it", repeated)
|
|
363
|
+
.option("--phone <phone>", "a phone number to give it, in international form starting with +; repeat it for more; replaces its list, " +
|
|
364
|
+
"and an empty one clears it", repeated)
|
|
360
365
|
.option("--owner <user>", "the CRM user it is assigned to; anyone but the sign-in's own user checks crm.assign-others")
|
|
361
366
|
.option("--field <key=value>", "one of the CRM's own fields, by its code or label as `fields list` prints them, and its value; a list " +
|
|
362
367
|
"field takes its option's label or id; repeat it for more fields, or for each value of a multiple field; " +
|
|
@@ -369,8 +374,10 @@ const addRecordWrites = (group, noun, { within, example, writes }) => {
|
|
|
369
374
|
command.option("--operation-id <id>", "the operation_id a write that ended in outcome_unknown named: the CRM answers the repeat from its record");
|
|
370
375
|
return command;
|
|
371
376
|
};
|
|
372
|
-
const inputOf = ({ field, operationId: _id, ...rest }) => ({
|
|
377
|
+
const inputOf = ({ field, email, phone, operationId: _id, ...rest }) => ({
|
|
373
378
|
...Object.fromEntries(Object.entries(rest).filter(([, value]) => value !== undefined)),
|
|
379
|
+
...(email ? { emails: email } : {}),
|
|
380
|
+
...(phone ? { phones: phone } : {}),
|
|
374
381
|
...(field?.length ? { custom: fieldPairsOf(field) } : {}),
|
|
375
382
|
});
|
|
376
383
|
const contextOf = (run, command) => {
|
|
@@ -385,15 +392,16 @@ const addRecordWrites = (group, noun, { within, example, writes }) => {
|
|
|
385
392
|
names: { id: "--operation-id" },
|
|
386
393
|
};
|
|
387
394
|
};
|
|
388
|
-
const answered = ({ operationId, record, stored }) => itemOf({
|
|
395
|
+
const answered = ({ operationId, record, stored, removed }) => itemOf({
|
|
389
396
|
operation_id: operationId,
|
|
390
397
|
stored,
|
|
398
|
+
...(removed === undefined ? {} : { removed }),
|
|
391
399
|
[noun.kind]: noun.of(record),
|
|
392
400
|
untrusted: (person ? PERSON_UNTRUSTED : ORGANIZATION_UNTRUSTED).map((one) => `${noun.kind}.${one}`),
|
|
393
401
|
});
|
|
394
402
|
const command = (name, description) => answerEntity(annotate(group.command(name), { effects: REMOTE_WRITE, offline: false }), "crm_record_write", crmRecordWrite).description(description);
|
|
395
403
|
if (writes.verbs.includes("create"))
|
|
396
|
-
example(fields(command("create", `make a ${noun.kind} in the CRM, then store the CRM's copy of it`)), person
|
|
404
|
+
example(fields(command("create", `make ${noun.kind === "organization" ? "an" : "a"} ${noun.kind} in the CRM, then store the CRM's copy of it`)), person
|
|
397
405
|
? `${noun.noun} create --first-name Anna --last-name Example --position Buyer`
|
|
398
406
|
: `${noun.noun} create --name "Example LLC"`).action(async (opts, leaf) => {
|
|
399
407
|
await within(leaf, async (run) => {
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { Command } from "commander";
|
|
2
2
|
import type { AppIdentity } from "../../ports/app.js";
|
|
3
|
-
import type {
|
|
3
|
+
import type { CopyViewer } from "../../store/store.js";
|
|
4
|
+
import type { CrmRun, CrmWithin } from "./index.js";
|
|
4
5
|
/**
|
|
5
6
|
* `search all` in a CRM CLI: the shared search over the store, its own deals and leads beside the messages, notes and
|
|
6
7
|
* events other WireCat tools stored. A locked profile reads only its own account's deals.
|
|
7
8
|
*/
|
|
8
|
-
export declare const addCrmSearchAll: (searches: Command, { within, app, example, }: {
|
|
9
|
+
export declare const addCrmSearchAll: (searches: Command, { within, app, example, viewerOf, }: {
|
|
9
10
|
within: CrmWithin;
|
|
10
11
|
app: AppIdentity;
|
|
11
12
|
example: (command: Command, ...lines: string[]) => Command;
|
|
13
|
+
/** This profile's rule over its own account's copy, as the other reads of the copy take it. */
|
|
14
|
+
viewerOf: (run: CrmRun) => Promise<CopyViewer | undefined>;
|
|
12
15
|
}) => void;
|
|
@@ -11,7 +11,8 @@ import { commandAt, rootOf } from "../../cli/steps.js";
|
|
|
11
11
|
import { DEAL_KINDS } from "../../domain/deals.js";
|
|
12
12
|
import { SAVE_HELP } from "../../meetings/index.js";
|
|
13
13
|
import { meaningLane } from "../../services/messages-meaning.js";
|
|
14
|
-
import { searchAll } from "../../services/search-all.js";
|
|
14
|
+
import { crmAccounts, searchAll } from "../../services/search-all.js";
|
|
15
|
+
import { crmViewerOf } from "../viewer.js";
|
|
15
16
|
/** What a CRM CLI's `search all` reads: its deals and leads first, then what the other tools stored. */
|
|
16
17
|
const SEARCHED = ["crm", ...SOURCE_SEARCHED];
|
|
17
18
|
const DEFAULT_LIMIT = 20;
|
|
@@ -19,7 +20,7 @@ const DEFAULT_LIMIT = 20;
|
|
|
19
20
|
* `search all` in a CRM CLI: the shared search over the store, its own deals and leads beside the messages, notes and
|
|
20
21
|
* events other WireCat tools stored. A locked profile reads only its own account's deals.
|
|
21
22
|
*/
|
|
22
|
-
export const addCrmSearchAll = (searches, { within, app, example, }) => {
|
|
23
|
+
export const addCrmSearchAll = (searches, { within, app, example, viewerOf, }) => {
|
|
23
24
|
const all = annotate(new Command("all"), { effects: LOCAL_READ, partial: true, output: { entity: "hit" } })
|
|
24
25
|
.summary("search stored deals and leads, messages, notes, calendar events and meetings")
|
|
25
26
|
.description("search the CRM's deals and leads by the words of their title, company, contacts and activities, and the " +
|
|
@@ -47,6 +48,16 @@ export const addCrmSearchAll = (searches, { within, app, example, }) => {
|
|
|
47
48
|
const limit = opts.limit ?? DEFAULT_LIMIT;
|
|
48
49
|
const offset = offsetOf(opts.page, limit);
|
|
49
50
|
const env = run.env ?? process.env;
|
|
51
|
+
const own = await viewerOf(run);
|
|
52
|
+
// A profile limited on its own account reads no other copy; anyone else reads each as its owner's sign-in may.
|
|
53
|
+
const alone = run.locked === true || own !== undefined;
|
|
54
|
+
const others = alone
|
|
55
|
+
? []
|
|
56
|
+
: (await crmAccounts(run.store)).filter((one) => one.provider !== run.account.provider || one.account !== run.account.account);
|
|
57
|
+
const viewers = [
|
|
58
|
+
...(own ? [own] : []),
|
|
59
|
+
...(await Promise.all(others.map(async (one) => (await crmViewerOf(run.store, one, (await run.store.crm.ownUser(one)) ?? null)).viewer))).filter((one) => one !== undefined),
|
|
60
|
+
];
|
|
50
61
|
const found = await searchAll(run.store, run.account, {
|
|
51
62
|
text: words.join(" "),
|
|
52
63
|
limit,
|
|
@@ -57,8 +68,9 @@ export const addCrmSearchAll = (searches, { within, app, example, }) => {
|
|
|
57
68
|
only,
|
|
58
69
|
...(opts.only?.includes("meetings") ? { meetingsAsked: true } : {}),
|
|
59
70
|
...(crmKinds.length < DEAL_KINDS.length ? { crmKinds } : {}),
|
|
71
|
+
...(viewers.length ? { crmViewers: viewers } : {}),
|
|
60
72
|
...(opts.source === undefined ? {} : { source: opts.source }),
|
|
61
|
-
...(
|
|
73
|
+
...(alone ? { scope: { crm: [run.account] } } : {}),
|
|
62
74
|
}, { app, hintCommand: commandAt(rootOf(leaf), "models download") ? app.command : null }, undefined, meaningLane({ env, messenger: { app }, store: async () => run.store }));
|
|
63
75
|
const note = (text) => outputFor(leaf).renderer.note(text);
|
|
64
76
|
for (const { why } of denied)
|
package/dist/crm/source.d.ts
CHANGED
|
@@ -142,8 +142,9 @@ export interface CrmSource {
|
|
|
142
142
|
/**
|
|
143
143
|
* The kind's entries into a stage that the CRM's own history recorded, oldest first, a page at a time: after the
|
|
144
144
|
* entry `after` (its `externalId`), or from `since` (epoch ms) without it; the last page's `cursor` is the last entry
|
|
145
|
-
* read, or `after` when none came.
|
|
146
|
-
*
|
|
145
|
+
* read, or `after` when none came. Read on every sync, 90 days back at first: the funnel and the changes count each
|
|
146
|
+
* stage entered, one passed between two syncs too, and stage tasks open on them. An entry on a deal outside the copy
|
|
147
|
+
* is kept and counts once the deal arrives.
|
|
147
148
|
*/
|
|
148
149
|
stageHistory?(kind: DealKind, since: number, after?: string, context?: CrmCallContext): Promise<CrmPage<CrmStageEntry>>;
|
|
149
150
|
}
|
package/dist/crm/stage-tasks.js
CHANGED
|
@@ -12,7 +12,7 @@ export const openStageTasks = async (store, account, now, { history = false } =
|
|
|
12
12
|
return 0;
|
|
13
13
|
const changes = await store.crm.stageMoves(account);
|
|
14
14
|
// A CRM with a stage history names every stage entered; the copy's own changes miss one passed between two syncs.
|
|
15
|
-
const moves = history ? await store.crm.stageEntries(account) : changes.moves;
|
|
15
|
+
const moves = history ? await store.crm.stageEntries(account, now) : changes.moves;
|
|
16
16
|
// Every deal entering a stage gets the same title, so the duplicate review would pair tasks of different deals.
|
|
17
17
|
const { reviewDuplicates: _review, ...tasks } = store.tasks;
|
|
18
18
|
const service = createTaskService({ store: tasks, now: () => new Date(now) });
|
package/dist/crm/sync.js
CHANGED
|
@@ -6,6 +6,7 @@ import { DEAL_KINDS } from "../domain/deals.js";
|
|
|
6
6
|
import { isCliFailure } from "../errors/failures.js";
|
|
7
7
|
import { crmSettingKey } from "../store/sqlite/crm-settings.js";
|
|
8
8
|
import { ungroupedHint } from "./group-hint.js";
|
|
9
|
+
import { recordCrmViewer } from "./viewer.js";
|
|
9
10
|
const DAY = 86_400_000;
|
|
10
11
|
/** A row the CRM changed this close to the id pass's start may be newer than the list, by the two clocks' skew. */
|
|
11
12
|
const CLOCK_MARGIN = 60 * 60_000;
|
|
@@ -215,23 +216,19 @@ const readActivities = async (run) => {
|
|
|
215
216
|
}
|
|
216
217
|
};
|
|
217
218
|
const STAGE_HISTORY = "crm:stage-history:";
|
|
219
|
+
/** As far back as activities: the funnel and the changes of the last quarter count exact moves. */
|
|
220
|
+
const HISTORY_DAYS = ACTIVITY_DAYS;
|
|
218
221
|
/**
|
|
219
|
-
* The CRM's own stage history of each kind
|
|
220
|
-
*
|
|
221
|
-
* history is not read, and its place is dropped, so rules set again later start from then.
|
|
222
|
+
* The CRM's own stage history of each kind, after the last entry read, on every sync: the funnel and the stage
|
|
223
|
+
* tasks count it. A first read goes `HISTORY_DAYS` back.
|
|
222
224
|
*/
|
|
223
225
|
const readStageHistory = async (run) => {
|
|
224
226
|
const { source, store, account } = run;
|
|
225
227
|
if (!source.stageHistory)
|
|
226
228
|
return null;
|
|
227
|
-
const
|
|
228
|
-
const
|
|
229
|
-
for (const kind of DEAL_KINDS) {
|
|
229
|
+
const since = run.now - HISTORY_DAYS * DAY;
|
|
230
|
+
for (const kind of DEAL_KINDS.filter((one) => source.traits.kinds.includes(one))) {
|
|
230
231
|
const key = `${STAGE_HISTORY}${kind}`;
|
|
231
|
-
if (since === null || Object.keys(rules[kind]).length === 0) {
|
|
232
|
-
await store.sync.clearSyncState(account, key);
|
|
233
|
-
continue;
|
|
234
|
-
}
|
|
235
232
|
let after = (await store.sync.syncState(account, key))?.value;
|
|
236
233
|
for (;;) {
|
|
237
234
|
const why = run.budget.take();
|
|
@@ -251,6 +248,7 @@ const readStageHistory = async (run) => {
|
|
|
251
248
|
return "cancelled";
|
|
252
249
|
}
|
|
253
250
|
}
|
|
251
|
+
await store.crm.expireStageEntries(account);
|
|
254
252
|
return null;
|
|
255
253
|
};
|
|
256
254
|
/** Runs a step whose source call may spend the budget mid-way: a spent budget is a stop, not a failure. */
|
|
@@ -493,6 +491,7 @@ export const syncCrm = async (source, store, account, options = {}) => {
|
|
|
493
491
|
const self = source.self ? normalCrmUserId(await source.self()) : undefined;
|
|
494
492
|
if (self !== undefined && !isCrmUserId(self))
|
|
495
493
|
throw new CliError("invalid_response", `${source.provider} named its sign-in's user as "${self}", not a user id`);
|
|
494
|
+
await recordCrmViewer(source, store, account, self);
|
|
496
495
|
if (self !== undefined) {
|
|
497
496
|
const owner = await store.crm.ownUser(account);
|
|
498
497
|
if (owner === undefined)
|
|
@@ -631,6 +630,9 @@ export const syncCrm = async (source, store, account, options = {}) => {
|
|
|
631
630
|
}
|
|
632
631
|
// A relationship held back for an end a later page or kind brought is written now; it costs no request.
|
|
633
632
|
run.droppedPending += (await store.crm.retryPending(account)).dropped;
|
|
633
|
+
// Activities and the stage history name no contact: their stop leaves the rows whole, and their catch-up may take
|
|
634
|
+
// several runs.
|
|
635
|
+
const rowsStopped = stopped;
|
|
634
636
|
if (!stopped && kinds.some(isItemKind)) {
|
|
635
637
|
try {
|
|
636
638
|
stopped = await stopping(() => readActivities(run));
|
|
@@ -664,7 +666,7 @@ export const syncCrm = async (source, store, account, options = {}) => {
|
|
|
664
666
|
const narrowed = report.synced.some((one) => one.outOfScope > 0);
|
|
665
667
|
const whole = report.synced.every((one) => one.state === "complete" && one.error === undefined && !one.held && one.pass !== "held");
|
|
666
668
|
// Contacts have no scope of their own and no deletion check: they leave with the last deal or lead naming them.
|
|
667
|
-
if (scope.mode === "mine" && (options.rescan || narrowed) && whole && !
|
|
669
|
+
if (scope.mode === "mine" && (options.rescan || narrowed) && whole && !rowsStopped && kinds.includes("person"))
|
|
668
670
|
report.contactsRemoved = await store.crm.removeContacts(account, { users: scopeIds(scope) ?? [], before: now });
|
|
669
671
|
if (run.unlistedUsers)
|
|
670
672
|
report.issues.push({
|
|
@@ -117,9 +117,13 @@ export const crmWriteContract = (make) => {
|
|
|
117
117
|
for (const kind of ["person", "organization"].filter((one) => writer.capabilities.kinds.includes(one))) {
|
|
118
118
|
const made = kind === "person"
|
|
119
119
|
? await writer.create(kind, { firstName: "Anna", lastName: "Example", emails: ["anna@example.com"] }, operation())
|
|
120
|
-
: await writer.create(kind, { name: "Example contract company" }, operation());
|
|
120
|
+
: await writer.create(kind, { name: "Example contract company", phones: ["+34600000003"], emails: ["company@example.com"] }, operation());
|
|
121
121
|
assert.ok(made.externalId, `${kind}: an id`);
|
|
122
122
|
assert.ok(made.name, `${kind}: a name`);
|
|
123
|
+
if (kind === "organization") {
|
|
124
|
+
assert.ok((made.phones ?? []).includes("+34600000003"), "organization: its phone");
|
|
125
|
+
assert.ok((made.emails ?? []).includes("company@example.com"), "organization: its e-mail");
|
|
126
|
+
}
|
|
123
127
|
if (!writer.capabilities.verbs.includes("update"))
|
|
124
128
|
continue;
|
|
125
129
|
const changed = kind === "person"
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { CopyViewer, MessageStore } from "../store/store.js";
|
|
2
|
+
import type { AccountKey } from "../store/types.js";
|
|
3
|
+
import type { CrmSource } from "./source.js";
|
|
4
|
+
/**
|
|
5
|
+
* What the sync's sign-in may read of the copy offline, asked while the sync is online anyway. Saved before the copy's
|
|
6
|
+
* owner is checked, so another administrator's refused sync still records it; a failed answer records no.
|
|
7
|
+
*/
|
|
8
|
+
export declare const recordCrmViewer: (source: CrmSource, store: MessageStore, account: AccountKey, self: string | undefined) => Promise<void>;
|
|
9
|
+
export interface CrmViewing {
|
|
10
|
+
/** Undefined: the whole copy. */
|
|
11
|
+
viewer: CopyViewer | undefined;
|
|
12
|
+
/** Why the profile sees less than its rule would give it, and what to do; the answer is then incomplete. */
|
|
13
|
+
note?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Whose deals and leads of the copy a profile reads offline (SEC-197): every one when the CRM limits nobody or a sync
|
|
17
|
+
* found `user` its administrator; otherwise, a copy no sync recorded the rule of too, only those assigned to `user`,
|
|
18
|
+
* and none when `user` is unknown. The network is never asked.
|
|
19
|
+
*/
|
|
20
|
+
export declare const crmViewerOf: (store: MessageStore, account: AccountKey, user: string | null) => Promise<CrmViewing>;
|
|
21
|
+
/** What a command says beside an answer the viewer rule left short, by the run it read for. */
|
|
22
|
+
export declare const shortened: WeakMap<object, string>;
|
|
23
|
+
/** `crmViewerOf` for one command's run; its note waits in `shortened` for the answer. */
|
|
24
|
+
export declare const viewerOf: (run: {
|
|
25
|
+
store: MessageStore;
|
|
26
|
+
account: AccountKey;
|
|
27
|
+
user: string | null;
|
|
28
|
+
}) => Promise<CopyViewer | undefined>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { normalCrmUserId } from "../domain/crm.js";
|
|
2
|
+
import { crmIdentityId } from "../store/sqlite/crm-rows.js";
|
|
3
|
+
// A sync state, not a CRM setting: `config set` must never make a profile the administrator.
|
|
4
|
+
const VIEWERS = "crm:viewers";
|
|
5
|
+
const viewersOf = async (store, account) => {
|
|
6
|
+
try {
|
|
7
|
+
const found = JSON.parse((await store.sync.syncState(account, VIEWERS))?.value ?? "null");
|
|
8
|
+
if (!found || typeof found.limited !== "boolean")
|
|
9
|
+
return undefined;
|
|
10
|
+
return { limited: found.limited, admins: typeof found.admins === "object" && found.admins ? found.admins : {} };
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* What the sync's sign-in may read of the copy offline, asked while the sync is online anyway. Saved before the copy's
|
|
18
|
+
* owner is checked, so another administrator's refused sync still records it; a failed answer records no.
|
|
19
|
+
*/
|
|
20
|
+
export const recordCrmViewer = async (source, store, account, self) => {
|
|
21
|
+
const limited = self !== undefined || source.isAdmin !== undefined;
|
|
22
|
+
const known = await viewersOf(store, account);
|
|
23
|
+
const admins = { ...(known?.admins ?? {}) };
|
|
24
|
+
if (self !== undefined)
|
|
25
|
+
admins[self] = source.isAdmin ? await source.isAdmin().catch(() => false) : false;
|
|
26
|
+
await store.sync.setSyncState(account, VIEWERS, JSON.stringify({ limited, admins }));
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Whose deals and leads of the copy a profile reads offline (SEC-197): every one when the CRM limits nobody or a sync
|
|
30
|
+
* found `user` its administrator; otherwise, a copy no sync recorded the rule of too, only those assigned to `user`,
|
|
31
|
+
* and none when `user` is unknown. The network is never asked.
|
|
32
|
+
*/
|
|
33
|
+
export const crmViewerOf = async (store, account, user) => {
|
|
34
|
+
const known = await viewersOf(store, account);
|
|
35
|
+
if (known?.limited === false)
|
|
36
|
+
return { viewer: undefined };
|
|
37
|
+
const id = user === null ? undefined : normalCrmUserId(user);
|
|
38
|
+
if (id !== undefined && known?.admins[id] === true)
|
|
39
|
+
return { viewer: undefined };
|
|
40
|
+
if (id === undefined)
|
|
41
|
+
return {
|
|
42
|
+
viewer: { account, assignee: null },
|
|
43
|
+
note: "this profile's sign-in names no CRM user, so offline reads show none of the copy's deals and leads: sign in " +
|
|
44
|
+
"again (session start) so the sign-in keeps its user",
|
|
45
|
+
};
|
|
46
|
+
const viewer = { account, assignee: crmIdentityId(account, id, "user") };
|
|
47
|
+
return known
|
|
48
|
+
? { viewer }
|
|
49
|
+
: {
|
|
50
|
+
viewer,
|
|
51
|
+
note: "no sync of this version recorded who may read this copy, so offline reads show only your own deals and " +
|
|
52
|
+
"leads: run sync once",
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
/** What a command says beside an answer the viewer rule left short, by the run it read for. */
|
|
56
|
+
export const shortened = new WeakMap();
|
|
57
|
+
/** `crmViewerOf` for one command's run; its note waits in `shortened` for the answer. */
|
|
58
|
+
export const viewerOf = async (run) => {
|
|
59
|
+
const { viewer, note } = await crmViewerOf(run.store, run.account, run.user);
|
|
60
|
+
if (note)
|
|
61
|
+
shortened.set(run, note);
|
|
62
|
+
return viewer;
|
|
63
|
+
};
|
package/dist/crm/writer.d.ts
CHANGED
|
@@ -59,6 +59,9 @@ export interface CrmPersonFields {
|
|
|
59
59
|
/** A company's fields; `null` clears one. */
|
|
60
60
|
export interface CrmOrganizationFields {
|
|
61
61
|
name?: string;
|
|
62
|
+
/** Each list replaces the CRM's whole list. */
|
|
63
|
+
emails?: string[];
|
|
64
|
+
phones?: string[];
|
|
62
65
|
owner?: string | null;
|
|
63
66
|
custom?: Record<string, string | string[] | null>;
|
|
64
67
|
}
|
package/dist/crm/writes.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type { AppIdentity } from "../ports/app.js";
|
|
|
5
5
|
import type { Warn } from "../runs/events.js";
|
|
6
6
|
import { type Asker, type GuardSettings } from "../sends/guard.js";
|
|
7
7
|
import { type PermissionKey } from "../sends/permissions.js";
|
|
8
|
-
import type { Deal, PipelineReport } from "../store/sqlite/deals.js";
|
|
8
|
+
import type { Deal, PipelineOptions, PipelineReport } from "../store/sqlite/deals.js";
|
|
9
9
|
import type { MessageStore } from "../store/store.js";
|
|
10
10
|
import type { AccountKey } from "../store/types.js";
|
|
11
11
|
import type { CrmSource } from "./source.js";
|
|
@@ -58,7 +58,7 @@ export interface CrmFieldsInput {
|
|
|
58
58
|
custom?: readonly (readonly [name: string, value: string])[];
|
|
59
59
|
}
|
|
60
60
|
/** The named pipeline of this profile's CRM account, or its only one; several and none named is refused. */
|
|
61
|
-
export declare const pipelineOf: (context: Pick<CrmWriteContext, "store" | "account">, kind: DealKind | undefined, reference: string | undefined, param?: string) => Promise<PipelineReport>;
|
|
61
|
+
export declare const pipelineOf: (context: Pick<CrmWriteContext, "store" | "account">, kind: DealKind | undefined, reference: string | undefined, param?: string, window?: Pick<PipelineOptions, "since" | "until" | "zone" | "viewer">) => Promise<PipelineReport>;
|
|
62
62
|
/** What the field lookup reads of a field definition: a CRM CLI's own type fits. */
|
|
63
63
|
export type NamedField = Pick<CrmField, "code" | "label" | "options">;
|
|
64
64
|
/**
|
|
@@ -123,6 +123,11 @@ export interface CrmRecordWritten<K extends CrmRecordKind> {
|
|
|
123
123
|
record: K extends "person" ? CrmPerson : CrmOrganization;
|
|
124
124
|
/** Whether the copy keeps it: only the sign-in's own clients are stored (NEED-2661). */
|
|
125
125
|
stored: boolean;
|
|
126
|
+
/**
|
|
127
|
+
* After an update that gave it to someone else, whether the copy let it go: `false` when it stays, because the
|
|
128
|
+
* owner decided about the contact, something else points at it, or one of the owner's deals names it.
|
|
129
|
+
*/
|
|
130
|
+
removed?: boolean;
|
|
126
131
|
}
|
|
127
132
|
/** A CRM id as a list prints it; shared with the commands, not exported from the package. */
|
|
128
133
|
export declare const RECORD_ID: RegExp;
|
package/dist/crm/writes.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CliError, isCliError } from "@wirecat/cli-base";
|
|
2
2
|
import { currencyOf, minorUnits } from "../domain/money.js";
|
|
3
|
+
import { e164Of } from "../domain/phones.js";
|
|
3
4
|
import { foldName } from "../resolve.js";
|
|
4
5
|
import { guardFor } from "../sends/guard.js";
|
|
5
6
|
import { guardedWrite } from "../sends/guarded.js";
|
|
@@ -73,8 +74,8 @@ const copyOf = async (context, kind, reference) => {
|
|
|
73
74
|
return found;
|
|
74
75
|
};
|
|
75
76
|
/** The named pipeline of this profile's CRM account, or its only one; several and none named is refused. */
|
|
76
|
-
export const pipelineOf = async (context, kind, reference, param = "--pipeline") => {
|
|
77
|
-
const scope = { ...(kind ? { kind } : {}), account: context.account };
|
|
77
|
+
export const pipelineOf = async (context, kind, reference, param = "--pipeline", window = {}) => {
|
|
78
|
+
const scope = { ...window, ...(kind ? { kind } : {}), account: context.account };
|
|
78
79
|
if (reference !== undefined) {
|
|
79
80
|
const found = await context.store.deals.pipeline(reference, scope);
|
|
80
81
|
if (found.externalId === null)
|
|
@@ -377,7 +378,7 @@ export const removeCrmItem = async (context, kind, reference, options = {}) => {
|
|
|
377
378
|
const EMAIL = /^[^\s@]{1,64}@[^\s@]{1,255}$/u;
|
|
378
379
|
/** A CRM id as a list prints it; shared with the commands, not exported from the package. */
|
|
379
380
|
export const RECORD_ID = /^[\w.:-]{1,100}$/u;
|
|
380
|
-
const PERSON_ONLY = ["firstName", "lastName", "position"
|
|
381
|
+
const PERSON_ONLY = ["firstName", "lastName", "position"];
|
|
381
382
|
const recordInvalid = (message, param) => new CliError("validation_error", message, { reason: "crm.record-field", ...(param ? { param } : {}) });
|
|
382
383
|
const cleared = (value) => (value === undefined ? undefined : value.trim() || null);
|
|
383
384
|
const listed = (values, param, valid) => {
|
|
@@ -391,7 +392,7 @@ const listed = (values, param, valid) => {
|
|
|
391
392
|
throw recordInvalid(`${param} got ${one}`, param);
|
|
392
393
|
return all;
|
|
393
394
|
};
|
|
394
|
-
/** Typed fields into the port's; a company takes
|
|
395
|
+
/** Typed fields into the port's; a company takes no name parts and no position. */
|
|
395
396
|
const recordFieldsOf = (kind, input) => {
|
|
396
397
|
if (kind === "organization") {
|
|
397
398
|
const given = PERSON_ONLY.find((one) => input[one] !== undefined);
|
|
@@ -413,7 +414,12 @@ const recordFieldsOf = (kind, input) => {
|
|
|
413
414
|
if (position !== undefined)
|
|
414
415
|
fields.position = position;
|
|
415
416
|
const emails = listed(input.emails, "--email", EMAIL);
|
|
416
|
-
const phones = listed(input.phones, "--phone")
|
|
417
|
+
const phones = listed(input.phones, "--phone")?.map((phone) => {
|
|
418
|
+
const international = e164Of(phone, undefined);
|
|
419
|
+
if (typeof international !== "string")
|
|
420
|
+
throw recordInvalid(`--phone takes a number in international form, starting with +: not ${phone}`, "--phone");
|
|
421
|
+
return international;
|
|
422
|
+
});
|
|
417
423
|
if (emails)
|
|
418
424
|
fields.emails = emails;
|
|
419
425
|
if (phones)
|
|
@@ -441,25 +447,24 @@ const keptRecord = async (context, kind, record) => {
|
|
|
441
447
|
}
|
|
442
448
|
};
|
|
443
449
|
/**
|
|
444
|
-
* A company the update gave to someone else leaves the copy (NEED-2661), as `removeCrmItem` removes a deal
|
|
445
|
-
*
|
|
446
|
-
* phones and e-mails it was saved with.
|
|
450
|
+
* A contact or company the update gave to someone else leaves the copy (NEED-2661), as `removeCrmItem` removes a deal:
|
|
451
|
+
* a contact with its identity, which held the phones and e-mails it was saved with.
|
|
447
452
|
*/
|
|
448
|
-
const forgotten = async (context, kind, record) =>
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
};
|
|
453
|
+
const forgotten = async (context, kind, record) => (kind === "organization"
|
|
454
|
+
? await context.store.crm.remove(context.account, kind, [record.externalId])
|
|
455
|
+
: (await context.store.crm.removeContact(context.account, record.externalId)).contacts) > 0;
|
|
452
456
|
const writeRecord = async (context, kind, verb, fields, operationId, act) => {
|
|
453
457
|
if (!context.source)
|
|
454
458
|
throw new CliError("configuration_error", "a contact or company write needs the CRM source to tell your clients");
|
|
455
459
|
const rule = foreignContacts(context.source(), undefined);
|
|
456
460
|
const record = await written(context, keysOf(context, kind, verb, { owner: fields.owner }), operationId, act);
|
|
457
461
|
const stored = await rule.own(kind, record);
|
|
462
|
+
let removed;
|
|
458
463
|
try {
|
|
459
464
|
if (stored)
|
|
460
465
|
await keptRecord(context, kind, record);
|
|
461
466
|
else if (verb === "update")
|
|
462
|
-
await forgotten(context, kind, record);
|
|
467
|
+
removed = await forgotten(context, kind, record);
|
|
463
468
|
}
|
|
464
469
|
catch (error) {
|
|
465
470
|
if (isCliError(error))
|
|
@@ -467,7 +472,12 @@ const writeRecord = async (context, kind, verb, fields, operationId, act) => {
|
|
|
467
472
|
throw error;
|
|
468
473
|
}
|
|
469
474
|
const [shown] = stored ? [record] : await rule.strip(kind, [record]);
|
|
470
|
-
return {
|
|
475
|
+
return {
|
|
476
|
+
operationId,
|
|
477
|
+
record: shown,
|
|
478
|
+
stored,
|
|
479
|
+
...(removed === undefined ? {} : { removed }),
|
|
480
|
+
};
|
|
471
481
|
};
|
|
472
482
|
/** A new contact or company in the CRM, through the guarded path; the CRM's answer is stored as a sync stores it. */
|
|
473
483
|
export const createCrmRecord = async (context, kind, input, options = {}) => {
|
package/dist/domain/crm.d.ts
CHANGED
|
@@ -104,8 +104,8 @@ export interface CrmPerson {
|
|
|
104
104
|
/** The CRM user the contact is assigned to: under `mine`, one assigned to a user in scope stays with no deal. */
|
|
105
105
|
assignee?: string | null;
|
|
106
106
|
/**
|
|
107
|
-
* What a live read (`CrmSource.list`, `get`) and a write answer give beside the copy's fields; the store keeps
|
|
108
|
-
*
|
|
107
|
+
* What a live read (`CrmSource.list`, `get`) and a write answer give beside the copy's fields; the store keeps them
|
|
108
|
+
* too, a field left out keeping its stored value. Times are epoch ms; `url` is the CRM's own page for it.
|
|
109
109
|
*/
|
|
110
110
|
createdAt?: number | null;
|
|
111
111
|
updatedAt?: number | null;
|
|
@@ -133,8 +133,8 @@ export interface CrmOrganization {
|
|
|
133
133
|
/** A live read's contacts of the company, by their id. */
|
|
134
134
|
people?: string[];
|
|
135
135
|
/**
|
|
136
|
-
* What a live read (`CrmSource.list`, `get`) and a write answer give beside the copy's fields; the store
|
|
137
|
-
*
|
|
136
|
+
* What a live read (`CrmSource.list`, `get`) and a write answer give beside the copy's fields; the store keeps them,
|
|
137
|
+
* one left out keeping its stored value. `assignee` is the CRM user it is assigned to; times are epoch ms.
|
|
138
138
|
*/
|
|
139
139
|
assignee?: string | null;
|
|
140
140
|
createdAt?: number | null;
|
|
@@ -192,9 +192,9 @@ export interface CrmRecords {
|
|
|
192
192
|
export declare const CRM_ACTIVITY_KINDS: readonly ["call", "email", "meeting", "todo", "task", "comment", "other"];
|
|
193
193
|
export type CrmActivityKind = (typeof CRM_ACTIVITY_KINDS)[number];
|
|
194
194
|
/**
|
|
195
|
-
* An activity on the CRM's deals and
|
|
196
|
-
* of every kind (a source with separate lists prefixes them). Contact data the CRM keeps on it,
|
|
197
|
-
* phone number, is never part of it: it would bypass the limits on reading clients' contacts.
|
|
195
|
+
* An activity on the CRM's deals, leads, contacts and companies, as the CRM has it. `externalId` is unique among the
|
|
196
|
+
* account's activities of every kind (a source with separate lists prefixes them). Contact data the CRM keeps on it,
|
|
197
|
+
* such as a call's phone number, is never part of it: it would bypass the limits on reading clients' contacts.
|
|
198
198
|
*/
|
|
199
199
|
export interface CrmActivity {
|
|
200
200
|
externalId: string;
|
|
@@ -209,9 +209,9 @@ export interface CrmActivity {
|
|
|
209
209
|
assignee?: string | null;
|
|
210
210
|
createdAt: number;
|
|
211
211
|
updatedAt: number;
|
|
212
|
-
/** The deals and
|
|
212
|
+
/** The deals, leads, contacts and companies it is on, by the CRM's ids; one on none the copy holds is not stored. */
|
|
213
213
|
on: {
|
|
214
|
-
kind:
|
|
214
|
+
kind: CrmKind;
|
|
215
215
|
externalId: string;
|
|
216
216
|
}[];
|
|
217
217
|
}
|
package/dist/domain/moment.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export interface MomentZone {
|
|
|
6
6
|
}
|
|
7
7
|
/** A typed date or time as epoch ms, with the flag named when it is not one. */
|
|
8
8
|
export declare const calendarMoment: (wanted: string, flag: string, { zone, end }?: MomentZone) => number;
|
|
9
|
+
/** The `YYYY-MM-DD` day `at` falls on in `zone`. */
|
|
10
|
+
export declare const dayIn: (at: number, zone: string) => string;
|
|
9
11
|
export declare const MINUTE = 60000;
|
|
10
12
|
export declare const YEAR: number;
|
|
11
13
|
export interface Reading {
|
package/dist/domain/moment.js
CHANGED
|
@@ -12,6 +12,8 @@ export const calendarMoment = (wanted, flag, { zone, end = false } = {}) => {
|
|
|
12
12
|
throw new CliError("validation_error", `${flag}: "${wanted}" is not a time${alternative ? ` — ${alternative}` : ""}`);
|
|
13
13
|
}
|
|
14
14
|
};
|
|
15
|
+
/** The `YYYY-MM-DD` day `at` falls on in `zone`. */
|
|
16
|
+
export const dayIn = (at, zone) => new Intl.DateTimeFormat("en-CA", { timeZone: zone, year: "numeric", month: "2-digit", day: "2-digit" }).format(at);
|
|
15
17
|
export const MINUTE = 60_000;
|
|
16
18
|
export const YEAR = 365 * 24 * 60 * MINUTE;
|
|
17
19
|
const RELATIVE = /^(\d+)(m|h|d)$/;
|
|
@@ -147,7 +147,7 @@ const { untrusted: _, ...dealRow } = deal.entries;
|
|
|
147
147
|
/** One deal or lead: the row, its stages, client, people, project, money and what its people said in its time. */
|
|
148
148
|
export const dealContext = v.strictObject({
|
|
149
149
|
deal: v.strictObject(dealRow),
|
|
150
|
-
stages: section(v.strictObject({ from: nullableText, to: text, pipeline: reference, at: time })),
|
|
150
|
+
stages: section(v.strictObject({ from: nullableText, to: text, pipeline: reference, at: time, exact: v.boolean() })),
|
|
151
151
|
organization: v.nullable(v.strictObject({ ref: reference, name: nullableText, categories })),
|
|
152
152
|
participants: section(related),
|
|
153
153
|
owner: v.nullable(related),
|