@wirecat/cli-core 0.605.0 → 0.607.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 +284 -1
- package/dist/calendar/index.d.ts +1 -0
- package/dist/calendar/index.js +1 -0
- package/dist/calendar/reads.js +5 -0
- package/dist/cli/app-mcp-command.d.ts +6 -2
- package/dist/cli/app-mcp-command.js +11 -2
- package/dist/cli/csv.d.ts +10 -0
- package/dist/cli/csv.js +22 -0
- package/dist/cli/early-refusal.d.ts +10 -0
- package/dist/cli/early-refusal.js +19 -0
- package/dist/cli/index.d.ts +4 -1
- package/dist/cli/index.js +4 -0
- package/dist/cli/profile-token.d.ts +29 -0
- package/dist/cli/profile-token.js +65 -0
- package/dist/cli/program.js +4 -2
- package/dist/cli/store-settings.js +38 -4
- package/dist/crm/cli/csv.d.ts +23 -0
- package/dist/crm/cli/csv.js +135 -0
- package/dist/crm/cli/index.d.ts +1 -1
- package/dist/crm/cli/index.js +36 -22
- package/dist/crm/cli/pipeline-view.d.ts +3 -0
- package/dist/crm/cli/pipeline-view.js +36 -0
- package/dist/crm/index.d.ts +2 -2
- package/dist/crm/index.js +1 -1
- package/dist/crm/source.d.ts +35 -5
- package/dist/crm/sync.d.ts +15 -7
- package/dist/crm/sync.js +169 -11
- package/dist/crm/writes.d.ts +6 -4
- package/dist/crm/writes.js +17 -13
- package/dist/documents.d.ts +2 -0
- package/dist/documents.js +2 -0
- package/dist/domain/business.d.ts +79 -0
- package/dist/domain/business.js +42 -0
- package/dist/domain/crm.d.ts +41 -2
- package/dist/domain/deals.d.ts +5 -2
- package/dist/domain/deals.js +5 -2
- package/dist/domain/finance.d.ts +2 -0
- package/dist/domain/finance.js +2 -2
- package/dist/domain/google-documents.d.ts +2 -0
- package/dist/domain/google-documents.js +3 -1
- package/dist/domain/money.d.ts +2 -0
- package/dist/domain/money.js +11 -3
- package/dist/domain/providers.d.ts +36 -1
- package/dist/domain/providers.js +69 -7
- package/dist/domain/record-fields.d.ts +42 -0
- package/dist/domain/record-fields.js +122 -0
- package/dist/domain/record-types.d.ts +489 -16
- package/dist/domain/record-types.js +374 -7
- package/dist/domain/records.d.ts +96 -5
- package/dist/domain/records.js +178 -32
- package/dist/domain/ref-summaries.js +5 -0
- package/dist/domain/references.d.ts +1 -1
- package/dist/domain/references.js +5 -0
- package/dist/domain/relations.d.ts +125 -0
- package/dist/domain/relations.js +107 -0
- package/dist/domain/vcard.d.ts +25 -0
- package/dist/domain/vcard.js +267 -0
- package/dist/entities/deal.d.ts +16 -2
- package/dist/entities/deal.js +5 -1
- package/dist/entities/index.d.ts +29 -13
- package/dist/entities/relationship.d.ts +8 -2
- package/dist/entities/relationship.js +8 -2
- package/dist/entities/task-results.d.ts +10 -10
- package/dist/errors/reasons.js +73 -2
- package/dist/fetch/addresses.d.ts +5 -0
- package/dist/fetch/addresses.js +76 -0
- package/dist/fetch/page.d.ts +64 -0
- package/dist/fetch/page.js +256 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.js +6 -3
- package/dist/mail/cli/review.js +20 -6
- package/dist/mail/index.d.ts +1 -1
- package/dist/mail/marks.d.ts +10 -5
- package/dist/mail/marks.js +16 -8
- package/dist/mail/review.d.ts +5 -0
- package/dist/meetings/calendar-text.d.ts +1 -1
- package/dist/meetings/calendar-text.js +4 -0
- package/dist/meetings/event-store.d.ts +2 -2
- package/dist/search/account-list.d.ts +4 -0
- package/dist/search/account-list.js +19 -8
- package/dist/search/query.js +2 -2
- package/dist/services/events-search.js +2 -2
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +1 -1
- package/dist/services/ingest.d.ts +56 -8
- package/dist/services/ingest.js +95 -61
- package/dist/services/link-scorers/scan.js +2 -1
- package/dist/services/link-scorers/shared.js +2 -1
- package/dist/services/messages-search.js +3 -2
- package/dist/services/metadata.d.ts +1 -1
- package/dist/services/notes-search.js +1 -0
- package/dist/services/project-status/index.js +2 -1
- package/dist/services/proposals/kinds/contracts.d.ts +4 -0
- package/dist/services/proposals/kinds/contracts.js +9 -0
- package/dist/services/proposals/kinds/finance.d.ts +4 -0
- package/dist/services/proposals/kinds/finance.js +14 -35
- package/dist/services/proposals/kinds/orders.js +7 -30
- package/dist/services/proposals/kinds/shared.d.ts +27 -1
- package/dist/services/proposals/kinds/shared.js +34 -0
- package/dist/services/proposals/kinds/source-review.d.ts +4 -0
- package/dist/services/proposals/kinds/source-review.js +45 -0
- package/dist/services/proposals/kinds/subscriptions.d.ts +9 -0
- package/dist/services/proposals/kinds/subscriptions.js +24 -0
- package/dist/services/proposals/kinds/transactions.d.ts +4 -0
- package/dist/services/proposals/kinds/transactions.js +10 -0
- package/dist/services/proposals/registry.js +9 -0
- package/dist/services/search-all-meetings.js +2 -2
- package/dist/services/search-all.js +4 -3
- package/dist/services/search-kind.js +3 -3
- package/dist/services/stored-project-gathering.js +4 -3
- package/dist/store/decisions.js +5 -1
- package/dist/store/exports.d.ts +1 -0
- package/dist/store/exports.js +1 -0
- package/dist/store/index.d.ts +20 -4
- package/dist/store/index.js +4 -0
- package/dist/store/people.js +1 -1
- package/dist/store/project-transfer/request.d.ts +4 -1
- package/dist/store/project-transfer/schema.d.ts +9 -2
- package/dist/store/project-transfer/schema.js +4 -0
- package/dist/store/project-transfer/snapshot-model.d.ts +8 -2
- package/dist/store/source-review.d.ts +64 -0
- package/dist/store/source-review.js +7 -0
- package/dist/store/sqlite/accounts.d.ts +1 -1
- package/dist/store/sqlite/accounts.js +24 -1
- package/dist/store/sqlite/added-sources.d.ts +82 -0
- package/dist/store/sqlite/added-sources.js +237 -0
- package/dist/store/sqlite/business-contracts.d.ts +90 -0
- package/dist/store/sqlite/business-contracts.js +182 -0
- package/dist/store/sqlite/calendar-events.js +7 -3
- package/dist/store/sqlite/calendar-forget.js +3 -0
- package/dist/store/sqlite/calendar.d.ts +21 -1
- package/dist/store/sqlite/calendar.js +27 -7
- package/dist/store/sqlite/chat-rekey.js +7 -0
- package/dist/store/sqlite/chats.d.ts +3 -1
- package/dist/store/sqlite/chats.js +15 -1
- package/dist/store/sqlite/crm-rows.d.ts +44 -0
- package/dist/store/sqlite/crm-rows.js +195 -0
- package/dist/store/sqlite/crm-settings.d.ts +28 -0
- package/dist/store/sqlite/crm-settings.js +80 -0
- package/dist/store/sqlite/crm.d.ts +42 -11
- package/dist/store/sqlite/crm.js +402 -196
- package/dist/store/sqlite/deals.d.ts +4 -8
- package/dist/store/sqlite/deals.js +8 -5
- package/dist/store/sqlite/document-transfer/snapshot.js +2 -2
- package/dist/store/sqlite/emails.js +2 -0
- package/dist/store/sqlite/entity-types.d.ts +14 -9
- package/dist/store/sqlite/entity-types.js +4 -10
- package/dist/store/sqlite/evidence-signature.d.ts +114 -0
- package/dist/store/sqlite/evidence-signature.js +349 -0
- package/dist/store/sqlite/evidence-sources.d.ts +2 -1
- package/dist/store/sqlite/evidence-sources.js +7 -3
- package/dist/store/sqlite/facts.d.ts +7 -2
- package/dist/store/sqlite/facts.js +21 -5
- package/dist/store/sqlite/finance.d.ts +18 -105
- package/dist/store/sqlite/finance.js +100 -219
- package/dist/store/sqlite/involvement-queue.js +4 -3
- package/dist/store/sqlite/knowledge.d.ts +14 -4
- package/dist/store/sqlite/knowledge.js +108 -102
- package/dist/store/sqlite/labelled.d.ts +3 -0
- package/dist/store/sqlite/labelled.js +11 -0
- package/dist/store/sqlite/link-kinds.d.ts +4 -3
- package/dist/store/sqlite/link-kinds.js +6 -2
- package/dist/store/sqlite/manifest.js +7 -0
- package/dist/store/sqlite/memories.d.ts +24 -10
- package/dist/store/sqlite/memories.js +73 -19
- package/dist/store/sqlite/messages.js +2 -0
- package/dist/store/sqlite/migrations.generated.js +134 -0
- package/dist/store/sqlite/money-lines.d.ts +48 -0
- package/dist/store/sqlite/money-lines.js +76 -0
- package/dist/store/sqlite/money-transactions.d.ts +132 -0
- package/dist/store/sqlite/money-transactions.js +295 -0
- package/dist/store/sqlite/notes.d.ts +14 -4
- package/dist/store/sqlite/notes.js +43 -31
- package/dist/store/sqlite/orders.d.ts +36 -6
- package/dist/store/sqlite/orders.js +126 -46
- package/dist/store/sqlite/organizations.d.ts +8 -0
- package/dist/store/sqlite/organizations.js +14 -0
- package/dist/store/sqlite/payment-facts.js +1 -1
- package/dist/store/sqlite/person-preparation.d.ts +1 -1
- package/dist/store/sqlite/person-preparation.js +3 -2
- package/dist/store/sqlite/private-people.js +2 -0
- package/dist/store/sqlite/products.d.ts +78 -0
- package/dist/store/sqlite/products.js +131 -0
- package/dist/store/sqlite/project-transfer/records.js +39 -3
- package/dist/store/sqlite/project-transfer/snapshot.d.ts +4 -1
- package/dist/store/sqlite/project-transfer/snapshot.js +11 -3
- package/dist/store/sqlite/proposed-actions.d.ts +1 -1
- package/dist/store/sqlite/proposed-actions.js +24 -7
- package/dist/store/sqlite/providers.d.ts +0 -4
- package/dist/store/sqlite/providers.js +20 -2
- package/dist/store/sqlite/record-matching.d.ts +80 -0
- package/dist/store/sqlite/record-matching.js +264 -0
- package/dist/store/sqlite/record-projection.d.ts +89 -0
- package/dist/store/sqlite/record-projection.js +50 -0
- package/dist/store/sqlite/record-projectors-direct.d.ts +17 -0
- package/dist/store/sqlite/record-projectors-direct.js +125 -0
- package/dist/store/sqlite/record-projectors-money.d.ts +26 -0
- package/dist/store/sqlite/record-projectors-money.js +214 -0
- package/dist/store/sqlite/record-projectors-phone.d.ts +28 -0
- package/dist/store/sqlite/record-projectors-phone.js +212 -0
- package/dist/store/sqlite/record-projectors.d.ts +3 -44
- package/dist/store/sqlite/record-projectors.js +7 -67
- package/dist/store/sqlite/record-relations.d.ts +24 -0
- package/dist/store/sqlite/record-relations.js +92 -0
- package/dist/store/sqlite/record-remap.d.ts +12 -0
- package/dist/store/sqlite/record-remap.js +92 -0
- package/dist/store/sqlite/record-run.d.ts +71 -0
- package/dist/store/sqlite/record-run.js +179 -0
- package/dist/store/sqlite/record-settling.d.ts +9 -0
- package/dist/store/sqlite/record-settling.js +74 -0
- package/dist/store/sqlite/record-stamps.d.ts +13 -0
- package/dist/store/sqlite/record-stamps.js +30 -0
- package/dist/store/sqlite/record-writes.d.ts +43 -0
- package/dist/store/sqlite/record-writes.js +307 -0
- package/dist/store/sqlite/records.d.ts +52 -42
- package/dist/store/sqlite/records.js +123 -353
- package/dist/store/sqlite/ref-summaries.d.ts +6 -5
- package/dist/store/sqlite/ref-summaries.js +60 -8
- package/dist/store/sqlite/refunds.d.ts +31 -0
- package/dist/store/sqlite/refunds.js +112 -0
- package/dist/store/sqlite/relation-list.d.ts +43 -0
- package/dist/store/sqlite/relation-list.js +133 -0
- package/dist/store/sqlite/relation-proposals.d.ts +28 -6
- package/dist/store/sqlite/relation-proposals.js +134 -10
- package/dist/store/sqlite/relation-reads.d.ts +8 -0
- package/dist/store/sqlite/relation-reads.js +7 -0
- package/dist/store/sqlite/relation-roles.d.ts +26 -0
- package/dist/store/sqlite/relation-roles.js +91 -0
- package/dist/store/sqlite/reported-relations.d.ts +63 -0
- package/dist/store/sqlite/reported-relations.js +132 -0
- package/dist/store/sqlite/reviewed.d.ts +109 -0
- package/dist/store/sqlite/reviewed.js +173 -0
- package/dist/store/sqlite/schema/link-revisions.d.ts +136 -0
- package/dist/store/sqlite/schema/link-revisions.js +11 -0
- package/dist/store/sqlite/schema/links.d.ts +360 -0
- package/dist/store/sqlite/schema/links.js +32 -0
- package/dist/store/sqlite/schema.d.ts +2769 -306
- package/dist/store/sqlite/schema.js +258 -25
- package/dist/store/sqlite/source-review-marks.d.ts +54 -0
- package/dist/store/sqlite/source-review-marks.js +143 -0
- package/dist/store/sqlite/source-review.d.ts +54 -0
- package/dist/store/sqlite/source-review.js +221 -0
- package/dist/store/sqlite/stage-amounts.d.ts +14 -0
- package/dist/store/sqlite/stage-amounts.js +23 -0
- package/dist/store/sqlite/subscriptions.d.ts +135 -0
- package/dist/store/sqlite/subscriptions.js +294 -0
- package/dist/store/sqlite/text-input.d.ts +3 -0
- package/dist/store/sqlite/text-input.js +13 -0
- package/dist/store/sqlite/things.js +2 -9
- package/dist/store/sqlite/transaction.d.ts +15 -0
- package/dist/store/sqlite/transaction.js +57 -0
- package/dist/store/sqlite/wallets.d.ts +88 -0
- package/dist/store/sqlite/wallets.js +143 -0
- package/dist/store/store-errors.d.ts +1 -0
- package/dist/store/store-errors.js +45 -0
- package/dist/store/store.d.ts +19 -1
- package/dist/store/store.js +35 -6
- package/package.json +6 -3
- package/schemas/examples/records-business.jsonl +11 -0
- package/schemas/records-envelope.v1.schema.json +1160 -5
package/dist/domain/records.js
CHANGED
|
@@ -1,26 +1,33 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { holdsCredential, secretNamed } from "../runs/secrets.js";
|
|
3
|
-
import {
|
|
3
|
+
import { PHONE_COUNTRIES } from "./phones.js";
|
|
4
|
+
import { DECIMAL_PATTERN, fieldError, isObject } from "./record-fields.js";
|
|
5
|
+
import { AMOUNT_ROLES, article, isRecordType, nestedOf, RECORD_ENTITY_TYPES, RECORD_TYPES, referencesOf, roleOf, rolesOf, TYPE_PATTERN, TYPE_RULE, WORD_BODY, } from "./record-types.js";
|
|
4
6
|
export { RECORD_ENTITY_TYPES } from "./record-types.js";
|
|
5
7
|
export const RECORDS_SCHEMA_VERSION = 1;
|
|
6
8
|
export const RECORD_OPERATIONS = ["upsert", "delete"];
|
|
9
|
+
/** How an envelope's lines read `product`: `text` keeps an invoice line's product as data, as v1 did. */
|
|
10
|
+
export const LINE_OPTIONS = ["text", "products"];
|
|
7
11
|
const WORD = `^${WORD_BODY}$`;
|
|
8
12
|
const MAX_ID = 512;
|
|
9
13
|
const MAX_FIELDS_BYTES = 65_536;
|
|
10
14
|
const MAX_RELATIONS = 100;
|
|
11
|
-
const DECIMAL =
|
|
15
|
+
const DECIMAL = DECIMAL_PATTERN;
|
|
16
|
+
const REF = "^[a-z]+:[^\\s]{1,500}$";
|
|
17
|
+
const fieldsDef = (type) => `fields-${type}`;
|
|
12
18
|
/** The v1 envelope as JSON Schema (Draft 2020-12); `schemas/records-envelope.v1.schema.json` is this, written out. */
|
|
13
19
|
export const RECORDS_ENVELOPE_SCHEMA = {
|
|
14
20
|
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
15
21
|
title: "WireCat records envelope, version 1",
|
|
16
|
-
description: "Records one source sends to the store. JSONL is the same model: the first line holds schema_version
|
|
17
|
-
"
|
|
22
|
+
description: "Records one source sends to the store. JSONL is the same model: the first line holds schema_version, source " +
|
|
23
|
+
"and options, each further line one record.",
|
|
18
24
|
type: "object",
|
|
19
25
|
required: ["schema_version", "source", "records"],
|
|
20
26
|
additionalProperties: false,
|
|
21
27
|
properties: {
|
|
22
28
|
schema_version: { const: RECORDS_SCHEMA_VERSION },
|
|
23
29
|
source: { $ref: "#/$defs/source" },
|
|
30
|
+
options: { $ref: "#/$defs/options" },
|
|
24
31
|
records: { type: "array", items: { $ref: "#/$defs/record" } },
|
|
25
32
|
},
|
|
26
33
|
$defs: {
|
|
@@ -34,6 +41,23 @@ export const RECORDS_ENVELOPE_SCHEMA = {
|
|
|
34
41
|
batch_id: { type: "string", minLength: 1, maxLength: MAX_ID, description: "This import, for the receipt" },
|
|
35
42
|
},
|
|
36
43
|
},
|
|
44
|
+
options: {
|
|
45
|
+
type: "object",
|
|
46
|
+
additionalProperties: false,
|
|
47
|
+
properties: {
|
|
48
|
+
lines: {
|
|
49
|
+
enum: [...LINE_OPTIONS],
|
|
50
|
+
description: "products: an invoice line's product (an external id) names its product; text, the default, keeps it " +
|
|
51
|
+
"as data. Order lines always read it, and product_ref (a product: ref) is read on every line.",
|
|
52
|
+
},
|
|
53
|
+
phone_region: {
|
|
54
|
+
type: "string",
|
|
55
|
+
pattern: "^[A-Za-z]{2}$",
|
|
56
|
+
description: "The country a person's phone without + is read in. Without it such a number is an identifier only when " +
|
|
57
|
+
"exactly one country of the store's phone_countries setting reads it as valid",
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
37
61
|
record: {
|
|
38
62
|
type: "object",
|
|
39
63
|
required: ["key", "type", "external_id", "operation"],
|
|
@@ -50,15 +74,27 @@ export const RECORDS_ENVELOPE_SCHEMA = {
|
|
|
50
74
|
revision: { type: "integer", minimum: 0, maximum: Number.MAX_SAFE_INTEGER },
|
|
51
75
|
relations: { type: "array", maxItems: MAX_RELATIONS, items: { $ref: "#/$defs/relation" } },
|
|
52
76
|
},
|
|
77
|
+
allOf: RECORD_ENTITY_TYPES.map((type) => ({
|
|
78
|
+
if: { properties: { type: { const: type } }, required: ["type"] },
|
|
79
|
+
// biome-ignore lint/suspicious/noThenProperty: JSON Schema's own if/then keyword, never awaited.
|
|
80
|
+
then: { properties: { fields: { $ref: `#/$defs/${fieldsDef(type)}` } } },
|
|
81
|
+
})),
|
|
53
82
|
},
|
|
54
83
|
relation: {
|
|
55
84
|
type: "object",
|
|
56
|
-
required: ["role"
|
|
85
|
+
required: ["role"],
|
|
57
86
|
additionalProperties: false,
|
|
87
|
+
oneOf: [{ required: ["type", "external_id"] }, { required: ["ref"] }],
|
|
58
88
|
properties: {
|
|
59
89
|
role: { type: "string", pattern: WORD },
|
|
60
90
|
type: { type: "string", pattern: TYPE_PATTERN },
|
|
61
91
|
external_id: { type: "string", minLength: 1, maxLength: MAX_ID },
|
|
92
|
+
ref: { type: "string", pattern: REF, description: "A store entity, such as organization:12" },
|
|
93
|
+
as: {
|
|
94
|
+
type: "string",
|
|
95
|
+
pattern: WORD,
|
|
96
|
+
description: "On a relationship role: the part the target plays, such as decision-maker",
|
|
97
|
+
},
|
|
62
98
|
amount: {
|
|
63
99
|
type: "string",
|
|
64
100
|
pattern: DECIMAL,
|
|
@@ -66,12 +102,13 @@ export const RECORDS_ENVELOPE_SCHEMA = {
|
|
|
66
102
|
},
|
|
67
103
|
},
|
|
68
104
|
},
|
|
105
|
+
...Object.fromEntries(RECORD_ENTITY_TYPES.map((type) => [fieldsDef(type), RECORD_TYPES[type].fields])),
|
|
69
106
|
},
|
|
70
107
|
};
|
|
71
108
|
const wordPattern = new RegExp(WORD);
|
|
72
109
|
const typePattern = new RegExp(TYPE_PATTERN);
|
|
73
110
|
const decimalPattern = new RegExp(DECIMAL);
|
|
74
|
-
const
|
|
111
|
+
const refPattern = new RegExp(REF);
|
|
75
112
|
const escapePointer = (segment) => String(segment).replaceAll("~", "~0").replaceAll("/", "~1");
|
|
76
113
|
export const pointerOf = (...segments) => segments.map((s) => `/${escapePointer(s)}`).join("");
|
|
77
114
|
const idText = (value) => typeof value === "string" && value.trim() !== "" && value.length <= MAX_ID && !value.includes("\0");
|
|
@@ -99,6 +136,8 @@ const secretIn = (value, at) => {
|
|
|
99
136
|
}
|
|
100
137
|
return undefined;
|
|
101
138
|
};
|
|
139
|
+
/** The type a relation points at: its own `type`, or the prefix of its `ref`. */
|
|
140
|
+
export const targetTypeOf = (relation) => relation.ref === undefined ? String(relation.type) : relation.ref.slice(0, relation.ref.indexOf(":"));
|
|
102
141
|
const relationErrors = (relations, at) => {
|
|
103
142
|
if (relations === undefined)
|
|
104
143
|
return [];
|
|
@@ -108,15 +147,25 @@ const relationErrors = (relations, at) => {
|
|
|
108
147
|
const here = [...at, index];
|
|
109
148
|
if (!isObject(relation))
|
|
110
149
|
return [invalid(here, "a relation is an object")];
|
|
111
|
-
const extra = extraKeys(relation, ["role", "type", "external_id", "amount"]);
|
|
150
|
+
const extra = extraKeys(relation, ["role", "type", "external_id", "ref", "as", "amount"]);
|
|
112
151
|
if (extra.length > 0)
|
|
113
|
-
return [invalid([...here, extra[0]], "a relation holds role, type, external_id and amount")];
|
|
152
|
+
return [invalid([...here, extra[0]], "a relation holds role, type, external_id, ref, as and amount")];
|
|
114
153
|
if (typeof relation.role !== "string" || !wordPattern.test(relation.role))
|
|
115
154
|
return [invalid([...here, "role"], "a role is lowercase letters, digits, - and _")];
|
|
116
|
-
if (
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
155
|
+
if (relation.ref !== undefined) {
|
|
156
|
+
if (relation.type !== undefined || relation.external_id !== undefined)
|
|
157
|
+
return [invalid([...here, "ref"], "a relation names its target by ref, or by type and external_id")];
|
|
158
|
+
if (typeof relation.ref !== "string" || !refPattern.test(relation.ref))
|
|
159
|
+
return [invalid([...here, "ref"], "a ref is a store reference, such as organization:12")];
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
if (typeof relation.type !== "string" || !typePattern.test(relation.type))
|
|
163
|
+
return [invalid([...here, "type"], TYPE_RULE)];
|
|
164
|
+
if (!idText(relation.external_id))
|
|
165
|
+
return [invalid([...here, "external_id"], `an external id takes 1–${MAX_ID} characters`)];
|
|
166
|
+
}
|
|
167
|
+
if (relation.as !== undefined && (typeof relation.as !== "string" || !wordPattern.test(relation.as)))
|
|
168
|
+
return [invalid([...here, "as"], "as is lowercase letters, digits, - and _")];
|
|
120
169
|
if (relation.amount !== undefined && (typeof relation.amount !== "string" || !decimalPattern.test(relation.amount)))
|
|
121
170
|
return [invalid([...here, "amount"], 'an amount is a decimal string, such as "13000.00"')];
|
|
122
171
|
if (relation.amount !== undefined && !AMOUNT_ROLES.includes(relation.role))
|
|
@@ -124,6 +173,37 @@ const relationErrors = (relations, at) => {
|
|
|
124
173
|
return [];
|
|
125
174
|
});
|
|
126
175
|
};
|
|
176
|
+
/** What the type's registry entry says about its fields and roles: run once the record's shape is right. */
|
|
177
|
+
const typeErrors = (record, at) => {
|
|
178
|
+
if (record.operation !== "upsert" || !isRecordType(record.type))
|
|
179
|
+
return [];
|
|
180
|
+
const { type } = record;
|
|
181
|
+
const wrong = fieldError(RECORD_TYPES[type].fields, record.fields);
|
|
182
|
+
if (wrong)
|
|
183
|
+
return [invalid([...at, "fields", ...wrong.at], wrong.message)];
|
|
184
|
+
const relations = record.relations ?? [];
|
|
185
|
+
for (const [index, relation] of relations.entries()) {
|
|
186
|
+
const here = [...at, "relations", index];
|
|
187
|
+
const role = roleOf(type, relation.role);
|
|
188
|
+
if (relation.as !== undefined && !role?.relationship)
|
|
189
|
+
return [
|
|
190
|
+
invalid([...here, "as"], `${article(type)} ${type}'s ${relation.role} is not a relationship: it takes no as`),
|
|
191
|
+
];
|
|
192
|
+
if (role === undefined || role.v1)
|
|
193
|
+
continue;
|
|
194
|
+
const target = targetTypeOf(relation);
|
|
195
|
+
if (!role.targets.includes(target))
|
|
196
|
+
return [
|
|
197
|
+
invalid(here, `${article(type)} ${type}'s ${relation.role} is ${role.targets.map((one) => `${article(one)} ${one}`).join(" or ")}`),
|
|
198
|
+
];
|
|
199
|
+
if (!role.many && relations.some((other, before) => before < index && other.role === relation.role))
|
|
200
|
+
return [invalid(here, `${article(type)} ${type} has one ${relation.role}`, "record.relation-ambiguous")];
|
|
201
|
+
}
|
|
202
|
+
for (const [name, role] of rolesOf(type))
|
|
203
|
+
if (role.required && !relations.some((relation) => relation.role === name))
|
|
204
|
+
return [invalid([...at, "relations"], `${article(type)} ${type} names its ${name} in relations`)];
|
|
205
|
+
return [];
|
|
206
|
+
};
|
|
127
207
|
const invalid = (at, message, code = "record.invalid") => ({
|
|
128
208
|
pointer: pointerOf(...at),
|
|
129
209
|
code,
|
|
@@ -161,13 +241,23 @@ export const recordErrors = (record, index) => {
|
|
|
161
241
|
return withKey([
|
|
162
242
|
invalid(secret, "a secret never goes into the store: drop this field at the source", "record.secret-field"),
|
|
163
243
|
]);
|
|
164
|
-
|
|
244
|
+
const relations = relationErrors(record.relations, [...at, "relations"]);
|
|
245
|
+
return withKey(relations.length > 0 ? relations : typeErrors(record, at));
|
|
165
246
|
};
|
|
166
247
|
/** The envelope's own errors (not its records'): when there is one, no record is taken. */
|
|
167
248
|
export const envelopeErrors = (envelope, account) => {
|
|
249
|
+
const header = headerErrors(envelope, account, ["records"]);
|
|
250
|
+
if (header.length > 0)
|
|
251
|
+
return header;
|
|
252
|
+
if (!Array.isArray(envelope.records))
|
|
253
|
+
return [invalid(["records"], "records is a list", "envelope.invalid")];
|
|
254
|
+
return [];
|
|
255
|
+
};
|
|
256
|
+
/** A JSONL stream's first line, or an envelope without its records: `schema_version`, `source` and `options`. */
|
|
257
|
+
export const headerErrors = (envelope, account, more = []) => {
|
|
168
258
|
if (!isObject(envelope))
|
|
169
259
|
return [invalid([], "an envelope is an object", "envelope.invalid")];
|
|
170
|
-
const extra = extraKeys(envelope, ["schema_version", "source", "
|
|
260
|
+
const extra = extraKeys(envelope, ["schema_version", "source", "options", ...more]);
|
|
171
261
|
if (extra.length > 0)
|
|
172
262
|
return [invalid([extra[0]], `an envelope has no ${extra[0]}`, "envelope.invalid")];
|
|
173
263
|
if (envelope.schema_version !== RECORDS_SCHEMA_VERSION)
|
|
@@ -192,8 +282,22 @@ export const envelopeErrors = (envelope, account) => {
|
|
|
192
282
|
return [
|
|
193
283
|
invalid(["source", "account"], `the envelope is for account ${JSON.stringify(source.account)}, the import for ${JSON.stringify(account)}`, "envelope.account-mismatch"),
|
|
194
284
|
];
|
|
195
|
-
|
|
196
|
-
|
|
285
|
+
const { options } = envelope;
|
|
286
|
+
if (options !== undefined) {
|
|
287
|
+
if (!isObject(options))
|
|
288
|
+
return [invalid(["options"], "options is an object", "envelope.invalid")];
|
|
289
|
+
const unknown = extraKeys(options, ["lines", "phone_region"]);
|
|
290
|
+
if (unknown.length > 0)
|
|
291
|
+
return [invalid(["options", unknown[0]], "options holds lines and phone_region", "envelope.invalid")];
|
|
292
|
+
if (options.phone_region !== undefined &&
|
|
293
|
+
(typeof options.phone_region !== "string" ||
|
|
294
|
+
!PHONE_COUNTRIES.includes(options.phone_region.toLowerCase())))
|
|
295
|
+
return [
|
|
296
|
+
invalid(["options", "phone_region"], `options.phone_region is one of ${PHONE_COUNTRIES.join(", ")}`, "envelope.invalid"),
|
|
297
|
+
];
|
|
298
|
+
if (options.lines !== undefined && !LINE_OPTIONS.includes(options.lines))
|
|
299
|
+
return [invalid(["options", "lines"], `options.lines is ${LINE_OPTIONS.join(" or ")}`, "envelope.invalid")];
|
|
300
|
+
}
|
|
197
301
|
return [];
|
|
198
302
|
};
|
|
199
303
|
const canonical = (value) => {
|
|
@@ -210,39 +314,81 @@ export const recordFingerprint = (fields, relations) => createHash("sha256")
|
|
|
210
314
|
.update(JSON.stringify(canonical({
|
|
211
315
|
fields,
|
|
212
316
|
relations: relations
|
|
213
|
-
//
|
|
317
|
+
// What a relation did not carry adds nothing, so every fingerprint stored before amounts, refs and
|
|
318
|
+
// `as` existed stays the same. `ref=` and `as=` cannot be taken for an amount, which is a bare decimal.
|
|
214
319
|
.map((relation) => [
|
|
215
320
|
relation.role,
|
|
216
|
-
relation.type,
|
|
217
|
-
relation.external_id,
|
|
321
|
+
...(relation.ref === undefined ? [relation.type, relation.external_id] : [`ref=${relation.ref}`]),
|
|
218
322
|
...(relation.amount === undefined ? [] : [relation.amount]),
|
|
323
|
+
...(relation.as === undefined ? [] : [`as=${relation.as}`]),
|
|
219
324
|
])
|
|
220
325
|
.sort((a, b) => a.join("\0").localeCompare(b.join("\0"))),
|
|
221
326
|
})))
|
|
222
327
|
.digest("hex");
|
|
328
|
+
const BOM = "\uFEFF";
|
|
329
|
+
/** A file's text without the byte-order mark a spreadsheet export puts before it. */
|
|
330
|
+
export const withoutBom = (text) => (text.startsWith(BOM) ? text.slice(1) : text);
|
|
331
|
+
/** One JSONL line as JSON; a line that is not JSON comes back as the text, for the record check to refuse at its index. */
|
|
332
|
+
export const jsonLine = (line) => {
|
|
333
|
+
try {
|
|
334
|
+
return JSON.parse(withoutBom(line));
|
|
335
|
+
}
|
|
336
|
+
catch {
|
|
337
|
+
return line;
|
|
338
|
+
}
|
|
339
|
+
};
|
|
223
340
|
/**
|
|
224
|
-
* A JSONL import as the envelope it stands for: the first line holds `schema_version` and `
|
|
225
|
-
* non-empty line one record.
|
|
341
|
+
* A JSONL import as the envelope it stands for: the first line holds `schema_version`, `source` and `options`, each
|
|
342
|
+
* further non-empty line one record.
|
|
226
343
|
*/
|
|
227
344
|
export const envelopeOfJsonl = (text) => {
|
|
228
|
-
const lines = text.split(/\r?\n/);
|
|
345
|
+
const lines = withoutBom(text).split(/\r?\n/);
|
|
229
346
|
const first = lines.findIndex((line) => line.trim() !== "");
|
|
230
347
|
if (first < 0)
|
|
231
348
|
return {};
|
|
232
|
-
const
|
|
233
|
-
try {
|
|
234
|
-
return JSON.parse(line);
|
|
235
|
-
}
|
|
236
|
-
catch {
|
|
237
|
-
return line;
|
|
238
|
-
}
|
|
239
|
-
};
|
|
240
|
-
const header = parse(lines[first]);
|
|
349
|
+
const header = jsonLine(lines[first]);
|
|
241
350
|
const records = [];
|
|
242
351
|
for (let at = first + 1; at < lines.length; at++) {
|
|
243
352
|
const line = lines[at];
|
|
244
353
|
if (line.trim() !== "")
|
|
245
|
-
records.push(
|
|
354
|
+
records.push(jsonLine(line));
|
|
246
355
|
}
|
|
247
356
|
return isObject(header) ? { ...header, records } : header;
|
|
248
357
|
};
|
|
358
|
+
/**
|
|
359
|
+
* A file's text as the envelope it stands for: one JSON envelope, or JSONL, told apart by the first non-empty line;
|
|
360
|
+
* a JSON object holding `records` is a whole envelope.
|
|
361
|
+
*/
|
|
362
|
+
export const envelopeOfText = (text) => {
|
|
363
|
+
const clean = withoutBom(text);
|
|
364
|
+
const first = clean.split(/\r?\n/).find((line) => line.trim() !== "");
|
|
365
|
+
const head = first === undefined ? undefined : jsonLine(first);
|
|
366
|
+
if (isObject(head) && !Object.hasOwn(head, "records"))
|
|
367
|
+
return envelopeOfJsonl(clean);
|
|
368
|
+
return jsonLine(clean);
|
|
369
|
+
};
|
|
370
|
+
/**
|
|
371
|
+
* What a record's fields name by external id: a deal's pipeline, a line's product. An invoice's line products only
|
|
372
|
+
* with `options.lines: products`, since v1 kept them as data.
|
|
373
|
+
*/
|
|
374
|
+
export const fieldReferences = (record, options = {}) => {
|
|
375
|
+
if (record.operation !== "upsert" || !isRecordType(record.type))
|
|
376
|
+
return [];
|
|
377
|
+
const fields = record.fields ?? {};
|
|
378
|
+
const found = [];
|
|
379
|
+
for (const [key, type] of referencesOf(record.type))
|
|
380
|
+
if (typeof fields[key] === "string")
|
|
381
|
+
found.push({ role: `fields.${key}`, type, external_id: fields[key] });
|
|
382
|
+
if (record.type === "invoice" && options.lines !== "products")
|
|
383
|
+
return found;
|
|
384
|
+
for (const [list, spec] of nestedOf(record.type)) {
|
|
385
|
+
const items = fields[list];
|
|
386
|
+
if (!Array.isArray(items))
|
|
387
|
+
continue;
|
|
388
|
+
for (const item of items)
|
|
389
|
+
for (const [key, type] of Object.entries(spec.references ?? {}))
|
|
390
|
+
if (isObject(item) && typeof item[key] === "string")
|
|
391
|
+
found.push({ role: `fields.${list}.${key}`, type, external_id: item[key] });
|
|
392
|
+
}
|
|
393
|
+
return found;
|
|
394
|
+
};
|
|
@@ -55,6 +55,11 @@ const WORDS = {
|
|
|
55
55
|
deal: "Deal",
|
|
56
56
|
lead: "Lead",
|
|
57
57
|
pipeline: "Pipeline",
|
|
58
|
+
product: "Product",
|
|
59
|
+
contract: "Contract",
|
|
60
|
+
subscription: "Subscription",
|
|
61
|
+
wallet: "Wallet",
|
|
62
|
+
transaction: "Transaction",
|
|
58
63
|
email: "Email",
|
|
59
64
|
email_thread: "Email thread",
|
|
60
65
|
unknown: "Unknown",
|
|
@@ -33,7 +33,7 @@ export type Reference = {
|
|
|
33
33
|
* Things named by their store id (a task also by its key or the task package's id). `entity` is the name
|
|
34
34
|
* before organizations and projects: it still parses, so an old file's reference resolves as not found.
|
|
35
35
|
*/
|
|
36
|
-
export declare const SIMPLE_REFERENCES: readonly ["note", "document", "person", "organization", "project", "task", "memory", "fact", "bot", "event", "invoice", "payment", "order", "deal", "lead", "pipeline", "entity"];
|
|
36
|
+
export declare const SIMPLE_REFERENCES: readonly ["note", "document", "person", "organization", "project", "task", "memory", "fact", "bot", "event", "invoice", "payment", "order", "deal", "lead", "pipeline", "product", "contract", "subscription", "wallet", "transaction", "entity"];
|
|
37
37
|
export type SimpleReference = (typeof SIMPLE_REFERENCES)[number];
|
|
38
38
|
export type RefKindName = Exclude<Reference["type"], "entity">;
|
|
39
39
|
export interface RefKindSpec<R extends Reference = Reference> {
|
|
@@ -22,6 +22,11 @@ export const SIMPLE_REFERENCES = [
|
|
|
22
22
|
"deal",
|
|
23
23
|
"lead",
|
|
24
24
|
"pipeline",
|
|
25
|
+
"product",
|
|
26
|
+
"contract",
|
|
27
|
+
"subscription",
|
|
28
|
+
"wallet",
|
|
29
|
+
"transaction",
|
|
25
30
|
"entity",
|
|
26
31
|
];
|
|
27
32
|
const malformed = (text, shape) => new CliError("validation_error", `"${singleLine(text)}" is not a reference — expected ${shape}`, {
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/** What a person is to an organization; who reports to whom is `reports-to`, and a job title is never a role. */
|
|
2
|
+
export declare const ORGANIZATION_ROLES: readonly ["employee", "contractor", "founder", "owner", "manager", "adviser", "contact"];
|
|
3
|
+
export declare const PROJECT_ROLES: readonly ["owner", "manager", "contributor", "client-contact", "approver"];
|
|
4
|
+
type End = "person" | "organization" | "project" | "task" | "chat" | "email_thread" | "deal" | "lead";
|
|
5
|
+
interface RelationSpec {
|
|
6
|
+
from: readonly End[];
|
|
7
|
+
to: readonly End[];
|
|
8
|
+
/** Role lists by the end that names them; where none applies, a role is free text. */
|
|
9
|
+
roles?: {
|
|
10
|
+
from?: Partial<Record<End, readonly string[]>>;
|
|
11
|
+
to?: Partial<Record<End, readonly string[]>>;
|
|
12
|
+
};
|
|
13
|
+
/** Shown when a relationship is read from its `to` end; never stored. */
|
|
14
|
+
inverse?: string;
|
|
15
|
+
/** Stored once, the lower id as `from`. */
|
|
16
|
+
symmetric?: true;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Every relationship `knowledge.relate` makes, its ends, its roles and how it reads backwards. An organization may
|
|
20
|
+
* still be a member: owner data from before this list holds such links.
|
|
21
|
+
*/
|
|
22
|
+
export declare const RELATIONS: {
|
|
23
|
+
readonly "member-of": {
|
|
24
|
+
readonly from: readonly ["person", "organization", "project", "chat", "email_thread"];
|
|
25
|
+
readonly to: readonly ["organization", "project"];
|
|
26
|
+
readonly roles: {
|
|
27
|
+
readonly to: {
|
|
28
|
+
readonly organization: readonly ["employee", "contractor", "founder", "owner", "manager", "adviser", "contact"];
|
|
29
|
+
readonly project: readonly ["owner", "manager", "contributor", "client-contact", "approver"];
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
readonly "reports-to": {
|
|
34
|
+
readonly from: readonly ["person"];
|
|
35
|
+
readonly to: readonly ["person"];
|
|
36
|
+
readonly inverse: "manages";
|
|
37
|
+
};
|
|
38
|
+
readonly "customer-of": {
|
|
39
|
+
readonly from: readonly ["person", "organization"];
|
|
40
|
+
readonly to: readonly ["organization"];
|
|
41
|
+
readonly inverse: "supplier of";
|
|
42
|
+
};
|
|
43
|
+
readonly "partner-of": {
|
|
44
|
+
readonly from: readonly ["organization"];
|
|
45
|
+
readonly to: readonly ["organization"];
|
|
46
|
+
readonly symmetric: true;
|
|
47
|
+
};
|
|
48
|
+
readonly "related-to": {
|
|
49
|
+
readonly from: readonly ["person", "organization", "project", "task", "deal", "lead"];
|
|
50
|
+
readonly to: readonly ["person", "organization", "project", "task"];
|
|
51
|
+
readonly roles: {
|
|
52
|
+
readonly from: {
|
|
53
|
+
readonly deal: readonly ["owner", "contact", "decision-maker", "influencer"];
|
|
54
|
+
readonly lead: readonly ["owner", "contact", "decision-maker", "influencer"];
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
export type RelationKind = keyof typeof RELATIONS;
|
|
60
|
+
export declare const RELATION_KINDS: RelationKind[];
|
|
61
|
+
export declare const isRelationKind: (kind: string) => kind is RelationKind;
|
|
62
|
+
export declare const checkEnds: (kind: string, from: string, to: string) => RelationSpec;
|
|
63
|
+
/**
|
|
64
|
+
* The roles a relationship takes, or `undefined` for free text. A member's list is the organization's or the
|
|
65
|
+
* project's only when the member is a person: a chat's or a sub-project's role was never one of them.
|
|
66
|
+
*/
|
|
67
|
+
export declare const rolesOf: (kind: string, from: string, to: string) => readonly string[] | undefined;
|
|
68
|
+
/** The role lists the owner may add to; each is a list the registry gives some relationship's ends. */
|
|
69
|
+
export declare const ROLE_LISTS: {
|
|
70
|
+
readonly organization: readonly ["employee", "contractor", "founder", "owner", "manager", "adviser", "contact"];
|
|
71
|
+
readonly project: readonly ["owner", "manager", "contributor", "client-contact", "approver"];
|
|
72
|
+
readonly deal: readonly ["owner", "contact", "decision-maker", "influencer"];
|
|
73
|
+
};
|
|
74
|
+
export type RoleList = keyof typeof ROLE_LISTS;
|
|
75
|
+
/** Which list a relationship's role comes from, or `undefined` where a role is free text. */
|
|
76
|
+
export declare const roleListOf: (kind: string, from: string, to: string) => RoleList | undefined;
|
|
77
|
+
/**
|
|
78
|
+
* The role as given when the relationship has no list, or when it is on the list or among `extra` (the owner's own
|
|
79
|
+
* roles for that list); refused otherwise.
|
|
80
|
+
*/
|
|
81
|
+
export declare const checkRole: (kind: string, from: string, to: string, role: string, extra?: readonly string[]) => string;
|
|
82
|
+
export declare const isSymmetric: (kind: string) => boolean;
|
|
83
|
+
/** The label of a relationship read from its `to` end: `manages` for `reports-to`; the kind itself when symmetric. */
|
|
84
|
+
export declare const inverseOf: (kind: string) => string | null;
|
|
85
|
+
/**
|
|
86
|
+
* What an answer says about a relationship, derived, never stored: the owner's, a source's that nobody confirmed, an
|
|
87
|
+
* agent's suggestion waiting for review, or one that no longer holds.
|
|
88
|
+
*/
|
|
89
|
+
export declare const STANDINGS: readonly ["confirmed", "reported", "proposed", "ended"];
|
|
90
|
+
export type Standing = (typeof STANDINGS)[number];
|
|
91
|
+
/** A link's columns `standing` reads; a column an older store lacks is `null`. */
|
|
92
|
+
export interface StandingFields {
|
|
93
|
+
confirmed: boolean;
|
|
94
|
+
accountId?: number | null;
|
|
95
|
+
/** Epoch ms, exclusive. */
|
|
96
|
+
endsAt?: number | null;
|
|
97
|
+
deletedAt?: number | null;
|
|
98
|
+
}
|
|
99
|
+
/** What a `links` row of a relationship kind holds beyond any link, when set. */
|
|
100
|
+
export interface RelationshipColumns {
|
|
101
|
+
/** The `accounts` row of the source that reported it. */
|
|
102
|
+
accountId?: number;
|
|
103
|
+
/** When it held, epoch ms; `endsAt` exclusive. */
|
|
104
|
+
startsAt?: number;
|
|
105
|
+
endsAt?: number;
|
|
106
|
+
/** The source stopped reporting it. */
|
|
107
|
+
deletedAt?: number;
|
|
108
|
+
/** A job title beside it (`metadata.position`), never its role. */
|
|
109
|
+
position?: string;
|
|
110
|
+
/** The source's link this owner link overrides (`metadata.overrides`). */
|
|
111
|
+
overrides?: number;
|
|
112
|
+
}
|
|
113
|
+
/** The owner's confirmation outlives a source that stopped reporting it; only its own end date ends it. */
|
|
114
|
+
export declare const standingOf: (link: StandingFields, now: number) => Standing;
|
|
115
|
+
/** A relationship a context or a traversal counts: the owner's, or a source's that still holds. */
|
|
116
|
+
export declare const isUsable: (standing: Standing) => boolean;
|
|
117
|
+
export declare const usableLink: (link: StandingFields, now: number) => boolean;
|
|
118
|
+
/**
|
|
119
|
+
* The `links` row aliased `alias` has no owner link overriding it (`metadata.overrides`, an owner's edit of a link a
|
|
120
|
+
* source reported): where one does, the override is the answer.
|
|
121
|
+
*/
|
|
122
|
+
export declare const notOverriddenSql: (alias: string) => string;
|
|
123
|
+
/** `usableLink` as SQL over the `links` row aliased `alias`; without `now` an end date is not checked. */
|
|
124
|
+
export declare const usableLinkSql: (alias: string, now?: number) => string;
|
|
125
|
+
export {};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { DEAL_ROLES } from "./deals.js";
|
|
3
|
+
/** What a person is to an organization; who reports to whom is `reports-to`, and a job title is never a role. */
|
|
4
|
+
export const ORGANIZATION_ROLES = [
|
|
5
|
+
"employee",
|
|
6
|
+
"contractor",
|
|
7
|
+
"founder",
|
|
8
|
+
"owner",
|
|
9
|
+
"manager",
|
|
10
|
+
"adviser",
|
|
11
|
+
"contact",
|
|
12
|
+
];
|
|
13
|
+
export const PROJECT_ROLES = ["owner", "manager", "contributor", "client-contact", "approver"];
|
|
14
|
+
/**
|
|
15
|
+
* Every relationship `knowledge.relate` makes, its ends, its roles and how it reads backwards. An organization may
|
|
16
|
+
* still be a member: owner data from before this list holds such links.
|
|
17
|
+
*/
|
|
18
|
+
export const RELATIONS = {
|
|
19
|
+
"member-of": {
|
|
20
|
+
from: ["person", "organization", "project", "chat", "email_thread"],
|
|
21
|
+
to: ["organization", "project"],
|
|
22
|
+
roles: { to: { organization: ORGANIZATION_ROLES, project: PROJECT_ROLES } },
|
|
23
|
+
},
|
|
24
|
+
"reports-to": { from: ["person"], to: ["person"], inverse: "manages" },
|
|
25
|
+
"customer-of": { from: ["person", "organization"], to: ["organization"], inverse: "supplier of" },
|
|
26
|
+
"partner-of": { from: ["organization"], to: ["organization"], symmetric: true },
|
|
27
|
+
"related-to": {
|
|
28
|
+
from: ["person", "organization", "project", "task", "deal", "lead"],
|
|
29
|
+
to: ["person", "organization", "project", "task"],
|
|
30
|
+
roles: { from: { deal: DEAL_ROLES, lead: DEAL_ROLES } },
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
export const RELATION_KINDS = Object.keys(RELATIONS);
|
|
34
|
+
const specOf = (kind) => Object.hasOwn(RELATIONS, kind) ? RELATIONS[kind] : undefined;
|
|
35
|
+
export const isRelationKind = (kind) => specOf(kind) !== undefined;
|
|
36
|
+
/** A chat or a mail thread is only ever a project's member: its reference names one account. */
|
|
37
|
+
const memberOnly = (type) => type === "chat" || type === "email_thread";
|
|
38
|
+
export const checkEnds = (kind, from, to) => {
|
|
39
|
+
const spec = specOf(kind);
|
|
40
|
+
if (!spec)
|
|
41
|
+
throw new CliError("validation_error", `a relationship's kind is one of ${RELATION_KINDS.join(", ")}`);
|
|
42
|
+
if (memberOnly(from) && (kind !== "member-of" || to !== "project"))
|
|
43
|
+
throw new CliError("validation_error", `a ${from === "chat" ? "chat" : "mail thread"} is related only as member-of a project`);
|
|
44
|
+
if (!spec.from.includes(from) || !spec.to.includes(to))
|
|
45
|
+
throw new CliError("validation_error", `${kind} joins ${spec.from.join(", ")} to ${spec.to.join(", ")}, not ${from} to ${to}`);
|
|
46
|
+
return spec;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* The roles a relationship takes, or `undefined` for free text. A member's list is the organization's or the
|
|
50
|
+
* project's only when the member is a person: a chat's or a sub-project's role was never one of them.
|
|
51
|
+
*/
|
|
52
|
+
export const rolesOf = (kind, from, to) => {
|
|
53
|
+
const spec = specOf(kind);
|
|
54
|
+
if (kind === "member-of" && from !== "person")
|
|
55
|
+
return undefined;
|
|
56
|
+
return spec?.roles?.from?.[from] ?? spec?.roles?.to?.[to];
|
|
57
|
+
};
|
|
58
|
+
/** The role lists the owner may add to; each is a list the registry gives some relationship's ends. */
|
|
59
|
+
export const ROLE_LISTS = { organization: ORGANIZATION_ROLES, project: PROJECT_ROLES, deal: DEAL_ROLES };
|
|
60
|
+
/** Which list a relationship's role comes from, or `undefined` where a role is free text. */
|
|
61
|
+
export const roleListOf = (kind, from, to) => {
|
|
62
|
+
const roles = rolesOf(kind, from, to);
|
|
63
|
+
return Object.keys(ROLE_LISTS).find((list) => ROLE_LISTS[list] === roles);
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* The role as given when the relationship has no list, or when it is on the list or among `extra` (the owner's own
|
|
67
|
+
* roles for that list); refused otherwise.
|
|
68
|
+
*/
|
|
69
|
+
export const checkRole = (kind, from, to, role, extra = []) => {
|
|
70
|
+
const roles = rolesOf(kind, from, to)?.concat(extra);
|
|
71
|
+
if (roles && !roles.includes(role))
|
|
72
|
+
throw new CliError("validation_error", `a ${from}'s role on ${kind} ${to} is one of ${roles.join(", ")}`, {
|
|
73
|
+
reason: "relationship.role-unknown",
|
|
74
|
+
});
|
|
75
|
+
return role;
|
|
76
|
+
};
|
|
77
|
+
export const isSymmetric = (kind) => specOf(kind)?.symmetric === true;
|
|
78
|
+
/** The label of a relationship read from its `to` end: `manages` for `reports-to`; the kind itself when symmetric. */
|
|
79
|
+
export const inverseOf = (kind) => specOf(kind)?.symmetric ? kind : (specOf(kind)?.inverse ?? null);
|
|
80
|
+
/**
|
|
81
|
+
* What an answer says about a relationship, derived, never stored: the owner's, a source's that nobody confirmed, an
|
|
82
|
+
* agent's suggestion waiting for review, or one that no longer holds.
|
|
83
|
+
*/
|
|
84
|
+
export const STANDINGS = ["confirmed", "reported", "proposed", "ended"];
|
|
85
|
+
/** The owner's confirmation outlives a source that stopped reporting it; only its own end date ends it. */
|
|
86
|
+
export const standingOf = (link, now) => {
|
|
87
|
+
if (link.endsAt != null && link.endsAt <= now)
|
|
88
|
+
return "ended";
|
|
89
|
+
if (link.confirmed)
|
|
90
|
+
return "confirmed";
|
|
91
|
+
if (link.deletedAt != null)
|
|
92
|
+
return "ended";
|
|
93
|
+
return link.accountId != null ? "reported" : "proposed";
|
|
94
|
+
};
|
|
95
|
+
/** A relationship a context or a traversal counts: the owner's, or a source's that still holds. */
|
|
96
|
+
export const isUsable = (standing) => standing === "confirmed" || standing === "reported";
|
|
97
|
+
export const usableLink = (link, now) => isUsable(standingOf(link, now));
|
|
98
|
+
/**
|
|
99
|
+
* The `links` row aliased `alias` has no owner link overriding it (`metadata.overrides`, an owner's edit of a link a
|
|
100
|
+
* source reported): where one does, the override is the answer.
|
|
101
|
+
*/
|
|
102
|
+
export const notOverriddenSql = (alias) => `NOT EXISTS (SELECT 1 FROM links override WHERE override.kind = ${alias}.kind AND override.from_type = ${alias}.from_type ` +
|
|
103
|
+
`AND override.from_id = ${alias}.from_id AND override.to_type = ${alias}.to_type AND override.to_id = ${alias}.to_id ` +
|
|
104
|
+
`AND json_extract(override.metadata, '$.overrides') = ${alias}.id)`;
|
|
105
|
+
/** `usableLink` as SQL over the `links` row aliased `alias`; without `now` an end date is not checked. */
|
|
106
|
+
export const usableLinkSql = (alias, now) => `(${alias}.confirmed = 1 OR (${alias}.account_id IS NOT NULL AND ${alias}.deleted_at IS NULL))` +
|
|
107
|
+
(now === undefined ? "" : ` AND (${alias}.ends_at IS NULL OR ${alias}.ends_at > ${Math.trunc(now)})`);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { PhoneCountry } from "./phones.js";
|
|
2
|
+
import type { RecordsEnvelope } from "./records.js";
|
|
3
|
+
export interface VcardOptions {
|
|
4
|
+
provider: string;
|
|
5
|
+
account: string;
|
|
6
|
+
batchId: string;
|
|
7
|
+
/** The country a number without `+` is read in. Left out, such a number is kept as text and reported. */
|
|
8
|
+
region?: PhoneCountry;
|
|
9
|
+
}
|
|
10
|
+
export interface VcardIssue {
|
|
11
|
+
/** The card's place in the file, from 0. */
|
|
12
|
+
card: number;
|
|
13
|
+
message: string;
|
|
14
|
+
}
|
|
15
|
+
/** Whether a file is a vCard address book rather than a JSON or JSONL envelope. */
|
|
16
|
+
export declare const isVcard: (text: string) => boolean;
|
|
17
|
+
/**
|
|
18
|
+
* A vCard file (2.1, 3.0 or 4.0) as a records envelope: one person per card, matched later by its numbers and
|
|
19
|
+
* addresses, and one organization per `ORG` name the person is a member of. Nothing is guessed: a national number
|
|
20
|
+
* is read only in `region`, and a card says nothing about a person it does not hold.
|
|
21
|
+
*/
|
|
22
|
+
export declare const envelopeOfVcard: (text: string, { provider, account, batchId, region }: VcardOptions) => {
|
|
23
|
+
envelope: RecordsEnvelope;
|
|
24
|
+
issues: VcardIssue[];
|
|
25
|
+
};
|