@wirecat/cli-core 0.675.0 → 0.677.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 +104 -0
- package/dist/calendar/mcp/index.d.ts +12 -12
- package/dist/cli/command-runs.d.ts +2 -0
- package/dist/cli/command-runs.js +2 -0
- package/dist/cli/documents/words.d.ts +1 -1
- package/dist/cli/index.d.ts +2 -2
- package/dist/cli/index.js +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 +2 -0
- package/dist/meetings/mcp/index.d.ts +15 -15
- package/dist/services/metadata.d.ts +1 -1
- 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/identity-links.d.ts +73 -0
- package/dist/store/identity-links.js +14 -0
- package/dist/store/index.d.ts +1 -1
- package/dist/store/index.js +1 -1
- package/dist/store/people.d.ts +23 -1
- package/dist/store/people.js +12 -1
- package/dist/store/sqlite/agent-links.d.ts +34 -0
- package/dist/store/sqlite/agent-links.js +184 -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/identity-proposals.d.ts +2 -0
- package/dist/store/sqlite/identity-proposals.js +2 -0
- package/dist/store/sqlite/person-match-scan.d.ts +29 -0
- package/dist/store/sqlite/person-match-scan.js +4 -4
- package/dist/store/sqlite/person-merges.d.ts +3 -0
- 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
|
@@ -2,33 +2,65 @@ import { accountPk, findAccountPk } from "./accounts.js";
|
|
|
2
2
|
import { SOURCE, TABLES } from "./crm-rows.js";
|
|
3
3
|
import { write } from "./transaction.js";
|
|
4
4
|
export const STAGE_ENTRY_RECORD = "crm.stage-entry";
|
|
5
|
+
/** An entry whose deal never reached the copy is dropped after this long: the history is read this far back. */
|
|
6
|
+
export const STAGE_ENTRY_DAYS = 90;
|
|
7
|
+
/** A stage entered longer ago than this opens no task: the copy missed the deal for more than a sync or two. */
|
|
8
|
+
const STAGE_TASK_DAYS = 7;
|
|
9
|
+
const DAY = 86_400_000;
|
|
5
10
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
|
|
11
|
+
* Every stage entry of the kind's rows in one pipeline, or of one row, oldest first per row: the CRM's own history
|
|
12
|
+
* where a row has any, else the copy's diffs, never both for one row. Entries handled or expired stay history.
|
|
13
|
+
*/
|
|
14
|
+
export const stageEntriesOf = (database, kind, filter) => {
|
|
15
|
+
const one = "item" in filter;
|
|
16
|
+
const exact = database
|
|
17
|
+
.prepare(`SELECT d.id AS item_id, s.id AS stage_id, json_extract(r.fields, '$.at') AS at FROM records r ` +
|
|
18
|
+
"JOIN record_relations rr ON rr.record_id = r.id AND rr.role = 'on' AND rr.target_type = ? " +
|
|
19
|
+
`JOIN ${TABLES[kind]} d ON d.account_id = r.account_id AND d.external_id = rr.target_external_id ` +
|
|
20
|
+
"JOIN pipeline_stages s ON s.id = json_extract(r.fields, '$.stage_id') WHERE r.type = ? " +
|
|
21
|
+
(one
|
|
22
|
+
? `AND rr.target_external_id = (SELECT external_id FROM ${TABLES[kind]} WHERE id = ?) AND d.id = ? `
|
|
23
|
+
: "AND s.pipeline_id = ? ") +
|
|
24
|
+
"ORDER BY d.id, json_extract(r.fields, '$.at'), r.id")
|
|
25
|
+
.all(kind, STAGE_ENTRY_RECORD, ...(one ? [filter.item, filter.item] : [filter.pipeline]))
|
|
26
|
+
.map((row) => ({ itemId: Number(row.item_id), toStageId: Number(row.stage_id), at: Number(row.at), exact: true }));
|
|
27
|
+
const withHistory = new Set(exact.map(({ itemId }) => itemId));
|
|
28
|
+
const copied = database
|
|
29
|
+
.prepare("SELECT item_id, to_stage_id, changed_at FROM pipeline_stage_changes WHERE item_type = ? " +
|
|
30
|
+
`AND ${one ? "item_id" : "pipeline_id"} = ? ORDER BY item_id, id`)
|
|
31
|
+
.all(kind, one ? filter.item : filter.pipeline)
|
|
32
|
+
.filter((row) => !withHistory.has(Number(row.item_id)))
|
|
33
|
+
.map((row) => ({
|
|
34
|
+
itemId: Number(row.item_id),
|
|
35
|
+
toStageId: Number(row.to_stage_id),
|
|
36
|
+
at: Number(row.changed_at),
|
|
37
|
+
exact: false,
|
|
38
|
+
}));
|
|
39
|
+
return [...exact, ...copied];
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* The CRM's own record of each stage a deal or lead entered, read on every sync. An entry waits for its stage task
|
|
43
|
+
* until it is handled; one on a deal the copy does not hold yet is kept, and counts once the deal arrives.
|
|
9
44
|
*/
|
|
10
45
|
export const crmStageEntriesOver = (context) => {
|
|
11
46
|
const { database, now } = context;
|
|
12
|
-
/** Answers how many were new; an entry
|
|
47
|
+
/** Answers how many were new; an entry into a stage the copy does not hold is not kept. */
|
|
13
48
|
const save = (key, entries) => write(database, () => {
|
|
14
49
|
const accountId = accountPk(context, key);
|
|
15
50
|
const at = now();
|
|
16
51
|
let saved = 0;
|
|
17
52
|
for (const entry of entries) {
|
|
18
|
-
const item = database
|
|
19
|
-
.prepare(`SELECT id FROM ${TABLES[entry.kind]} WHERE account_id = ? AND external_id = ? AND deleted_at IS NULL`)
|
|
20
|
-
.get(accountId, entry.item);
|
|
21
53
|
const stage = database
|
|
22
54
|
.prepare("SELECT s.id FROM pipeline_stages s JOIN pipelines p ON p.id = s.pipeline_id " +
|
|
23
55
|
"WHERE p.account_id = ? AND p.kind = ? AND p.external_id = ? AND s.external_id = ?")
|
|
24
56
|
.get(accountId, entry.kind, entry.pipeline, entry.stage);
|
|
25
|
-
if (!
|
|
57
|
+
if (!stage)
|
|
26
58
|
continue;
|
|
27
59
|
const externalId = `${entry.kind}/${entry.externalId}`;
|
|
28
60
|
const inserted = database
|
|
29
61
|
.prepare("INSERT INTO records (account_id, type, external_id, fields, fingerprint, batch_id, created_at, updated_at) " +
|
|
30
62
|
"VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT DO NOTHING RETURNING id")
|
|
31
|
-
.get(accountId, STAGE_ENTRY_RECORD, externalId, JSON.stringify({ kind: entry.kind,
|
|
63
|
+
.get(accountId, STAGE_ENTRY_RECORD, externalId, JSON.stringify({ kind: entry.kind, item: entry.item, stage_id: Number(stage.id), at: entry.at }), externalId, SOURCE, at, at);
|
|
32
64
|
if (!inserted)
|
|
33
65
|
continue;
|
|
34
66
|
database
|
|
@@ -39,19 +71,27 @@ export const crmStageEntriesOver = (context) => {
|
|
|
39
71
|
}
|
|
40
72
|
return saved;
|
|
41
73
|
});
|
|
42
|
-
/**
|
|
43
|
-
|
|
74
|
+
/**
|
|
75
|
+
* The entries on rows the copy holds whose stage task is not handled yet, made at or after `since` and within
|
|
76
|
+
* `STAGE_TASK_DAYS` of `at`, oldest first, as stage moves whose task source names the CRM's history row.
|
|
77
|
+
*/
|
|
78
|
+
const pending = (key, since, at) => {
|
|
44
79
|
const accountId = findAccountPk(context, key);
|
|
45
80
|
if (accountId === undefined)
|
|
46
81
|
return [];
|
|
47
82
|
return database
|
|
48
|
-
.prepare("SELECT r.id, r.external_id, json_extract(r.fields, '$.kind') AS kind, " +
|
|
49
|
-
"
|
|
50
|
-
"
|
|
83
|
+
.prepare("SELECT r.id, r.external_id, json_extract(r.fields, '$.kind') AS kind, coalesce(d.id, l.id) AS item_id, " +
|
|
84
|
+
"s.external_id AS stage, s.name AS stage_name, p.name AS pipeline_name FROM records r " +
|
|
85
|
+
"JOIN record_relations rr ON rr.record_id = r.id AND rr.role = 'on' " +
|
|
86
|
+
"LEFT JOIN deals d ON rr.target_type = 'deal' AND d.account_id = r.account_id " +
|
|
87
|
+
"AND d.external_id = rr.target_external_id AND d.deleted_at IS NULL " +
|
|
88
|
+
"LEFT JOIN leads l ON rr.target_type = 'lead' AND l.account_id = r.account_id " +
|
|
89
|
+
"AND l.external_id = rr.target_external_id AND l.deleted_at IS NULL " +
|
|
51
90
|
"JOIN pipeline_stages s ON s.id = json_extract(r.fields, '$.stage_id') JOIN pipelines p ON p.id = s.pipeline_id " +
|
|
52
|
-
"WHERE r.account_id = ? AND r.type = ? AND r.deleted_at IS NULL " +
|
|
91
|
+
"WHERE r.account_id = ? AND r.type = ? AND r.deleted_at IS NULL AND coalesce(d.id, l.id) IS NOT NULL " +
|
|
92
|
+
"AND json_extract(r.fields, '$.handled') IS NULL AND json_extract(r.fields, '$.at') >= ? " +
|
|
53
93
|
"ORDER BY json_extract(r.fields, '$.at'), r.id")
|
|
54
|
-
.all(accountId, STAGE_ENTRY_RECORD)
|
|
94
|
+
.all(accountId, STAGE_ENTRY_RECORD, Math.max(since, at - STAGE_TASK_DAYS * DAY))
|
|
55
95
|
.map((row) => {
|
|
56
96
|
const ref = `${String(row.kind)}:${String(row.item_id)}`;
|
|
57
97
|
return {
|
|
@@ -68,7 +108,26 @@ export const crmStageEntriesOver = (context) => {
|
|
|
68
108
|
const handled = (ids) => write(database, () => {
|
|
69
109
|
const at = now();
|
|
70
110
|
for (const id of ids)
|
|
71
|
-
database
|
|
111
|
+
database
|
|
112
|
+
.prepare("UPDATE records SET fields = json_set(fields, '$.handled', 1), updated_at = ? WHERE id = ?")
|
|
113
|
+
.run(at, id);
|
|
114
|
+
});
|
|
115
|
+
/** Entries no stored row has claimed for `STAGE_ENTRY_DAYS`: their deal is outside the copy for good. */
|
|
116
|
+
const expire = (key) => write(database, () => {
|
|
117
|
+
const accountId = findAccountPk(context, key);
|
|
118
|
+
if (accountId === undefined)
|
|
119
|
+
return 0;
|
|
120
|
+
const at = now();
|
|
121
|
+
return database
|
|
122
|
+
.prepare("UPDATE records SET deleted_at = ? WHERE account_id = ? AND type = ? AND deleted_at IS NULL " +
|
|
123
|
+
"AND json_extract(fields, '$.handled') IS NULL AND json_extract(fields, '$.at') < ? " +
|
|
124
|
+
"AND NOT EXISTS (SELECT 1 FROM record_relations rr JOIN deals d ON rr.target_type = 'deal' " +
|
|
125
|
+
"AND d.account_id = records.account_id AND d.external_id = rr.target_external_id " +
|
|
126
|
+
"WHERE rr.record_id = records.id) " +
|
|
127
|
+
"AND NOT EXISTS (SELECT 1 FROM record_relations rr JOIN leads l ON rr.target_type = 'lead' " +
|
|
128
|
+
"AND l.account_id = records.account_id AND l.external_id = rr.target_external_id " +
|
|
129
|
+
"WHERE rr.record_id = records.id)")
|
|
130
|
+
.run(at, accountId, STAGE_ENTRY_RECORD, at - STAGE_ENTRY_DAYS * DAY).changes;
|
|
72
131
|
});
|
|
73
|
-
return { save, pending, handled };
|
|
132
|
+
return { save, pending, handled, expire };
|
|
74
133
|
};
|
|
@@ -97,13 +97,18 @@ export interface CrmCopies {
|
|
|
97
97
|
through: number;
|
|
98
98
|
}>;
|
|
99
99
|
markStageMoves(key: AccountKey, through: number): Promise<void>;
|
|
100
|
-
/** When stage
|
|
100
|
+
/** When the stage-task rules were first set; null without rules. */
|
|
101
101
|
stageTasksSince(key: AccountKey): Promise<number | null>;
|
|
102
|
-
/** Entries from the CRM's stage history on
|
|
102
|
+
/** Entries from the CRM's stage history, on deals and leads stored or not yet; answers how many were new. */
|
|
103
103
|
saveStageEntries(key: AccountKey, entries: readonly CrmStageEntry[]): Promise<number>;
|
|
104
|
-
/**
|
|
105
|
-
|
|
104
|
+
/**
|
|
105
|
+
* The history entries on stored rows whose task is not handled yet, made since the rules were first set and within
|
|
106
|
+
* `STAGE_TASK_DAYS` of `now`: a deal that joins the copy late opens tasks for its recent moves only.
|
|
107
|
+
*/
|
|
108
|
+
stageEntries(key: AccountKey, now: number): Promise<CrmStageMove[]>;
|
|
106
109
|
handleStageEntries(ids: readonly number[]): Promise<void>;
|
|
110
|
+
/** Drops the history entries no stored row claimed within `STAGE_ENTRY_DAYS`; answers how many. */
|
|
111
|
+
expireStageEntries(key: AccountKey): Promise<number>;
|
|
107
112
|
/** Whether the owner turned on reading activities and tasks (`activities` setting). */
|
|
108
113
|
activitiesOn(key: AccountKey): Promise<boolean>;
|
|
109
114
|
/** Activities and tasks, each on the kept deals and leads it names; one on none of them is not stored. */
|
|
@@ -127,6 +132,11 @@ export interface CrmCopies {
|
|
|
127
132
|
before: number;
|
|
128
133
|
dryRun?: boolean;
|
|
129
134
|
}): Promise<CrmContactsRemoved>;
|
|
135
|
+
/**
|
|
136
|
+
* Removes one contact from the copy, as `removeContacts` removes those outside the scope: a write made it another
|
|
137
|
+
* user's client, which the copy does not keep (NEED-2661). Answers what went.
|
|
138
|
+
*/
|
|
139
|
+
removeContact(key: AccountKey, externalId: string): Promise<CrmContactsRemoved>;
|
|
130
140
|
/** How many of the kind the account holds, not deleted; every contact it ever saved. */
|
|
131
141
|
count(key: AccountKey, kind: CrmKind): Promise<number>;
|
|
132
142
|
/** The stored copy's `deal:<id>` or `lead:<id>`, deleted or not; `null` when the store holds none. */
|
package/dist/store/sqlite/crm.js
CHANGED
|
@@ -4,9 +4,9 @@ import { DEAL_ROLES } from "../../domain/deals.js";
|
|
|
4
4
|
import { recordFingerprint } from "../../domain/records.js";
|
|
5
5
|
import { accountPk, findAccountPk } from "./accounts.js";
|
|
6
6
|
import { suggestBaseStages } from "./base-suggest.js";
|
|
7
|
-
import { crmActivitiesOver } from "./crm-activities.js";
|
|
7
|
+
import { crmActivitiesOver, forgetActivitiesOn, } from "./crm-activities.js";
|
|
8
8
|
import { removeCrmContacts } from "./crm-contacts.js";
|
|
9
|
-
import { crmAccountPrefix, crmIdentityId, crmRowsOver, SOURCE, TABLES, textOf } from "./crm-rows.js";
|
|
9
|
+
import { crmAccountPrefix, crmIdentityId, crmRowsOver, PERSON_RECORD, SOURCE, TABLES, textOf, } from "./crm-rows.js";
|
|
10
10
|
import { crmActivitiesOn, crmCompanyTypes, crmOwnUser, crmScope, crmScopeUsers, crmSettingKey, crmStageMoves, crmStageTasks, crmStageTasksSince, saveCrmSetting, saveStageMovesMark, } from "./crm-settings.js";
|
|
11
11
|
import { crmStageEntriesOver } from "./crm-stage-entries.js";
|
|
12
12
|
import { syncReportedIn } from "./reported-relations.js";
|
|
@@ -23,6 +23,8 @@ const relationNotes = ({ issues, skippedOwnerRejected, unlistedUsers, droppedPen
|
|
|
23
23
|
...(unlistedUsers ? { unlistedUsers } : {}),
|
|
24
24
|
...(droppedPending ? { droppedPending } : {}),
|
|
25
25
|
});
|
|
26
|
+
/** The fields of a contact's record that are not its custom values. */
|
|
27
|
+
const PERSON_FIELDS = new Set(["name", "assignee", "created_at", "updated_at", "url"]);
|
|
26
28
|
/** At most this many of a contact's phones or addresses: a CRM field holds what people typed into it. */
|
|
27
29
|
const MOST_CONTACTS = 20;
|
|
28
30
|
const listOf = (values, most) => values.flatMap((value) => textOf(value, most) ?? []).slice(0, MOST_CONTACTS);
|
|
@@ -322,6 +324,46 @@ export const crmCopiesOver = (context, people) => {
|
|
|
322
324
|
"WHERE provider = ? AND external_id = ? AND json_extract(metadata, '$.assignee') IS NOT ?")
|
|
323
325
|
.run(user, user, key.provider, crmIdentityId(key, externalId), user);
|
|
324
326
|
};
|
|
327
|
+
/** Owner and times as the CRM gave them; one left out keeps what was stored. */
|
|
328
|
+
const ownerAndTimes = (key, previous, record) => {
|
|
329
|
+
const given = {
|
|
330
|
+
...(record.assignee === undefined
|
|
331
|
+
? {}
|
|
332
|
+
: { assignee: record.assignee ? crmIdentityId(key, normalCrmUserId(record.assignee), "user") : null }),
|
|
333
|
+
...(record.createdAt == null ? {} : { created_at: record.createdAt }),
|
|
334
|
+
...(record.updatedAt == null ? {} : { updated_at: record.updatedAt }),
|
|
335
|
+
};
|
|
336
|
+
return Object.fromEntries(["assignee", "created_at", "updated_at"].flatMap((field) => field in given
|
|
337
|
+
? [[field, given[field]]]
|
|
338
|
+
: field in previous
|
|
339
|
+
? [[field, previous[field]]]
|
|
340
|
+
: []));
|
|
341
|
+
};
|
|
342
|
+
/** The contact's record beside its identity: a custom value list given replaces the stored one whole. */
|
|
343
|
+
const savePersonRecord = (key, accountId, contact, at) => {
|
|
344
|
+
const held = database
|
|
345
|
+
.prepare("SELECT id, fields FROM records WHERE account_id = ? AND type = ? AND external_id = ?")
|
|
346
|
+
.get(accountId, PERSON_RECORD, contact.externalId);
|
|
347
|
+
const previous = held ? JSON.parse(String(held.fields)) : {};
|
|
348
|
+
const url = contact.url === undefined ? previous.url : contact.url;
|
|
349
|
+
const custom = contact.metadata ?? Object.fromEntries(Object.entries(previous).filter(([field]) => !PERSON_FIELDS.has(field)));
|
|
350
|
+
const fields = {
|
|
351
|
+
name: textOf(contact.name, 200),
|
|
352
|
+
...ownerAndTimes(key, previous, contact),
|
|
353
|
+
...(url ? { url } : {}),
|
|
354
|
+
...custom,
|
|
355
|
+
};
|
|
356
|
+
const fingerprint = recordFingerprint(fields, []);
|
|
357
|
+
if (held)
|
|
358
|
+
database
|
|
359
|
+
.prepare("UPDATE records SET fields = ?, fingerprint = ?, updated_at = ?, deleted_at = NULL WHERE id = ?")
|
|
360
|
+
.run(JSON.stringify(fields), fingerprint, at, Number(held.id));
|
|
361
|
+
else
|
|
362
|
+
database
|
|
363
|
+
.prepare("INSERT INTO records (account_id, type, external_id, fields, fingerprint, batch_id, created_at, updated_at) " +
|
|
364
|
+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
|
|
365
|
+
.run(accountId, PERSON_RECORD, contact.externalId, JSON.stringify(fields), fingerprint, SOURCE, at, at);
|
|
366
|
+
};
|
|
325
367
|
const saveItems = (key, kind, items, users) => write(database, () => {
|
|
326
368
|
const accountId = account(key);
|
|
327
369
|
const at = now();
|
|
@@ -347,18 +389,19 @@ export const crmCopiesOver = (context, people) => {
|
|
|
347
389
|
const at = now();
|
|
348
390
|
for (const organization of organizations) {
|
|
349
391
|
const name = textOf(organization.name, 200) ?? `${key.provider} company ${organization.externalId}`;
|
|
392
|
+
const held = database
|
|
393
|
+
.prepare("SELECT * FROM records WHERE account_id = ? AND type = 'organization' AND external_id = ?")
|
|
394
|
+
.get(accountId, organization.externalId);
|
|
395
|
+
const previous = held ? JSON.parse(String(held.fields)) : {};
|
|
350
396
|
const fields = {
|
|
351
397
|
name,
|
|
352
398
|
...(organization.url ? { url: organization.url } : {}),
|
|
353
399
|
...(organization.own ? { own: true } : {}),
|
|
354
400
|
...(organization.type === undefined ? {} : { type: organization.type }),
|
|
401
|
+
...ownerAndTimes(key, previous, organization),
|
|
355
402
|
...(organization.metadata ?? {}),
|
|
356
403
|
};
|
|
357
404
|
const fingerprint = recordFingerprint(fields, []);
|
|
358
|
-
const held = database
|
|
359
|
-
.prepare("SELECT * FROM records WHERE account_id = ? AND type = 'organization' AND external_id = ?")
|
|
360
|
-
.get(accountId, organization.externalId);
|
|
361
|
-
const previous = held ? JSON.parse(String(held.fields)) : {};
|
|
362
405
|
const before = previous.name;
|
|
363
406
|
let entity = held?.entity_type === "organization" && held.entity_id != null ? Number(held.entity_id) : null;
|
|
364
407
|
if (entity === null)
|
|
@@ -519,6 +562,8 @@ export const crmCopiesOver = (context, people) => {
|
|
|
519
562
|
for (const row of rows)
|
|
520
563
|
mark.run(at, Number(row.id));
|
|
521
564
|
const accountId = findAccountPk(context, key);
|
|
565
|
+
if (kind === "organization" && accountId !== undefined)
|
|
566
|
+
forgetActivitiesOn(database, accountId, kind, rows.map((row) => String(row.external_id)), at);
|
|
522
567
|
if (isItemKind(kind) && accountId !== undefined && rows.length) {
|
|
523
568
|
const pending = pendingBook(accountId);
|
|
524
569
|
for (const row of rows)
|
|
@@ -543,9 +588,11 @@ export const crmCopiesOver = (context, people) => {
|
|
|
543
588
|
const accountId = account(key);
|
|
544
589
|
const tally = tallied();
|
|
545
590
|
const pending = pendingBook(accountId);
|
|
591
|
+
const at = now();
|
|
546
592
|
for (const contact of contacts) {
|
|
547
593
|
if (contact.assignee !== undefined)
|
|
548
594
|
assignContact(key, contact.externalId, contact.assignee);
|
|
595
|
+
savePersonRecord(key, accountId, contact, at);
|
|
549
596
|
const person = nameUnnamed(key, contact.externalId, "contact", textOf(contact.name, 200));
|
|
550
597
|
const email = textOf(contact.email, 320)?.toLowerCase();
|
|
551
598
|
const sharing = email ? peopleSharing(key, contact.externalId, email) : 0;
|
|
@@ -614,8 +661,12 @@ export const crmCopiesOver = (context, people) => {
|
|
|
614
661
|
markStageMoves: async (key, through) => saveStageMovesMark(database, key, through, now()),
|
|
615
662
|
stageTasksSince: async (key) => crmStageTasksSince(database, key),
|
|
616
663
|
saveStageEntries: async (key, entries) => stageEntryStore.save(key, entries),
|
|
617
|
-
stageEntries: async (key) =>
|
|
664
|
+
stageEntries: async (key, at) => {
|
|
665
|
+
const since = crmStageTasksSince(database, key);
|
|
666
|
+
return since === null ? [] : stageEntryStore.pending(key, since, at);
|
|
667
|
+
},
|
|
618
668
|
handleStageEntries: async (ids) => stageEntryStore.handled(ids),
|
|
669
|
+
expireStageEntries: async (key) => stageEntryStore.expire(key),
|
|
619
670
|
activitiesOn: async (key) => crmActivitiesOn(database, key),
|
|
620
671
|
saveActivities: async (key, activities) => activityStore.save(key, activities),
|
|
621
672
|
activitiesOf: async (key, kind, externalId, limit) => activityStore.of(key, kind, externalId, limit),
|
|
@@ -635,6 +686,7 @@ export const crmCopiesOver = (context, people) => {
|
|
|
635
686
|
...options,
|
|
636
687
|
users: options.users ?? crmScopeUsers(database, key).map(normalCrmUserId),
|
|
637
688
|
}),
|
|
689
|
+
removeContact: async (key, externalId) => removeCrmContacts(context, key, { before: now(), only: [externalId] }),
|
|
638
690
|
count: async (key, kind) => kind === "person"
|
|
639
691
|
? Number(database
|
|
640
692
|
.prepare("SELECT count(*) AS n FROM identities WHERE provider = ? AND substr(external_id, 1, ?) = ?")
|
|
@@ -3,10 +3,11 @@ import type { CrmValue } from "../../domain/crm.js";
|
|
|
3
3
|
import { type DealKind, type DealRole, type DealStatus, type StageOutcome } from "../../domain/deals.js";
|
|
4
4
|
import type { AccountKey } from "../types.js";
|
|
5
5
|
import { type ProposedByFilter } from "./actors.js";
|
|
6
|
+
import { type CopyViewer } from "./copy-viewer.js";
|
|
6
7
|
import { type Author } from "./memories.js";
|
|
7
8
|
import type { StoreContext } from "./open.js";
|
|
9
|
+
import { type FunnelCounts, type PipelinePeriod } from "./pipeline-period.js";
|
|
8
10
|
import { type AuthorInfo, type Party } from "./reviewed.js";
|
|
9
|
-
import { type StageAmounts } from "./stage-amounts.js";
|
|
10
11
|
export interface PipelineStage {
|
|
11
12
|
name: string;
|
|
12
13
|
position: number;
|
|
@@ -29,32 +30,30 @@ export interface Pipeline {
|
|
|
29
30
|
stages: PipelineStage[];
|
|
30
31
|
createdAt: string;
|
|
31
32
|
}
|
|
32
|
-
export interface FunnelStage extends PipelineStage,
|
|
33
|
-
/** Rows that got this far: entered this stage or a later one that is not lost, or were won. */
|
|
34
|
-
reached: number;
|
|
35
|
-
/** `reached` of the next stage that is not lost over this stage's, 0–1; `null` for the last or an empty one. */
|
|
36
|
-
conversion: number | null;
|
|
33
|
+
export interface FunnelStage extends PipelineStage, FunnelCounts {
|
|
37
34
|
}
|
|
38
|
-
/** A pipeline with its funnel
|
|
39
|
-
export interface PipelineReport extends Omit<Pipeline, "stages"> {
|
|
35
|
+
/** A pipeline with its funnel and outcomes over a window (`pipelinePeriod`). */
|
|
36
|
+
export interface PipelineReport extends Omit<Pipeline, "stages">, Omit<PipelinePeriod, "stages"> {
|
|
40
37
|
stages: FunnelStage[];
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
lost: number;
|
|
46
|
-
winRate: number | null;
|
|
38
|
+
/** The window's bounds as ISO times; `null` is open. */
|
|
39
|
+
window: {
|
|
40
|
+
since: string | null;
|
|
41
|
+
until: string | null;
|
|
47
42
|
};
|
|
48
|
-
/** `YYYY-MM-DD`:
|
|
43
|
+
/** `YYYY-MM-DD`: the UTC day `window.since` falls on; `null` for all. */
|
|
49
44
|
since: string | null;
|
|
50
45
|
}
|
|
51
|
-
/**
|
|
46
|
+
/**
|
|
47
|
+
* One stage a deal or a lead entered: from the CRM's own history (`exact`), else as the store saw it, a CRM copy's
|
|
48
|
+
* change dated by the sync that saw it.
|
|
49
|
+
*/
|
|
52
50
|
export interface StageChange {
|
|
53
|
-
/** `null` for the stage it started in. */
|
|
51
|
+
/** `null` for the stage it started in, or the first the CRM's history names. */
|
|
54
52
|
from: string | null;
|
|
55
53
|
to: string;
|
|
56
54
|
pipeline: string;
|
|
57
55
|
at: string;
|
|
56
|
+
exact: boolean;
|
|
58
57
|
}
|
|
59
58
|
export interface DealPerson extends Party {
|
|
60
59
|
role: DealRole;
|
|
@@ -142,8 +141,20 @@ export interface DealInput {
|
|
|
142
141
|
reason?: string;
|
|
143
142
|
evidence?: string[];
|
|
144
143
|
}
|
|
144
|
+
export interface PipelineOptions {
|
|
145
|
+
kind?: DealKind;
|
|
146
|
+
since?: string;
|
|
147
|
+
until?: string;
|
|
148
|
+
zone?: string;
|
|
149
|
+
account?: DealScope;
|
|
150
|
+
viewer?: CopyViewer;
|
|
151
|
+
}
|
|
145
152
|
/** Whose pipelines: the owner's own, one CRM account's copies, or, left out, every one. */
|
|
146
153
|
export type DealScope = "own" | AccountKey;
|
|
154
|
+
export interface TimeBounds {
|
|
155
|
+
since?: number;
|
|
156
|
+
until?: number;
|
|
157
|
+
}
|
|
147
158
|
export interface DealFilter {
|
|
148
159
|
/** `deal` when left out. */
|
|
149
160
|
kind?: DealKind;
|
|
@@ -159,6 +170,15 @@ export interface DealFilter {
|
|
|
159
170
|
project?: string;
|
|
160
171
|
organization?: string;
|
|
161
172
|
proposedBy?: ProposedByFilter;
|
|
173
|
+
/** A CRM copy's assignee as the identity `<account>/user/<id>`; `null` is none. */
|
|
174
|
+
owner?: string | null;
|
|
175
|
+
/** Epoch ms bounds, inclusive, on when the CRM changed or made it. */
|
|
176
|
+
updated?: TimeBounds;
|
|
177
|
+
created?: TimeBounds;
|
|
178
|
+
/** On the day it was won or lost, the bounds' days in `zone` (the system's when left out). */
|
|
179
|
+
closed?: TimeBounds;
|
|
180
|
+
zone?: string;
|
|
181
|
+
viewer?: CopyViewer;
|
|
162
182
|
/** 1–500, 100 by default; newest written first. */
|
|
163
183
|
limit?: number;
|
|
164
184
|
/** Rows to skip before the first one answered, for the next page. */
|
|
@@ -178,6 +198,7 @@ export interface DealSearch {
|
|
|
178
198
|
accounts?: AccountKey[];
|
|
179
199
|
/** Only these kinds; absent, deals and leads. */
|
|
180
200
|
kinds?: readonly DealKind[];
|
|
201
|
+
viewers?: CopyViewer[];
|
|
181
202
|
/** 1–500, 100 by default. */
|
|
182
203
|
limit?: number;
|
|
183
204
|
offset?: number;
|
|
@@ -214,13 +235,13 @@ export interface DealsStore {
|
|
|
214
235
|
pipelines(filter?: {
|
|
215
236
|
kind?: DealKind;
|
|
216
237
|
account?: DealScope;
|
|
238
|
+
viewer?: CopyViewer;
|
|
217
239
|
}): Promise<Pipeline[]>;
|
|
218
|
-
/**
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}): Promise<PipelineReport>;
|
|
240
|
+
/**
|
|
241
|
+
* One pipeline by `pipeline:<id>` or a name only one pipeline has, with its funnel and outcomes over the window:
|
|
242
|
+
* `since` and `until` are a `YYYY-MM-DD` day or an ISO time, `zone` the owner's for closed days.
|
|
243
|
+
*/
|
|
244
|
+
pipeline(reference: string, options?: PipelineOptions): Promise<PipelineReport>;
|
|
224
245
|
add(input: DealInput): Promise<Deal>;
|
|
225
246
|
/** `deal:<id>` or `lead:<id>`; a bare id is a deal. */
|
|
226
247
|
get(reference: string): Promise<Deal>;
|
|
@@ -257,15 +278,12 @@ export interface DealsStore {
|
|
|
257
278
|
}
|
|
258
279
|
export declare const dealOperationsOver: (context: StoreContext) => {
|
|
259
280
|
addPipeline: ({ name, kind, stages, by }: PipelineInput) => Pipeline;
|
|
260
|
-
pipelines: (
|
|
281
|
+
pipelines: ({ kind, account, viewer }?: {
|
|
261
282
|
kind?: DealKind;
|
|
262
283
|
account?: DealScope;
|
|
284
|
+
viewer?: CopyViewer;
|
|
263
285
|
}) => Pipeline[];
|
|
264
|
-
pipeline: (reference: string, options?:
|
|
265
|
-
kind?: DealKind;
|
|
266
|
-
since?: string;
|
|
267
|
-
account?: DealScope;
|
|
268
|
-
}) => PipelineReport;
|
|
286
|
+
pipeline: (reference: string, options?: PipelineOptions) => PipelineReport;
|
|
269
287
|
add: (input: DealInput) => Deal;
|
|
270
288
|
get: (reference: string) => Deal;
|
|
271
289
|
stages: (reference: string) => StageChange[];
|