@wirecat/cli-core 0.676.0 → 0.678.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 +47 -0
- package/dist/background/daily-archive-timer.d.ts +1 -1
- package/dist/background/report/render.d.ts +3 -1
- package/dist/background/report/render.js +2 -2
- package/dist/background/report/schema.d.ts +7 -4
- package/dist/background/report/schema.js +23 -7
- package/dist/background/report/templates/morning-digest.email.en.liquid +3 -2
- package/dist/background/report/templates/morning-digest.email.ru.liquid +3 -2
- package/dist/background/report/templates/morning-digest.telegram.en.liquid +3 -2
- package/dist/background/report/templates/morning-digest.telegram.ru.liquid +3 -2
- package/dist/background/report/templates/morning-digest.text.en.liquid +3 -2
- package/dist/background/report/templates/morning-digest.text.ru.liquid +3 -2
- package/dist/background/report/view.js +2 -0
- package/dist/background/schedules.js +21 -11
- package/dist/cli/command-runs.d.ts +2 -0
- package/dist/cli/command-runs.js +2 -0
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.js +1 -1
- package/dist/cli/list-query.d.ts +1 -1
- package/dist/cli/list-query.js +2 -2
- package/dist/cli/setup-command.js +5 -2
- package/dist/crm/cli/activities.d.ts +17 -0
- package/dist/crm/cli/activities.js +133 -0
- package/dist/crm/cli/changes.d.ts +23 -0
- package/dist/crm/cli/changes.js +255 -0
- package/dist/crm/cli/index.d.ts +1 -0
- package/dist/crm/cli/index.js +7 -4
- package/dist/crm/digest.d.ts +27 -0
- package/dist/crm/digest.js +166 -0
- package/dist/crm/stage-tasks.d.ts +1 -1
- package/dist/crm/stage-tasks.js +1 -1
- package/dist/crm/sync.d.ts +9 -0
- package/dist/crm/sync.js +27 -0
- package/dist/crm/testing/source.d.ts +5 -1
- package/dist/crm/testing/source.js +19 -0
- package/dist/crm/testing/sync-contract.d.ts +2 -0
- package/dist/crm/testing/sync-contract.js +56 -2
- package/dist/errors/reasons.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 +3 -1
- package/dist/store/index.js +2 -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/crm-activities.d.ts +41 -1
- package/dist/store/sqlite/crm-activities.js +152 -14
- package/dist/store/sqlite/crm-changes.d.ts +84 -0
- package/dist/store/sqlite/crm-changes.js +199 -0
- package/dist/store/sqlite/crm.d.ts +2 -1
- package/dist/store/sqlite/crm.js +2 -0
- 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/package.json +1 -1
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type { CrmActivity, CrmActivityKind } from "../../domain/crm.js";
|
|
1
|
+
import type { CrmActivity, CrmActivityKind, CrmKind } from "../../domain/crm.js";
|
|
2
2
|
import type { DealKind } from "../../domain/deals.js";
|
|
3
3
|
import type { CacheDatabase } from "../driver.js";
|
|
4
4
|
import type { AccountKey } from "../types.js";
|
|
5
|
+
import { type CopyViewer } from "./copy-viewer.js";
|
|
5
6
|
import type { StoreContext } from "./open.js";
|
|
6
7
|
export declare const ACTIVITY_RECORD = "crm.activity";
|
|
7
8
|
/**
|
|
@@ -22,6 +23,40 @@ export interface StoredActivity {
|
|
|
22
23
|
createdAt: number;
|
|
23
24
|
updatedAt: number;
|
|
24
25
|
}
|
|
26
|
+
/** What `activities list` narrows by; every bound is inclusive epoch ms. */
|
|
27
|
+
export interface ActivityFilter {
|
|
28
|
+
status?: "open" | "done";
|
|
29
|
+
due?: {
|
|
30
|
+
since?: number;
|
|
31
|
+
until?: number;
|
|
32
|
+
};
|
|
33
|
+
/** Open and due before this moment. */
|
|
34
|
+
overdueAt?: number;
|
|
35
|
+
kinds?: readonly CrmActivityKind[];
|
|
36
|
+
/** On this deal, lead, contact or company, by the store's reference (`deal:12`, `person:4`, `organization:3`). */
|
|
37
|
+
on?: string;
|
|
38
|
+
/** The identity `<account>/user/<id>` it is assigned to; `null` is none. */
|
|
39
|
+
owner?: string | null;
|
|
40
|
+
/**
|
|
41
|
+
* A profile that may read only part of the copy (SEC-197): the activities on its deals and leads, and those on
|
|
42
|
+
* contacts or companies alone that are assigned to it. A hidden deal or lead is never named in `on`.
|
|
43
|
+
*/
|
|
44
|
+
viewer?: CopyViewer;
|
|
45
|
+
/** 1–500. */
|
|
46
|
+
limit: number;
|
|
47
|
+
offset: number;
|
|
48
|
+
}
|
|
49
|
+
/** One row the activity is on, with the store's reference and title when the copy holds it. */
|
|
50
|
+
export interface ActivityTarget {
|
|
51
|
+
kind: CrmKind;
|
|
52
|
+
externalId: string;
|
|
53
|
+
ref: string | null;
|
|
54
|
+
title: string | null;
|
|
55
|
+
}
|
|
56
|
+
export interface ListedActivity extends StoredActivity {
|
|
57
|
+
doneAt: number | null;
|
|
58
|
+
on: ActivityTarget[];
|
|
59
|
+
}
|
|
25
60
|
/** The subjects and texts of the activities on each deal or lead of a kind, by `<account id>\u0000<external id>`. */
|
|
26
61
|
export declare const activityTexts: (database: StoreContext["database"], kind: DealKind) => Map<string, string[]>;
|
|
27
62
|
export interface CrmActivitiesSaved {
|
|
@@ -32,4 +67,9 @@ export interface CrmActivitiesSaved {
|
|
|
32
67
|
export declare const crmActivitiesOver: (context: StoreContext) => {
|
|
33
68
|
save: (key: AccountKey, activities: readonly CrmActivity[]) => CrmActivitiesSaved;
|
|
34
69
|
of: (key: AccountKey, kind: DealKind, externalId: string, limit: number) => StoredActivity[];
|
|
70
|
+
list: (key: AccountKey, filter: ActivityFilter) => {
|
|
71
|
+
items: ListedActivity[];
|
|
72
|
+
hasMore: boolean;
|
|
73
|
+
};
|
|
74
|
+
count: (key: AccountKey, filter: Omit<ActivityFilter, "limit" | "offset">) => number;
|
|
35
75
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { recordFingerprint } from "../../domain/records.js";
|
|
2
2
|
import { accountPk, findAccountPk } from "./accounts.js";
|
|
3
|
-
import {
|
|
3
|
+
import { viewerSql } from "./copy-viewer.js";
|
|
4
|
+
import { crmAccountPrefix, crmIdentityId, SOURCE, TABLES, textOf } from "./crm-rows.js";
|
|
4
5
|
import { write } from "./transaction.js";
|
|
5
6
|
export const ACTIVITY_RECORD = "crm.activity";
|
|
6
7
|
/**
|
|
@@ -20,6 +21,17 @@ export const forgetActivitiesOn = (database, accountId, kind, externalIds, at) =
|
|
|
20
21
|
/** A CRM's text field holds what people typed or pasted, a whole e-mail thread too. */
|
|
21
22
|
const MOST_BODY = 20_000;
|
|
22
23
|
const MOST_SUBJECT = 500;
|
|
24
|
+
const activityOf = (externalId, fields) => ({
|
|
25
|
+
externalId,
|
|
26
|
+
kind: fields.kind,
|
|
27
|
+
subject: String(fields.subject ?? ""),
|
|
28
|
+
body: fields.body ?? null,
|
|
29
|
+
dueAt: fields.due_at ?? null,
|
|
30
|
+
done: fields.done === true,
|
|
31
|
+
assignee: fields.assignee ?? null,
|
|
32
|
+
createdAt: Number(fields.created_at),
|
|
33
|
+
updatedAt: Number(fields.updated_at),
|
|
34
|
+
});
|
|
23
35
|
/** The subjects and texts of the activities on each deal or lead of a kind, by `<account id>\u0000<external id>`. */
|
|
24
36
|
export const activityTexts = (database, kind) => {
|
|
25
37
|
const found = new Map();
|
|
@@ -121,20 +133,146 @@ export const crmActivitiesOver = (context) => {
|
|
|
121
133
|
"CASE WHEN json_extract(r.fields, '$.done') THEN NULL ELSE json_extract(r.fields, '$.due_at') END ASC, " +
|
|
122
134
|
"json_extract(r.fields, '$.updated_at') DESC LIMIT ?")
|
|
123
135
|
.all(accountId, ACTIVITY_RECORD, kind, externalId, limit)
|
|
124
|
-
.map((row) =>
|
|
125
|
-
|
|
136
|
+
.map((row) => activityOf(String(row.external_id), JSON.parse(String(row.fields))));
|
|
137
|
+
};
|
|
138
|
+
/** The CRM ids of the activity targets a store reference names in this account, by kind. */
|
|
139
|
+
const targetOf = (key, accountId, ref, viewer) => {
|
|
140
|
+
const [type = "", id = ""] = ref.split(":");
|
|
141
|
+
const one = (sql, ...params) => JSON.stringify(database
|
|
142
|
+
.prepare(sql)
|
|
143
|
+
.all(...params)
|
|
144
|
+
.map((row) => String(row.id)));
|
|
145
|
+
if (type === "deal" || type === "lead") {
|
|
146
|
+
const seen = viewerSql(TABLES[type], viewer);
|
|
147
|
+
return {
|
|
148
|
+
kind: type,
|
|
149
|
+
ids: one(`SELECT external_id AS id FROM ${TABLES[type]} WHERE account_id = ? AND id = ? AND ${seen.sql}`, accountId, Number(id), ...seen.params),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
if (type === "organization")
|
|
126
153
|
return {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
body: fields.body ?? null,
|
|
131
|
-
dueAt: fields.due_at ?? null,
|
|
132
|
-
done: fields.done === true,
|
|
133
|
-
assignee: fields.assignee ?? null,
|
|
134
|
-
createdAt: Number(fields.created_at),
|
|
135
|
-
updatedAt: Number(fields.updated_at),
|
|
154
|
+
kind: type,
|
|
155
|
+
ids: one("SELECT external_id AS id FROM records WHERE account_id = ? AND type = 'organization' " +
|
|
156
|
+
"AND entity_type = 'organization' AND entity_id = ? AND deleted_at IS NULL", accountId, Number(id)),
|
|
136
157
|
};
|
|
137
|
-
}
|
|
158
|
+
const prefix = `${crmAccountPrefix(key)}contact/`;
|
|
159
|
+
return {
|
|
160
|
+
kind: "person",
|
|
161
|
+
ids: one("SELECT substr(i.external_id, ?) AS id FROM identities i JOIN identity_links l ON l.identity_id = i.id " +
|
|
162
|
+
"WHERE i.provider = ? AND substr(i.external_id, 1, ?) = ? AND l.person_id = ?", prefix.length + 1, key.provider, prefix.length, prefix, Number(id)),
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
const targets = (key, accountId, recordId, viewer) => database
|
|
166
|
+
.prepare("SELECT target_type, target_external_id FROM record_relations WHERE record_id = ? AND role = 'on'")
|
|
167
|
+
.all(recordId)
|
|
168
|
+
.map((row) => {
|
|
169
|
+
const kind = String(row.target_type);
|
|
170
|
+
const externalId = String(row.target_external_id);
|
|
171
|
+
const found = kind === "deal" || kind === "lead"
|
|
172
|
+
? database
|
|
173
|
+
.prepare(`SELECT '${kind}:' || id AS ref, title FROM ${TABLES[kind]} d WHERE account_id = ? ` +
|
|
174
|
+
`AND external_id = ? AND ${viewerSql("d", viewer).sql}`)
|
|
175
|
+
.get(accountId, externalId, ...viewerSql("d", viewer).params)
|
|
176
|
+
: kind === "organization"
|
|
177
|
+
? database
|
|
178
|
+
.prepare("SELECT 'organization:' || o.id AS ref, o.name AS title FROM records r " +
|
|
179
|
+
"JOIN organizations o ON o.id = r.entity_id WHERE r.account_id = ? AND r.type = 'organization' " +
|
|
180
|
+
"AND r.entity_type = 'organization' AND r.external_id = ?")
|
|
181
|
+
.get(accountId, externalId)
|
|
182
|
+
: database
|
|
183
|
+
.prepare("SELECT 'person:' || l.person_id AS ref, coalesce(p.name, i.name) AS title FROM identities i " +
|
|
184
|
+
"LEFT JOIN identity_links l ON l.identity_id = i.id LEFT JOIN persons p ON p.id = l.person_id " +
|
|
185
|
+
"WHERE i.provider = ? AND i.external_id = ?")
|
|
186
|
+
.get(key.provider, crmIdentityId(key, externalId));
|
|
187
|
+
return {
|
|
188
|
+
kind,
|
|
189
|
+
externalId,
|
|
190
|
+
ref: found?.ref == null ? null : String(found.ref),
|
|
191
|
+
title: found?.title == null ? null : String(found.title),
|
|
192
|
+
};
|
|
193
|
+
});
|
|
194
|
+
const field = (name) => `json_extract(r.fields, '$.${name}')`;
|
|
195
|
+
const whereOf = (key, accountId, filter) => {
|
|
196
|
+
const where = [];
|
|
197
|
+
if (filter.status)
|
|
198
|
+
where.push({ sql: `${field("done")} = ?`, params: [filter.status === "done" ? 1 : 0] });
|
|
199
|
+
if (filter.due?.since !== undefined)
|
|
200
|
+
where.push({ sql: `${field("due_at")} >= ?`, params: [filter.due.since] });
|
|
201
|
+
if (filter.due?.until !== undefined)
|
|
202
|
+
where.push({ sql: `${field("due_at")} <= ?`, params: [filter.due.until] });
|
|
203
|
+
if (filter.overdueAt !== undefined)
|
|
204
|
+
where.push({ sql: `${field("done")} = 0 AND ${field("due_at")} < ?`, params: [filter.overdueAt] });
|
|
205
|
+
if (filter.kinds?.length)
|
|
206
|
+
where.push({
|
|
207
|
+
sql: `${field("kind")} IN (SELECT value FROM json_each(?))`,
|
|
208
|
+
params: [JSON.stringify(filter.kinds)],
|
|
209
|
+
});
|
|
210
|
+
if (filter.owner !== undefined)
|
|
211
|
+
where.push(filter.owner === null
|
|
212
|
+
? { sql: `${field("assignee")} IS NULL`, params: [] }
|
|
213
|
+
: { sql: `${field("assignee")} = ?`, params: [filter.owner] });
|
|
214
|
+
if (filter.on !== undefined) {
|
|
215
|
+
const target = targetOf(key, accountId, filter.on, filter.viewer);
|
|
216
|
+
where.push({
|
|
217
|
+
sql: "EXISTS (SELECT 1 FROM record_relations rr WHERE rr.record_id = r.id AND rr.role = 'on' " +
|
|
218
|
+
"AND rr.target_type = ? AND rr.target_external_id IN (SELECT value FROM json_each(?)))",
|
|
219
|
+
params: [target.kind, target.ids],
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
if (filter.viewer)
|
|
223
|
+
where.push(seenSql(filter.viewer));
|
|
224
|
+
return where;
|
|
225
|
+
};
|
|
226
|
+
/** One page of the account's activities: open ones first, overdue before undated, then the rest, newest first. */
|
|
227
|
+
const list = (key, filter) => {
|
|
228
|
+
const accountId = findAccountPk(context, key);
|
|
229
|
+
if (accountId === undefined)
|
|
230
|
+
return { items: [], hasMore: false };
|
|
231
|
+
const where = whereOf(key, accountId, filter);
|
|
232
|
+
const open = `CASE WHEN ${field("done")} THEN NULL ELSE ${field("due_at")} END`;
|
|
233
|
+
const rows = database
|
|
234
|
+
.prepare("SELECT r.id, r.external_id, r.fields FROM records r WHERE r.account_id = ? AND r.type = ? " +
|
|
235
|
+
`AND r.deleted_at IS NULL ${where.map(({ sql }) => `AND ${sql}`).join(" ")} ` +
|
|
236
|
+
`ORDER BY ${field("done")} ASC, ${field("done")} = 0 AND ${field("due_at")} IS NULL, ${open} ASC, ` +
|
|
237
|
+
`${field("updated_at")} DESC, r.id DESC LIMIT ? OFFSET ?`)
|
|
238
|
+
.all(accountId, ACTIVITY_RECORD, ...where.flatMap(({ params }) => params), filter.limit + 1, filter.offset);
|
|
239
|
+
return {
|
|
240
|
+
hasMore: rows.length > filter.limit,
|
|
241
|
+
items: rows.slice(0, filter.limit).map((row) => {
|
|
242
|
+
const fields = JSON.parse(String(row.fields));
|
|
243
|
+
return {
|
|
244
|
+
...activityOf(String(row.external_id), fields),
|
|
245
|
+
doneAt: fields.done_at ?? null,
|
|
246
|
+
on: targets(key, accountId, Number(row.id), filter.viewer),
|
|
247
|
+
};
|
|
248
|
+
}),
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
/** How many activities `filter` matches, every page together. */
|
|
252
|
+
const count = (key, filter) => {
|
|
253
|
+
const accountId = findAccountPk(context, key);
|
|
254
|
+
if (accountId === undefined)
|
|
255
|
+
return 0;
|
|
256
|
+
const where = whereOf(key, accountId, filter);
|
|
257
|
+
return Number(database
|
|
258
|
+
.prepare("SELECT count(*) AS n FROM records r WHERE r.account_id = ? AND r.type = ? AND r.deleted_at IS NULL " +
|
|
259
|
+
where.map(({ sql }) => `AND ${sql}`).join(" "))
|
|
260
|
+
.get(accountId, ACTIVITY_RECORD, ...where.flatMap(({ params }) => params))?.n ?? 0);
|
|
261
|
+
};
|
|
262
|
+
return { save, of, list, count };
|
|
263
|
+
};
|
|
264
|
+
/** An activity a viewer may read: on one of its deals or leads, or on contacts alone and assigned to it. */
|
|
265
|
+
const seenSql = (viewer) => {
|
|
266
|
+
if (viewer.assignee === null)
|
|
267
|
+
return { sql: "0", params: [] };
|
|
268
|
+
const on = (table, kind) => `EXISTS (SELECT 1 FROM record_relations rr JOIN ${table} d ON d.account_id = r.account_id ` +
|
|
269
|
+
"AND d.external_id = rr.target_external_id AND d.deleted_at IS NULL " +
|
|
270
|
+
`WHERE rr.record_id = r.id AND rr.role = 'on' AND rr.target_type = '${kind}' ` +
|
|
271
|
+
"AND json_extract(d.metadata, '$.assignee') = ?)";
|
|
272
|
+
return {
|
|
273
|
+
sql: `(${on("deals", "deal")} OR ${on("leads", "lead")} OR (NOT EXISTS (SELECT 1 FROM record_relations rr ` +
|
|
274
|
+
"WHERE rr.record_id = r.id AND rr.role = 'on' AND rr.target_type IN ('deal', 'lead')) " +
|
|
275
|
+
"AND json_extract(r.fields, '$.assignee') = ?))",
|
|
276
|
+
params: [viewer.assignee, viewer.assignee, viewer.assignee],
|
|
138
277
|
};
|
|
139
|
-
return { save, of };
|
|
140
278
|
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { CrmActivityKind, CrmKind } from "../../domain/crm.js";
|
|
2
|
+
import type { AccountKey } from "../types.js";
|
|
3
|
+
import type { CopyViewer } from "./copy-viewer.js";
|
|
4
|
+
import { type ActivityFilter, type ListedActivity } from "./crm-activities.js";
|
|
5
|
+
import type { StoreContext } from "./open.js";
|
|
6
|
+
export declare const CRM_CHANGES: readonly ["created", "moved", "won", "lost", "owner_changed", "activity_added", "activity_done", "person_added"];
|
|
7
|
+
export type CrmChangeKind = (typeof CRM_CHANGES)[number];
|
|
8
|
+
/** One thing that happened in a CRM account's copy, newest first in a page. */
|
|
9
|
+
export interface CrmChange {
|
|
10
|
+
change: CrmChangeKind;
|
|
11
|
+
at: number;
|
|
12
|
+
kind: CrmKind;
|
|
13
|
+
/** The deal, lead or contact as the store names it; `null` when the copy no longer links it. */
|
|
14
|
+
ref: string | null;
|
|
15
|
+
externalId: string;
|
|
16
|
+
title: string | null;
|
|
17
|
+
pipeline: string | null;
|
|
18
|
+
pipelineName: string | null;
|
|
19
|
+
/** A move's stages by name; an owner change's users as `<account>/user/<id>`. */
|
|
20
|
+
from: string | null;
|
|
21
|
+
to: string | null;
|
|
22
|
+
amount: string | null;
|
|
23
|
+
currency: string | null;
|
|
24
|
+
/** A move from the CRM's own stage history rather than the copy's diffs between syncs. */
|
|
25
|
+
exact: boolean;
|
|
26
|
+
/** Why a lost deal was lost, as the CRM says. */
|
|
27
|
+
reason: string | null;
|
|
28
|
+
/** The identity `<account>/user/<id>` the row is assigned to. */
|
|
29
|
+
owner: string | null;
|
|
30
|
+
activity: {
|
|
31
|
+
externalId: string;
|
|
32
|
+
kind: CrmActivityKind;
|
|
33
|
+
subject: string;
|
|
34
|
+
dueAt: number | null;
|
|
35
|
+
done: boolean;
|
|
36
|
+
} | null;
|
|
37
|
+
}
|
|
38
|
+
/** What `changes list` narrows by: `since` and `until` are inclusive epoch ms. */
|
|
39
|
+
export interface ChangeFilter {
|
|
40
|
+
since: number;
|
|
41
|
+
until: number;
|
|
42
|
+
changes?: readonly CrmChangeKind[];
|
|
43
|
+
/** The identity `<account>/user/<id>`; `null` is none. */
|
|
44
|
+
owner?: string | null;
|
|
45
|
+
/** A pipeline's store id: only the changes of its deals or leads. */
|
|
46
|
+
pipeline?: number;
|
|
47
|
+
/**
|
|
48
|
+
* A profile that may read only part of the copy (SEC-197): the deals and leads assigned to it, the activities on
|
|
49
|
+
* them, and the contacts linked to them or assigned to it.
|
|
50
|
+
*/
|
|
51
|
+
viewer?: CopyViewer;
|
|
52
|
+
limit: number;
|
|
53
|
+
offset: number;
|
|
54
|
+
}
|
|
55
|
+
/** How many changes of each kind `filter`'s window holds, every page together. */
|
|
56
|
+
export declare const crmChangeCounts: (context: StoreContext, key: AccountKey, filter: Omit<ChangeFilter, "limit" | "offset">) => Record<CrmChangeKind, number>;
|
|
57
|
+
/**
|
|
58
|
+
* Every change of an account's copy in a window, newest first, paged in SQL: deals and leads made, moved, won, lost
|
|
59
|
+
* and given another owner, activities added and done, contacts added. A move comes from the CRM's own stage history
|
|
60
|
+
* where a row has any, else from the copy's changes, never both; a move into a won or lost stage is listed as won or
|
|
61
|
+
* lost. A day the CRM gives without a time, as a closed day, is taken at its noon in UTC, the same day everywhere.
|
|
62
|
+
*/
|
|
63
|
+
export declare const crmChanges: (context: StoreContext, key: AccountKey, filter: ChangeFilter) => {
|
|
64
|
+
items: CrmChange[];
|
|
65
|
+
hasMore: boolean;
|
|
66
|
+
};
|
|
67
|
+
/** The pages `activities list`, `changes list` and the digest read of one account's copy. */
|
|
68
|
+
export interface CrmReads {
|
|
69
|
+
/** One page of the account's activities: open ones first by due time, then the rest, newest first. */
|
|
70
|
+
activities(key: AccountKey, filter: ActivityFilter): Promise<{
|
|
71
|
+
items: ListedActivity[];
|
|
72
|
+
hasMore: boolean;
|
|
73
|
+
}>;
|
|
74
|
+
/** How many activities a filter matches, every page together. */
|
|
75
|
+
activityCount(key: AccountKey, filter: Omit<ActivityFilter, "limit" | "offset">): Promise<number>;
|
|
76
|
+
/** One page of what changed in the account's copy in a window, newest first (`crmChanges`). */
|
|
77
|
+
changes(key: AccountKey, filter: ChangeFilter): Promise<{
|
|
78
|
+
items: CrmChange[];
|
|
79
|
+
hasMore: boolean;
|
|
80
|
+
}>;
|
|
81
|
+
/** How many changes of each kind a window holds (`crmChangeCounts`). */
|
|
82
|
+
changeCounts(key: AccountKey, filter: Omit<ChangeFilter, "limit" | "offset">): Promise<Record<CrmChangeKind, number>>;
|
|
83
|
+
}
|
|
84
|
+
export declare const crmReadsOver: (context: StoreContext) => CrmReads;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { formatDecimal, scaleOf } from "../../domain/money.js";
|
|
2
|
+
import { findAccountPk } from "./accounts.js";
|
|
3
|
+
import { ACTIVITY_RECORD, crmActivitiesOver } from "./crm-activities.js";
|
|
4
|
+
import { crmAccountPrefix, OWNER_CHANGE_RECORD, PERSON_RECORD } from "./crm-rows.js";
|
|
5
|
+
import { STAGE_ENTRY_RECORD } from "./crm-stage-entries.js";
|
|
6
|
+
export const CRM_CHANGES = [
|
|
7
|
+
"created",
|
|
8
|
+
"moved",
|
|
9
|
+
"won",
|
|
10
|
+
"lost",
|
|
11
|
+
"owner_changed",
|
|
12
|
+
"activity_added",
|
|
13
|
+
"activity_done",
|
|
14
|
+
"person_added",
|
|
15
|
+
];
|
|
16
|
+
const HALF_DAY = 43_200_000;
|
|
17
|
+
const DAY = 86_400_000;
|
|
18
|
+
const utcDay = (at) => new Date(at).toISOString().slice(0, 10);
|
|
19
|
+
/** The SQL of every change in `filter`'s window, unordered, and its numbered parameters (`?13`, `?14` page it). */
|
|
20
|
+
const changesQuery = (context, key, filter) => {
|
|
21
|
+
const accountId = findAccountPk(context, key);
|
|
22
|
+
if (accountId === undefined)
|
|
23
|
+
return undefined;
|
|
24
|
+
// ?15 is set when a viewer limits the read, ?16 its assignee; `= NULL` then hides every row.
|
|
25
|
+
const items = (table, kind) => `SELECT '${kind}' AS kind, d.id, d.external_id, d.status, d.created_at, d.closed_date, ` +
|
|
26
|
+
`json_extract(d.metadata, '$.assignee') AS owner, s.pipeline_id FROM ${table} d ` +
|
|
27
|
+
"JOIN pipeline_stages s ON s.id = d.stage_id WHERE d.account_id = ?1 AND d.deleted_at IS NULL " +
|
|
28
|
+
"AND (?15 IS NULL OR json_extract(d.metadata, '$.assignee') = ?16)";
|
|
29
|
+
const entries = "SELECT r.id AS record, i.kind, i.id AS item, i.owner, i.pipeline_id, " +
|
|
30
|
+
"CAST(json_extract(r.fields, '$.at') AS INTEGER) AS at, CAST(json_extract(r.fields, '$.stage_id') AS INTEGER) AS stage, " +
|
|
31
|
+
"lag(CAST(json_extract(r.fields, '$.stage_id') AS INTEGER)) OVER (PARTITION BY i.kind, i.id " +
|
|
32
|
+
"ORDER BY json_extract(r.fields, '$.at'), r.id) AS before FROM records r " +
|
|
33
|
+
"JOIN record_relations rr ON rr.record_id = r.id AND rr.role = 'on' " +
|
|
34
|
+
"JOIN items i ON i.kind = rr.target_type AND i.external_id = rr.target_external_id " +
|
|
35
|
+
"WHERE r.account_id = ?1 AND r.type = ?2 AND r.deleted_at IS NULL";
|
|
36
|
+
const closing = "(SELECT outcome FROM pipeline_stages WHERE id = m.stage) IN ('won', 'lost')";
|
|
37
|
+
const itemOf = (record) => "(SELECT i.kind || ':' || i.id FROM record_relations rr JOIN items i ON i.kind = rr.target_type " +
|
|
38
|
+
`AND i.external_id = rr.target_external_id WHERE rr.record_id = ${record} AND rr.role = 'on' LIMIT 1)`;
|
|
39
|
+
const closedAt = "(SELECT max(m.at) FROM entries m WHERE m.kind = i.kind AND m.item = i.id " +
|
|
40
|
+
"AND (SELECT outcome FROM pipeline_stages WHERE id = m.stage) = i.status)";
|
|
41
|
+
const onDealOrLead = "EXISTS (SELECT 1 FROM record_relations x WHERE x.record_id = a.id AND x.role = 'on' " +
|
|
42
|
+
"AND x.target_type IN ('deal', 'lead'))";
|
|
43
|
+
const linkedToSeen = "EXISTS (SELECT 1 FROM identities n JOIN identity_links l ON l.identity_id = n.id " +
|
|
44
|
+
"JOIN links k ON k.to_type = 'person' AND k.to_id = l.person_id AND k.deleted_at IS NULL " +
|
|
45
|
+
"JOIN items v ON v.kind = k.from_type AND v.id = k.from_id WHERE n.provider = ?17 AND n.external_id = ?18 || r.external_id)";
|
|
46
|
+
const parts = [
|
|
47
|
+
"SELECT 'created' AS change, created_at AS at, kind, id AS item, NULL AS record, NULL AS from_id, NULL AS to_id, " +
|
|
48
|
+
"0 AS exact, owner, pipeline_id FROM items",
|
|
49
|
+
`SELECT 'moved', m.at, m.kind, m.item, m.record, m.before, m.stage, 1, m.owner, m.pipeline_id FROM entries m ` +
|
|
50
|
+
`WHERE m.before IS NOT NULL AND NOT ${closing}`,
|
|
51
|
+
"SELECT 'moved', c.changed_at, i.kind, i.id, NULL, c.from_stage_id, c.to_stage_id, 0, i.owner, i.pipeline_id " +
|
|
52
|
+
"FROM pipeline_stage_changes c JOIN items i ON i.kind = c.item_type AND i.id = c.item_id " +
|
|
53
|
+
"WHERE c.from_stage_id IS NOT NULL AND c.changed_at BETWEEN ?6 AND ?7 " +
|
|
54
|
+
"AND (SELECT outcome FROM pipeline_stages WHERE id = c.to_stage_id) NOT IN ('won', 'lost') " +
|
|
55
|
+
// The CRM's own entry of this move replaces it; an entry with no earlier one in the copy cannot say where from.
|
|
56
|
+
"AND NOT EXISTS (SELECT 1 FROM entries e WHERE e.kind = i.kind AND e.item = i.id " +
|
|
57
|
+
"AND e.stage = c.to_stage_id AND e.before IS NOT NULL)",
|
|
58
|
+
// The closed day only narrows the rows; the window is checked on `at`, so a deal is listed in one window only.
|
|
59
|
+
`SELECT i.status, coalesce(${closedAt}, CAST(strftime('%s', i.closed_date) AS INTEGER) * 1000 + ${HALF_DAY}), ` +
|
|
60
|
+
`i.kind, i.id, NULL, NULL, NULL, ${closedAt} IS NOT NULL, i.owner, i.pipeline_id FROM items i ` +
|
|
61
|
+
"WHERE i.status IN ('won', 'lost') AND i.closed_date BETWEEN ?8 AND ?9",
|
|
62
|
+
"SELECT 'owner_changed', CAST(json_extract(r.fields, '$.at') AS INTEGER), i.kind, i.id, r.id, NULL, NULL, 0, " +
|
|
63
|
+
"json_extract(r.fields, '$.to'), i.pipeline_id FROM records r " +
|
|
64
|
+
"JOIN record_relations rr ON rr.record_id = r.id AND rr.role = 'on' " +
|
|
65
|
+
"JOIN items i ON i.kind = rr.target_type AND i.external_id = rr.target_external_id " +
|
|
66
|
+
"WHERE r.account_id = ?1 AND r.type = ?3 AND r.deleted_at IS NULL " +
|
|
67
|
+
"AND json_extract(r.fields, '$.at') BETWEEN ?6 AND ?7",
|
|
68
|
+
...["created_at", "done_at"].map((field) => `SELECT '${field === "created_at" ? "activity_added" : "activity_done"}', ` +
|
|
69
|
+
`CAST(json_extract(a.fields, '$.${field}') AS INTEGER), coalesce(substr(a.item, 1, instr(a.item, ':') - 1), a.target), ` +
|
|
70
|
+
"CAST(substr(a.item, instr(a.item, ':') + 1) AS INTEGER), a.id, NULL, NULL, 0, " +
|
|
71
|
+
"coalesce((SELECT owner FROM items i WHERE i.kind || ':' || i.id = a.item), " +
|
|
72
|
+
"json_extract(a.fields, '$.assignee')), (SELECT pipeline_id FROM items i WHERE i.kind || ':' || i.id = a.item) " +
|
|
73
|
+
`FROM (SELECT r.id, r.fields, ${itemOf("r.id")} AS item, (SELECT target_type FROM record_relations ` +
|
|
74
|
+
"WHERE record_id = r.id AND role = 'on' LIMIT 1) AS target FROM records r WHERE r.account_id = ?1 " +
|
|
75
|
+
`AND r.type = ?4 AND r.deleted_at IS NULL AND json_extract(r.fields, '$.${field}') ` +
|
|
76
|
+
"BETWEEN ?6 AND ?7) a " +
|
|
77
|
+
// A viewer reads an activity on its own deal or lead, or one on contacts alone that is assigned to it.
|
|
78
|
+
`WHERE ?15 IS NULL OR a.item IS NOT NULL OR (NOT ${onDealOrLead} AND json_extract(a.fields, '$.assignee') = ?16)`),
|
|
79
|
+
"SELECT 'person_added', CAST(json_extract(r.fields, '$.created_at') AS INTEGER), 'person', NULL, r.id, NULL, " +
|
|
80
|
+
"NULL, 0, json_extract(r.fields, '$.assignee'), NULL FROM records r WHERE r.account_id = ?1 " +
|
|
81
|
+
"AND r.type = ?5 AND r.deleted_at IS NULL AND json_extract(r.fields, '$.created_at') BETWEEN ?6 AND ?7 " +
|
|
82
|
+
`AND (?15 IS NULL OR json_extract(r.fields, '$.assignee') = ?16 OR ${linkedToSeen})`,
|
|
83
|
+
];
|
|
84
|
+
const where = [
|
|
85
|
+
"at BETWEEN ?6 AND ?7",
|
|
86
|
+
...(filter.changes?.length ? ["change IN (SELECT value FROM json_each(?10))"] : []),
|
|
87
|
+
...(filter.owner === undefined ? [] : [filter.owner === null ? "owner IS NULL" : "owner = ?11"]),
|
|
88
|
+
...(filter.pipeline === undefined ? [] : ["pipeline_id = ?12"]),
|
|
89
|
+
];
|
|
90
|
+
return {
|
|
91
|
+
sql: `WITH items AS (${items("deals", "deal")} UNION ALL ${items("leads", "lead")}), entries AS (${entries}) ` +
|
|
92
|
+
`SELECT * FROM (${parts.join(" UNION ALL ")}) WHERE ${where.join(" AND ")}`,
|
|
93
|
+
params: [
|
|
94
|
+
accountId,
|
|
95
|
+
STAGE_ENTRY_RECORD,
|
|
96
|
+
OWNER_CHANGE_RECORD,
|
|
97
|
+
ACTIVITY_RECORD,
|
|
98
|
+
PERSON_RECORD,
|
|
99
|
+
filter.since,
|
|
100
|
+
filter.until,
|
|
101
|
+
// A day without a time is taken at its noon in UTC, which is never more than a day from the window's days.
|
|
102
|
+
utcDay(filter.since - DAY),
|
|
103
|
+
utcDay(filter.until + DAY),
|
|
104
|
+
filter.changes?.length ? JSON.stringify(filter.changes) : null,
|
|
105
|
+
filter.owner ?? null,
|
|
106
|
+
filter.pipeline ?? null,
|
|
107
|
+
filter.limit + 1,
|
|
108
|
+
filter.offset,
|
|
109
|
+
filter.viewer ? 1 : null,
|
|
110
|
+
filter.viewer?.assignee ?? null,
|
|
111
|
+
key.provider,
|
|
112
|
+
`${crmAccountPrefix(key)}contact/`,
|
|
113
|
+
],
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
/** How many changes of each kind `filter`'s window holds, every page together. */
|
|
117
|
+
export const crmChangeCounts = (context, key, filter) => {
|
|
118
|
+
const counts = Object.fromEntries(CRM_CHANGES.map((one) => [one, 0]));
|
|
119
|
+
const query = changesQuery(context, key, { ...filter, limit: 0, offset: 0 });
|
|
120
|
+
if (!query)
|
|
121
|
+
return counts;
|
|
122
|
+
for (const row of context.database
|
|
123
|
+
.prepare(`SELECT change, count(*) AS n FROM (${query.sql}) GROUP BY change`)
|
|
124
|
+
.all(...query.params))
|
|
125
|
+
counts[row.change] = Number(row.n);
|
|
126
|
+
return counts;
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Every change of an account's copy in a window, newest first, paged in SQL: deals and leads made, moved, won, lost
|
|
130
|
+
* and given another owner, activities added and done, contacts added. A move comes from the CRM's own stage history
|
|
131
|
+
* where a row has any, else from the copy's changes, never both; a move into a won or lost stage is listed as won or
|
|
132
|
+
* lost. A day the CRM gives without a time, as a closed day, is taken at its noon in UTC, the same day everywhere.
|
|
133
|
+
*/
|
|
134
|
+
export const crmChanges = (context, key, filter) => {
|
|
135
|
+
const { database } = context;
|
|
136
|
+
const query = changesQuery(context, key, filter);
|
|
137
|
+
if (!query)
|
|
138
|
+
return { items: [], hasMore: false };
|
|
139
|
+
const rows = database
|
|
140
|
+
.prepare(`${query.sql} ORDER BY at DESC, change, kind, item DESC, record DESC LIMIT ?13 OFFSET ?14`)
|
|
141
|
+
.all(...query.params);
|
|
142
|
+
const stageName = database.prepare("SELECT name FROM pipeline_stages WHERE id = ?");
|
|
143
|
+
const nameOf = (id) => id == null ? null : (stageName.get(Number(id))?.name ?? null);
|
|
144
|
+
const itemStatements = Object.fromEntries(["deal", "lead"].map((kind) => [
|
|
145
|
+
kind,
|
|
146
|
+
database.prepare("SELECT d.external_id, d.title, d.currency, d.amount, json_extract(d.metadata, '$.closed_reason') AS reason, " +
|
|
147
|
+
`p.id AS pipeline, p.name AS pipeline_name FROM ${kind === "deal" ? "deals" : "leads"} d ` +
|
|
148
|
+
"JOIN pipeline_stages s ON s.id = d.stage_id JOIN pipelines p ON p.id = s.pipeline_id WHERE d.id = ?"),
|
|
149
|
+
]));
|
|
150
|
+
const itemRow = (kind, id) => id == null || (kind !== "deal" && kind !== "lead") ? undefined : itemStatements[kind]?.get(Number(id));
|
|
151
|
+
const recordRow = database.prepare("SELECT external_id, fields FROM records WHERE id = ?");
|
|
152
|
+
return {
|
|
153
|
+
hasMore: rows.length > filter.limit,
|
|
154
|
+
items: rows.slice(0, filter.limit).map((row) => {
|
|
155
|
+
const change = row.change;
|
|
156
|
+
const item = itemRow(String(row.kind), row.item);
|
|
157
|
+
const record = row.record == null ? undefined : recordRow.get(Number(row.record));
|
|
158
|
+
const fields = record ? JSON.parse(String(record.fields)) : {};
|
|
159
|
+
const activity = change === "activity_added" || change === "activity_done";
|
|
160
|
+
const person = change === "person_added";
|
|
161
|
+
const currency = item?.currency ?? null;
|
|
162
|
+
return {
|
|
163
|
+
change,
|
|
164
|
+
at: Number(row.at),
|
|
165
|
+
kind: row.kind,
|
|
166
|
+
ref: item ? `${String(row.kind)}:${String(row.item)}` : null,
|
|
167
|
+
externalId: String(item?.external_id ?? record?.external_id ?? ""),
|
|
168
|
+
title: item ? String(item.title) : person ? (fields.name ?? null) : null,
|
|
169
|
+
pipeline: item ? `pipeline:${String(item.pipeline)}` : null,
|
|
170
|
+
pipelineName: item ? String(item.pipeline_name) : null,
|
|
171
|
+
from: change === "owner_changed" ? (fields.from ?? null) : nameOf(row.from_id),
|
|
172
|
+
to: change === "owner_changed" ? (fields.to ?? null) : nameOf(row.to_id),
|
|
173
|
+
amount: item?.amount == null || currency === null ? null : formatDecimal(Number(item.amount), scaleOf(currency)),
|
|
174
|
+
currency,
|
|
175
|
+
exact: row.exact === 1,
|
|
176
|
+
reason: change === "lost" ? (item?.reason ?? null) : null,
|
|
177
|
+
owner: row.owner ?? null,
|
|
178
|
+
activity: activity
|
|
179
|
+
? {
|
|
180
|
+
externalId: String(record?.external_id),
|
|
181
|
+
kind: fields.kind,
|
|
182
|
+
subject: String(fields.subject ?? ""),
|
|
183
|
+
dueAt: fields.due_at ?? null,
|
|
184
|
+
done: fields.done === true,
|
|
185
|
+
}
|
|
186
|
+
: null,
|
|
187
|
+
};
|
|
188
|
+
}),
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
export const crmReadsOver = (context) => {
|
|
192
|
+
const activities = crmActivitiesOver(context);
|
|
193
|
+
return {
|
|
194
|
+
activities: async (key, filter) => activities.list(key, filter),
|
|
195
|
+
activityCount: async (key, filter) => activities.count(key, filter),
|
|
196
|
+
changes: async (key, filter) => crmChanges(context, key, filter),
|
|
197
|
+
changeCounts: async (key, filter) => crmChangeCounts(context, key, filter),
|
|
198
|
+
};
|
|
199
|
+
};
|
|
@@ -3,6 +3,7 @@ import { type DealKind } from "../../domain/deals.js";
|
|
|
3
3
|
import type { AccountPeople } from "../people.js";
|
|
4
4
|
import type { AccountKey } from "../types.js";
|
|
5
5
|
import { type CrmActivitiesSaved, type StoredActivity } from "./crm-activities.js";
|
|
6
|
+
import { type CrmReads } from "./crm-changes.js";
|
|
6
7
|
import { type CrmContactsRemoved } from "./crm-contacts.js";
|
|
7
8
|
import { type CrmSkipped } from "./crm-rows.js";
|
|
8
9
|
import { type CrmStageMove, type CrmStageTasks } from "./crm-settings.js";
|
|
@@ -35,7 +36,7 @@ export interface CrmSaved {
|
|
|
35
36
|
* nothing is ever matched by name (WC-DATA-6). Rows the CRM no longer has are marked deleted, never removed
|
|
36
37
|
* (WC-DATA-7). Every write is one transaction per call.
|
|
37
38
|
*/
|
|
38
|
-
export interface CrmCopies {
|
|
39
|
+
export interface CrmCopies extends CrmReads {
|
|
39
40
|
/** One kind's pipelines and their stages, by the CRM's ids. A stage the CRM dropped stays: deals may stand in it. */
|
|
40
41
|
savePipelines(key: AccountKey, kind: DealKind, pipelines: CrmPipeline[]): Promise<CrmSaved>;
|
|
41
42
|
/** Deals or leads; one whose stage is not stored, or whose fields the store refuses, is skipped and named. */
|
package/dist/store/sqlite/crm.js
CHANGED
|
@@ -5,6 +5,7 @@ import { recordFingerprint } from "../../domain/records.js";
|
|
|
5
5
|
import { accountPk, findAccountPk } from "./accounts.js";
|
|
6
6
|
import { suggestBaseStages } from "./base-suggest.js";
|
|
7
7
|
import { crmActivitiesOver, forgetActivitiesOn, } from "./crm-activities.js";
|
|
8
|
+
import { crmReadsOver } from "./crm-changes.js";
|
|
8
9
|
import { removeCrmContacts } from "./crm-contacts.js";
|
|
9
10
|
import { crmAccountPrefix, crmIdentityId, crmRowsOver, PERSON_RECORD, SOURCE, TABLES, textOf, } from "./crm-rows.js";
|
|
10
11
|
import { crmActivitiesOn, crmCompanyTypes, crmOwnUser, crmScope, crmScopeUsers, crmSettingKey, crmStageMoves, crmStageTasks, crmStageTasksSince, saveCrmSetting, saveStageMovesMark, } from "./crm-settings.js";
|
|
@@ -670,6 +671,7 @@ export const crmCopiesOver = (context, people) => {
|
|
|
670
671
|
activitiesOn: async (key) => crmActivitiesOn(database, key),
|
|
671
672
|
saveActivities: async (key, activities) => activityStore.save(key, activities),
|
|
672
673
|
activitiesOf: async (key, kind, externalId, limit) => activityStore.of(key, kind, externalId, limit),
|
|
674
|
+
...crmReadsOver(context),
|
|
673
675
|
removeOutOfScope: async (key, kind, userIds, { dryRun } = {}) => write(database, () => {
|
|
674
676
|
const keep = new Set(userIds.map((id) => crmIdentityId(key, id, "user")));
|
|
675
677
|
const accountId = findAccountPk(context, key);
|
|
@@ -19,6 +19,8 @@ export interface Opening {
|
|
|
19
19
|
candidates?: number[] | undefined;
|
|
20
20
|
shared?: SharedMailbox | undefined;
|
|
21
21
|
mailbox?: string | undefined;
|
|
22
|
+
strength?: IdentityLinkPayload["strength"];
|
|
23
|
+
run?: string | undefined;
|
|
22
24
|
}
|
|
23
25
|
export type Opened = {
|
|
24
26
|
outcome: "proposed" | "open";
|
|
@@ -70,6 +70,8 @@ export const openIdentityLink = (context, opening, index) => write(context.datab
|
|
|
70
70
|
evidence: opening.evidence ?? [],
|
|
71
71
|
...(opening.shared === undefined ? {} : { shared: opening.shared }),
|
|
72
72
|
...(opening.mailbox === undefined ? {} : { mailbox: opening.mailbox }),
|
|
73
|
+
...(opening.strength === undefined ? {} : { strength: opening.strength }),
|
|
74
|
+
...(opening.run === undefined ? {} : { run: opening.run }),
|
|
73
75
|
};
|
|
74
76
|
const action = operations.propose({
|
|
75
77
|
kind: IDENTITY_LINK_KIND,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { CacheDatabase } from "../driver.js";
|
|
1
2
|
import type { MatchScan } from "../identity-links.js";
|
|
2
3
|
import type { Author } from "./memories.js";
|
|
3
4
|
import type { StoreContext } from "./open.js";
|
|
@@ -18,6 +19,34 @@ export declare const PROPOSAL_SCORE: {
|
|
|
18
19
|
group: number;
|
|
19
20
|
nameInChats: number;
|
|
20
21
|
};
|
|
22
|
+
/**
|
|
23
|
+
* NEED-2445, safe option until the owner rules: a number two people share joins them only when a messenger, which
|
|
24
|
+
* keeps its user's own number, holds it; a CRM's or a card's number is someone's word and waits as a proposal.
|
|
25
|
+
* NEED-2600: a call's other side, kept as `phone:<number>`, is that number as the phone network gave it, as strong
|
|
26
|
+
* as a messenger's; it joins only on a mobile number, since an office's landline is everyone's who works there.
|
|
27
|
+
*/
|
|
28
|
+
export declare const phonePairJoins: (phone: string, holders: Holder[]) => boolean;
|
|
29
|
+
export interface Holder {
|
|
30
|
+
identity: number;
|
|
31
|
+
provider: string;
|
|
32
|
+
externalId: string;
|
|
33
|
+
/** The list it comes from: its source, and for a CRM the kind of record, `<portal>/contact/` or `<portal>/user/`. */
|
|
34
|
+
list: string;
|
|
35
|
+
/** An organization holds this identity: its number or address is the company's. */
|
|
36
|
+
company: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* SEC-177: a company's line or mailbox is everyone's who works there, so it never says two people are one. It is one
|
|
40
|
+
* when an organization holds it, or when two people of one list have it, such as two CRM contacts with one number. A
|
|
41
|
+
* CRM's contact and its user may be one person, so they are two lists. A messenger keeps one account per number, so
|
|
42
|
+
* two of its accounts on one number are a number that changed hands, not a company's line.
|
|
43
|
+
*/
|
|
44
|
+
export declare const sharedLine: (context: StoreContext, holders: Holder[]) => boolean;
|
|
45
|
+
/** Identities by the value they share. */
|
|
46
|
+
export type Holders = Map<string, Holder[]>;
|
|
47
|
+
export declare const phoneHolders: (database: CacheDatabase) => Holders;
|
|
48
|
+
/** A company's address is an `org-email` identity, never a person's, so it never joins people. */
|
|
49
|
+
export declare const emailHolders: (database: CacheDatabase) => Holders;
|
|
21
50
|
/**
|
|
22
51
|
* One pass of the people matcher. It reads what may match outside any transaction and takes the write lock only for
|
|
23
52
|
* each group it changes, so other writers are never kept waiting for the whole pass. After the first pass, only the
|