@wirecat/cli-core 0.618.0 → 0.620.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 +453 -1
- package/README.md +1 -1
- package/THIRD_PARTY_NOTICES +3 -0
- package/dist/background/deliver.d.ts +5 -2
- package/dist/background/deliver.js +30 -12
- package/dist/background/report/render.d.ts +3 -1
- package/dist/background/report/render.js +3 -1
- package/dist/calendar/cli/answers.d.ts +1 -0
- package/dist/calendar/cli/answers.js +6 -1
- package/dist/calendar/cli/index.js +2 -14
- package/dist/calendar/cli/people-link.d.ts +3 -0
- package/dist/calendar/cli/people-link.js +23 -0
- package/dist/calendar/cli/search-all.d.ts +1 -1
- package/dist/calendar/cli/search-all.js +9 -10
- package/dist/calendar/index.d.ts +1 -1
- package/dist/calendar/index.js +1 -1
- package/dist/calendar/mcp/index.d.ts +12 -12
- package/dist/calendar/prepare.js +1 -0
- package/dist/cli/base-stages-command.d.ts +22 -0
- package/dist/cli/base-stages-command.js +174 -0
- package/dist/cli/bot/buttons.d.ts +3 -0
- package/dist/cli/bot/buttons.js +24 -0
- package/dist/cli/bot/messages.js +14 -1
- package/dist/cli/bot/port.d.ts +8 -0
- package/dist/cli/command-runs.d.ts +2 -0
- package/dist/cli/command-runs.js +3 -1
- package/dist/cli/config-command.js +6 -1
- package/dist/cli/context.js +1 -0
- package/dist/cli/contract-templates.js +31 -2
- package/dist/cli/group-lock.d.ts +42 -0
- package/dist/cli/group-lock.js +130 -0
- package/dist/cli/index.d.ts +7 -2
- package/dist/cli/index.js +7 -2
- package/dist/cli/messenger/backfill-command.js +27 -15
- package/dist/cli/messenger/contacts-command.js +12 -4
- package/dist/cli/messenger/context.js +1 -0
- package/dist/cli/messenger/conversations-command.js +2 -1
- package/dist/cli/messenger/doctor-command.js +3 -1
- package/dist/cli/messenger/message-items.js +1 -1
- package/dist/cli/messenger/models-command.js +1 -1
- package/dist/cli/messenger/search-answers.d.ts +5 -5
- package/dist/cli/messenger/search-answers.js +9 -20
- package/dist/cli/messenger/search-command.d.ts +2 -2
- package/dist/cli/messenger/search-command.js +8 -18
- package/dist/cli/messenger/store-maintenance-command.d.ts +22 -2
- package/dist/cli/messenger/store-maintenance-command.js +9 -4
- package/dist/cli/model-catalogue-command.d.ts +3 -1
- package/dist/cli/model-catalogue-command.js +48 -2
- package/dist/cli/profile.d.ts +8 -0
- package/dist/cli/profile.js +25 -0
- package/dist/cli/program.js +2 -0
- package/dist/cli/runs/search-query.d.ts +1 -1
- package/dist/cli/runs/search-query.js +4 -0
- package/dist/cli/runs/search.d.ts +5 -1
- package/dist/cli/runs/search.js +12 -1
- package/dist/cli/search-harder-consents-command.d.ts +18 -0
- package/dist/cli/search-harder-consents-command.js +83 -0
- package/dist/cli/search-left-out.d.ts +6 -1
- package/dist/cli/search-left-out.js +9 -4
- package/dist/cli/settings.d.ts +8 -0
- package/dist/cli/settings.js +17 -2
- package/dist/cli/shared-command-exports.d.ts +16 -0
- package/dist/cli/steps.js +0 -6
- package/dist/cli/store-recover.d.ts +3 -3
- package/dist/cli/store-recover.js +26 -26
- package/dist/cli/store-settings.d.ts +2 -0
- package/dist/cli/store-settings.js +37 -9
- package/dist/cli/templates-command.js +4 -4
- package/dist/crm/cli/index.js +40 -8
- package/dist/crm/cli/search-all.js +7 -6
- package/dist/crm/group-hint.d.ts +8 -0
- package/dist/crm/group-hint.js +13 -0
- package/dist/crm/source.d.ts +12 -1
- package/dist/crm/sync.d.ts +31 -2
- package/dist/crm/sync.js +110 -3
- package/dist/crm/testing/acme-scenario.d.ts +40 -0
- package/dist/crm/testing/acme-scenario.js +170 -0
- package/dist/crm/testing/index.d.ts +1 -0
- package/dist/crm/testing/index.js +1 -0
- package/dist/crm/testing/source-contract.js +12 -0
- package/dist/crm/testing/source.d.ts +2 -0
- package/dist/crm/testing/source.js +1 -0
- package/dist/domain/addresses.d.ts +13 -0
- package/dist/domain/addresses.js +63 -0
- package/dist/domain/categories.d.ts +3 -0
- package/dist/domain/categories.js +2 -0
- package/dist/domain/crm.d.ts +23 -0
- package/dist/domain/crm.js +7 -0
- package/dist/domain/mentions.d.ts +2 -12
- package/dist/domain/mentions.js +2 -19
- package/dist/domain/models.d.ts +2 -0
- package/dist/domain/orders.d.ts +8 -1
- package/dist/domain/orders.js +6 -2
- package/dist/domain/phones.d.ts +26 -0
- package/dist/domain/phones.js +48 -0
- package/dist/domain/record-types.d.ts +1 -1
- package/dist/domain/record-types.js +1 -0
- package/dist/domain/records.d.ts +5 -0
- package/dist/domain/ref-summaries.d.ts +39 -0
- package/dist/domain/ref-summaries.js +51 -3
- package/dist/domain/relations.d.ts +12 -2
- package/dist/domain/relations.js +14 -3
- package/dist/domain/vcard.d.ts +2 -2
- package/dist/domain/vcard.js +4 -10
- package/dist/embeddings/models.d.ts +11 -3
- package/dist/embeddings/models.js +28 -4
- package/dist/entities/business-context.d.ts +149 -0
- package/dist/entities/business-context.js +192 -0
- package/dist/entities/contact-activity.d.ts +2 -0
- package/dist/entities/contact-activity.js +7 -1
- package/dist/entities/contact-context.d.ts +160 -0
- package/dist/entities/contact-context.js +6 -0
- package/dist/entities/deal.d.ts +10 -0
- package/dist/entities/deal.js +2 -0
- package/dist/entities/hit.d.ts +9 -2
- package/dist/entities/hit.js +6 -2
- package/dist/entities/index.d.ts +32 -4
- package/dist/entities/index.js +5 -0
- package/dist/entities/mention.d.ts +4 -3
- package/dist/entities/mention.js +2 -1
- package/dist/entities/message-transfer.d.ts +4 -2
- package/dist/entities/message-transfer.js +2 -2
- package/dist/entities/model.d.ts +2 -2
- package/dist/entities/model.js +1 -1
- package/dist/entities/relationship.d.ts +4 -0
- package/dist/entities/relationship.js +4 -0
- package/dist/errors/reasons.js +39 -4
- package/dist/fetch/page.js +2 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/meetings/context-store.d.ts +2 -0
- package/dist/meetings/findings-rules.d.ts +4 -0
- package/dist/meetings/findings-rules.js +5 -2
- package/dist/meetings/hit-query.d.ts +1 -1
- package/dist/meetings/hit-query.js +2 -2
- package/dist/meetings/index.d.ts +1 -0
- package/dist/meetings/index.js +1 -0
- package/dist/meetings/result-items.d.ts +1 -0
- package/dist/meetings/result-items.js +1 -0
- package/dist/meetings/store.d.ts +4 -0
- package/dist/meetings/store.js +2 -1
- package/dist/models/anthropic.js +5 -0
- package/dist/models/index.js +22 -5
- package/dist/runs/events.d.ts +11 -1
- package/dist/runs/events.js +3 -1
- package/dist/runs/log-attributes.js +4 -0
- package/dist/runs/log-events.js +2 -0
- package/dist/runs/recording.d.ts +3 -0
- package/dist/runs/recording.js +36 -12
- package/dist/runs/run.d.ts +12 -3
- package/dist/runs/run.js +32 -13
- package/dist/search/account-list.d.ts +6 -0
- package/dist/search/account-list.js +9 -3
- package/dist/search/groups.d.ts +68 -0
- package/dist/search/groups.js +172 -0
- package/dist/search/question-period.d.ts +10 -0
- package/dist/search/question-period.js +133 -0
- package/dist/sends/buttons.d.ts +2 -0
- package/dist/sends/buttons.js +6 -1
- package/dist/services/account-groups.d.ts +28 -0
- package/dist/services/account-groups.js +89 -0
- package/dist/services/archive.d.ts +7 -1
- package/dist/services/archive.js +19 -3
- package/dist/services/business-context.d.ts +113 -0
- package/dist/services/business-context.js +156 -0
- package/dist/services/communications.d.ts +50 -0
- package/dist/services/communications.js +140 -0
- package/dist/services/deal-context.d.ts +90 -0
- package/dist/services/deal-context.js +131 -0
- package/dist/services/deps.d.ts +2 -0
- package/dist/services/enrichment.d.ts +12 -2
- package/dist/services/enrichment.js +47 -6
- package/dist/services/events-search.js +2 -1
- package/dist/services/group-guard.d.ts +28 -0
- package/dist/services/group-guard.js +131 -0
- package/dist/services/index.d.ts +19 -4
- package/dist/services/index.js +12 -3
- package/dist/services/ingest.d.ts +12 -0
- package/dist/services/ingest.js +4 -0
- package/dist/services/meaning-model.d.ts +7 -0
- package/dist/services/meaning-model.js +20 -0
- package/dist/services/meeting-context.d.ts +6 -1
- package/dist/services/meeting-context.js +12 -5
- package/dist/services/message-resolution.js +2 -3
- package/dist/services/messages-meaning.js +4 -5
- package/dist/services/messages-search.d.ts +5 -1
- package/dist/services/messages-search.js +1 -1
- package/dist/services/messages.d.ts +1 -4
- package/dist/services/messages.js +0 -2
- package/dist/services/metadata.d.ts +1 -1
- package/dist/services/model-downloads.d.ts +1 -1
- package/dist/services/model-downloads.js +6 -4
- package/dist/services/money-context.d.ts +73 -0
- package/dist/services/money-context.js +77 -0
- package/dist/services/notes-search.d.ts +4 -1
- package/dist/services/notes-search.js +19 -7
- package/dist/services/organization-context.d.ts +80 -0
- package/dist/services/organization-context.js +154 -0
- package/dist/services/people.d.ts +22 -6
- package/dist/services/people.js +38 -8
- package/dist/services/person-business.d.ts +43 -0
- package/dist/services/person-business.js +67 -0
- package/dist/services/person-context.d.ts +11 -1
- package/dist/services/person-context.js +25 -10
- package/dist/services/person-preparation.d.ts +5 -1
- package/dist/services/person-preparation.js +10 -3
- package/dist/services/person-timeline.d.ts +6 -1
- package/dist/services/person-timeline.js +10 -2
- package/dist/services/proposals/kinds/identity-link.d.ts +4 -0
- package/dist/services/proposals/kinds/identity-link.js +48 -0
- package/dist/services/proposals/kinds/orders.js +1 -1
- package/dist/services/proposals/registry.js +2 -0
- package/dist/services/search-all-deprecated.d.ts +40 -0
- package/dist/services/search-all-deprecated.js +10 -0
- package/dist/services/search-all-meetings.d.ts +50 -52
- package/dist/services/search-all-meetings.js +265 -272
- package/dist/services/search-all.d.ts +34 -6
- package/dist/services/search-all.js +118 -24
- package/dist/services/search-harder.d.ts +64 -0
- package/dist/services/search-harder.js +194 -0
- package/dist/services/search-kind.d.ts +12 -5
- package/dist/services/search-kind.js +49 -10
- package/dist/services/text-model-choice.js +1 -1
- package/dist/store/accounts.d.ts +18 -0
- package/dist/store/accounts.js +18 -3
- package/dist/store/chats.d.ts +3 -1
- package/dist/store/chats.js +6 -0
- package/dist/store/damage.d.ts +43 -10
- package/dist/store/damage.js +119 -36
- package/dist/store/decisions.js +3 -1
- package/dist/store/evidence-findings.d.ts +14 -0
- package/dist/store/evidence-findings.js +2 -0
- package/dist/store/exports.d.ts +2 -1
- package/dist/store/identity-links.d.ts +92 -0
- package/dist/store/identity-links.js +6 -0
- package/dist/store/index.d.ts +11 -5
- package/dist/store/index.js +5 -2
- package/dist/store/journal.d.ts +10 -0
- package/dist/store/journal.js +33 -0
- package/dist/store/marks.d.ts +19 -1
- package/dist/store/marks.js +23 -1
- package/dist/store/people.d.ts +28 -2
- package/dist/store/people.js +15 -1
- package/dist/store/sqlite/accounts.js +2 -0
- package/dist/store/sqlite/added-sources.d.ts +15 -3
- package/dist/store/sqlite/added-sources.js +26 -5
- package/dist/store/sqlite/base-stages.d.ts +122 -0
- package/dist/store/sqlite/base-stages.js +443 -0
- package/dist/store/sqlite/base-suggest.d.ts +13 -0
- package/dist/store/sqlite/base-suggest.js +62 -0
- package/dist/store/sqlite/business-contracts.js +10 -1
- package/dist/store/sqlite/chats.d.ts +2 -0
- package/dist/store/sqlite/chats.js +6 -0
- package/dist/store/sqlite/crm-settings.d.ts +12 -2
- package/dist/store/sqlite/crm-settings.js +56 -13
- package/dist/store/sqlite/crm.d.ts +13 -1
- package/dist/store/sqlite/crm.js +20 -4
- package/dist/store/sqlite/deals.d.ts +33 -0
- package/dist/store/sqlite/deals.js +42 -4
- package/dist/store/sqlite/decision-requests.js +7 -3
- package/dist/store/sqlite/email-index.js +8 -4
- package/dist/store/sqlite/evidence-findings.js +29 -5
- package/dist/store/sqlite/findings-core.js +4 -2
- package/dist/store/sqlite/identity-link-rows.d.ts +50 -0
- package/dist/store/sqlite/identity-link-rows.js +146 -0
- package/dist/store/sqlite/identity-proposals.d.ts +53 -0
- package/dist/store/sqlite/identity-proposals.js +234 -0
- package/dist/store/sqlite/involvements.d.ts +1 -1
- package/dist/store/sqlite/involvements.js +4 -2
- package/dist/store/sqlite/knowledge.js +2 -2
- package/dist/store/sqlite/link-kinds.d.ts +1 -0
- package/dist/store/sqlite/link-kinds.js +1 -0
- package/dist/store/sqlite/link-row.d.ts +6 -0
- package/dist/store/sqlite/link-row.js +35 -0
- package/dist/store/sqlite/link-source.d.ts +10 -0
- package/dist/store/sqlite/link-source.js +16 -0
- package/dist/store/sqlite/manifest.js +1 -0
- package/dist/store/sqlite/meeting-context-links.js +9 -6
- package/dist/store/sqlite/meeting-hit-query.d.ts +5 -0
- package/dist/store/sqlite/meeting-hit-query.js +33 -0
- package/dist/store/sqlite/meeting-hit-search.js +10 -8
- package/dist/store/sqlite/meetings.js +1 -1
- package/dist/store/sqlite/memories.js +9 -7
- package/dist/store/sqlite/mentions.d.ts +4 -1
- package/dist/store/sqlite/mentions.js +14 -9
- package/dist/store/sqlite/migrations.generated.js +8 -0
- package/dist/store/sqlite/notes.d.ts +8 -2
- package/dist/store/sqlite/notes.js +17 -34
- package/dist/store/sqlite/open-store.js +3 -0
- package/dist/store/sqlite/open.js +11 -2
- package/dist/store/sqlite/orders.d.ts +2 -0
- package/dist/store/sqlite/orders.js +9 -3
- package/dist/store/sqlite/organization-categories.d.ts +18 -0
- package/dist/store/sqlite/organization-categories.js +34 -0
- package/dist/store/sqlite/organizations.d.ts +5 -1
- package/dist/store/sqlite/organizations.js +14 -4
- package/dist/store/sqlite/person-links.d.ts +19 -4
- package/dist/store/sqlite/person-links.js +49 -11
- package/dist/store/sqlite/person-match-scan.d.ts +13 -0
- package/dist/store/sqlite/person-match-scan.js +251 -0
- package/dist/store/sqlite/person-matcher.d.ts +16 -0
- package/dist/store/sqlite/person-matcher.js +32 -0
- package/dist/store/sqlite/person-preparation.d.ts +7 -3
- package/dist/store/sqlite/person-preparation.js +20 -6
- package/dist/store/sqlite/proposed-actions.js +6 -0
- package/dist/store/sqlite/record-matching.d.ts +2 -8
- package/dist/store/sqlite/record-matching.js +8 -40
- package/dist/store/sqlite/record-projectors-phone.js +2 -1
- package/dist/store/sqlite/record-relationships.d.ts +52 -0
- package/dist/store/sqlite/record-relationships.js +243 -0
- package/dist/store/sqlite/record-run.d.ts +5 -0
- package/dist/store/sqlite/record-run.js +62 -2
- package/dist/store/sqlite/record-writes.d.ts +1 -1
- package/dist/store/sqlite/record-writes.js +4 -2
- package/dist/store/sqlite/records.js +27 -2
- package/dist/store/sqlite/ref-accounts.d.ts +13 -0
- package/dist/store/sqlite/ref-accounts.js +117 -0
- package/dist/store/sqlite/ref-summaries.js +5 -1
- package/dist/store/sqlite/relation-list.d.ts +4 -1
- package/dist/store/sqlite/relation-list.js +22 -4
- package/dist/store/sqlite/relation-proposals.d.ts +6 -0
- package/dist/store/sqlite/relation-proposals.js +3 -0
- package/dist/store/sqlite/relation-roles.js +12 -6
- package/dist/store/sqlite/reported-relations.d.ts +11 -1
- package/dist/store/sqlite/reported-relations.js +59 -18
- package/dist/store/sqlite/schema.d.ts +32 -0
- package/dist/store/sqlite/schema.js +3 -0
- package/dist/store/sqlite/transaction.js +11 -1
- package/dist/store/store.d.ts +3 -0
- package/dist/store/store.js +5 -0
- package/dist/store/write-events.d.ts +18 -0
- package/dist/store/write-events.js +88 -0
- package/package.json +3 -3
- package/parity.json +37 -16
- package/schemas/records-envelope.v1.schema.json +3 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { isDocumentAccountProvider, isFolderProvider } from "../../domain/providers.js";
|
|
2
|
+
import { parseReference } from "../../domain/references.js";
|
|
3
|
+
import { ENTITY_TABLES, isEntityType } from "./entity-types.js";
|
|
4
|
+
import { storedThing, thingOf } from "./things.js";
|
|
5
|
+
const accountKeys = (database, ids) => {
|
|
6
|
+
const unique = [...new Set(ids)];
|
|
7
|
+
if (unique.length === 0)
|
|
8
|
+
return [];
|
|
9
|
+
return database
|
|
10
|
+
.prepare(`SELECT id, provider, external_id FROM accounts WHERE id IN (${unique.map(() => "?").join(",")})`)
|
|
11
|
+
.all(...unique)
|
|
12
|
+
.map(({ id, provider, external_id }) =>
|
|
13
|
+
// A notes folder or a documents account is `notes:<its store id>`, as source lists and groups name it.
|
|
14
|
+
isFolderProvider(String(provider)) || isDocumentAccountProvider(String(provider))
|
|
15
|
+
? { provider: "notes", account: String(id) }
|
|
16
|
+
: { provider: String(provider), account: String(external_id) });
|
|
17
|
+
};
|
|
18
|
+
const hasAccountColumn = new Map();
|
|
19
|
+
const accountColumnIn = (database, table) => {
|
|
20
|
+
const known = hasAccountColumn.get(table);
|
|
21
|
+
if (known !== undefined)
|
|
22
|
+
return known;
|
|
23
|
+
const has = database
|
|
24
|
+
.prepare(`PRAGMA table_info(${table})`)
|
|
25
|
+
.all()
|
|
26
|
+
.some((column) => column.name === "account_id");
|
|
27
|
+
hasAccountColumn.set(table, has);
|
|
28
|
+
return has;
|
|
29
|
+
};
|
|
30
|
+
const ids = (rows) => rows.flatMap(({ account_id }) => (account_id === null || account_id === undefined ? [] : [Number(account_id)]));
|
|
31
|
+
/** Every source record of the row, as a CRM's or another import's copy of it. */
|
|
32
|
+
const recordAccounts = (database, type, id) => ids(database.prepare("SELECT account_id FROM records WHERE entity_type = ? AND entity_id = ?").all(type, id));
|
|
33
|
+
/** The accounts that hold one of the person's identities. */
|
|
34
|
+
const personAccounts = (database, id) => ids(database
|
|
35
|
+
.prepare("SELECT ai.account_id FROM identity_links l JOIN account_identities ai ON ai.identity_id = l.identity_id " +
|
|
36
|
+
"WHERE l.person_id = ?")
|
|
37
|
+
.all(id));
|
|
38
|
+
const thingAccountIds = (database, thing) => {
|
|
39
|
+
switch (thing.type) {
|
|
40
|
+
// A note written in memo, never a file of a folder (`document:`), is the owner's: no account brought it.
|
|
41
|
+
case "note":
|
|
42
|
+
return [];
|
|
43
|
+
case "account":
|
|
44
|
+
return [thing.id];
|
|
45
|
+
case "message":
|
|
46
|
+
return ids(database
|
|
47
|
+
.prepare("SELECT c.account_id FROM messages m JOIN chats c ON c.id = m.chat_id WHERE m.id = ?")
|
|
48
|
+
.all(thing.id));
|
|
49
|
+
// An event is its copies' calendars' and its meetings' accounts; one with no copy is the owner's own.
|
|
50
|
+
case "event":
|
|
51
|
+
return [
|
|
52
|
+
...ids(database
|
|
53
|
+
.prepare("SELECT c.account_id FROM calendar_entries e JOIN calendars c ON c.id = e.calendar_id WHERE e.event_id = ?")
|
|
54
|
+
.all(thing.id)),
|
|
55
|
+
...ids(database.prepare("SELECT account_id FROM meetings WHERE event_id = ?").all(thing.id)),
|
|
56
|
+
];
|
|
57
|
+
case "identity":
|
|
58
|
+
return ids(database.prepare("SELECT account_id FROM account_identities WHERE identity_id = ?").all(thing.id));
|
|
59
|
+
}
|
|
60
|
+
if (!isEntityType(thing.type))
|
|
61
|
+
return [];
|
|
62
|
+
const table = ENTITY_TABLES[thing.type];
|
|
63
|
+
const own = accountColumnIn(database, table)
|
|
64
|
+
? ids(database.prepare(`SELECT account_id FROM ${table} WHERE id = ?`).all(thing.id))
|
|
65
|
+
: [];
|
|
66
|
+
return [
|
|
67
|
+
...own,
|
|
68
|
+
...recordAccounts(database, thing.type, thing.id),
|
|
69
|
+
...(thing.type === "person" ? personAccounts(database, thing.id) : []),
|
|
70
|
+
];
|
|
71
|
+
};
|
|
72
|
+
const endOf = (database, type, id) => type === null || type === undefined || id === null || id === undefined
|
|
73
|
+
? []
|
|
74
|
+
: [accountKeys(database, thingAccountIds(database, storedThing(type, id)))];
|
|
75
|
+
/** A proposed action (`action:<id>`) and a suggested link (`link:<id>`): ids of the proposal registry, not references. */
|
|
76
|
+
const PROPOSAL = /^(action|link):([1-9]\d*)$/;
|
|
77
|
+
/**
|
|
78
|
+
* Each part a referenced thing belongs to, every one of which a group must hold: one part for most things; a proposed
|
|
79
|
+
* action's own account and its target; a link's two ends. `undefined` when the store holds nothing for it.
|
|
80
|
+
*/
|
|
81
|
+
export const refEndsOf = (database, text) => {
|
|
82
|
+
const proposal = PROPOSAL.exec(text.trim());
|
|
83
|
+
if (proposal) {
|
|
84
|
+
const [, kind, id] = proposal;
|
|
85
|
+
if (kind === "action") {
|
|
86
|
+
const row = database
|
|
87
|
+
.prepare("SELECT account_id, target_type, target_id FROM proposed_actions WHERE id = ?")
|
|
88
|
+
.get(Number(id));
|
|
89
|
+
if (!row)
|
|
90
|
+
return undefined;
|
|
91
|
+
return [
|
|
92
|
+
...(row.account_id === null ? [] : [accountKeys(database, [Number(row.account_id)])]),
|
|
93
|
+
...endOf(database, row.target_type, row.target_id),
|
|
94
|
+
];
|
|
95
|
+
}
|
|
96
|
+
const link = database.prepare("SELECT from_type, from_id, to_type, to_id FROM links WHERE id = ?").get(Number(id));
|
|
97
|
+
if (!link)
|
|
98
|
+
return undefined;
|
|
99
|
+
return [...endOf(database, link.from_type, link.from_id), ...endOf(database, link.to_type, link.to_id)];
|
|
100
|
+
}
|
|
101
|
+
let reference;
|
|
102
|
+
try {
|
|
103
|
+
reference = parseReference(text);
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
if (reference.type === "message" || reference.type === "chat")
|
|
109
|
+
return [[{ provider: reference.provider, account: reference.account }]];
|
|
110
|
+
const thing = thingOf(database, reference);
|
|
111
|
+
if (!thing)
|
|
112
|
+
return undefined;
|
|
113
|
+
if (reference.type === "meeting")
|
|
114
|
+
return [accountKeys(database, [reference.accountId])];
|
|
115
|
+
return [accountKeys(database, thingAccountIds(database, thing))];
|
|
116
|
+
};
|
|
117
|
+
export const refAccountsOf = (database, text) => refEndsOf(database, text)?.flat();
|
|
@@ -5,6 +5,7 @@ import { orderLine } from "../../domain/orders.js";
|
|
|
5
5
|
import { filesAccountSql, providerLabel, providerNamed } from "../../domain/providers.js";
|
|
6
6
|
import { oneLine, } from "../../domain/ref-summaries.js";
|
|
7
7
|
import { formatReference, parseReference } from "../../domain/references.js";
|
|
8
|
+
import { refAccountsOf, refEndsOf } from "./ref-accounts.js";
|
|
8
9
|
const EXCERPT_CHARS = 280;
|
|
9
10
|
const TITLE_CHARS = 120;
|
|
10
11
|
const MEMORY_TITLE_CHARS = 80;
|
|
@@ -214,7 +215,8 @@ const READERS = {
|
|
|
214
215
|
deleted: "x.deleted_at",
|
|
215
216
|
})),
|
|
216
217
|
order: byId("order", "orders", () => ({
|
|
217
|
-
title: "json_object('id', x.id, 'number', x.number, 'supplierName', x.supplier_name, '
|
|
218
|
+
title: "json_object('id', x.id, 'number', x.number, 'supplierName', x.supplier_name, 'direction', x.direction, " +
|
|
219
|
+
`'customerName', ${subjectName("x.customer_type", "x.customer_id")}, 'total', x.total, ` +
|
|
218
220
|
"'currency', x.currency, 'status', x.status, 'review', x.review)",
|
|
219
221
|
holder: "(SELECT name FROM projects WHERE id = x.project_id)",
|
|
220
222
|
at: "x.created_at",
|
|
@@ -487,4 +489,6 @@ const summaryOf = ({ sender, excerpt, ...summary }, withExcerpt) => withExcerpt
|
|
|
487
489
|
export const refSummariesOf = (database, texts, options = {}) => new Map([...refReadsOf(database, texts, options)].map(([text, read]) => [text, summaryOf(read, options.excerpt === true)]));
|
|
488
490
|
export const refsStoreOver = (context) => ({
|
|
489
491
|
summaries: async (texts, options) => refSummariesOf(context.database, texts, { ...options, now: context.now() }),
|
|
492
|
+
accounts: async (text) => refAccountsOf(context.database, text),
|
|
493
|
+
ends: async (text) => refEndsOf(context.database, text),
|
|
490
494
|
});
|
|
@@ -15,7 +15,10 @@ export interface RelationFilter {
|
|
|
15
15
|
at?: number;
|
|
16
16
|
/** Ended relationships are left out unless asked for, or unless `standing` is `ended`. */
|
|
17
17
|
includeEnded?: boolean;
|
|
18
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* A source's relationships only from these accounts; the owner's and agents' too, unless an end — a CRM's deal or
|
|
20
|
+
* lead, a chat, a mail thread — belongs to an account outside them.
|
|
21
|
+
*/
|
|
19
22
|
accounts?: AccountKey[];
|
|
20
23
|
/** No row with an end of these types, such as `deal` and `lead` a profile denies. */
|
|
21
24
|
deniedKinds?: string[];
|
|
@@ -5,8 +5,16 @@ import { idsOf } from "./merged-persons.js";
|
|
|
5
5
|
import { linkOf } from "./notes.js";
|
|
6
6
|
import { proposalOperationsOver } from "./proposed-actions.js";
|
|
7
7
|
import { relationOf } from "./relation-proposals.js";
|
|
8
|
+
import { extraRolesOf } from "./relation-roles.js";
|
|
8
9
|
import { thingOf } from "./things.js";
|
|
9
10
|
const KINDS = RELATION_KINDS.map(linkKind).join(", ");
|
|
11
|
+
/** The ends a relationship may have that belong to one account. */
|
|
12
|
+
const ACCOUNT_ENDS = [
|
|
13
|
+
["deal", "deals"],
|
|
14
|
+
["lead", "leads"],
|
|
15
|
+
["chat", "chats"],
|
|
16
|
+
["email_thread", "email_threads"],
|
|
17
|
+
];
|
|
10
18
|
const typeOfRef = (column) => `substr(${column}, 1, instr(${column}, ':') - 1)`;
|
|
11
19
|
const idOfRef = (column) => `CAST(substr(${column}, instr(${column}, ':') + 1) AS INTEGER)`;
|
|
12
20
|
export const relationListOver = (context) => {
|
|
@@ -46,8 +54,10 @@ export const relationListOver = (context) => {
|
|
|
46
54
|
if (filter.to !== undefined)
|
|
47
55
|
end("to", filter.to);
|
|
48
56
|
const about = filter.about === undefined ? undefined : thing(filter.about, "the reference");
|
|
57
|
+
const aboutIds = about ? idsOf(database, about.type, about.id) : "[]";
|
|
49
58
|
if (about)
|
|
50
|
-
add(`((r.from_type = ? AND r.from_id IN (SELECT value FROM json_each(?))) OR (r.to_type = ? AND r.to_id IN (SELECT value FROM json_each(?))))`, about.type,
|
|
59
|
+
add(`((r.from_type = ? AND r.from_id IN (SELECT value FROM json_each(?))) OR (r.to_type = ? AND r.to_id IN (SELECT value FROM json_each(?))))`, about.type, aboutIds, about.type, aboutIds);
|
|
60
|
+
const aboutSame = new Set(JSON.parse(aboutIds));
|
|
51
61
|
if (filter.at !== undefined)
|
|
52
62
|
add("(r.starts_at IS NULL OR r.starts_at <= ?) AND (r.ends_at IS NULL OR ? < r.ends_at)", filter.at, filter.at);
|
|
53
63
|
if (filter.standing !== undefined)
|
|
@@ -61,7 +71,13 @@ export const relationListOver = (context) => {
|
|
|
61
71
|
.get(key.provider, key.account);
|
|
62
72
|
return row ? [Number(row.id)] : [];
|
|
63
73
|
});
|
|
64
|
-
|
|
74
|
+
const list = ids.map(() => "?").join(", ") || "NULL";
|
|
75
|
+
add(`(r.account_id IS NULL OR r.account_id IN (${list}))`, ...ids);
|
|
76
|
+
// An end that belongs to an account outside the scope keeps the row out, whoever wrote it.
|
|
77
|
+
for (const side of ["from", "to"])
|
|
78
|
+
for (const [type, table] of ACCOUNT_ENDS)
|
|
79
|
+
add(`NOT (r.${side}_type = '${type}' AND EXISTS (SELECT 1 FROM ${table} e WHERE e.id = r.${side}_id ` +
|
|
80
|
+
`AND e.account_id IS NOT NULL AND e.account_id NOT IN (${list})))`, ...ids);
|
|
65
81
|
}
|
|
66
82
|
if (filter.deniedKinds?.length) {
|
|
67
83
|
const list = filter.deniedKinds.map(() => "?").join(", ");
|
|
@@ -89,7 +105,7 @@ export const relationListOver = (context) => {
|
|
|
89
105
|
if (!about)
|
|
90
106
|
return item;
|
|
91
107
|
const from = thingOf(database, item.from);
|
|
92
|
-
const outgoing = from?.type === about.type && from.id
|
|
108
|
+
const outgoing = from?.type === about.type && aboutSame.has(from.id);
|
|
93
109
|
return outgoing
|
|
94
110
|
? { ...item, direction: "outgoing" }
|
|
95
111
|
: { ...item, direction: "incoming", inverse: inverseOf(item.kind) };
|
|
@@ -101,7 +117,9 @@ export const relationListOver = (context) => {
|
|
|
101
117
|
const fromThing = thingOf(database, from);
|
|
102
118
|
const toThing = thingOf(database, to);
|
|
103
119
|
const roles = fromThing && toThing ? rolesOf(kind, fromThing.type, toThing.type) : undefined;
|
|
104
|
-
|
|
120
|
+
if (role === null || roles === undefined || roles.includes(role) || !fromThing || !toThing)
|
|
121
|
+
return false;
|
|
122
|
+
return !extraRolesOf(database, kind, fromThing.type, toThing.type).includes(role);
|
|
105
123
|
}
|
|
106
124
|
function linkItem(id, at) {
|
|
107
125
|
const relation = relationOf(linkOf(database, database.prepare("SELECT * FROM links WHERE id = ?").get(id)), at);
|
|
@@ -26,9 +26,15 @@ export interface KnowledgeRelation {
|
|
|
26
26
|
roleNote: string | null;
|
|
27
27
|
/** How much the owner deals with a person on a project himself; `null` is unknown. */
|
|
28
28
|
engagement: Engagement | null;
|
|
29
|
+
/** The source's own words for it, off the role list (`metadata.source_role`), never its role. */
|
|
30
|
+
sourceRoles?: string[];
|
|
29
31
|
/** When it held, as ISO times; `endsAt` exclusive; `null` when nobody said. */
|
|
30
32
|
startsAt: string | null;
|
|
31
33
|
endsAt: string | null;
|
|
34
|
+
/** The source account that reported it, `<provider>:<account>`; absent for the owner's and an agent's. */
|
|
35
|
+
account?: string;
|
|
36
|
+
/** How a source made it when it stated none, such as `department-head`. */
|
|
37
|
+
derived?: string;
|
|
32
38
|
createdAt: string;
|
|
33
39
|
}
|
|
34
40
|
export interface RelateInput {
|
|
@@ -23,8 +23,11 @@ export const relationOf = (link, now) => ({
|
|
|
23
23
|
position: link.position ?? null,
|
|
24
24
|
roleNote: link.roleNote ?? null,
|
|
25
25
|
engagement: link.engagement ?? null,
|
|
26
|
+
...(link.sourceRoles ? { sourceRoles: link.sourceRoles } : {}),
|
|
26
27
|
startsAt: isoOf(link.startsAt),
|
|
27
28
|
endsAt: isoOf(link.endsAt),
|
|
29
|
+
...(link.account === undefined ? {} : { account: link.account }),
|
|
30
|
+
...(link.derived === undefined ? {} : { derived: link.derived }),
|
|
28
31
|
createdAt: link.createdAt,
|
|
29
32
|
});
|
|
30
33
|
/** `relationship` proposals in `proposed_actions`: an agent's or a rule's, and the rejected ones a scan skips. */
|
|
@@ -28,17 +28,23 @@ export const extraRolesOf = (database, kind, from, to) => {
|
|
|
28
28
|
};
|
|
29
29
|
/** `checkRole` with the owner's own roles for the list the ends take. */
|
|
30
30
|
export const checkStoredRole = (database, kind, from, to, role) => checkRole(kind, from, to, role, extraRolesOf(database, kind, from, to));
|
|
31
|
-
/** The links whose role comes from
|
|
31
|
+
/** The `related-to` links whose role comes from a list, by the types that name it. */
|
|
32
|
+
const RELATED_FROM = { deal: "'deal', 'lead'", contract: "'contract'" };
|
|
33
|
+
/**
|
|
34
|
+
* The links whose role comes from this list: a person's on an organization or a project, a deal's or a lead's people,
|
|
35
|
+
* a contract's parties.
|
|
36
|
+
*/
|
|
32
37
|
const usedBy = (database, list, role) => {
|
|
33
|
-
const
|
|
34
|
-
|
|
38
|
+
const related = RELATED_FROM[list];
|
|
39
|
+
const links = related
|
|
40
|
+
? `kind = 'related-to' AND from_type IN (${related})`
|
|
35
41
|
: `kind = 'member-of' AND from_type = 'person' AND to_type = '${list}'`;
|
|
36
42
|
// A pending proposal would make the role a relationship's once approved: it counts as a use.
|
|
37
|
-
const proposals =
|
|
38
|
-
?
|
|
43
|
+
const proposals = related
|
|
44
|
+
? `target_type IN (${related}) AND json_extract(payload, '$.kind') = 'related-to'`
|
|
39
45
|
: `target_type = 'person' AND json_extract(payload, '$.kind') = 'member-of' AND json_extract(payload, '$.to') LIKE '${list}:%'`;
|
|
40
46
|
return Number(database
|
|
41
|
-
.prepare(`SELECT (SELECT count(*) FROM links WHERE role = ? AND ${links}) + (SELECT count(*) FROM proposed_actions ` +
|
|
47
|
+
.prepare(`SELECT (SELECT count(*) FROM links WHERE role = ? AND deleted_at IS NULL AND ${links}) + (SELECT count(*) FROM proposed_actions ` +
|
|
42
48
|
`WHERE status = 'proposed' AND ${proposals} AND ((kind = 'relationship' AND json_extract(payload, '$.role') = ?) ` +
|
|
43
49
|
"OR (kind = 'relationship_update' AND json_extract(payload, '$.changes.role') = ?))) AS n")
|
|
44
50
|
.get(role, role, role)?.n);
|
|
@@ -42,11 +42,21 @@ export interface ReportedSync {
|
|
|
42
42
|
skippedOwnerRejected: number;
|
|
43
43
|
issues: ReportedIssue[];
|
|
44
44
|
}
|
|
45
|
+
/** What a call did to one relationship. */
|
|
46
|
+
export interface ReportedOutcome {
|
|
47
|
+
outcome: "added" | "kept" | "restored" | "withdrawn" | "proposed" | "skipped";
|
|
48
|
+
/** `kind|from|to|role`, by the stored ends (a partnership's lower id first): one per relationship. */
|
|
49
|
+
key: string;
|
|
50
|
+
/** The link that answers for it, the owner's when the owner holds it; `null` when proposed or skipped. */
|
|
51
|
+
id: string | null;
|
|
52
|
+
}
|
|
45
53
|
export interface ReportedOptions {
|
|
46
54
|
/** `links.source` of a link it writes; `import` when absent. */
|
|
47
55
|
source?: string;
|
|
48
56
|
/** Who proposes in `review` mode; the source's provider, as a rule, when absent. */
|
|
49
57
|
by?: Exclude<Author, "owner">;
|
|
58
|
+
/** Called once per relationship, so a caller that reports in parts counts each one once per run. */
|
|
59
|
+
onOutcome?: (outcome: ReportedOutcome) => void;
|
|
50
60
|
}
|
|
51
61
|
export interface RelationsStore {
|
|
52
62
|
/**
|
|
@@ -60,5 +70,5 @@ export interface RelationsStore {
|
|
|
60
70
|
}
|
|
61
71
|
export declare const KEYED = "kind IN ('member-of', 'related-to', 'reports-to', 'customer-of', 'partner-of') AND to_id IS NOT NULL";
|
|
62
72
|
/** The call, inside a transaction the caller holds: a CRM page saves its rows and their relationships together. */
|
|
63
|
-
export declare const syncReportedIn: (context: StoreContext, key: AccountKey, relationships: ReportedRelation[], scope: ReportedScope, { source, by }?: ReportedOptions) => ReportedSync;
|
|
73
|
+
export declare const syncReportedIn: (context: StoreContext, key: AccountKey, relationships: ReportedRelation[], scope: ReportedScope, { source, by, onOutcome }?: ReportedOptions) => ReportedSync;
|
|
64
74
|
export declare const relationsStoreOver: (context: StoreContext) => RelationsStore;
|
|
@@ -8,12 +8,13 @@ import { relationProposalsOver } from "./relation-proposals.js";
|
|
|
8
8
|
import { checkStoredRole } from "./relation-roles.js";
|
|
9
9
|
import { referenceOfThing, thingOf } from "./things.js";
|
|
10
10
|
import { write } from "./transaction.js";
|
|
11
|
+
const keyOf = (kind, fromType, fromId, toType, toId, role) => `${String(kind)}|${String(fromType)}:${String(fromId)}|${String(toType)}:${String(toId)}|${role == null ? "" : String(role)}`;
|
|
11
12
|
// links_relationship_key's WHERE, word for word: without it SQLite cannot use that partial index, picks
|
|
12
13
|
// (to_type, to_id) and reads every link of a busy end, such as a deal owner's, for each relationship (#2623).
|
|
13
14
|
export const KEYED = "kind IN ('member-of', 'related-to', 'reports-to', 'customer-of', 'partner-of') AND to_id IS NOT NULL";
|
|
14
15
|
const metadataOf = (relation) => relation.metadata && Object.keys(relation.metadata).length ? JSON.stringify(relation.metadata) : null;
|
|
15
16
|
/** The call, inside a transaction the caller holds: a CRM page saves its rows and their relationships together. */
|
|
16
|
-
export const syncReportedIn = (context, key, relationships, scope, { source = "import", by = { bot: key.provider, kind: "rule" } } = {}) => {
|
|
17
|
+
export const syncReportedIn = (context, key, relationships, scope, { source = "import", by = { bot: key.provider, kind: "rule" }, onOutcome } = {}) => {
|
|
17
18
|
const { database, now } = context;
|
|
18
19
|
const proposals = relationProposalsOver(context);
|
|
19
20
|
const accountId = accountPk(context, key);
|
|
@@ -30,8 +31,8 @@ export const syncReportedIn = (context, key, relationships, scope, { source = "i
|
|
|
30
31
|
};
|
|
31
32
|
const seen = new Set();
|
|
32
33
|
const issue = (relation, reason, message) => result.issues.push({ reason, message, from: relation.from, to: relation.to, kind: relation.kind });
|
|
33
|
-
const
|
|
34
|
-
|
|
34
|
+
const samePair = `${KEYED} AND kind = ? AND from_type = ? AND from_id = ? AND to_type = ? AND to_id = ? AND anchor IS NULL`;
|
|
35
|
+
const sameKey = `${samePair} AND ifnull(role, '') = ?`;
|
|
35
36
|
for (const relation of relationships) {
|
|
36
37
|
let from = thingOf(database, relation.from);
|
|
37
38
|
let to = thingOf(database, relation.to);
|
|
@@ -56,12 +57,26 @@ export const syncReportedIn = (context, key, relationships, scope, { source = "i
|
|
|
56
57
|
if (isSymmetric(relation.kind) && to.id < from.id)
|
|
57
58
|
[from, to] = [to, from];
|
|
58
59
|
const wanted = [relation.kind, from.type, from.id, to.type, to.id, role ?? ""];
|
|
60
|
+
const linkKey = keyOf(relation.kind, from.type, from.id, to.type, to.id, role);
|
|
61
|
+
const counted = (outcome, id) => {
|
|
62
|
+
result[outcome === "skipped" ? "skippedOwnerRejected" : outcome] += 1;
|
|
63
|
+
onOutcome?.({ outcome, key: linkKey, id: id == null ? null : String(id) });
|
|
64
|
+
};
|
|
59
65
|
// The owner's link of the same role answers for the source too, and a confirmed one is never withdrawn.
|
|
60
|
-
|
|
66
|
+
const owned = database
|
|
67
|
+
.prepare(`SELECT id, role, metadata FROM links WHERE ${samePair} AND confirmed = 1`)
|
|
68
|
+
.all(...wanted.slice(0, 5));
|
|
69
|
+
const confirmed = owned.find((link) => (link.role ?? "") === (role ?? "")) ?? ownerTook(context, owned, role);
|
|
70
|
+
if (confirmed) {
|
|
71
|
+
// The overridden link is the source's own: still reported, so it is not withdrawn.
|
|
72
|
+
if (confirmed.reported != null)
|
|
73
|
+
seen.add(Number(confirmed.reported));
|
|
74
|
+
counted("kept", confirmed.id);
|
|
61
75
|
continue;
|
|
76
|
+
}
|
|
62
77
|
const toRef = referenceOfThing(database, to);
|
|
63
78
|
if (rejected(context, from, toRef, relation.kind, role)) {
|
|
64
|
-
|
|
79
|
+
counted("skipped", null);
|
|
65
80
|
continue;
|
|
66
81
|
}
|
|
67
82
|
const held = database.prepare(`SELECT * FROM links WHERE ${sameKey} AND account_id = ?`).get(...wanted, accountId);
|
|
@@ -70,13 +85,13 @@ export const syncReportedIn = (context, key, relationships, scope, { source = "i
|
|
|
70
85
|
const metadata = metadataOf(relation);
|
|
71
86
|
if (held) {
|
|
72
87
|
seen.add(Number(held.id));
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
88
|
+
counted(held.deleted_at != null ? "restored" : "kept", held.id);
|
|
89
|
+
const same = held.deleted_at == null && held.starts_at === startsAt && held.ends_at === endsAt && held.metadata === metadata;
|
|
90
|
+
// Any write to a link queues its ends for involvement again, so an unchanged one is left alone.
|
|
91
|
+
if (!same)
|
|
92
|
+
database
|
|
93
|
+
.prepare("UPDATE links SET deleted_at = NULL, starts_at = ?, ends_at = ?, metadata = ?, updated_at = ? WHERE id = ?")
|
|
94
|
+
.run(startsAt, endsAt, metadata, at, Number(held.id));
|
|
80
95
|
continue;
|
|
81
96
|
}
|
|
82
97
|
if (review) {
|
|
@@ -87,16 +102,16 @@ export const syncReportedIn = (context, key, relationships, scope, { source = "i
|
|
|
87
102
|
...(role === null ? {} : { role }),
|
|
88
103
|
reason: `reported by ${key.provider}:${key.account}`,
|
|
89
104
|
}, by);
|
|
90
|
-
|
|
105
|
+
counted("proposed", null);
|
|
91
106
|
continue;
|
|
92
107
|
}
|
|
93
108
|
const row = database
|
|
94
109
|
.prepare("INSERT INTO links (from_type, from_id, to_type, to_id, kind, role, source, metadata, confirmed, account_id, " +
|
|
95
|
-
"starts_at, ends_at, created_at, author, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?,
|
|
110
|
+
"starts_at, ends_at, created_at, author, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?, ?, ?) " +
|
|
96
111
|
"RETURNING id")
|
|
97
|
-
.get(from.type, from.id, to.type, to.id, relation.kind, role, source, metadata, accountId, startsAt, endsAt, at, at);
|
|
112
|
+
.get(from.type, from.id, to.type, to.id, relation.kind, role, source, metadata, accountId, startsAt, endsAt, at, by.kind ?? "agent", at);
|
|
98
113
|
seen.add(Number(row?.id));
|
|
99
|
-
|
|
114
|
+
counted("added", row?.id);
|
|
100
115
|
}
|
|
101
116
|
const withdraw = database.prepare("UPDATE links SET deleted_at = ?, updated_at = ? WHERE id = ?");
|
|
102
117
|
const kinds = scope.kinds.map(linkKind).join(", ");
|
|
@@ -115,19 +130,45 @@ export const syncReportedIn = (context, key, relationships, scope, { source = "i
|
|
|
115
130
|
const match = end === "from"
|
|
116
131
|
? `((from_type = ? AND from_id IN (SELECT value FROM json_each(?))) OR (kind = ${linkKind("partner-of")} AND to_type = ? AND to_id IN (SELECT value FROM json_each(?))))`
|
|
117
132
|
: `((to_type = ? AND to_id IN (SELECT value FROM json_each(?))) OR (kind = ${linkKind("partner-of")} AND from_type = ? AND from_id IN (SELECT value FROM json_each(?))))`;
|
|
133
|
+
// Only the writer's own links: a CRM sync and an import on one account never withdraw each other's.
|
|
118
134
|
const rows = database
|
|
119
|
-
.prepare(`SELECT id FROM links WHERE account_id = ? AND confirmed = 0 AND deleted_at IS NULL AND kind IN (${kinds}) ` +
|
|
135
|
+
.prepare(`SELECT id, kind, from_type, from_id, to_type, to_id, role FROM links WHERE account_id = ? AND source = ? AND confirmed = 0 AND deleted_at IS NULL AND kind IN (${kinds}) ` +
|
|
120
136
|
`AND ${match}${roles ? ` AND ifnull(role, '') IN (${roles})` : ""}`)
|
|
121
|
-
.all(accountId, thing.type, ids, thing.type, ids, ...(scope.roles ?? []));
|
|
137
|
+
.all(accountId, source, thing.type, ids, thing.type, ids, ...(scope.roles ?? []));
|
|
122
138
|
for (const row of rows)
|
|
123
139
|
if (!seen.has(Number(row.id))) {
|
|
124
140
|
seen.add(Number(row.id));
|
|
125
141
|
withdraw.run(at, at, Number(row.id));
|
|
126
142
|
result.withdrawn += 1;
|
|
143
|
+
const linkKey = keyOf(row.kind, row.from_type, row.from_id, row.to_type, row.to_id, row.role);
|
|
144
|
+
onOutcome?.({ outcome: "withdrawn", key: linkKey, id: String(row.id) });
|
|
127
145
|
}
|
|
128
146
|
}
|
|
129
147
|
return result;
|
|
130
148
|
};
|
|
149
|
+
const parsed = (metadata) => (metadata == null ? {} : JSON.parse(String(metadata)));
|
|
150
|
+
/**
|
|
151
|
+
* The owner confirmed, or overrode, the pair's no-role link whose source called it `role`: once the role is listed, the
|
|
152
|
+
* source reports it with that role, and it is the same relationship. `source_role` is one word or a list of them.
|
|
153
|
+
* The answer names the overridden link as `reported`, if that is how the owner took it.
|
|
154
|
+
*/
|
|
155
|
+
const ownerTook = ({ database }, owned, role) => {
|
|
156
|
+
if (role === null)
|
|
157
|
+
return undefined;
|
|
158
|
+
const named = (link) => link?.role == null && [parsed(link?.metadata).source_role].flat().includes(role);
|
|
159
|
+
const bare = owned.find((link) => link.role == null && named(link));
|
|
160
|
+
if (bare)
|
|
161
|
+
return bare;
|
|
162
|
+
for (const link of owned) {
|
|
163
|
+
const overridden = parsed(link.metadata).overrides;
|
|
164
|
+
if (typeof overridden !== "number")
|
|
165
|
+
continue;
|
|
166
|
+
const source = database.prepare("SELECT id, role, metadata FROM links WHERE id = ?").get(overridden);
|
|
167
|
+
if (named(source))
|
|
168
|
+
return { ...link, reported: source?.id };
|
|
169
|
+
}
|
|
170
|
+
return undefined;
|
|
171
|
+
};
|
|
131
172
|
/** The owner rejected or removed exactly this relationship: its pair, kind and role, whoever reported it. */
|
|
132
173
|
const rejected = ({ database }, from, to, kind, role) => database
|
|
133
174
|
.prepare("SELECT 1 FROM proposed_actions WHERE kind = 'relationship' AND status = 'rejected' AND target_type = ? " +
|
|
@@ -15840,6 +15840,38 @@ export declare const pipelineStages: import("drizzle-orm/sqlite-core").SQLiteTab
|
|
|
15840
15840
|
identity: undefined;
|
|
15841
15841
|
generated: undefined;
|
|
15842
15842
|
}, {}>;
|
|
15843
|
+
baseStageId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
15844
|
+
name: string;
|
|
15845
|
+
tableName: "pipeline_stages";
|
|
15846
|
+
dataType: "number int53";
|
|
15847
|
+
data: number;
|
|
15848
|
+
driverParam: number;
|
|
15849
|
+
notNull: false;
|
|
15850
|
+
hasDefault: false;
|
|
15851
|
+
isPrimaryKey: false;
|
|
15852
|
+
isAutoincrement: false;
|
|
15853
|
+
hasRuntimeDefault: false;
|
|
15854
|
+
enumValues: undefined;
|
|
15855
|
+
baseColumn: never;
|
|
15856
|
+
identity: undefined;
|
|
15857
|
+
generated: undefined;
|
|
15858
|
+
}, {}>;
|
|
15859
|
+
baseConfirmed: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
15860
|
+
name: string;
|
|
15861
|
+
tableName: "pipeline_stages";
|
|
15862
|
+
dataType: "number int53";
|
|
15863
|
+
data: number;
|
|
15864
|
+
driverParam: number;
|
|
15865
|
+
notNull: true;
|
|
15866
|
+
hasDefault: true;
|
|
15867
|
+
isPrimaryKey: false;
|
|
15868
|
+
isAutoincrement: false;
|
|
15869
|
+
hasRuntimeDefault: false;
|
|
15870
|
+
enumValues: undefined;
|
|
15871
|
+
baseColumn: never;
|
|
15872
|
+
identity: undefined;
|
|
15873
|
+
generated: undefined;
|
|
15874
|
+
}, {}>;
|
|
15843
15875
|
};
|
|
15844
15876
|
dialect: 'sqlite';
|
|
15845
15877
|
isAlias: false;
|
|
@@ -1583,12 +1583,15 @@ export const pipelineStages = sqliteTable("pipeline_stages", {
|
|
|
1583
1583
|
createdAt: integer("created_at").notNull(),
|
|
1584
1584
|
updatedAt: integer("updated_at").notNull(),
|
|
1585
1585
|
deletedAt: integer("deleted_at"),
|
|
1586
|
+
baseStageId: integer("base_stage_id").references(() => pipelineStages.id),
|
|
1587
|
+
baseConfirmed: integer("base_confirmed").notNull().default(0),
|
|
1586
1588
|
}, (table) => [
|
|
1587
1589
|
unique().on(table.pipelineId, table.nameFolded),
|
|
1588
1590
|
uniqueIndex("pipeline_stages_by_pipeline_id_external_id")
|
|
1589
1591
|
.on(table.pipelineId, table.externalId)
|
|
1590
1592
|
.where(sql `external_id IS NOT NULL`),
|
|
1591
1593
|
index("pipeline_stages_by_pipeline_id_position").on(table.pipelineId, table.position),
|
|
1594
|
+
index("pipeline_stages_by_base_stage_id").on(table.baseStageId),
|
|
1592
1595
|
]);
|
|
1593
1596
|
/** A deal: what the owner hopes to sell, where it stands in its pipeline, and whether it ended. */
|
|
1594
1597
|
export const deals = sqliteTable("deals", {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
2
|
import { CliError } from "@wirecat/cli-base";
|
|
3
3
|
import { sqliteCode } from "../../errors/failures.js";
|
|
4
|
+
import { checkJournal } from "../journal.js";
|
|
5
|
+
import { committedWrites, droppedWrites } from "../write-events.js";
|
|
4
6
|
import { drainInvolvementQueue } from "./involvement-queue.js";
|
|
5
7
|
import { drainMentions } from "./mention-drain.js";
|
|
6
8
|
import { drainStems, stemmerCache } from "./stems.js";
|
|
@@ -77,7 +79,9 @@ const transact = (database, body, drain, beforeCommit) => {
|
|
|
77
79
|
const outer = connection.depth === 0;
|
|
78
80
|
const name = `w${connection.depth + 1}`;
|
|
79
81
|
const waited = outer && beginImmediate(database);
|
|
80
|
-
if (
|
|
82
|
+
if (outer)
|
|
83
|
+
droppedWrites(database);
|
|
84
|
+
else
|
|
81
85
|
database.exec(`SAVEPOINT ${name}`);
|
|
82
86
|
connection.depth += 1;
|
|
83
87
|
try {
|
|
@@ -88,7 +92,11 @@ const transact = (database, body, drain, beforeCommit) => {
|
|
|
88
92
|
drainInvolvementQueue(database);
|
|
89
93
|
}
|
|
90
94
|
beforeCommit?.();
|
|
95
|
+
if (outer)
|
|
96
|
+
checkJournal(database);
|
|
91
97
|
database.exec(outer ? "COMMIT" : `RELEASE ${name}`);
|
|
98
|
+
if (outer)
|
|
99
|
+
committedWrites(database);
|
|
92
100
|
// A write that had to wait has others waiting behind it, and its caller's next write would begin before they see
|
|
93
101
|
// the lock free. Pausing only then costs an uncontended write nothing.
|
|
94
102
|
if (outer && (waited || !drain))
|
|
@@ -97,6 +105,8 @@ const transact = (database, body, drain, beforeCommit) => {
|
|
|
97
105
|
}
|
|
98
106
|
catch (error) {
|
|
99
107
|
rollBack(database, outer ? "ROLLBACK" : `ROLLBACK TO ${name}; RELEASE ${name}`);
|
|
108
|
+
if (outer)
|
|
109
|
+
droppedWrites(database);
|
|
100
110
|
throw error;
|
|
101
111
|
}
|
|
102
112
|
finally {
|
package/dist/store/store.d.ts
CHANGED
|
@@ -20,6 +20,7 @@ import { type RosterStore } from "./roster.js";
|
|
|
20
20
|
import { type SearchIndexStore } from "./search-index.js";
|
|
21
21
|
import { type SearchesStore } from "./searches.js";
|
|
22
22
|
import { type AgentActionsStore } from "./sqlite/agent-actions.js";
|
|
23
|
+
import { type BaseStagesStore } from "./sqlite/base-stages.js";
|
|
23
24
|
import { type BotUpdateStore } from "./sqlite/bot-updates.js";
|
|
24
25
|
import { type ContractsStore } from "./sqlite/business-contracts.js";
|
|
25
26
|
import { type BusinessSearchStore } from "./sqlite/business-search.js";
|
|
@@ -119,6 +120,8 @@ export interface MessageStore {
|
|
|
119
120
|
readonly orders: OrdersStore;
|
|
120
121
|
/** The owner's sales pipelines and the deals in them, written directly by the owner and his agents. */
|
|
121
122
|
readonly deals: DealsStore;
|
|
123
|
+
/** The owner's base stages of deals and of leads, and every other pipeline's stages mapped onto them. */
|
|
124
|
+
readonly baseStages: BaseStagesStore;
|
|
122
125
|
/** What the owner or a supplier sells, with its price; written directly. */
|
|
123
126
|
readonly products: ProductsStore;
|
|
124
127
|
/** The contracts the owner works under; one with a value from an agent or an import waits for the owner. */
|
package/dist/store/store.js
CHANGED
|
@@ -16,6 +16,7 @@ import * as accounts from "./sqlite/accounts.js";
|
|
|
16
16
|
import { addedSourcesOver } from "./sqlite/added-sources.js";
|
|
17
17
|
import { agentActionsStoreOver } from "./sqlite/agent-actions.js";
|
|
18
18
|
import * as attachmentTexts from "./sqlite/attachment-texts.js";
|
|
19
|
+
import { baseStagesStoreOver } from "./sqlite/base-stages.js";
|
|
19
20
|
import { botUpdateStoreOver } from "./sqlite/bot-updates.js";
|
|
20
21
|
import { contractsStoreOver } from "./sqlite/business-contracts.js";
|
|
21
22
|
import { businessSearchOver } from "./sqlite/business-search.js";
|
|
@@ -128,6 +129,7 @@ const storeOver = (context) => {
|
|
|
128
129
|
const finance = once(() => financeStoreOver(context));
|
|
129
130
|
const orders = once(() => ordersStoreOver(context));
|
|
130
131
|
const deals = once(() => dealsStoreOver(context));
|
|
132
|
+
const baseStages = once(() => baseStagesStoreOver(context));
|
|
131
133
|
const products = once(() => productsStoreOver(context));
|
|
132
134
|
const contracts = once(() => contractsStoreOver(context));
|
|
133
135
|
const subscriptions = once(() => subscriptionsStoreOver(context));
|
|
@@ -255,6 +257,9 @@ const storeOver = (context) => {
|
|
|
255
257
|
get deals() {
|
|
256
258
|
return deals();
|
|
257
259
|
},
|
|
260
|
+
get baseStages() {
|
|
261
|
+
return baseStages();
|
|
262
|
+
},
|
|
258
263
|
get products() {
|
|
259
264
|
return products();
|
|
260
265
|
},
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { StoreEvent } from "../runs/events.js";
|
|
2
|
+
type Target = {
|
|
3
|
+
table: string;
|
|
4
|
+
op: StoreEvent["op"];
|
|
5
|
+
};
|
|
6
|
+
/** The table a statement writes and how, read from its text; a read, DDL or a temp table answers undefined. */
|
|
7
|
+
export declare const writeTarget: (sql: string) => Target | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Counts the rows each write statement changes, per table, on a writable store connection: `methods` are the client's
|
|
10
|
+
* prepare calls, which both the `CacheDatabase` seam and Drizzle go through. A transaction's counts become run events
|
|
11
|
+
* when `write` commits it; a statement outside one is its own event.
|
|
12
|
+
*/
|
|
13
|
+
export declare const countWrites: <C extends object>(client: C, methods: readonly string[], inTransaction: () => boolean) => C;
|
|
14
|
+
/** The seam built over a counted client shares its counts. */
|
|
15
|
+
export declare const shareCounts: (from: object, to: object) => void;
|
|
16
|
+
export declare const committedWrites: (database: object) => void;
|
|
17
|
+
export declare const droppedWrites: (database: object) => void;
|
|
18
|
+
export {};
|