@wirecat/cli-core 0.617.0 → 0.619.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 +406 -1
- package/README.md +1 -1
- package/THIRD_PARTY_NOTICES +3 -0
- package/dist/background/backfill.d.ts +21 -0
- package/dist/background/backfill.js +41 -0
- package/dist/background/daily-archive-timer.d.ts +1 -1
- package/dist/background/deliver.d.ts +5 -2
- package/dist/background/deliver.js +30 -12
- package/dist/background/report/messages.js +4 -1
- package/dist/background/report/render.d.ts +3 -1
- package/dist/background/report/render.js +3 -1
- package/dist/background/report/view.js +4 -1
- package/dist/background/schedules.d.ts +17 -0
- package/dist/background/schedules.js +21 -8
- 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/index.d.ts +1 -1
- package/dist/calendar/index.js +1 -1
- package/dist/calendar/mcp/index.d.ts +12 -12
- package/dist/cli/base-stages-command.d.ts +22 -0
- package/dist/cli/base-stages-command.js +174 -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.d.ts +5 -0
- package/dist/cli/context.js +1 -0
- package/dist/cli/group-lock.d.ts +42 -0
- package/dist/cli/group-lock.js +130 -0
- package/dist/cli/index.d.ts +10 -4
- package/dist/cli/index.js +10 -4
- package/dist/cli/messenger/backfill-command.js +27 -15
- package/dist/cli/messenger/contacts-command.js +10 -3
- package/dist/cli/messenger/context.js +1 -0
- package/dist/cli/messenger/doctor-command.js +3 -1
- package/dist/cli/messenger/models-command.js +1 -1
- package/dist/cli/messenger/serve-command.js +24 -2
- package/dist/cli/messenger/serve-outdated.d.ts +22 -0
- package/dist/cli/messenger/serve-outdated.js +100 -0
- 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 +46 -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/schedule/schedule-command.d.ts +9 -0
- package/dist/cli/schedule/schedule-command.js +13 -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/store-recover.d.ts +3 -3
- package/dist/cli/store-recover.js +47 -23
- package/dist/cli/store-settings.d.ts +2 -0
- package/dist/cli/store-settings.js +37 -9
- package/dist/crm/cli/index.js +38 -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/links.d.ts +17 -4
- package/dist/domain/links.js +20 -7
- package/dist/domain/mentions.d.ts +2 -12
- package/dist/domain/mentions.js +2 -19
- 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 +27 -2
- package/dist/domain/relations.js +37 -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 +157 -0
- package/dist/entities/contact-context.js +2 -0
- package/dist/entities/deal.d.ts +10 -0
- package/dist/entities/deal.js +2 -0
- package/dist/entities/index.d.ts +23 -3
- 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 +44 -4
- package/dist/fetch/page.js +2 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +4 -3
- 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/mcp/index.d.ts +15 -15
- package/dist/meetings/result-items.d.ts +1 -0
- package/dist/meetings/result-items.js +1 -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/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 +7 -2
- package/dist/services/enrichment.js +46 -5
- 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 +15 -2
- package/dist/services/index.js +9 -2
- package/dist/services/ingest.d.ts +12 -0
- package/dist/services/ingest.js +4 -0
- package/dist/services/link-scorers/chat-senders.js +39 -29
- package/dist/services/link-scorers/member-chats.js +4 -3
- package/dist/services/link-scorers/member-dialog.js +5 -7
- package/dist/services/link-scorers/scan.js +17 -8
- package/dist/services/link-scorers/shared.d.ts +6 -2
- package/dist/services/link-scorers/shared.js +6 -2
- package/dist/services/link-scorers/types.d.ts +2 -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/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 +9 -1
- package/dist/services/person-context.js +5 -3
- 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-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/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/migrations.d.ts +1 -1
- package/dist/store/migrations.js +35 -12
- package/dist/store/people.d.ts +22 -0
- package/dist/store/people.js +15 -1
- package/dist/store/recover.d.ts +34 -0
- package/dist/store/recover.js +188 -6
- package/dist/store/refill.d.ts +28 -0
- package/dist/store/refill.js +120 -0
- package/dist/store/sqlite/accounts.js +2 -0
- 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/clients.d.ts +7 -3
- package/dist/store/sqlite/clients.js +11 -7
- 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 +17 -15
- 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/knowledge.js +5 -3
- 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/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/note-index.js +9 -7
- package/dist/store/sqlite/notes.d.ts +2 -3
- package/dist/store/sqlite/notes.js +6 -29
- 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/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 +20 -2
- package/dist/store/sqlite/relation-proposals.d.ts +18 -1
- package/dist/store/sqlite/relation-proposals.js +53 -11
- 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.d.ts +13 -0
- package/dist/store/sqlite/transaction.js +33 -15
- 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 +11 -4
- package/schemas/records-envelope.v1.schema.json +3 -0
|
@@ -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) => {
|
|
@@ -61,7 +69,13 @@ export const relationListOver = (context) => {
|
|
|
61
69
|
.get(key.provider, key.account);
|
|
62
70
|
return row ? [Number(row.id)] : [];
|
|
63
71
|
});
|
|
64
|
-
|
|
72
|
+
const list = ids.map(() => "?").join(", ") || "NULL";
|
|
73
|
+
add(`(r.account_id IS NULL OR r.account_id IN (${list}))`, ...ids);
|
|
74
|
+
// An end that belongs to an account outside the scope keeps the row out, whoever wrote it.
|
|
75
|
+
for (const side of ["from", "to"])
|
|
76
|
+
for (const [type, table] of ACCOUNT_ENDS)
|
|
77
|
+
add(`NOT (r.${side}_type = '${type}' AND EXISTS (SELECT 1 FROM ${table} e WHERE e.id = r.${side}_id ` +
|
|
78
|
+
`AND e.account_id IS NOT NULL AND e.account_id NOT IN (${list})))`, ...ids);
|
|
65
79
|
}
|
|
66
80
|
if (filter.deniedKinds?.length) {
|
|
67
81
|
const list = filter.deniedKinds.map(() => "?").join(", ");
|
|
@@ -101,7 +115,9 @@ export const relationListOver = (context) => {
|
|
|
101
115
|
const fromThing = thingOf(database, from);
|
|
102
116
|
const toThing = thingOf(database, to);
|
|
103
117
|
const roles = fromThing && toThing ? rolesOf(kind, fromThing.type, toThing.type) : undefined;
|
|
104
|
-
|
|
118
|
+
if (role === null || roles === undefined || roles.includes(role) || !fromThing || !toThing)
|
|
119
|
+
return false;
|
|
120
|
+
return !extraRolesOf(database, kind, fromThing.type, toThing.type).includes(role);
|
|
105
121
|
}
|
|
106
122
|
function linkItem(id, at) {
|
|
107
123
|
const relation = relationOf(linkOf(database, database.prepare("SELECT * FROM links WHERE id = ?").get(id)), at);
|
|
@@ -124,6 +140,8 @@ export const relationListOver = (context) => {
|
|
|
124
140
|
confidenceCategory: "weak",
|
|
125
141
|
standing: "proposed",
|
|
126
142
|
position: payload.position ?? null,
|
|
143
|
+
roleNote: payload.roleNote ?? null,
|
|
144
|
+
engagement: payload.engagement ?? null,
|
|
127
145
|
startsAt: iso(payload.startsAt),
|
|
128
146
|
endsAt: iso(payload.endsAt),
|
|
129
147
|
createdAt: action.createdAt,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type RelationKind, type Standing } from "../../domain/relations.js";
|
|
1
|
+
import { type Engagement, type RelationKind, type Standing } from "../../domain/relations.js";
|
|
2
2
|
import { type Decider } from "../decisions.js";
|
|
3
3
|
import type { Author } from "./memories.js";
|
|
4
4
|
import { type Link } from "./notes.js";
|
|
@@ -22,9 +22,19 @@ export interface KnowledgeRelation {
|
|
|
22
22
|
standing: Standing;
|
|
23
23
|
/** A job title beside the relationship, never its role. */
|
|
24
24
|
position: string | null;
|
|
25
|
+
/** What the role means here, in the owner's words. */
|
|
26
|
+
roleNote: string | null;
|
|
27
|
+
/** How much the owner deals with a person on a project himself; `null` is unknown. */
|
|
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[];
|
|
25
31
|
/** When it held, as ISO times; `endsAt` exclusive; `null` when nobody said. */
|
|
26
32
|
startsAt: string | null;
|
|
27
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;
|
|
28
38
|
createdAt: string;
|
|
29
39
|
}
|
|
30
40
|
export interface RelateInput {
|
|
@@ -37,6 +47,8 @@ export interface RelateInput {
|
|
|
37
47
|
confirmed?: boolean;
|
|
38
48
|
provenance?: string;
|
|
39
49
|
position?: string;
|
|
50
|
+
roleNote?: string;
|
|
51
|
+
engagement?: Engagement;
|
|
40
52
|
/** UTC ms; `endsAt` exclusive and after `startsAt`. */
|
|
41
53
|
startsAt?: number;
|
|
42
54
|
endsAt?: number;
|
|
@@ -70,6 +82,8 @@ export declare const relationProposalsOver: (context: StoreContext) => {
|
|
|
70
82
|
role?: string;
|
|
71
83
|
purpose?: string;
|
|
72
84
|
position?: string;
|
|
85
|
+
roleNote?: string;
|
|
86
|
+
engagement?: Engagement;
|
|
73
87
|
startsAt?: number;
|
|
74
88
|
endsAt?: number;
|
|
75
89
|
evidence?: string[];
|
|
@@ -109,11 +123,14 @@ export interface RelationUpdate {
|
|
|
109
123
|
role?: string | null;
|
|
110
124
|
purpose?: string | null;
|
|
111
125
|
position?: string | null;
|
|
126
|
+
roleNote?: string | null;
|
|
127
|
+
engagement?: Engagement | null;
|
|
112
128
|
startsAt?: number | null;
|
|
113
129
|
endsAt?: number | null;
|
|
114
130
|
}
|
|
115
131
|
/** A relationship ends after it starts. */
|
|
116
132
|
export declare const spanOf: (startsAt: number | null | undefined, endsAt: number | null | undefined) => void;
|
|
133
|
+
export declare const roleNoteOf: (note: string) => string;
|
|
117
134
|
export declare const positionOf: (position: string) => string;
|
|
118
135
|
export interface LinkDecision {
|
|
119
136
|
confirm: boolean;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
|
-
import { standingOf } from "../../domain/relations.js";
|
|
2
|
+
import { checkEngagement, standingOf, } from "../../domain/relations.js";
|
|
3
3
|
import { assertMayDecide, botKey, deciderAuthor } from "../decisions.js";
|
|
4
4
|
import { actorOfOrigin, botNamed, ownerPerson } from "./actors.js";
|
|
5
5
|
import { insertLink, linkOf, purposeOf } from "./notes.js";
|
|
@@ -21,8 +21,13 @@ export const relationOf = (link, now) => ({
|
|
|
21
21
|
confidenceCategory: link.confirmed ? "owner-confirmed" : "weak",
|
|
22
22
|
standing: standingOf(link, now),
|
|
23
23
|
position: link.position ?? null,
|
|
24
|
+
roleNote: link.roleNote ?? null,
|
|
25
|
+
engagement: link.engagement ?? null,
|
|
26
|
+
...(link.sourceRoles ? { sourceRoles: link.sourceRoles } : {}),
|
|
24
27
|
startsAt: isoOf(link.startsAt),
|
|
25
28
|
endsAt: isoOf(link.endsAt),
|
|
29
|
+
...(link.account === undefined ? {} : { account: link.account }),
|
|
30
|
+
...(link.derived === undefined ? {} : { derived: link.derived }),
|
|
26
31
|
createdAt: link.createdAt,
|
|
27
32
|
});
|
|
28
33
|
/** `relationship` proposals in `proposed_actions`: an agent's or a rule's, and the rejected ones a scan skips. */
|
|
@@ -47,15 +52,30 @@ export const relationProposalsOver = (context) => {
|
|
|
47
52
|
.run(from?.type ?? null, from?.id ?? null, JSON.stringify({ from: link.from, to: link.to, kind: link.kind, ...(link.role ? { role: link.role } : {}) }), link.evidence, proposed.type, proposed.id, decided.type, decided.id, at, JSON.stringify({ reason: why.join(": "), link: link.id }), at, at);
|
|
48
53
|
};
|
|
49
54
|
/** The role lists may have changed since a proposal was made: a role is checked when it is written. */
|
|
50
|
-
const checkedRole = (link, role) => {
|
|
51
|
-
if (typeof role !== "string")
|
|
52
|
-
return;
|
|
55
|
+
const checkedRole = (link, { role, engagement }) => {
|
|
53
56
|
const [fromType, , toType] = fromTo(link);
|
|
54
|
-
|
|
57
|
+
if (typeof role === "string")
|
|
58
|
+
checkStoredRole(database, link.kind, fromType, toType, role);
|
|
59
|
+
if (typeof engagement === "string")
|
|
60
|
+
checkEngagement(link.kind, fromType, toType, engagement);
|
|
61
|
+
};
|
|
62
|
+
/** The link's metadata with the changed fields set or, when `null`, removed; `undefined` when none changed. */
|
|
63
|
+
const metadataOf = (link, changes) => {
|
|
64
|
+
const fields = METADATA_FIELDS.filter((field) => field in changes);
|
|
65
|
+
if (!fields.length)
|
|
66
|
+
return;
|
|
67
|
+
const row = database.prepare("SELECT metadata FROM links WHERE id = ?").get(Number(link.id));
|
|
68
|
+
const metadata = row?.metadata == null ? {} : JSON.parse(String(row.metadata));
|
|
69
|
+
for (const field of fields)
|
|
70
|
+
if (changes[field] == null)
|
|
71
|
+
delete metadata[field];
|
|
72
|
+
else
|
|
73
|
+
metadata[field] = changes[field];
|
|
74
|
+
return JSON.stringify(metadata);
|
|
55
75
|
};
|
|
56
76
|
const applyUpdate = (link, input) => {
|
|
57
77
|
const changes = changesOf(input);
|
|
58
|
-
checkedRole(link, changes
|
|
78
|
+
checkedRole(link, changes);
|
|
59
79
|
if ("role" in changes) {
|
|
60
80
|
const taken = database
|
|
61
81
|
.prepare("SELECT b.id FROM links a JOIN links b ON b.kind = a.kind AND b.from_type = a.from_type AND b.from_id = a.from_id " +
|
|
@@ -70,12 +90,12 @@ export const relationProposalsOver = (context) => {
|
|
|
70
90
|
const startsAt = "startsAt" in changes ? (changes.startsAt ?? undefined) : link.startsAt;
|
|
71
91
|
const endsAt = "endsAt" in changes ? (changes.endsAt ?? undefined) : link.endsAt;
|
|
72
92
|
spanOf(startsAt, endsAt);
|
|
93
|
+
const metadata = metadataOf(link, changes);
|
|
73
94
|
database
|
|
74
95
|
.prepare("UPDATE links SET role = CASE WHEN ? THEN ? ELSE role END, purpose = CASE WHEN ? THEN ? ELSE purpose END, " +
|
|
75
96
|
"starts_at = CASE WHEN ? THEN ? ELSE starts_at END, ends_at = CASE WHEN ? THEN ? ELSE ends_at END, " +
|
|
76
|
-
"metadata = CASE WHEN ? THEN
|
|
77
|
-
"
|
|
78
|
-
.run(Number("role" in changes), changes.role ?? null, Number("purpose" in changes), changes.purpose ?? null, Number("startsAt" in changes), changes.startsAt ?? null, Number("endsAt" in changes), changes.endsAt ?? null, Number("position" in changes), changes.position ?? null, changes.position ?? null, now(), Number(link.id));
|
|
97
|
+
"metadata = CASE WHEN ? THEN ? ELSE metadata END, updated_at = ? WHERE id = ?")
|
|
98
|
+
.run(Number("role" in changes), changes.role ?? null, Number("purpose" in changes), changes.purpose ?? null, Number("startsAt" in changes), changes.startsAt ?? null, Number("endsAt" in changes), changes.endsAt ?? null, Number(metadata !== undefined), metadata ?? null, now(), Number(link.id));
|
|
79
99
|
return linkOf(database, database.prepare("SELECT * FROM links WHERE id = ?").get(Number(link.id)));
|
|
80
100
|
};
|
|
81
101
|
/**
|
|
@@ -99,6 +119,8 @@ export const relationProposalsOver = (context) => {
|
|
|
99
119
|
const metadata = JSON.stringify({
|
|
100
120
|
overrides: Number(link.id),
|
|
101
121
|
...(link.position ? { position: link.position } : {}),
|
|
122
|
+
...(link.roleNote ? { roleNote: link.roleNote } : {}),
|
|
123
|
+
...(link.engagement ? { engagement: link.engagement } : {}),
|
|
102
124
|
});
|
|
103
125
|
// An owner link of that role on the pair already (from `relate`) becomes the override rather than a second one.
|
|
104
126
|
const same = database
|
|
@@ -211,6 +233,8 @@ export const relationProposalsOver = (context) => {
|
|
|
211
233
|
...(input.role === undefined ? {} : { role: input.role }),
|
|
212
234
|
...(input.purpose === undefined ? {} : { purpose: purposeOf(input.purpose) }),
|
|
213
235
|
...(input.position === undefined ? {} : { position: positionOf(input.position) }),
|
|
236
|
+
...(input.roleNote === undefined ? {} : { roleNote: roleNoteOf(input.roleNote) }),
|
|
237
|
+
...(input.engagement === undefined ? {} : { engagement: input.engagement }),
|
|
214
238
|
...(input.startsAt === undefined ? {} : { startsAt: input.startsAt }),
|
|
215
239
|
...(input.endsAt === undefined ? {} : { endsAt: input.endsAt }),
|
|
216
240
|
evidence,
|
|
@@ -224,7 +248,7 @@ export const relationProposalsOver = (context) => {
|
|
|
224
248
|
update: (link, input) => write(database, () => ownerUpdate(link, input)),
|
|
225
249
|
proposeUpdate: (link, input, by) => {
|
|
226
250
|
const changes = changesOf(input);
|
|
227
|
-
checkedRole(link, changes
|
|
251
|
+
checkedRole(link, changes);
|
|
228
252
|
return write(database, () => {
|
|
229
253
|
const from = thingOf(database, link.from);
|
|
230
254
|
const held = database
|
|
@@ -248,6 +272,8 @@ export const relationProposalsOver = (context) => {
|
|
|
248
272
|
role: link.role,
|
|
249
273
|
purpose: link.purpose,
|
|
250
274
|
position: link.position ?? null,
|
|
275
|
+
roleNote: link.roleNote ?? null,
|
|
276
|
+
engagement: link.engagement ?? null,
|
|
251
277
|
startsAt: link.startsAt ?? null,
|
|
252
278
|
endsAt: link.endsAt ?? null,
|
|
253
279
|
},
|
|
@@ -293,6 +319,8 @@ export const relationProposalsOver = (context) => {
|
|
|
293
319
|
const ends = key ? undefined : [thingOf(database, String(payload.from)), thingOf(database, String(payload.to))];
|
|
294
320
|
if (ends?.[0] && ends[1] && payload.role !== undefined)
|
|
295
321
|
checkStoredRole(database, payload.kind ?? "member-of", ends[0].type, ends[1].type, payload.role);
|
|
322
|
+
if (ends?.[0] && ends[1] && payload.engagement !== undefined)
|
|
323
|
+
checkEngagement(payload.kind ?? "member-of", ends[0].type, ends[1].type, payload.engagement);
|
|
296
324
|
const link = insertLink(context, {
|
|
297
325
|
from: String(key ? payload.ref : payload.from),
|
|
298
326
|
to: String(key ? payload.project : payload.to),
|
|
@@ -300,6 +328,8 @@ export const relationProposalsOver = (context) => {
|
|
|
300
328
|
...(key ? { role: "key" } : payload.role === undefined ? {} : { role: payload.role }),
|
|
301
329
|
...(key || payload.purpose === undefined ? {} : { purpose: payload.purpose }),
|
|
302
330
|
...(key || payload.position === undefined ? {} : { position: payload.position }),
|
|
331
|
+
...(key || payload.roleNote === undefined ? {} : { roleNote: payload.roleNote }),
|
|
332
|
+
...(key || payload.engagement === undefined ? {} : { engagement: payload.engagement }),
|
|
303
333
|
...(key || payload.startsAt === undefined ? {} : { startsAt: payload.startsAt }),
|
|
304
334
|
...(key || payload.endsAt === undefined ? {} : { endsAt: payload.endsAt }),
|
|
305
335
|
...(proposal.reason === null ? {} : { evidence: proposal.reason }),
|
|
@@ -357,6 +387,14 @@ export const spanOf = (startsAt, endsAt) => {
|
|
|
357
387
|
if (startsAt != null && endsAt != null && endsAt <= startsAt)
|
|
358
388
|
throw new CliError("validation_error", "a relationship ends after it starts");
|
|
359
389
|
};
|
|
390
|
+
/** The relationship fields kept in `links.metadata`. */
|
|
391
|
+
const METADATA_FIELDS = ["position", "roleNote", "engagement"];
|
|
392
|
+
export const roleNoteOf = (note) => {
|
|
393
|
+
const value = note.trim();
|
|
394
|
+
if (!value || value.length > 200)
|
|
395
|
+
throw new CliError("validation_error", "a role note takes 1–200 characters");
|
|
396
|
+
return value;
|
|
397
|
+
};
|
|
360
398
|
export const positionOf = (position) => {
|
|
361
399
|
const value = position.trim();
|
|
362
400
|
if (!value || value.length > 200)
|
|
@@ -372,12 +410,16 @@ const changesOf = (input) => {
|
|
|
372
410
|
changes.purpose = input.purpose === null ? null : purposeOf(input.purpose);
|
|
373
411
|
if (input.position !== undefined)
|
|
374
412
|
changes.position = input.position === null ? null : positionOf(input.position);
|
|
413
|
+
if (input.roleNote !== undefined)
|
|
414
|
+
changes.roleNote = input.roleNote === null ? null : roleNoteOf(input.roleNote);
|
|
415
|
+
if (input.engagement !== undefined)
|
|
416
|
+
changes.engagement = input.engagement;
|
|
375
417
|
if (input.startsAt !== undefined)
|
|
376
418
|
changes.startsAt = input.startsAt === null ? null : timeOf(input.startsAt, "a start");
|
|
377
419
|
if (input.endsAt !== undefined)
|
|
378
420
|
changes.endsAt = input.endsAt === null ? null : timeOf(input.endsAt, "an end");
|
|
379
421
|
if (!Object.keys(changes).length)
|
|
380
|
-
throw new CliError("validation_error", "a relationship update sets a role, a purpose, a position or its dates");
|
|
422
|
+
throw new CliError("validation_error", "a relationship update sets a role, a role note, an engagement level, a purpose, a position or its dates");
|
|
381
423
|
return changes;
|
|
382
424
|
};
|
|
383
425
|
const roleOf = (role) => {
|
|
@@ -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,5 +1,18 @@
|
|
|
1
1
|
import type { CacheDatabase } from "../driver.js";
|
|
2
2
|
export declare const isBusy: (error: unknown) => boolean;
|
|
3
|
+
/**
|
|
4
|
+
* Takes the write lock, waiting as long as the connection's `busy_timeout`. SQLite's own wait sleeps 1 ms, then up to
|
|
5
|
+
* 100 ms between tries, so a process that commits and begins again at once is never seen free and the waiter fails
|
|
6
|
+
* with "database is locked" after the full timeout. Trying every millisecond sees a gap of a fraction of a
|
|
7
|
+
* millisecond between two writes; a writer that leaves none still starves it, hence the pause after a write that
|
|
8
|
+
* waited. Answers whether it had to wait.
|
|
9
|
+
*/
|
|
10
|
+
export declare const beginImmediate: (database: CacheDatabase) => boolean;
|
|
11
|
+
/**
|
|
12
|
+
* On some errors (a full disk, an I/O error) SQLite has already rolled the transaction back, and the ROLLBACK then
|
|
13
|
+
* fails with "no transaction is active". That failure must not take the place of the error that ended it.
|
|
14
|
+
*/
|
|
15
|
+
export declare const rollBack: (database: CacheDatabase, sql?: string) => void;
|
|
3
16
|
/**
|
|
4
17
|
* One write transaction. The outermost takes the write lock at once (`BEGIN IMMEDIATE`): a deferred one that
|
|
5
18
|
* reads first fails with SQLITE_BUSY when another process writes in between, and tg, max, memo and zm share
|