@wirecat/cli-core 0.606.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 +186 -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 +3 -0
- package/dist/cli/index.js +3 -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/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 +33 -21
- package/dist/crm/cli/pipeline-view.d.ts +3 -0
- package/dist/crm/cli/pipeline-view.js +36 -0
- 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/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/vcard.d.ts +25 -0
- package/dist/domain/vcard.js +267 -0
- package/dist/entities/deal.d.ts +14 -0
- package/dist/entities/deal.js +3 -0
- package/dist/entities/index.d.ts +12 -0
- package/dist/errors/reasons.js +61 -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 +5 -3
- package/dist/index.js +5 -3
- 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/messages-search.js +3 -2
- package/dist/services/metadata.d.ts +1 -1
- package/dist/services/notes-search.js +1 -0
- 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/store/decisions.js +5 -1
- package/dist/store/index.d.ts +14 -3
- package/dist/store/index.js +3 -0
- package/dist/store/people.js +1 -1
- 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.d.ts +2 -11
- package/dist/store/sqlite/crm.js +11 -166
- package/dist/store/sqlite/deals.d.ts +4 -8
- package/dist/store/sqlite/deals.js +5 -3
- 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/knowledge.d.ts +8 -2
- package/dist/store/sqlite/knowledge.js +65 -71
- 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 +2 -0
- package/dist/store/sqlite/link-kinds.js +2 -0
- package/dist/store/sqlite/manifest.js +2 -0
- package/dist/store/sqlite/memories.d.ts +23 -9
- package/dist/store/sqlite/memories.js +41 -13
- package/dist/store/sqlite/messages.js +2 -0
- package/dist/store/sqlite/migrations.generated.js +103 -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 +11 -2
- package/dist/store/sqlite/notes.js +20 -25
- package/dist/store/sqlite/orders.d.ts +36 -6
- package/dist/store/sqlite/orders.js +126 -46
- package/dist/store/sqlite/payment-facts.js +1 -1
- 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 +26 -1
- package/dist/store/sqlite/project-transfer/snapshot.js +5 -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/reviewed.d.ts +109 -0
- package/dist/store/sqlite/reviewed.js +173 -0
- package/dist/store/sqlite/schema.d.ts +2840 -83
- package/dist/store/sqlite/schema.js +256 -1
- 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 +16 -1
- package/dist/store/store.js +30 -6
- package/package.json +5 -2
- package/schemas/examples/records-business.jsonl +11 -0
- package/schemas/records-envelope.v1.schema.json +1160 -5
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { AMOUNT_ROLES, DECIDED_TABLES, FIELD_REFERENCE_ROLES, hasV1Roles, isIdentityType, isRecordType, RECORD_TYPES, } from "../../domain/record-types.js";
|
|
3
|
+
import { fieldReferences, recordFingerprint, } from "../../domain/records.js";
|
|
4
|
+
import { contractOperationsOver } from "./business-contracts.js";
|
|
5
|
+
import { crmRowsOver } from "./crm.js";
|
|
6
|
+
import { financeOperationsOver } from "./finance.js";
|
|
7
|
+
import { transactionOperationsOver } from "./money-transactions.js";
|
|
8
|
+
import { requiredThing } from "./notes.js";
|
|
9
|
+
import { orderOperationsOver } from "./orders.js";
|
|
10
|
+
import { productOperationsOver } from "./products.js";
|
|
11
|
+
import { addIdentifiers, createEntity, entitiesByIdentifiers, identifiersOf, matchOf, renameMade, } from "./record-matching.js";
|
|
12
|
+
import { codeOf, refusal, stringField } from "./record-projection.js";
|
|
13
|
+
import { RECORD_PROJECTORS } from "./record-projectors.js";
|
|
14
|
+
import { importOnly } from "./record-projectors-phone.js";
|
|
15
|
+
import { recordRelationsOver } from "./record-relations.js";
|
|
16
|
+
import { recordSettlingOver } from "./record-settling.js";
|
|
17
|
+
import { importStampsOver } from "./record-stamps.js";
|
|
18
|
+
import { subscriptionOperationsOver } from "./subscriptions.js";
|
|
19
|
+
import { write } from "./transaction.js";
|
|
20
|
+
import { walletOperationsOver } from "./wallets.js";
|
|
21
|
+
const specOf = (type) => RECORD_TYPES[type];
|
|
22
|
+
/** A record's fingerprint without its settles relations: what an import may not change on a payment it proposed. */
|
|
23
|
+
const unsettled = (fields, relations) => recordFingerprint(fields, relations.filter((relation) => !AMOUNT_ROLES.includes(relation.role)));
|
|
24
|
+
const tableOf = (type) => typeof type === "string" && Object.hasOwn(DECIDED_TABLES, type)
|
|
25
|
+
? DECIDED_TABLES[type]
|
|
26
|
+
: undefined;
|
|
27
|
+
/** One record into the store and into its entity, as its type's registry entry says. */
|
|
28
|
+
export const recordWritesOver = (context) => {
|
|
29
|
+
const { database, now } = context;
|
|
30
|
+
const sql = (text) => database.prepare(text);
|
|
31
|
+
const finance = financeOperationsOver(context);
|
|
32
|
+
const relations = recordRelationsOver(context);
|
|
33
|
+
const { entityRef, relatedOf, storedRelations, relate } = relations;
|
|
34
|
+
const { settle, checkShares } = recordSettlingOver(context, finance);
|
|
35
|
+
const stamps = importStampsOver(context);
|
|
36
|
+
let tools;
|
|
37
|
+
const toolsOf = () => {
|
|
38
|
+
tools ??= {
|
|
39
|
+
context,
|
|
40
|
+
finance,
|
|
41
|
+
orders: orderOperationsOver(context),
|
|
42
|
+
contracts: contractOperationsOver(context),
|
|
43
|
+
subscriptions: subscriptionOperationsOver(context),
|
|
44
|
+
transactions: transactionOperationsOver(context),
|
|
45
|
+
products: productOperationsOver(context),
|
|
46
|
+
wallets: walletOperationsOver(context),
|
|
47
|
+
crm: crmRowsOver(context),
|
|
48
|
+
settle,
|
|
49
|
+
};
|
|
50
|
+
return tools;
|
|
51
|
+
};
|
|
52
|
+
const projectionOf = (row, source, related, options) => ({
|
|
53
|
+
externalId: String(row.external_id),
|
|
54
|
+
accountId: Number(row.account_id),
|
|
55
|
+
source,
|
|
56
|
+
options,
|
|
57
|
+
fields: JSON.parse(String(row.fields)),
|
|
58
|
+
related: related.related,
|
|
59
|
+
as: related.as,
|
|
60
|
+
shares: related.shares,
|
|
61
|
+
products: related.products,
|
|
62
|
+
});
|
|
63
|
+
const isProposed = (type, id) => {
|
|
64
|
+
const table = tableOf(type);
|
|
65
|
+
return table !== undefined && sql(`SELECT review FROM ${table} WHERE id = ?`).get(id)?.review === "proposed";
|
|
66
|
+
};
|
|
67
|
+
const setEntity = (recordId, ref, created) => {
|
|
68
|
+
const entity = requiredThing(database, ref);
|
|
69
|
+
sql("UPDATE records SET entity_type = ?, entity_id = ?, created_entity = ?, projected_fingerprint = fingerprint " +
|
|
70
|
+
"WHERE id = ?").run(entity.type, entity.id, created ? 1 : 0, recordId);
|
|
71
|
+
return entity;
|
|
72
|
+
};
|
|
73
|
+
const identify = (row, type, source, options) => {
|
|
74
|
+
const fields = JSON.parse(String(row.fields));
|
|
75
|
+
const ids = identifiersOf(type, fields, source, String(row.external_id), options);
|
|
76
|
+
const match = matchOf(context, type, ids);
|
|
77
|
+
const ignored = ids.ignored.length > 0 ? { ignored: ids.ignored } : {};
|
|
78
|
+
if (match.outcome === "ambiguous")
|
|
79
|
+
return {
|
|
80
|
+
...ignored,
|
|
81
|
+
missing: [],
|
|
82
|
+
unmatched: { reason: match.reason, candidates: match.ids.map((id) => entityRef(type, id)) },
|
|
83
|
+
};
|
|
84
|
+
if (match.outcome === "one") {
|
|
85
|
+
const ref = entityRef(type, match.id);
|
|
86
|
+
setEntity(Number(row.id), ref, false);
|
|
87
|
+
return { ...ignored, made: ref, missing: [] };
|
|
88
|
+
}
|
|
89
|
+
const related = relatedOf(Number(row.id), type);
|
|
90
|
+
if (related.missing.length > 0)
|
|
91
|
+
return { ...ignored, missing: related.missing };
|
|
92
|
+
const organization = related.related.get("organization")?.[0];
|
|
93
|
+
const id = createEntity(context, type, fields, ids, { organization: organization === undefined ? null : requiredThing(database, organization).id }, source, String(row.external_id));
|
|
94
|
+
const ref = entityRef(type, id);
|
|
95
|
+
setEntity(Number(row.id), ref, true);
|
|
96
|
+
return { ...ignored, made: ref, missing: [] };
|
|
97
|
+
};
|
|
98
|
+
/** Gives a record its entity once every reference it names has one: a proposal, a direct row, or a match. */
|
|
99
|
+
const project = (row, source, options = {}) => {
|
|
100
|
+
const type = row.type;
|
|
101
|
+
if (isIdentityType(type))
|
|
102
|
+
return identify(row, type, source, options);
|
|
103
|
+
const projector = RECORD_PROJECTORS[type];
|
|
104
|
+
if (projector === null)
|
|
105
|
+
return { missing: [] };
|
|
106
|
+
const related = relatedOf(Number(row.id), type);
|
|
107
|
+
if (related.missing.length > 0)
|
|
108
|
+
return { missing: related.missing };
|
|
109
|
+
const projection = projectionOf(row, source, related, options);
|
|
110
|
+
if (hasV1Roles(type))
|
|
111
|
+
for (const name of FIELD_REFERENCE_ROLES)
|
|
112
|
+
stringField(projection.fields, name);
|
|
113
|
+
const ref = projector.create(projection, toolsOf());
|
|
114
|
+
const entity = setEntity(Number(row.id), ref, false);
|
|
115
|
+
if (specOf(type).update === "re-propose")
|
|
116
|
+
stamps.stamp(type, entity.id);
|
|
117
|
+
return { made: ref, missing: [], ...(isProposed(type, entity.id) ? { proposed: ref } : {}) };
|
|
118
|
+
};
|
|
119
|
+
/** Records that waited on `target` and may now have their entity, and theirs in turn. */
|
|
120
|
+
const completeDependents = (target, source, proposed, laterError) => {
|
|
121
|
+
const queue = [target];
|
|
122
|
+
while (queue.length > 0) {
|
|
123
|
+
const rows = sql("SELECT DISTINCT r.* FROM record_relations rr JOIN records r ON r.id = rr.record_id WHERE rr.target_id = ? " +
|
|
124
|
+
"AND r.entity_id IS NULL AND r.deleted_at IS NULL AND r.type NOT LIKE 'custom:%'").all(queue.shift());
|
|
125
|
+
for (const row of rows) {
|
|
126
|
+
try {
|
|
127
|
+
const done = write(database, () => project(row, source));
|
|
128
|
+
if (done.proposed)
|
|
129
|
+
proposed(done.proposed);
|
|
130
|
+
if (done.made)
|
|
131
|
+
queue.push(Number(row.id));
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
if (!(error instanceof CliError))
|
|
135
|
+
throw error;
|
|
136
|
+
laterError({
|
|
137
|
+
pointer: "",
|
|
138
|
+
code: codeOf(error),
|
|
139
|
+
message: `${String(row.type)} ${String(row.external_id)} waits: ${error.message}`,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
/** Whether a deleted record's entity still counts: a rejected proposal does not. */
|
|
146
|
+
const liveEntity = (stored) => {
|
|
147
|
+
if (stored.entity_id == null)
|
|
148
|
+
return false;
|
|
149
|
+
const table = tableOf(stored.entity_type);
|
|
150
|
+
return (table === undefined ||
|
|
151
|
+
sql(`SELECT review FROM ${table} WHERE id = ?`).get(Number(stored.entity_id))?.review !== "rejected");
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Takes a changed record into the entity it already has, as its type's registry entry says: `undefined` when it
|
|
155
|
+
* did, else why the entity was left alone.
|
|
156
|
+
*/
|
|
157
|
+
const updateEntity = (stored, id, source, options, proposed, ignore) => {
|
|
158
|
+
const type = stored.type;
|
|
159
|
+
const entityId = Number(stored.entity_id);
|
|
160
|
+
const ref = entityRef(stored.entity_type, stored.entity_id);
|
|
161
|
+
const row = sql("SELECT * FROM records WHERE id = ?").get(id);
|
|
162
|
+
const fields = JSON.parse(String(row.fields));
|
|
163
|
+
const kind = specOf(type).update;
|
|
164
|
+
if (isIdentityType(type)) {
|
|
165
|
+
if (Number(stored.created_entity) !== 1)
|
|
166
|
+
return {};
|
|
167
|
+
const ids = identifiersOf(type, fields, source, String(row.external_id), options);
|
|
168
|
+
if (ids.ignored.length > 0)
|
|
169
|
+
ignore(ids.ignored);
|
|
170
|
+
const others = entitiesByIdentifiers(database, type, ids).filter((one) => one !== entityId);
|
|
171
|
+
if (others.length > 0)
|
|
172
|
+
return { reason: "record.ambiguous", candidates: others.map((one) => entityRef(type, one)) };
|
|
173
|
+
renameMade(context, type, entityId, fields.name, JSON.parse(String(stored.fields)).name);
|
|
174
|
+
addIdentifiers(context, type, entityId, ids, typeof fields.name === "string" ? fields.name : null, source);
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
const projector = RECORD_PROJECTORS[type];
|
|
178
|
+
if (!projector?.update || kind === "flag")
|
|
179
|
+
return {};
|
|
180
|
+
if (kind === "re-propose" && !stamps.untouched(type, entityId))
|
|
181
|
+
return {};
|
|
182
|
+
const related = relatedOf(id, type);
|
|
183
|
+
if (related.missing.length > 0)
|
|
184
|
+
return {};
|
|
185
|
+
if (!projector.update(ref, projectionOf(row, source, related, options), toolsOf()))
|
|
186
|
+
return {};
|
|
187
|
+
if (kind === "re-propose")
|
|
188
|
+
stamps.stamp(type, entityId);
|
|
189
|
+
if (isProposed(type, entityId))
|
|
190
|
+
proposed(ref);
|
|
191
|
+
return undefined;
|
|
192
|
+
};
|
|
193
|
+
const restorer = (type) => isRecordType(String(type)) ? RECORD_PROJECTORS[String(type)]?.restore : undefined;
|
|
194
|
+
const writeOne = (accountId, source, record, options, proposed) => {
|
|
195
|
+
if (isRecordType(record.type) && RECORD_TYPES[record.type].importOnly)
|
|
196
|
+
importOnly(source, record.type);
|
|
197
|
+
const stored = sql("SELECT * FROM records WHERE account_id = ? AND type = ? AND external_id = ?").get(accountId, record.type, record.external_id);
|
|
198
|
+
const sent = record.relations ?? [];
|
|
199
|
+
const fingerprint = record.operation === "upsert" ? recordFingerprint(record.fields ?? {}, sent) : null;
|
|
200
|
+
const stale = (why) => refusal(why, "record.stale-revision", "revision");
|
|
201
|
+
if (stored && stored.revision != null) {
|
|
202
|
+
const held = Number(stored.revision);
|
|
203
|
+
if (record.revision === undefined)
|
|
204
|
+
stale(`the store holds revision ${held}: send the record's revision`);
|
|
205
|
+
else if (record.revision < held)
|
|
206
|
+
stale(`revision ${record.revision} is older than the stored ${held}`);
|
|
207
|
+
else if (record.revision === held && fingerprint !== null && fingerprint !== stored.fingerprint)
|
|
208
|
+
stale(`revision ${held} is stored with other content: a change needs a higher revision`);
|
|
209
|
+
}
|
|
210
|
+
const time = now();
|
|
211
|
+
const revision = record.revision ?? (stored?.revision == null ? null : Number(stored.revision));
|
|
212
|
+
if (record.operation === "delete") {
|
|
213
|
+
if (!stored)
|
|
214
|
+
return refusal(`no ${record.type} ${record.external_id} to delete`, "record.not-found", "external_id");
|
|
215
|
+
if (stored.deleted_at != null)
|
|
216
|
+
return { status: "unchanged", id: Number(stored.id) };
|
|
217
|
+
sql("UPDATE records SET deleted_at = ?, updated_at = ?, revision = ?, batch_id = ? WHERE id = ?").run(time, time, revision, source.batch_id, Number(stored.id));
|
|
218
|
+
const remove = isRecordType(String(stored.type))
|
|
219
|
+
? RECORD_PROJECTORS[String(stored.type)]?.remove
|
|
220
|
+
: undefined;
|
|
221
|
+
if (remove && stored.entity_id != null) {
|
|
222
|
+
remove(entityRef(stored.entity_type, stored.entity_id), toolsOf());
|
|
223
|
+
return { status: "deleted", id: Number(stored.id) };
|
|
224
|
+
}
|
|
225
|
+
return { status: "deleted", id: Number(stored.id), ...(liveEntity(stored) ? { flagged: {} } : {}) };
|
|
226
|
+
}
|
|
227
|
+
const fields = JSON.stringify(record.fields);
|
|
228
|
+
const typed = isRecordType(record.type);
|
|
229
|
+
if (stored && stored.fingerprint === fingerprint && stored.deleted_at == null) {
|
|
230
|
+
if (stored.entity_id == null && isIdentityType(record.type)) {
|
|
231
|
+
const done = project(stored, source, options);
|
|
232
|
+
return {
|
|
233
|
+
status: "unchanged",
|
|
234
|
+
id: Number(stored.id),
|
|
235
|
+
complete: done.made !== undefined,
|
|
236
|
+
unmatched: done.unmatched,
|
|
237
|
+
ignored: done.ignored,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
return { status: "unchanged", id: Number(stored.id) };
|
|
241
|
+
}
|
|
242
|
+
checkShares(record);
|
|
243
|
+
// Whether the payment the record stands for differs in more than whom it settles, against the stored version.
|
|
244
|
+
const beyondSettles = stored?.entity_id != null &&
|
|
245
|
+
(stored.projected_fingerprint !== stored.fingerprint ||
|
|
246
|
+
unsettled(JSON.parse(String(stored.fields)), storedRelations(Number(stored.id))) !==
|
|
247
|
+
unsettled(record.fields ?? {}, sent));
|
|
248
|
+
const id = stored
|
|
249
|
+
? Number(stored.id)
|
|
250
|
+
: Number(sql("INSERT INTO records (account_id, type, external_id, fields, revision, fingerprint, batch_id, created_at, " +
|
|
251
|
+
"updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id").get(accountId, record.type, record.external_id, fields, revision, fingerprint, source.batch_id, time, time)
|
|
252
|
+
?.id);
|
|
253
|
+
if (stored)
|
|
254
|
+
sql("UPDATE records SET fields = ?, revision = ?, fingerprint = ?, batch_id = ?, updated_at = ?, deleted_at = NULL " +
|
|
255
|
+
"WHERE id = ?").run(fields, revision, fingerprint, source.batch_id, time, id);
|
|
256
|
+
else
|
|
257
|
+
sql("UPDATE record_relations SET target_id = ? WHERE target_id IS NULL AND target_type = ? AND target_external_id = ? " +
|
|
258
|
+
"AND record_id IN (SELECT id FROM records WHERE account_id = ?)").run(id, record.type, record.external_id, accountId);
|
|
259
|
+
relate(id, accountId, [...sent, ...fieldReferences(record, options)], time);
|
|
260
|
+
let made = false;
|
|
261
|
+
let unmatched;
|
|
262
|
+
let ignored;
|
|
263
|
+
let flagged = stored?.entity_id != null && stored.projected_fingerprint !== fingerprint ? {} : undefined;
|
|
264
|
+
if (typed && stored?.entity_id == null) {
|
|
265
|
+
const done = project(sql("SELECT * FROM records WHERE id = ?").get(id), source, options);
|
|
266
|
+
if (done.proposed)
|
|
267
|
+
proposed(done.proposed);
|
|
268
|
+
made = done.made !== undefined;
|
|
269
|
+
unmatched = done.unmatched;
|
|
270
|
+
ignored = done.ignored;
|
|
271
|
+
}
|
|
272
|
+
else if (typed && stored?.entity_id != null && stored.deleted_at != null && restorer(stored.type)) {
|
|
273
|
+
// A source copy's record that comes back is live again: the delete tombstoned it, so this brings it back.
|
|
274
|
+
const related = relatedOf(id, record.type);
|
|
275
|
+
if (related.missing.length === 0) {
|
|
276
|
+
restorer(stored.type)?.(entityRef(stored.entity_type, stored.entity_id), projectionOf(sql("SELECT * FROM records WHERE id = ?").get(id), source, related, options), toolsOf());
|
|
277
|
+
sql("UPDATE records SET projected_fingerprint = fingerprint WHERE id = ?").run(id);
|
|
278
|
+
flagged = undefined;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
else if (typed && stored?.entity_id != null && stored.deleted_at == null) {
|
|
282
|
+
const resettles = specOf(record.type).update === "re-settle";
|
|
283
|
+
// A payment takes its settles again even when only they changed, so its projected fingerprint lags behind.
|
|
284
|
+
if (flagged !== undefined || resettles) {
|
|
285
|
+
const left = updateEntity(stored, id, source, options, proposed, (list) => {
|
|
286
|
+
ignored = list;
|
|
287
|
+
});
|
|
288
|
+
if (left === undefined && !(resettles && beyondSettles)) {
|
|
289
|
+
sql("UPDATE records SET projected_fingerprint = fingerprint WHERE id = ?").run(id);
|
|
290
|
+
flagged = undefined;
|
|
291
|
+
}
|
|
292
|
+
else if (left !== undefined)
|
|
293
|
+
flagged = left;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return {
|
|
297
|
+
status: stored ? "updated" : "created",
|
|
298
|
+
id,
|
|
299
|
+
...(flagged === undefined ? {} : { flagged }),
|
|
300
|
+
unmatched,
|
|
301
|
+
ignored,
|
|
302
|
+
// Records waiting on this one move only when it has an entity: made now, or back from a delete.
|
|
303
|
+
complete: made || (stored?.deleted_at != null && stored.entity_id != null),
|
|
304
|
+
};
|
|
305
|
+
};
|
|
306
|
+
return { writeOne, project, completeDependents, entityRef, missingOf: relations.missingOf };
|
|
307
|
+
};
|
|
@@ -1,25 +1,34 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { EnvelopeSource, IngestError } from "../../domain/records.js";
|
|
2
2
|
import type { Author } from "./memories.js";
|
|
3
3
|
import type { StoreContext } from "./open.js";
|
|
4
|
+
import { type RecordsRun, type RunItem, type RunOptions } from "./record-run.js";
|
|
5
|
+
export type { FlaggedRecord, RecordsRun, RunItem, RunOptions, Unmatched } from "./record-run.js";
|
|
6
|
+
export { waitingPointer } from "./record-run.js";
|
|
4
7
|
export interface RecordKey {
|
|
5
8
|
provider: string;
|
|
6
9
|
account: string;
|
|
7
10
|
type: string;
|
|
8
11
|
externalId: string;
|
|
9
12
|
}
|
|
13
|
+
export interface SourceRecordRelation {
|
|
14
|
+
role: string;
|
|
15
|
+
type: string;
|
|
16
|
+
/** `null` when the relation names a store entity by `ref`. */
|
|
17
|
+
externalId: string | null;
|
|
18
|
+
ref: string | null;
|
|
19
|
+
as: string | null;
|
|
20
|
+
amount: string | null;
|
|
21
|
+
resolved: boolean;
|
|
22
|
+
}
|
|
10
23
|
export interface SourceRecord extends RecordKey {
|
|
11
24
|
fields: Record<string, unknown>;
|
|
12
25
|
revision: number | null;
|
|
13
26
|
fingerprint: string;
|
|
14
|
-
relations:
|
|
15
|
-
|
|
16
|
-
type: string;
|
|
17
|
-
externalId: string;
|
|
18
|
-
amount: string | null;
|
|
19
|
-
resolved: boolean;
|
|
20
|
-
}[];
|
|
21
|
-
/** The entity made from it or mapped to it by the owner; `null` while it waits or for a type without a table. */
|
|
27
|
+
relations: SourceRecordRelation[];
|
|
28
|
+
/** The entity made from it, matched to it or mapped to it by the owner; `null` while it waits or for `custom:`. */
|
|
22
29
|
entity: string | null;
|
|
30
|
+
/** The import made its person, organization or project: no entity had its identifiers. */
|
|
31
|
+
createdEntity: boolean;
|
|
23
32
|
/** The source changed or deleted it after its entity was made: the owner should look at the entity. */
|
|
24
33
|
flagged: boolean;
|
|
25
34
|
batchId: string;
|
|
@@ -27,41 +36,34 @@ export interface SourceRecord extends RecordKey {
|
|
|
27
36
|
updatedAt: string;
|
|
28
37
|
deletedAt: string | null;
|
|
29
38
|
}
|
|
30
|
-
export interface
|
|
31
|
-
/** The
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
status: "created" | "updated" | "unchanged" | "deleted" | "refused";
|
|
35
|
-
error?: IngestError;
|
|
36
|
-
}
|
|
37
|
-
export interface RecordsWritten {
|
|
38
|
-
outcomes: RecordOutcome[];
|
|
39
|
-
/** `invoice:` and `payment:` refs this import proposed, also for records of earlier imports it completed. */
|
|
39
|
+
export interface MappedRecord extends SourceRecord {
|
|
40
|
+
/** The entity the import had made for it, when nothing points at it any more: the owner's to delete or merge. */
|
|
41
|
+
unused: string | null;
|
|
42
|
+
/** Proposals made for the records that waited on it. */
|
|
40
43
|
proposed: string[];
|
|
41
|
-
/**
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
entity: string;
|
|
44
|
+
/** Records that waited on it whose entity was still refused. */
|
|
45
|
+
errors: IngestError[];
|
|
46
|
+
}
|
|
47
|
+
export interface MatchRun {
|
|
48
|
+
linked: number;
|
|
49
|
+
created: number;
|
|
50
|
+
unmatched: {
|
|
51
|
+
key: RecordKey;
|
|
52
|
+
reason: string;
|
|
53
|
+
candidates: string[];
|
|
52
54
|
}[];
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
proposed: string[];
|
|
56
|
+
errors: IngestError[];
|
|
57
|
+
/** A list stopped at 1000 entries; the counts are exact. */
|
|
58
|
+
truncated: boolean;
|
|
55
59
|
}
|
|
56
60
|
export interface RecordStore {
|
|
57
|
-
/**
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
chunk?: number;
|
|
64
|
-
}): Promise<RecordsWritten>;
|
|
61
|
+
/**
|
|
62
|
+
* Writes the records of one source, per chunk in one transaction, checking duplicates and collecting the receipt's
|
|
63
|
+
* lists in the store as it goes. A dry run is one transaction, rolled back at the end, and refuses every other
|
|
64
|
+
* read and write of the store handle while it lasts.
|
|
65
|
+
*/
|
|
66
|
+
run(source: EnvelopeSource, items: AsyncIterable<RunItem> | Iterable<RunItem>, options?: RunOptions): Promise<RecordsRun>;
|
|
65
67
|
get(key: RecordKey): Promise<SourceRecord | undefined>;
|
|
66
68
|
list(filter: {
|
|
67
69
|
provider: string;
|
|
@@ -69,14 +71,22 @@ export interface RecordStore {
|
|
|
69
71
|
type?: string;
|
|
70
72
|
/** Only records whose entity waits for a reference. */
|
|
71
73
|
waiting?: boolean;
|
|
74
|
+
/** Only persons, organizations and projects whose identifiers point at more than one entity. */
|
|
75
|
+
unmatched?: boolean;
|
|
72
76
|
flagged?: boolean;
|
|
73
77
|
/** 1–500, 100 by default. */
|
|
74
78
|
limit?: number;
|
|
75
79
|
}): Promise<SourceRecord[]>;
|
|
76
80
|
/**
|
|
77
81
|
* The owner says which entity a record is: `person:4` for a payer a sheet names. Never done by name. Records that
|
|
78
|
-
* waited for it get their entity, as proposals.
|
|
82
|
+
* waited for it get their entity, as proposals. A record whose entity the import made moves, with every record of
|
|
83
|
+
* the account linked to that entity and whatever their records filled from them.
|
|
79
84
|
*/
|
|
80
|
-
map(key: RecordKey, entity: string, by: Author): Promise<
|
|
85
|
+
map(key: RecordKey, entity: string, by: Author): Promise<MappedRecord>;
|
|
86
|
+
/** Matches or makes the entity of every waiting person, organization and project of one account, as an import does. */
|
|
87
|
+
match(account: {
|
|
88
|
+
provider: string;
|
|
89
|
+
account: string;
|
|
90
|
+
}): Promise<MatchRun>;
|
|
81
91
|
}
|
|
82
92
|
export declare const recordStoreOver: (context: StoreContext) => RecordStore;
|