@wirecat/cli-core 0.674.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 +101 -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/messenger/search-command.js +3 -1
- package/dist/cli/option-hints.d.ts +2 -0
- package/dist/cli/option-hints.js +4 -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/dist/store/sqlite/vectors.js +5 -3
- package/package.json +3 -3
|
@@ -38,6 +38,7 @@ export declare const meetingTools: ({
|
|
|
38
38
|
max_read_bytes?: undefined;
|
|
39
39
|
cursor?: undefined;
|
|
40
40
|
} | {
|
|
41
|
+
limit?: undefined;
|
|
41
42
|
event_id?: undefined;
|
|
42
43
|
series_id?: undefined;
|
|
43
44
|
page?: undefined;
|
|
@@ -53,11 +54,11 @@ export declare const meetingTools: ({
|
|
|
53
54
|
format?: undefined;
|
|
54
55
|
transcript_id?: undefined;
|
|
55
56
|
query?: undefined;
|
|
56
|
-
limit?: undefined;
|
|
57
57
|
scan_limit?: undefined;
|
|
58
58
|
max_read_bytes?: undefined;
|
|
59
59
|
cursor?: undefined;
|
|
60
60
|
} | {
|
|
61
|
+
limit?: undefined;
|
|
61
62
|
event_id?: undefined;
|
|
62
63
|
series_id?: undefined;
|
|
63
64
|
page?: undefined;
|
|
@@ -75,11 +76,11 @@ export declare const meetingTools: ({
|
|
|
75
76
|
format?: undefined;
|
|
76
77
|
transcript_id?: undefined;
|
|
77
78
|
query?: undefined;
|
|
78
|
-
limit?: undefined;
|
|
79
79
|
scan_limit?: undefined;
|
|
80
80
|
max_read_bytes?: undefined;
|
|
81
81
|
cursor?: undefined;
|
|
82
82
|
} | {
|
|
83
|
+
limit?: undefined;
|
|
83
84
|
event_id?: undefined;
|
|
84
85
|
series_id?: undefined;
|
|
85
86
|
page?: undefined;
|
|
@@ -101,7 +102,6 @@ export declare const meetingTools: ({
|
|
|
101
102
|
minimum: number;
|
|
102
103
|
};
|
|
103
104
|
query?: undefined;
|
|
104
|
-
limit?: undefined;
|
|
105
105
|
scan_limit?: undefined;
|
|
106
106
|
max_read_bytes?: undefined;
|
|
107
107
|
cursor?: undefined;
|
|
@@ -188,6 +188,7 @@ export declare const meetingTools: ({
|
|
|
188
188
|
max_read_bytes?: undefined;
|
|
189
189
|
cursor?: undefined;
|
|
190
190
|
} | {
|
|
191
|
+
limit?: undefined;
|
|
191
192
|
event_id?: undefined;
|
|
192
193
|
series_id?: undefined;
|
|
193
194
|
page?: undefined;
|
|
@@ -203,11 +204,11 @@ export declare const meetingTools: ({
|
|
|
203
204
|
format?: undefined;
|
|
204
205
|
transcript_id?: undefined;
|
|
205
206
|
query?: undefined;
|
|
206
|
-
limit?: undefined;
|
|
207
207
|
scan_limit?: undefined;
|
|
208
208
|
max_read_bytes?: undefined;
|
|
209
209
|
cursor?: undefined;
|
|
210
210
|
} | {
|
|
211
|
+
limit?: undefined;
|
|
211
212
|
event_id?: undefined;
|
|
212
213
|
series_id?: undefined;
|
|
213
214
|
page?: undefined;
|
|
@@ -225,11 +226,11 @@ export declare const meetingTools: ({
|
|
|
225
226
|
format?: undefined;
|
|
226
227
|
transcript_id?: undefined;
|
|
227
228
|
query?: undefined;
|
|
228
|
-
limit?: undefined;
|
|
229
229
|
scan_limit?: undefined;
|
|
230
230
|
max_read_bytes?: undefined;
|
|
231
231
|
cursor?: undefined;
|
|
232
232
|
} | {
|
|
233
|
+
limit?: undefined;
|
|
233
234
|
event_id?: undefined;
|
|
234
235
|
series_id?: undefined;
|
|
235
236
|
page?: undefined;
|
|
@@ -251,7 +252,6 @@ export declare const meetingTools: ({
|
|
|
251
252
|
minimum: number;
|
|
252
253
|
};
|
|
253
254
|
query?: undefined;
|
|
254
|
-
limit?: undefined;
|
|
255
255
|
scan_limit?: undefined;
|
|
256
256
|
max_read_bytes?: undefined;
|
|
257
257
|
cursor?: undefined;
|
|
@@ -338,6 +338,7 @@ export declare const meetingTools: ({
|
|
|
338
338
|
max_read_bytes?: undefined;
|
|
339
339
|
cursor?: undefined;
|
|
340
340
|
} | {
|
|
341
|
+
limit?: undefined;
|
|
341
342
|
event_id?: undefined;
|
|
342
343
|
series_id?: undefined;
|
|
343
344
|
page?: undefined;
|
|
@@ -353,11 +354,11 @@ export declare const meetingTools: ({
|
|
|
353
354
|
format?: undefined;
|
|
354
355
|
transcript_id?: undefined;
|
|
355
356
|
query?: undefined;
|
|
356
|
-
limit?: undefined;
|
|
357
357
|
scan_limit?: undefined;
|
|
358
358
|
max_read_bytes?: undefined;
|
|
359
359
|
cursor?: undefined;
|
|
360
360
|
} | {
|
|
361
|
+
limit?: undefined;
|
|
361
362
|
event_id?: undefined;
|
|
362
363
|
series_id?: undefined;
|
|
363
364
|
page?: undefined;
|
|
@@ -375,11 +376,11 @@ export declare const meetingTools: ({
|
|
|
375
376
|
format?: undefined;
|
|
376
377
|
transcript_id?: undefined;
|
|
377
378
|
query?: undefined;
|
|
378
|
-
limit?: undefined;
|
|
379
379
|
scan_limit?: undefined;
|
|
380
380
|
max_read_bytes?: undefined;
|
|
381
381
|
cursor?: undefined;
|
|
382
382
|
} | {
|
|
383
|
+
limit?: undefined;
|
|
383
384
|
event_id?: undefined;
|
|
384
385
|
series_id?: undefined;
|
|
385
386
|
page?: undefined;
|
|
@@ -401,7 +402,6 @@ export declare const meetingTools: ({
|
|
|
401
402
|
minimum: number;
|
|
402
403
|
};
|
|
403
404
|
query?: undefined;
|
|
404
|
-
limit?: undefined;
|
|
405
405
|
scan_limit?: undefined;
|
|
406
406
|
max_read_bytes?: undefined;
|
|
407
407
|
cursor?: undefined;
|
|
@@ -488,6 +488,7 @@ export declare const meetingTools: ({
|
|
|
488
488
|
max_read_bytes?: undefined;
|
|
489
489
|
cursor?: undefined;
|
|
490
490
|
} | {
|
|
491
|
+
limit?: undefined;
|
|
491
492
|
event_id?: undefined;
|
|
492
493
|
series_id?: undefined;
|
|
493
494
|
page?: undefined;
|
|
@@ -503,11 +504,11 @@ export declare const meetingTools: ({
|
|
|
503
504
|
format?: undefined;
|
|
504
505
|
transcript_id?: undefined;
|
|
505
506
|
query?: undefined;
|
|
506
|
-
limit?: undefined;
|
|
507
507
|
scan_limit?: undefined;
|
|
508
508
|
max_read_bytes?: undefined;
|
|
509
509
|
cursor?: undefined;
|
|
510
510
|
} | {
|
|
511
|
+
limit?: undefined;
|
|
511
512
|
event_id?: undefined;
|
|
512
513
|
series_id?: undefined;
|
|
513
514
|
page?: undefined;
|
|
@@ -525,11 +526,11 @@ export declare const meetingTools: ({
|
|
|
525
526
|
format?: undefined;
|
|
526
527
|
transcript_id?: undefined;
|
|
527
528
|
query?: undefined;
|
|
528
|
-
limit?: undefined;
|
|
529
529
|
scan_limit?: undefined;
|
|
530
530
|
max_read_bytes?: undefined;
|
|
531
531
|
cursor?: undefined;
|
|
532
532
|
} | {
|
|
533
|
+
limit?: undefined;
|
|
533
534
|
event_id?: undefined;
|
|
534
535
|
series_id?: undefined;
|
|
535
536
|
page?: undefined;
|
|
@@ -551,7 +552,6 @@ export declare const meetingTools: ({
|
|
|
551
552
|
minimum: number;
|
|
552
553
|
};
|
|
553
554
|
query?: undefined;
|
|
554
|
-
limit?: undefined;
|
|
555
555
|
scan_limit?: undefined;
|
|
556
556
|
max_read_bytes?: undefined;
|
|
557
557
|
cursor?: undefined;
|
|
@@ -638,6 +638,7 @@ export declare const meetingTools: ({
|
|
|
638
638
|
max_read_bytes?: undefined;
|
|
639
639
|
cursor?: undefined;
|
|
640
640
|
} | {
|
|
641
|
+
limit?: undefined;
|
|
641
642
|
event_id?: undefined;
|
|
642
643
|
series_id?: undefined;
|
|
643
644
|
page?: undefined;
|
|
@@ -653,11 +654,11 @@ export declare const meetingTools: ({
|
|
|
653
654
|
format?: undefined;
|
|
654
655
|
transcript_id?: undefined;
|
|
655
656
|
query?: undefined;
|
|
656
|
-
limit?: undefined;
|
|
657
657
|
scan_limit?: undefined;
|
|
658
658
|
max_read_bytes?: undefined;
|
|
659
659
|
cursor?: undefined;
|
|
660
660
|
} | {
|
|
661
|
+
limit?: undefined;
|
|
661
662
|
event_id?: undefined;
|
|
662
663
|
series_id?: undefined;
|
|
663
664
|
page?: undefined;
|
|
@@ -675,11 +676,11 @@ export declare const meetingTools: ({
|
|
|
675
676
|
format?: undefined;
|
|
676
677
|
transcript_id?: undefined;
|
|
677
678
|
query?: undefined;
|
|
678
|
-
limit?: undefined;
|
|
679
679
|
scan_limit?: undefined;
|
|
680
680
|
max_read_bytes?: undefined;
|
|
681
681
|
cursor?: undefined;
|
|
682
682
|
} | {
|
|
683
|
+
limit?: undefined;
|
|
683
684
|
event_id?: undefined;
|
|
684
685
|
series_id?: undefined;
|
|
685
686
|
page?: undefined;
|
|
@@ -701,7 +702,6 @@ export declare const meetingTools: ({
|
|
|
701
702
|
minimum: number;
|
|
702
703
|
};
|
|
703
704
|
query?: undefined;
|
|
704
|
-
limit?: undefined;
|
|
705
705
|
scan_limit?: undefined;
|
|
706
706
|
max_read_bytes?: undefined;
|
|
707
707
|
cursor?: undefined;
|
|
@@ -3,7 +3,7 @@ import { type HitFile } from "../domain/attachments.js";
|
|
|
3
3
|
import type { DealKind } from "../domain/deals.js";
|
|
4
4
|
import { type MailFields } from "../domain/models.js";
|
|
5
5
|
import type { MessengerTraits } from "../ports/traits.js";
|
|
6
|
-
import type { AccountKey, BusinessKind, BusinessSource, MessageStore, StoredAccount } from "../store/store.js";
|
|
6
|
+
import type { AccountKey, BusinessKind, BusinessSource, CopyViewer, MessageStore, StoredAccount } from "../store/store.js";
|
|
7
7
|
import type { SearchFound, SearchQuery } from "./messages.js";
|
|
8
8
|
import { type MeaningLane, type MeaningSkipped } from "./messages-discovery.js";
|
|
9
9
|
import type { SearchCoverage } from "./messages-search.js";
|
|
@@ -150,6 +150,8 @@ export interface SearchAllRequest {
|
|
|
150
150
|
meetingsAsked?: boolean;
|
|
151
151
|
/** The CRM kinds `crm` reads; absent, deals and leads. */
|
|
152
152
|
crmKinds?: readonly DealKind[];
|
|
153
|
+
/** The CRM accounts' copies a CRM CLI's profile may read only part of, each by its own rule (SEC-197). */
|
|
154
|
+
crmViewers?: CopyViewer[];
|
|
153
155
|
/**
|
|
154
156
|
* Search harder: a hosted model reorders the first 30 hits, only with the owner's consent and a key. Absent, nothing
|
|
155
157
|
* is read of the consent or the key and nothing is sent.
|
|
@@ -331,6 +331,7 @@ export const searchAll = async (store, account, asked, messenger = {}, searchMes
|
|
|
331
331
|
anyWord: anyWord || discover,
|
|
332
332
|
...(request.exact ? { exact: true } : {}),
|
|
333
333
|
...(request.crmKinds === undefined ? {} : { kinds: request.crmKinds }),
|
|
334
|
+
...(request.crmViewers === undefined ? {} : { viewers: request.crmViewers }),
|
|
334
335
|
limit: Math.min(depth, 500),
|
|
335
336
|
...(chosen === undefined ? {} : { accounts: chosen }),
|
|
336
337
|
});
|
package/dist/setup/define.d.ts
CHANGED
|
@@ -15,8 +15,23 @@ export interface SetupCheckInput {
|
|
|
15
15
|
/** The sign-in events (`progress`, `needs`…) Desktop reads on stderr. */
|
|
16
16
|
steps: SignInSteps;
|
|
17
17
|
}
|
|
18
|
-
/**
|
|
19
|
-
|
|
18
|
+
/**
|
|
19
|
+
* What `apply` keeps in place of its defaults, for a CLI whose `session start` keeps more than the typed secret (an
|
|
20
|
+
* account address beside a token) or knows the account the profile signs in to.
|
|
21
|
+
*/
|
|
22
|
+
export interface SetupKeep {
|
|
23
|
+
/** By secret name, the value to keep instead of the one typed; a name the path gave no secret for is refused. */
|
|
24
|
+
secrets?: Readonly<Record<string, string>>;
|
|
25
|
+
/** The account the profile is, as `session start` remembers it; the profile's name when left out. */
|
|
26
|
+
account?: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The CLI's own proof that the answers work, as its `session start` checks. A throw saves nothing. `keep` is taken
|
|
30
|
+
* out of the result, never printed.
|
|
31
|
+
*/
|
|
32
|
+
export type SetupCheck = (input: SetupCheckInput) => Promise<(Record<string, unknown> & {
|
|
33
|
+
keep?: SetupKeep;
|
|
34
|
+
}) | undefined>;
|
|
20
35
|
export interface SetupDefinition {
|
|
21
36
|
app: AppIdentity;
|
|
22
37
|
configuration: Pick<Configuration, "allSettings" | "settingSchema" | "resolveSettings" | "changeSetting">;
|
package/dist/store/exports.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export { CHAT_COLUMNS, TRACKED_CHAT_COLUMNS, titlePart } from "./sqlite/chats.js
|
|
|
26
26
|
export { storeClients } from "./sqlite/clients.js";
|
|
27
27
|
export { CHAT_LIST_KEY, type ChatCompleteness, type ChatCoverage, fetchedKey, historyStartKey, } from "./sqlite/completeness.js";
|
|
28
28
|
export { CONVERSATION_COLUMNS } from "./sqlite/conversations.js";
|
|
29
|
+
export { type CopyViewer, seenBy } from "./sqlite/copy-viewer.js";
|
|
29
30
|
export type { CounterTarget } from "./sqlite/counters.js";
|
|
30
31
|
export { crmSettingsOf, parseCrmSetting, parseCrmSettingValue, saveCrmSetting } from "./sqlite/crm-settings.js";
|
|
31
32
|
export { type EmailMarks, MAIL_AGENDA_COLUMNS, type MarkedEmail, type NewestEmail, type ThreadActivity, } from "./sqlite/email-activity.js";
|
package/dist/store/exports.js
CHANGED
|
@@ -8,6 +8,7 @@ export { CHAT_COLUMNS, TRACKED_CHAT_COLUMNS, titlePart } from "./sqlite/chats.js
|
|
|
8
8
|
export { storeClients } from "./sqlite/clients.js";
|
|
9
9
|
export { CHAT_LIST_KEY, fetchedKey, historyStartKey, } from "./sqlite/completeness.js";
|
|
10
10
|
export { CONVERSATION_COLUMNS } from "./sqlite/conversations.js";
|
|
11
|
+
export { seenBy } from "./sqlite/copy-viewer.js";
|
|
11
12
|
export { crmSettingsOf, parseCrmSetting, parseCrmSettingValue, saveCrmSetting } from "./sqlite/crm-settings.js";
|
|
12
13
|
export { MAIL_AGENDA_COLUMNS, } from "./sqlite/email-activity.js";
|
|
13
14
|
export { messageOfEmail, threadConversation } from "./sqlite/emails.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AccountKey } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* A profile that may read only part of one CRM account's copy (SEC-197): the deals and leads assigned to `assignee`,
|
|
4
|
+
* the identity `<account>/user/<id>`, or none when it is `null`.
|
|
5
|
+
*/
|
|
6
|
+
export interface CopyViewer {
|
|
7
|
+
account: AccountKey;
|
|
8
|
+
assignee: string | null;
|
|
9
|
+
}
|
|
10
|
+
/** The condition on a deal or lead row `alias` of the viewer's account, with its parameters. */
|
|
11
|
+
export declare const viewerSql: (alias: string, viewer: CopyViewer | undefined) => {
|
|
12
|
+
sql: string;
|
|
13
|
+
params: string[];
|
|
14
|
+
};
|
|
15
|
+
/** Whether a deal or lead row, with its account's `provider` and `account` and its `assignee`, is the viewer's to read. */
|
|
16
|
+
export declare const seenBy: (viewer: CopyViewer | undefined, row: Record<string, unknown>) => boolean;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** The condition on a deal or lead row `alias` of the viewer's account, with its parameters. */
|
|
2
|
+
export const viewerSql = (alias, viewer) => viewer === undefined
|
|
3
|
+
? { sql: "1", params: [] }
|
|
4
|
+
: viewer.assignee === null
|
|
5
|
+
? { sql: "0", params: [] }
|
|
6
|
+
: { sql: `json_extract(${alias}.metadata, '$.assignee') = ?`, params: [viewer.assignee] };
|
|
7
|
+
/** Whether a deal or lead row, with its account's `provider` and `account` and its `assignee`, is the viewer's to read. */
|
|
8
|
+
export const seenBy = (viewer, row) => viewer === undefined ||
|
|
9
|
+
row.provider !== viewer.account.provider ||
|
|
10
|
+
row.account !== viewer.account.account ||
|
|
11
|
+
(viewer.assignee !== null && row.assignee === viewer.assignee);
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import type { CrmActivity, CrmActivityKind } from "../../domain/crm.js";
|
|
2
2
|
import type { DealKind } from "../../domain/deals.js";
|
|
3
|
+
import type { CacheDatabase } from "../driver.js";
|
|
3
4
|
import type { AccountKey } from "../types.js";
|
|
4
5
|
import type { StoreContext } from "./open.js";
|
|
5
6
|
export declare const ACTIVITY_RECORD = "crm.activity";
|
|
7
|
+
/**
|
|
8
|
+
* Marks deleted the activities on nothing but these contacts or companies: their subject and body leave the copy with
|
|
9
|
+
* the rows they were on. Answers how many.
|
|
10
|
+
*/
|
|
11
|
+
export declare const forgetActivitiesOn: (database: CacheDatabase, accountId: number, kind: "person" | "organization", externalIds: readonly string[], at: number) => number;
|
|
6
12
|
/** One activity as `deals show` lists it. */
|
|
7
13
|
export interface StoredActivity {
|
|
8
14
|
externalId: string;
|
|
@@ -20,7 +26,7 @@ export interface StoredActivity {
|
|
|
20
26
|
export declare const activityTexts: (database: StoreContext["database"], kind: DealKind) => Map<string, string[]>;
|
|
21
27
|
export interface CrmActivitiesSaved {
|
|
22
28
|
saved: number;
|
|
23
|
-
/** On no deal or
|
|
29
|
+
/** On no deal, lead, contact or company this copy keeps, so not stored. */
|
|
24
30
|
outOfScope: number;
|
|
25
31
|
}
|
|
26
32
|
export declare const crmActivitiesOver: (context: StoreContext) => {
|
|
@@ -3,6 +3,20 @@ import { accountPk, findAccountPk } from "./accounts.js";
|
|
|
3
3
|
import { crmIdentityId, SOURCE, TABLES, textOf } from "./crm-rows.js";
|
|
4
4
|
import { write } from "./transaction.js";
|
|
5
5
|
export const ACTIVITY_RECORD = "crm.activity";
|
|
6
|
+
/**
|
|
7
|
+
* Marks deleted the activities on nothing but these contacts or companies: their subject and body leave the copy with
|
|
8
|
+
* the rows they were on. Answers how many.
|
|
9
|
+
*/
|
|
10
|
+
export const forgetActivitiesOn = (database, accountId, kind, externalIds, at) => {
|
|
11
|
+
const removed = "rr.target_type = ? AND rr.target_external_id IN (SELECT value FROM json_each(?))";
|
|
12
|
+
const ids = JSON.stringify(externalIds);
|
|
13
|
+
return database
|
|
14
|
+
.prepare("UPDATE records SET deleted_at = ? WHERE account_id = ? AND type = ? AND deleted_at IS NULL " +
|
|
15
|
+
`AND EXISTS (SELECT 1 FROM record_relations rr WHERE rr.record_id = records.id AND rr.role = 'on' AND ${removed}) ` +
|
|
16
|
+
"AND NOT EXISTS (SELECT 1 FROM record_relations rr WHERE rr.record_id = records.id AND rr.role = 'on' " +
|
|
17
|
+
`AND NOT (${removed}))`)
|
|
18
|
+
.run(at, accountId, ACTIVITY_RECORD, kind, ids, kind, ids).changes;
|
|
19
|
+
};
|
|
6
20
|
/** A CRM's text field holds what people typed or pasted, a whole e-mail thread too. */
|
|
7
21
|
const MOST_BODY = 20_000;
|
|
8
22
|
const MOST_SUBJECT = 500;
|
|
@@ -25,18 +39,29 @@ export const activityTexts = (database, kind) => {
|
|
|
25
39
|
};
|
|
26
40
|
export const crmActivitiesOver = (context) => {
|
|
27
41
|
const { database, now } = context;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
42
|
+
/** A deal or lead the copy keeps, a contact it holds, or a company it keeps. */
|
|
43
|
+
const kept = (key, accountId, kind, externalId) => (kind === "person"
|
|
44
|
+
? database
|
|
45
|
+
.prepare("SELECT 1 FROM identities WHERE provider = ? AND external_id = ?")
|
|
46
|
+
.get(key.provider, crmIdentityId(key, externalId))
|
|
47
|
+
: kind === "organization"
|
|
48
|
+
? database
|
|
49
|
+
.prepare("SELECT 1 FROM records WHERE account_id = ? AND type = 'organization' AND external_id = ? " +
|
|
50
|
+
"AND deleted_at IS NULL")
|
|
51
|
+
.get(accountId, externalId)
|
|
52
|
+
: database
|
|
53
|
+
.prepare(`SELECT 1 FROM ${TABLES[kind]} WHERE account_id = ? AND external_id = ? AND deleted_at IS NULL`)
|
|
54
|
+
.get(accountId, externalId)) !== undefined;
|
|
31
55
|
const save = (key, activities) => write(database, () => {
|
|
32
56
|
const accountId = accountPk(context, key);
|
|
33
57
|
const at = now();
|
|
34
58
|
let saved = 0;
|
|
35
59
|
let outOfScope = 0;
|
|
36
60
|
for (const activity of activities) {
|
|
37
|
-
const on = activity.on.filter((one) => kept(accountId, one.kind, one.externalId));
|
|
61
|
+
const on = activity.on.filter((one) => kept(key, accountId, one.kind, one.externalId));
|
|
38
62
|
const held = database
|
|
39
|
-
.prepare("SELECT id
|
|
63
|
+
.prepare("SELECT id, json_extract(fields, '$.done') AS done, json_extract(fields, '$.done_at') AS done_at " +
|
|
64
|
+
"FROM records WHERE account_id = ? AND type = ? AND external_id = ?")
|
|
40
65
|
.get(accountId, ACTIVITY_RECORD, activity.externalId);
|
|
41
66
|
if (on.length === 0) {
|
|
42
67
|
// It left every kept deal: one stored before goes with it.
|
|
@@ -51,6 +76,8 @@ export const crmActivitiesOver = (context) => {
|
|
|
51
76
|
body: textOf(activity.body, MOST_BODY),
|
|
52
77
|
due_at: activity.dueAt ?? null,
|
|
53
78
|
done: activity.done,
|
|
79
|
+
// When it was done, as near as the copy can tell: the CRM's change that closed it, kept once seen.
|
|
80
|
+
done_at: activity.done ? (held?.done ? (held.done_at ?? null) : activity.updatedAt) : null,
|
|
54
81
|
assignee: activity.assignee ? crmIdentityId(key, activity.assignee, "user") : null,
|
|
55
82
|
created_at: activity.createdAt,
|
|
56
83
|
updated_at: activity.updatedAt,
|
|
@@ -18,10 +18,13 @@ export declare const withoutPlaceholder: (name: string) => string | null;
|
|
|
18
18
|
* assign to one of `users`. A contact the owner decided about, or one anything else points at, stays; so does one
|
|
19
19
|
* saved at or after `before`, whose deal may still be on its way. A person the removed contacts leave empty is
|
|
20
20
|
* deleted unless anything still points at them. People named after a contact keep that name without the import's
|
|
21
|
-
* `null #<id>`; the owner's person is never renamed.
|
|
21
|
+
* `null #<id>`; the owner's person is never renamed. `only` removes just these contacts, by the CRM's id, whenever they
|
|
22
|
+
* were saved and whatever company names them, unless a kept deal or lead does. Activities on nothing but a removed
|
|
23
|
+
* contact are marked deleted.
|
|
22
24
|
*/
|
|
23
|
-
export declare const removeCrmContacts: (context: StoreContext, key: AccountKey, { users, before, dryRun }: {
|
|
25
|
+
export declare const removeCrmContacts: (context: StoreContext, key: AccountKey, { users, before, dryRun, only, }: {
|
|
24
26
|
users?: readonly string[];
|
|
25
27
|
before: number;
|
|
26
28
|
dryRun?: boolean;
|
|
29
|
+
only?: readonly string[];
|
|
27
30
|
}) => CrmContactsRemoved;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { findAccountPk, OWN, referencesTo } from "./accounts.js";
|
|
2
|
-
import {
|
|
2
|
+
import { forgetActivitiesOn } from "./crm-activities.js";
|
|
3
|
+
import { crmAccountPrefix, crmIdentityId, PERSON_RECORD } from "./crm-rows.js";
|
|
3
4
|
import { write } from "./transaction.js";
|
|
4
5
|
/** A CRM import can leave `null` or `null #<id>` in a name field; no person is called that. */
|
|
5
6
|
const PLACEHOLDER = /(?:^|\s)null(?:\s+#\d+)?(?=\s|$)/gi;
|
|
@@ -18,9 +19,11 @@ const NONE = { contacts: 0, persons: 0, personsKept: 0, names: 0 };
|
|
|
18
19
|
* assign to one of `users`. A contact the owner decided about, or one anything else points at, stays; so does one
|
|
19
20
|
* saved at or after `before`, whose deal may still be on its way. A person the removed contacts leave empty is
|
|
20
21
|
* deleted unless anything still points at them. People named after a contact keep that name without the import's
|
|
21
|
-
* `null #<id>`; the owner's person is never renamed.
|
|
22
|
+
* `null #<id>`; the owner's person is never renamed. `only` removes just these contacts, by the CRM's id, whenever they
|
|
23
|
+
* were saved and whatever company names them, unless a kept deal or lead does. Activities on nothing but a removed
|
|
24
|
+
* contact are marked deleted.
|
|
22
25
|
*/
|
|
23
|
-
export const removeCrmContacts = (context, key, { users, before, dryRun = false }) => {
|
|
26
|
+
export const removeCrmContacts = (context, key, { users, before, dryRun = false, only, }) => {
|
|
24
27
|
const { database } = context;
|
|
25
28
|
const accountId = findAccountPk(context, key);
|
|
26
29
|
if (accountId === undefined)
|
|
@@ -55,20 +58,24 @@ export const removeCrmContacts = (context, key, { users, before, dryRun = false
|
|
|
55
58
|
database
|
|
56
59
|
.prepare("INSERT INTO temp.crm_gone SELECT i.id, l.person_id FROM identities i " +
|
|
57
60
|
`LEFT JOIN identity_links l ON l.identity_id = i.id WHERE ${contactOf} ` +
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
61
|
+
(only === undefined
|
|
62
|
+
? "AND i.created_at < ? AND i.updated_at < ? " +
|
|
63
|
+
(users === undefined
|
|
64
|
+
? ""
|
|
65
|
+
: "AND coalesce(json_extract(i.metadata, '$.assignee'), '') NOT IN (SELECT id FROM temp.crm_scope) ") +
|
|
66
|
+
`AND NOT ${named("deals", "deal")} AND NOT ${named("leads", "lead")} ` +
|
|
67
|
+
"AND NOT EXISTS (SELECT 1 FROM links k JOIN records r ON r.entity_type = 'organization' " +
|
|
68
|
+
"AND r.entity_id = k.to_id WHERE k.from_type = 'person' AND k.from_id = l.person_id " +
|
|
69
|
+
"AND +k.to_type = 'organization' AND k.kind = 'member-of' AND k.role = 'contact' AND k.source = 'crm' " +
|
|
70
|
+
`AND k.deleted_at IS NULL AND r.account_id = ${accountId} AND r.type = 'organization' ` +
|
|
71
|
+
"AND r.deleted_at IS NULL) "
|
|
72
|
+
: "AND i.external_id IN (SELECT value FROM json_each(?)) " +
|
|
73
|
+
// A contact on the owner's own deal stays: the next sync would copy it back from that deal.
|
|
74
|
+
`AND NOT ${named("deals", "deal")} AND NOT ${named("leads", "lead")} `) +
|
|
68
75
|
"AND NOT EXISTS (SELECT 1 FROM identity_link_events e WHERE e.identity_id = i.id AND e.author = 'owner') " +
|
|
69
76
|
"AND NOT EXISTS (SELECT 1 FROM temp.crm_proposed c WHERE c.decided AND c.ref = i.provider || ':' || " +
|
|
70
77
|
"i.external_id)")
|
|
71
|
-
.run(...contact, before, before);
|
|
78
|
+
.run(...contact, ...(only === undefined ? [before, before] : [JSON.stringify(only.map((id) => crmIdentityId(key, id)))]));
|
|
72
79
|
database.exec(`DELETE FROM temp.crm_gone WHERE id IN (${identityRefs})`);
|
|
73
80
|
// A same-email join left the contact's first person empty; it goes too, unless something points at it.
|
|
74
81
|
database.exec("INSERT OR IGNORE INTO temp.crm_persons SELECT person FROM temp.crm_gone WHERE person IS NOT NULL");
|
|
@@ -101,6 +108,15 @@ export const removeCrmContacts = (context, key, { users, before, dryRun = false
|
|
|
101
108
|
database.exec("DELETE FROM proposed_actions WHERE kind = 'identity_link' AND status = 'proposed' AND (" +
|
|
102
109
|
`json_extract(payload, '$.identity') IN (SELECT provider || ':' || external_id FROM identities WHERE id IN ${gone}) ` +
|
|
103
110
|
`OR json_extract(payload, '$.with') IN (SELECT provider || ':' || external_id FROM identities WHERE id IN ${gone}))`);
|
|
111
|
+
// The contact's own fields go with it, marked deleted as the CRM's other rows are (WC-DATA-7).
|
|
112
|
+
database
|
|
113
|
+
.prepare(`UPDATE records SET deleted_at = ? WHERE account_id = ? AND type = ? AND deleted_at IS NULL AND external_id IN ` +
|
|
114
|
+
`(SELECT substr(external_id, ?) FROM identities WHERE id IN ${gone})`)
|
|
115
|
+
.run(context.now(), accountId, PERSON_RECORD, prefix.length + 1);
|
|
116
|
+
forgetActivitiesOn(database, accountId, "person", database
|
|
117
|
+
.prepare(`SELECT substr(external_id, ?) AS id FROM identities WHERE id IN ${gone}`)
|
|
118
|
+
.all(prefix.length + 1)
|
|
119
|
+
.map((row) => String(row.id)), context.now());
|
|
104
120
|
for (const table of ["identity_revisions", "identity_link_events", "identity_links", "account_identities"])
|
|
105
121
|
database.exec(`DELETE FROM ${table} WHERE identity_id IN ${gone}`);
|
|
106
122
|
const contacts = database.prepare(`DELETE FROM identities WHERE id IN ${gone}`).run().changes;
|
|
@@ -14,6 +14,13 @@ export declare const TABLES: {
|
|
|
14
14
|
};
|
|
15
15
|
/** Who writes a CRM's copies: the sync, not an agent, so `author.kind` is `rule`. */
|
|
16
16
|
export declare const SOURCE = "crm";
|
|
17
|
+
/** A deal or lead given to another CRM user, as the sync saw it. */
|
|
18
|
+
export declare const OWNER_CHANGE_RECORD = "crm.owner-change";
|
|
19
|
+
/**
|
|
20
|
+
* A contact's own fields beside its identity: who it is assigned to, when the CRM made and changed it, its page and
|
|
21
|
+
* its custom values. Not `person`: a records import's `person` without an entity is matched into people.
|
|
22
|
+
*/
|
|
23
|
+
export declare const PERSON_RECORD = "crm.person";
|
|
17
24
|
/**
|
|
18
25
|
* A contact's identity id: identities are unique per provider, and two portals of one CRM both have a contact 5.
|
|
19
26
|
* `contact:` references percent-encode it, so the `/` stays one part.
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { timezoneOf } from "@wirecat/cli-base/query";
|
|
2
3
|
import { STAGE_OUTCOMES } from "../../domain/deals.js";
|
|
4
|
+
import { dayIn } from "../../domain/moment.js";
|
|
3
5
|
import { currencyOf, minorUnits, scaleOf } from "../../domain/money.js";
|
|
4
6
|
import { fold } from "../normalize.js";
|
|
5
7
|
import { authorActor } from "./memories.js";
|
|
@@ -39,6 +41,13 @@ const moneyOf = (item) => {
|
|
|
39
41
|
};
|
|
40
42
|
/** Who writes a CRM's copies: the sync, not an agent, so `author.kind` is `rule`. */
|
|
41
43
|
export const SOURCE = "crm";
|
|
44
|
+
/** A deal or lead given to another CRM user, as the sync saw it. */
|
|
45
|
+
export const OWNER_CHANGE_RECORD = "crm.owner-change";
|
|
46
|
+
/**
|
|
47
|
+
* A contact's own fields beside its identity: who it is assigned to, when the CRM made and changed it, its page and
|
|
48
|
+
* its custom values. Not `person`: a records import's `person` without an entity is matched into people.
|
|
49
|
+
*/
|
|
50
|
+
export const PERSON_RECORD = "crm.person";
|
|
42
51
|
/**
|
|
43
52
|
* A contact's identity id: identities are unique per provider, and two portals of one CRM both have a contact 5.
|
|
44
53
|
* `contact:` references percent-encode it, so the `/` stays one part.
|
|
@@ -142,6 +151,21 @@ export const crmRowsOver = (context) => {
|
|
|
142
151
|
.get(accountId, externalId);
|
|
143
152
|
return row?.entity_id == null ? null : Number(row.entity_id);
|
|
144
153
|
};
|
|
154
|
+
/** Who a deal or lead went from and to, as `crm.owner-change` on it: a CRM's users are often not listed. */
|
|
155
|
+
const ownerChanged = (accountId, kind, item, change, at) => {
|
|
156
|
+
const when = epoch(item.updatedAt, at);
|
|
157
|
+
const externalId = `${kind}/${item.externalId}/${when}`;
|
|
158
|
+
const fields = { kind, ...change, at: when };
|
|
159
|
+
const inserted = database
|
|
160
|
+
.prepare("INSERT INTO records (account_id, type, external_id, fields, fingerprint, batch_id, created_at, updated_at) " +
|
|
161
|
+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT DO NOTHING RETURNING id")
|
|
162
|
+
.get(accountId, OWNER_CHANGE_RECORD, externalId, JSON.stringify(fields), externalId, SOURCE, at, at);
|
|
163
|
+
if (inserted)
|
|
164
|
+
database
|
|
165
|
+
.prepare("INSERT INTO record_relations (record_id, role, target_type, target_external_id, created_at) " +
|
|
166
|
+
"VALUES (?, 'on', ?, ?, ?)")
|
|
167
|
+
.run(Number(inserted.id), kind, item.externalId, at);
|
|
168
|
+
};
|
|
145
169
|
/** The deal or lead row of `(account, external id)`; `writer` gives what another writer than CRM sync resolved. */
|
|
146
170
|
const saveItemRow = (key, accountId, kind, item, at, writer = {}) => {
|
|
147
171
|
const pipeline = pipelineOf(accountId, kind, item.pipeline);
|
|
@@ -160,9 +184,18 @@ export const crmRowsOver = (context) => {
|
|
|
160
184
|
if (item.status != null && !STAGE_OUTCOMES.includes(item.status))
|
|
161
185
|
throw new CliError("validation_error", `${kind} ${item.externalId}'s status is open, won or lost`);
|
|
162
186
|
const status = item.status ?? String(stage.outcome);
|
|
187
|
+
const table = TABLES[kind];
|
|
188
|
+
const held = database
|
|
189
|
+
.prepare(`SELECT id, status, closed_date, json_extract(metadata, '$.assignee') AS assignee FROM ${table} ` +
|
|
190
|
+
"WHERE account_id = ? AND external_id = ?")
|
|
191
|
+
.get(accountId, item.externalId);
|
|
192
|
+
// A CRM that gives no closed day: the owner's day it was first seen so closed, never that of a later edit (BUG-8).
|
|
163
193
|
const closed = status === "open"
|
|
164
194
|
? null
|
|
165
|
-
: (day(item.closedDate, "a closed day") ??
|
|
195
|
+
: (day(item.closedDate, "a closed day") ??
|
|
196
|
+
(held && held.status === status && held.closed_date != null
|
|
197
|
+
? String(held.closed_date)
|
|
198
|
+
: dayIn(epoch(item.updatedAt, at), timezoneOf())));
|
|
166
199
|
const deal = writer.deal !== undefined
|
|
167
200
|
? writer.deal === null
|
|
168
201
|
? undefined
|
|
@@ -196,13 +229,12 @@ export const crmRowsOver = (context) => {
|
|
|
196
229
|
created: epoch(item.createdAt, at),
|
|
197
230
|
updated: epoch(item.updatedAt, at),
|
|
198
231
|
};
|
|
199
|
-
const table = TABLES[kind];
|
|
200
|
-
const held = database
|
|
201
|
-
.prepare(`SELECT id FROM ${table} WHERE account_id = ? AND external_id = ?`)
|
|
202
|
-
.get(accountId, item.externalId);
|
|
203
232
|
let id;
|
|
204
233
|
if (held) {
|
|
205
234
|
id = Number(held.id);
|
|
235
|
+
const owner = item.assignee === undefined ? undefined : (metadata.assignee ?? null);
|
|
236
|
+
if (owner !== undefined && owner !== (held.assignee ?? null))
|
|
237
|
+
ownerChanged(accountId, kind, item, { from: held.assignee ?? null, to: owner }, at);
|
|
206
238
|
database
|
|
207
239
|
.prepare(`UPDATE ${table} SET title = ?, organization_id = ?, stage_id = ?, status = ?, currency = ?, amount = ?, ` +
|
|
208
240
|
"close_date = ?, closed_date = ?, metadata = ?, updated_at = ?, deleted_at = NULL" +
|
|
@@ -61,8 +61,9 @@ export interface CrmStageMove {
|
|
|
61
61
|
stageName: string;
|
|
62
62
|
pipelineName: string;
|
|
63
63
|
}
|
|
64
|
-
/** When stage
|
|
64
|
+
/** When the stage-task rules were first set: a stage entered before then opens no task. */
|
|
65
65
|
export declare const crmStageTasksSince: (database: CacheDatabase, key: AccountKey) => number | null;
|
|
66
|
+
/** `at` stays when the rules were first set: the stage history counts from then, whenever the history arrives. */
|
|
66
67
|
export declare const saveStageMovesMark: (database: CacheDatabase, key: AccountKey, through: number, now: number) => void;
|
|
67
68
|
export declare const parseCrmSettingValue: (name: CrmSettingName, value: string) => unknown;
|
|
68
69
|
export declare const saveCrmSetting: (database: CacheDatabase, setting: string, value: unknown, now: number) => void;
|
|
@@ -156,16 +156,17 @@ export const crmStageMoves = (database, key, accountId) => {
|
|
|
156
156
|
}));
|
|
157
157
|
return { moves, through };
|
|
158
158
|
};
|
|
159
|
-
/** When stage
|
|
159
|
+
/** When the stage-task rules were first set: a stage entered before then opens no task. */
|
|
160
160
|
export const crmStageTasksSince = (database, key) => {
|
|
161
161
|
const row = database
|
|
162
162
|
.prepare("SELECT at FROM store_settings WHERE key = ?")
|
|
163
163
|
.get(stageTasksMark(crmSettingKey(key, "stage-tasks")));
|
|
164
164
|
return row ? Number(row.at) : null;
|
|
165
165
|
};
|
|
166
|
+
/** `at` stays when the rules were first set: the stage history counts from then, whenever the history arrives. */
|
|
166
167
|
export const saveStageMovesMark = (database, key, through, now) => {
|
|
167
168
|
database
|
|
168
|
-
.prepare("INSERT
|
|
169
|
+
.prepare("INSERT INTO store_settings (key, value, at) VALUES (?, ?, ?) ON CONFLICT (key) DO UPDATE SET value = excluded.value")
|
|
169
170
|
.run(stageTasksMark(crmSettingKey(key, "stage-tasks")), JSON.stringify(through), now);
|
|
170
171
|
};
|
|
171
172
|
export const parseCrmSettingValue = (name, value) => {
|