@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
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { ORDER_DIRECTIONS } from "../../domain/business.js";
|
|
2
3
|
import { sourceUrlOf } from "../../domain/external-links.js";
|
|
3
4
|
import { FINANCE_REVIEWS } from "../../domain/finance.js";
|
|
4
|
-
import { currencyOf, formatDecimal, minorUnits, scaleOf } from "../../domain/money.js";
|
|
5
|
+
import { currencyOf, formatDecimal, minorUnits, moneyLabel, scaleOf } from "../../domain/money.js";
|
|
5
6
|
import { ORDER_PURPOSES, ORDER_STATUSES, orderTitle } from "../../domain/orders.js";
|
|
6
|
-
import { assertMayDecide, deciderAuthor } from "../decisions.js";
|
|
7
7
|
import { fold } from "../normalize.js";
|
|
8
|
-
import { ownerPerson, proposedBySql
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
8
|
+
import { ownerPerson, proposedBySql } from "./actors.js";
|
|
9
|
+
import { projectId } from "./memories.js";
|
|
10
|
+
import { moneyLinesOver } from "./money-lines.js";
|
|
11
11
|
import { requiredThing } from "./notes.js";
|
|
12
|
+
import { isoDate, limitOf, oneOf, refuse, reviewedRowsOver, text, } from "./reviewed.js";
|
|
12
13
|
import { write } from "./transaction.js";
|
|
14
|
+
const MAX_LINES = 500;
|
|
13
15
|
const ORDERS = "SELECT o.*, b.name AS author_name, b.kind AS author_kind FROM orders o " +
|
|
14
16
|
"LEFT JOIN bots b ON o.author_type = 'bot' AND b.id = o.author_id";
|
|
15
17
|
export const orderOperationsOver = (context) => {
|
|
16
18
|
const { database, now } = context;
|
|
17
|
-
const { partyOf, partyThing, reviewedOf, written, keepEvidence, ownerOnly,
|
|
19
|
+
const { partyOf, partyThing, reviewedOf, written, keepEvidence, ownerOnly, linkedId, decideRow, mayChangeParts } = reviewedRowsOver(context);
|
|
20
|
+
const moneyLines = moneyLinesOver(context);
|
|
18
21
|
const orderOf = (row) => {
|
|
19
22
|
const currency = row.currency == null ? null : String(row.currency);
|
|
20
23
|
const total = row.total == null || currency === null ? null : Number(row.total);
|
|
@@ -44,82 +47,123 @@ export const orderOperationsOver = (context) => {
|
|
|
44
47
|
invoice: row.invoice_id == null ? null : `invoice:${row.invoice_id}`,
|
|
45
48
|
payment: row.payment_id == null ? null : `payment:${row.payment_id}`,
|
|
46
49
|
sourceUrl: row.source_url == null ? null : String(row.source_url),
|
|
50
|
+
direction: row.direction,
|
|
51
|
+
contract: row.contract_id == null ? null : `contract:${row.contract_id}`,
|
|
52
|
+
deal: row.deal_id == null ? null : `deal:${row.deal_id}`,
|
|
47
53
|
...reviewedOf(row, "order"),
|
|
48
54
|
};
|
|
49
55
|
};
|
|
56
|
+
const detailOf = (row) => ({
|
|
57
|
+
...orderOf(row),
|
|
58
|
+
lines: row.currency == null ? [] : moneyLines.shownLines("order", Number(row.id), String(row.currency)),
|
|
59
|
+
});
|
|
60
|
+
/** The order's total follows its lines in the same write: the trigger checks the sum when the order changes. */
|
|
61
|
+
const retotal = (orderId) => {
|
|
62
|
+
database
|
|
63
|
+
.prepare("UPDATE orders SET total = coalesce((SELECT sum(amount) FROM order_lines WHERE order_id = ?), total), " +
|
|
64
|
+
"updated_at = ? WHERE id = ?")
|
|
65
|
+
.run(orderId, now(), orderId);
|
|
66
|
+
};
|
|
67
|
+
const mayChangeLines = (row, by) => {
|
|
68
|
+
mayChangeParts(row, by, `only the owner changes the lines of order:${row.id}; a bot changes only an order it proposed that is still proposed`, "order.owner-only");
|
|
69
|
+
if (row.review === "rejected" || row.deleted_at != null)
|
|
70
|
+
refuse(`order:${row.id} is ${row.deleted_at != null ? "deleted" : "rejected"}`, "order.status");
|
|
71
|
+
};
|
|
50
72
|
const rowOf = (reference) => {
|
|
51
73
|
const thing = requiredThing(database, reference.includes(":") ? reference : `order:${reference}`, "the order");
|
|
52
74
|
if (thing.type !== "order")
|
|
53
75
|
return refuse(`${reference} is not an order`, "order.ref");
|
|
54
76
|
return database.prepare(`${ORDERS} WHERE o.id = ?`).get(thing.id);
|
|
55
77
|
};
|
|
56
|
-
const linked = (reference, type) =>
|
|
57
|
-
if (reference === undefined)
|
|
58
|
-
return null;
|
|
59
|
-
const thing = requiredThing(database, reference, `the ${type}`);
|
|
60
|
-
if (thing.type !== type)
|
|
61
|
-
return refuse(`${reference} is not an ${type}: reference`, "order.ref");
|
|
62
|
-
return thing.id;
|
|
63
|
-
};
|
|
78
|
+
const linked = (reference, type) => linkedId(reference, type, "order.ref");
|
|
64
79
|
const statusOf = (value) => oneOf(value, ORDER_STATUSES, "an order's status");
|
|
65
80
|
const decide = (reference, by, reason, confirm) => {
|
|
66
81
|
const row = rowOf(reference);
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (row.review !== "proposed")
|
|
70
|
-
refuse(`${ref} is already ${String(row.review)}`, "proposal.wrong-state");
|
|
71
|
-
write(database, () => {
|
|
72
|
-
if (confirm) {
|
|
73
|
-
const { actor } = authorActor(context, deciderAuthor(by));
|
|
74
|
-
database
|
|
75
|
-
.prepare("UPDATE orders SET review = 'confirmed', confirmed_by_type = ?, confirmed_by_id = ?, updated_at = ? WHERE id = ?")
|
|
76
|
-
.run(actor.type, actor.id, now(), Number(row.id));
|
|
77
|
-
}
|
|
78
|
-
else
|
|
79
|
-
database
|
|
80
|
-
.prepare("UPDATE orders SET review = 'rejected', updated_at = ? WHERE id = ?")
|
|
81
|
-
.run(now(), Number(row.id));
|
|
82
|
-
keepDecision("orders", Number(row.id), by, reason);
|
|
83
|
-
});
|
|
84
|
-
return orderOf(rowOf(ref));
|
|
82
|
+
decideRow("order", row, { by, reason, confirm });
|
|
83
|
+
return orderOf(rowOf(`order:${row.id}`));
|
|
85
84
|
};
|
|
86
|
-
|
|
87
|
-
|
|
85
|
+
/** Checks an add's input and writes it: a new order, or `held`'s columns and lines replaced. */
|
|
86
|
+
const save = (input, held) => {
|
|
87
|
+
{
|
|
88
88
|
const purpose = oneOf(input.purpose, ORDER_PURPOSES, "an order's purpose");
|
|
89
|
+
const direction = input.direction === undefined ? "purchase" : oneOf(input.direction, ORDER_DIRECTIONS, "a direction");
|
|
89
90
|
const status = input.status === undefined ? "placed" : statusOf(input.status);
|
|
90
|
-
const
|
|
91
|
+
const named = text(input.supplierName, "an order's supplier name", 200);
|
|
92
|
+
if (named === null && direction === "purchase")
|
|
91
93
|
refuse("an order names its shop or supplier", "order.field");
|
|
94
|
+
if (direction === "sale" && input.customer === undefined)
|
|
95
|
+
refuse("a sale names its customer, a person: or organization:", "order.field");
|
|
92
96
|
const number = text(input.number, "an order's number", 80);
|
|
93
|
-
|
|
97
|
+
const lines = input.lines ?? [];
|
|
98
|
+
if (!Array.isArray(lines) || lines.length > MAX_LINES)
|
|
99
|
+
refuse(`an order has at most ${MAX_LINES} lines`, "order.field");
|
|
100
|
+
if (lines.length && input.currency === undefined)
|
|
101
|
+
refuse("an order with lines names its currency", "order.total");
|
|
102
|
+
if (!lines.length && (input.total === undefined) !== (input.currency === undefined))
|
|
94
103
|
refuse("an order's amount and currency come together", "order.field");
|
|
95
104
|
const currency = input.currency === undefined ? null : currencyOf(input.currency);
|
|
96
|
-
const
|
|
105
|
+
const priced = currency === null ? [] : moneyLines.linesOf("order", lines, currency);
|
|
106
|
+
const sum = priced.reduce((all, line) => all + line.amount, 0);
|
|
107
|
+
const given = currency === null || input.total === undefined ? null : minorUnits(input.total, currency, "an order's amount");
|
|
108
|
+
if (priced.length && given !== null && given !== sum)
|
|
109
|
+
refuse(`the order's total ${moneyLabel(given, currency)} is not the sum of its lines, ` +
|
|
110
|
+
`${moneyLabel(sum, currency)}`, "order.total");
|
|
111
|
+
const total = priced.length ? sum : given;
|
|
97
112
|
if (total !== null && total < 0)
|
|
98
113
|
refuse("an order's amount is 0 or more", "money.amount");
|
|
99
114
|
const orderDate = isoDate(input.orderDate, "an order's date");
|
|
100
115
|
const fulfilledDate = isoDate(input.fulfilledDate, "an order's delivery date");
|
|
101
116
|
const sourceUrl = input.sourceUrl === undefined ? null : sourceUrlOf(input.sourceUrl, "order.source-url");
|
|
102
117
|
const id = write(database, () => {
|
|
103
|
-
if (number !== null && ordersOfShop(database, supplierName, number).length)
|
|
104
|
-
refuse(`order ${number} from ${supplierName} is already stored; change its status instead`, "order.duplicate");
|
|
105
118
|
const customer = partyThing(input.customer, "the customer") ?? ownerPerson(database);
|
|
106
|
-
const supplier = partyThing(input.supplier, "the supplier");
|
|
119
|
+
const supplier = partyThing(input.supplier, "the supplier") ?? (direction === "sale" ? ownerPerson(database) : undefined);
|
|
120
|
+
const party = partyOf(supplier?.type, supplier?.id);
|
|
121
|
+
const supplierName = named ?? party?.name ?? (party?.owner ? "owner" : null);
|
|
122
|
+
if (supplierName === null)
|
|
123
|
+
return refuse("an order names its shop or supplier", "order.field");
|
|
124
|
+
if (number !== null &&
|
|
125
|
+
ordersOfShop(database, supplierName, number).some((one) => held === undefined || one.id !== held.id))
|
|
126
|
+
refuse(`order ${number} from ${supplierName} is already stored; change its status instead`, "order.duplicate");
|
|
107
127
|
const invoice = linked(input.invoice, "invoice");
|
|
108
128
|
const payment = linked(input.payment, "payment");
|
|
109
|
-
const
|
|
129
|
+
const contract = linked(input.contract, "contract");
|
|
130
|
+
const deal = linked(input.deal, "deal");
|
|
110
131
|
const at = now();
|
|
132
|
+
if (held !== undefined) {
|
|
133
|
+
const id = Number(held.id);
|
|
134
|
+
database.prepare("DELETE FROM order_lines WHERE order_id = ?").run(id);
|
|
135
|
+
database
|
|
136
|
+
.prepare("UPDATE orders SET customer_type = ?, customer_id = ?, purpose = ?, supplier_type = ?, supplier_id = ?, " +
|
|
137
|
+
"supplier_name = ?, number = ?, currency = ?, total = ?, status = ?, order_date = ?, " +
|
|
138
|
+
"fulfilled_date = ?, invoice_id = ?, payment_id = ?, project_id = ?, source_url = ?, direction = ?, " +
|
|
139
|
+
"contract_id = ?, deal_id = ?, updated_at = ? WHERE id = ?")
|
|
140
|
+
.run(customer.type, customer.id, purpose, supplier?.type ?? null, supplier?.id ?? null, supplierName, number, currency, total, status, orderDate, fulfilledDate, invoice, payment, input.project === undefined ? null : projectId(database, input.project), sourceUrl, direction, contract, deal, at, id);
|
|
141
|
+
moneyLines.insertLines("order", id, priced, 1, at);
|
|
142
|
+
return id;
|
|
143
|
+
}
|
|
144
|
+
const by = written(input);
|
|
111
145
|
const made = Number(database
|
|
112
146
|
.prepare("INSERT INTO orders (customer_type, customer_id, purpose, supplier_type, supplier_id, supplier_name, number, " +
|
|
113
147
|
"currency, total, status, order_date, fulfilled_date, invoice_id, payment_id, project_id, review, " +
|
|
114
148
|
"author_type, author_id, model, confirmed_by_type, confirmed_by_id, source, metadata, created_at, updated_at, " +
|
|
115
|
-
"source_url
|
|
116
|
-
|
|
149
|
+
"source_url, direction, contract_id, deal_id) " +
|
|
150
|
+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id")
|
|
151
|
+
.get(customer.type, customer.id, purpose, supplier?.type ?? null, supplier?.id ?? null, supplierName, number, currency, total, status, orderDate, fulfilledDate, invoice, payment, by.project, by.review, by.actor.type, by.actor.id, by.model, by.confirmedType, by.confirmedId, by.source, by.metadata, at, at, sourceUrl, direction, contract, deal)?.id);
|
|
152
|
+
moneyLines.insertLines("order", made, priced, 1, at);
|
|
117
153
|
keepEvidence("order", made, by);
|
|
118
154
|
return made;
|
|
119
155
|
});
|
|
120
|
-
return
|
|
156
|
+
return id;
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
return {
|
|
160
|
+
add: (input) => detailOf(rowOf(`order:${save(input)}`)),
|
|
161
|
+
revise: (reference, input) => {
|
|
162
|
+
const row = rowOf(reference);
|
|
163
|
+
mayChangeLines(row, input.by);
|
|
164
|
+
return detailOf(rowOf(`order:${save(input, row)}`));
|
|
121
165
|
},
|
|
122
|
-
get: (reference) =>
|
|
166
|
+
get: (reference) => detailOf(rowOf(reference)),
|
|
123
167
|
list: (filter = {}) => {
|
|
124
168
|
const project = filter.project === undefined ? null : projectId(database, filter.project);
|
|
125
169
|
const customer = partyThing(filter.customer, "the customer");
|
|
@@ -129,7 +173,7 @@ export const orderOperationsOver = (context) => {
|
|
|
129
173
|
const review = filter.review === undefined ? null : oneOf(filter.review, FINANCE_REVIEWS, "a review");
|
|
130
174
|
const number = filter.number === undefined ? null : filter.number.trim();
|
|
131
175
|
const shop = filter.supplierName === undefined ? null : fold(filter.supplierName);
|
|
132
|
-
const limit =
|
|
176
|
+
const limit = limitOf(filter.limit);
|
|
133
177
|
const rows = database
|
|
134
178
|
.prepare(`${ORDERS} WHERE o.deleted_at IS NULL AND (? IS NULL OR o.status = ?) AND (? IS NULL OR o.purpose = ?) ` +
|
|
135
179
|
"AND (? IS NULL OR o.review = ?) AND (? IS NULL OR o.project_id = ?) AND (? IS NULL OR o.number = ?) " +
|
|
@@ -157,6 +201,39 @@ export const orderOperationsOver = (context) => {
|
|
|
157
201
|
},
|
|
158
202
|
confirm: (reference, by, reason) => decide(reference, by, reason, true),
|
|
159
203
|
reject: (reference, by, reason) => decide(reference, by, reason, false),
|
|
204
|
+
addLine: (reference, { by, ...line }) => {
|
|
205
|
+
const row = rowOf(reference);
|
|
206
|
+
mayChangeLines(row, by);
|
|
207
|
+
if (row.currency == null)
|
|
208
|
+
refuse(`order:${row.id} has no currency; a line needs one`, "order.total");
|
|
209
|
+
const [priced] = moneyLines.linesOf("order", [line], String(row.currency));
|
|
210
|
+
write(database, () => {
|
|
211
|
+
const last = Number(database
|
|
212
|
+
.prepare("SELECT coalesce(max(position), 0) AS n FROM order_lines WHERE order_id = ?")
|
|
213
|
+
.get(Number(row.id))?.n);
|
|
214
|
+
if (last >= MAX_LINES)
|
|
215
|
+
refuse(`an order has at most ${MAX_LINES} lines`, "order.field");
|
|
216
|
+
moneyLines.insertLines("order", Number(row.id), [priced], last + 1, now());
|
|
217
|
+
retotal(Number(row.id));
|
|
218
|
+
});
|
|
219
|
+
return detailOf(rowOf(`order:${row.id}`));
|
|
220
|
+
},
|
|
221
|
+
removeLine: (reference, { position, by }) => {
|
|
222
|
+
const row = rowOf(reference);
|
|
223
|
+
mayChangeLines(row, by);
|
|
224
|
+
write(database, () => {
|
|
225
|
+
const lines = Number(database.prepare("SELECT count(*) AS n FROM order_lines WHERE order_id = ?").get(Number(row.id))?.n);
|
|
226
|
+
const gone = database
|
|
227
|
+
.prepare("DELETE FROM order_lines WHERE order_id = ? AND position = ?")
|
|
228
|
+
.run(Number(row.id), position);
|
|
229
|
+
if (gone.changes === 0)
|
|
230
|
+
throw new CliError("not_found", `order:${row.id} has no line ${position}`, { reason: "order.no-line" });
|
|
231
|
+
if (lines === 1)
|
|
232
|
+
refuse(`line ${position} is the last of order:${row.id}; reject or cancel the order instead`, "order.line");
|
|
233
|
+
retotal(Number(row.id));
|
|
234
|
+
});
|
|
235
|
+
return detailOf(rowOf(`order:${row.id}`));
|
|
236
|
+
},
|
|
160
237
|
};
|
|
161
238
|
};
|
|
162
239
|
/** Orders of one shop with one number that still count: neither rejected nor deleted, the name in any letter case. */
|
|
@@ -185,5 +262,8 @@ export const ordersStoreOver = (context) => {
|
|
|
185
262
|
setStatus: async (reference, input) => operations.setStatus(reference, input),
|
|
186
263
|
confirm: async (reference, by, reason) => operations.confirm(reference, by, reason),
|
|
187
264
|
reject: async (reference, by, reason) => operations.reject(reference, by, reason),
|
|
265
|
+
addLine: async (reference, input) => operations.addLine(reference, input),
|
|
266
|
+
removeLine: async (reference, input) => operations.removeLine(reference, input),
|
|
267
|
+
revise: async (reference, input) => operations.revise(reference, input),
|
|
188
268
|
};
|
|
189
269
|
};
|
|
@@ -77,7 +77,7 @@ export const movePaymentFacts = (context) => {
|
|
|
77
77
|
keepHighestId(database, "facts", now());
|
|
78
78
|
for (const id of moved.keys()) {
|
|
79
79
|
database.prepare("UPDATE facts SET supersedes_id = NULL WHERE supersedes_id = ?").run(id);
|
|
80
|
-
dropLinksOf(database, { type: "fact", id });
|
|
80
|
+
dropLinksOf(database, { type: "fact", id }, now());
|
|
81
81
|
database.prepare("DELETE FROM links WHERE from_type = 'fact' AND from_id = ?").run(id);
|
|
82
82
|
database.prepare("DELETE FROM taggings WHERE taggable_type = 'fact' AND taggable_id = ?").run(id);
|
|
83
83
|
database.prepare("DELETE FROM facts WHERE id = ?").run(id);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
2
|
import { fold } from "../normalize.js";
|
|
3
|
+
import { forgetEvidenceToRow } from "./evidence-signature.js";
|
|
3
4
|
import { resolvePersonLinks } from "./person-resolution.js";
|
|
4
5
|
import { toIso } from "./values.js";
|
|
5
6
|
const required = ({ database }, key, personId) => {
|
|
@@ -81,6 +82,7 @@ export const editNote = (context, key, personId, id, text, revision) => {
|
|
|
81
82
|
};
|
|
82
83
|
export const removeNote = (context, key, personId, id) => {
|
|
83
84
|
note(context, key, personId, id);
|
|
85
|
+
forgetEvidenceToRow(context.database, { type: "note", id: Number(id) }, context.now());
|
|
84
86
|
context.database.prepare("DELETE FROM note_revisions WHERE note_id=?").run(id);
|
|
85
87
|
context.database.prepare("DELETE FROM notes WHERE id=?").run(id);
|
|
86
88
|
return { id, personId, removed: true };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { type CatalogKind } from "../../domain/business.js";
|
|
2
|
+
import { type Author } from "./memories.js";
|
|
3
|
+
import type { StoreContext } from "./open.js";
|
|
4
|
+
import { type AuthorInfo, type Party } from "./reviewed.js";
|
|
5
|
+
export interface Product {
|
|
6
|
+
id: string;
|
|
7
|
+
ref: string;
|
|
8
|
+
/** One line, as a list prints it. */
|
|
9
|
+
title: string;
|
|
10
|
+
kind: CatalogKind;
|
|
11
|
+
name: string;
|
|
12
|
+
sku: string | null;
|
|
13
|
+
/** `null` for the owner's own item. */
|
|
14
|
+
supplier: Party | null;
|
|
15
|
+
unit: string | null;
|
|
16
|
+
/** Both `null` when it has no price. */
|
|
17
|
+
unitPrice: string | null;
|
|
18
|
+
currency: string | null;
|
|
19
|
+
description: string | null;
|
|
20
|
+
/** `YYYY-MM-DD`: no longer sold; its lines and subscriptions keep pointing at it. */
|
|
21
|
+
archivedDate: string | null;
|
|
22
|
+
author: AuthorInfo;
|
|
23
|
+
model: string | null;
|
|
24
|
+
source: string;
|
|
25
|
+
createdAt: string;
|
|
26
|
+
updatedAt: string;
|
|
27
|
+
}
|
|
28
|
+
export interface ProductInput {
|
|
29
|
+
/** The owner and an agent write alike: a product feeds no total. */
|
|
30
|
+
by: Author;
|
|
31
|
+
/** `product` when left out. */
|
|
32
|
+
kind?: CatalogKind;
|
|
33
|
+
name: string;
|
|
34
|
+
sku?: string;
|
|
35
|
+
/** `person:` or `organization:`; the owner's own item when left out. */
|
|
36
|
+
supplier?: string;
|
|
37
|
+
unit?: string;
|
|
38
|
+
/** A decimal string in `currency`; give both or neither. */
|
|
39
|
+
unitPrice?: string;
|
|
40
|
+
currency?: string;
|
|
41
|
+
description?: string;
|
|
42
|
+
/** Where an agent's row came from, such as `agent`; the owner's is always `owner`. */
|
|
43
|
+
source?: string;
|
|
44
|
+
}
|
|
45
|
+
export interface ProductUpdate {
|
|
46
|
+
by: Author;
|
|
47
|
+
name?: string;
|
|
48
|
+
unit?: string | null;
|
|
49
|
+
/** Give both, or neither. A new price changes no line: each line holds its own copy; `null` for both drops it. */
|
|
50
|
+
unitPrice?: string | null;
|
|
51
|
+
currency?: string | null;
|
|
52
|
+
description?: string | null;
|
|
53
|
+
/** `YYYY-MM-DD` to stop selling it, `null` to sell it again. */
|
|
54
|
+
archivedDate?: string | null;
|
|
55
|
+
}
|
|
56
|
+
export interface ProductFilter {
|
|
57
|
+
kind?: CatalogKind;
|
|
58
|
+
supplier?: string;
|
|
59
|
+
/** `true` only archived ones, `false` only those still sold; both when left out. */
|
|
60
|
+
archived?: boolean;
|
|
61
|
+
/** Words of the name, in any letter case. */
|
|
62
|
+
name?: string;
|
|
63
|
+
/** 1–500, 100 by default; by name. */
|
|
64
|
+
limit?: number;
|
|
65
|
+
}
|
|
66
|
+
/** What the owner or a supplier sells, written directly by the owner or an agent. */
|
|
67
|
+
export interface ProductsStore {
|
|
68
|
+
add(input: ProductInput): Promise<Product>;
|
|
69
|
+
get(reference: string): Promise<Product>;
|
|
70
|
+
list(filter?: ProductFilter): Promise<Product[]>;
|
|
71
|
+
update(reference: string, input: ProductUpdate): Promise<Product>;
|
|
72
|
+
}
|
|
73
|
+
type ProductOperations = {
|
|
74
|
+
[K in keyof ProductsStore]: ProductsStore[K] extends (...args: infer A) => Promise<infer R> ? (...args: A) => R : never;
|
|
75
|
+
};
|
|
76
|
+
export declare const productOperationsOver: (context: StoreContext) => ProductOperations;
|
|
77
|
+
export declare const productsStoreOver: (context: StoreContext) => ProductsStore;
|
|
78
|
+
export {};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { CATALOG_KINDS, productLine } from "../../domain/business.js";
|
|
2
|
+
import { currencyOf, formatDecimal, minorUnits, scaleOf } from "../../domain/money.js";
|
|
3
|
+
import { fold } from "../normalize.js";
|
|
4
|
+
import { authorActor, authorOf } from "./memories.js";
|
|
5
|
+
import { requiredThing } from "./notes.js";
|
|
6
|
+
import { iso, isoDate, limitOf, oneOf, refuse, reviewedRowsOver, text, } from "./reviewed.js";
|
|
7
|
+
import { write } from "./transaction.js";
|
|
8
|
+
const PRODUCTS = "SELECT c.*, b.name AS author_name, b.kind AS author_kind FROM catalog_items c " +
|
|
9
|
+
"LEFT JOIN bots b ON c.author_type = 'bot' AND b.id = c.author_id";
|
|
10
|
+
export const productOperationsOver = (context) => {
|
|
11
|
+
const { database, now } = context;
|
|
12
|
+
const { partyOf, partyThing } = reviewedRowsOver(context);
|
|
13
|
+
const productOf = (row) => {
|
|
14
|
+
const currency = row.currency == null ? null : String(row.currency);
|
|
15
|
+
const units = row.unit_price == null || currency === null ? null : Number(row.unit_price);
|
|
16
|
+
const fields = {
|
|
17
|
+
name: String(row.name),
|
|
18
|
+
kind: row.kind,
|
|
19
|
+
unitPrice: units,
|
|
20
|
+
currency,
|
|
21
|
+
unit: row.unit == null ? null : String(row.unit),
|
|
22
|
+
archived: row.archived_date != null,
|
|
23
|
+
};
|
|
24
|
+
return {
|
|
25
|
+
id: String(row.id),
|
|
26
|
+
ref: `product:${row.id}`,
|
|
27
|
+
title: productLine(fields),
|
|
28
|
+
kind: fields.kind,
|
|
29
|
+
name: fields.name,
|
|
30
|
+
sku: row.sku == null ? null : String(row.sku),
|
|
31
|
+
supplier: partyOf(row.supplier_type, row.supplier_id),
|
|
32
|
+
unit: fields.unit,
|
|
33
|
+
unitPrice: units === null || currency === null ? null : formatDecimal(units, scaleOf(currency)),
|
|
34
|
+
currency,
|
|
35
|
+
description: row.description == null ? null : String(row.description),
|
|
36
|
+
archivedDate: row.archived_date == null ? null : String(row.archived_date),
|
|
37
|
+
author: authorOf(row),
|
|
38
|
+
model: row.model == null ? null : String(row.model),
|
|
39
|
+
source: String(row.source),
|
|
40
|
+
createdAt: iso(row.created_at),
|
|
41
|
+
updatedAt: iso(row.updated_at),
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
const rowOf = (reference) => {
|
|
45
|
+
const thing = requiredThing(database, reference.includes(":") ? reference : `product:${reference}`, "the product");
|
|
46
|
+
if (thing.type !== "product")
|
|
47
|
+
return refuse(`${reference} is not a product`, "product.ref");
|
|
48
|
+
return database.prepare(`${PRODUCTS} WHERE c.id = ?`).get(thing.id);
|
|
49
|
+
};
|
|
50
|
+
const priceOf = (unitPrice, currency) => {
|
|
51
|
+
if ((unitPrice == null) !== (currency == null))
|
|
52
|
+
refuse("a product's price and currency come together", "product.field");
|
|
53
|
+
if (unitPrice == null || currency == null)
|
|
54
|
+
return { units: null, currency: null };
|
|
55
|
+
const code = currencyOf(currency);
|
|
56
|
+
return { units: minorUnits(unitPrice, code, "a product's price"), currency: code };
|
|
57
|
+
};
|
|
58
|
+
const skuTaken = (sku, supplier, except) => database
|
|
59
|
+
.prepare("SELECT id FROM catalog_items WHERE sku = ? AND deleted_at IS NULL AND supplier_type IS ? AND supplier_id IS ? " +
|
|
60
|
+
"AND id IS NOT ?")
|
|
61
|
+
.get(sku, supplier?.type ?? null, supplier?.id ?? null, except);
|
|
62
|
+
return {
|
|
63
|
+
add: (input) => {
|
|
64
|
+
const kind = input.kind === undefined ? "product" : oneOf(input.kind, CATALOG_KINDS, "a product's kind");
|
|
65
|
+
const name = text(input.name, "a product's name", 200) ?? refuse("a product needs a name", "product.field");
|
|
66
|
+
const sku = text(input.sku, "a product's code", 80);
|
|
67
|
+
const unit = text(input.unit, "a product's unit", 40);
|
|
68
|
+
const description = text(input.description, "a product's description", 2000);
|
|
69
|
+
const price = priceOf(input.unitPrice, input.currency);
|
|
70
|
+
const id = write(database, () => {
|
|
71
|
+
const supplier = partyThing(input.supplier, "the supplier");
|
|
72
|
+
if (sku !== null && skuTaken(sku, supplier, null))
|
|
73
|
+
refuse(`a product with the code ${sku} is already stored for that supplier`, "product.duplicate");
|
|
74
|
+
const { actor, model } = authorActor(context, input.by);
|
|
75
|
+
const source = input.by === "owner" ? "owner" : (text(input.source, "a source", 80) ?? "agent");
|
|
76
|
+
const at = now();
|
|
77
|
+
return Number(database
|
|
78
|
+
.prepare("INSERT INTO catalog_items (kind, name, name_folded, sku, supplier_type, supplier_id, unit, unit_price, " +
|
|
79
|
+
"currency, description, author_type, author_id, model, source, created_at, updated_at) " +
|
|
80
|
+
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING id")
|
|
81
|
+
.get(kind, name, fold(name), sku, supplier?.type ?? null, supplier?.id ?? null, unit, price.units, price.currency, description, actor.type, actor.id, model, source, at, at)?.id);
|
|
82
|
+
});
|
|
83
|
+
return productOf(rowOf(`product:${id}`));
|
|
84
|
+
},
|
|
85
|
+
get: (reference) => productOf(rowOf(reference)),
|
|
86
|
+
list: (filter = {}) => {
|
|
87
|
+
const kind = filter.kind === undefined ? null : oneOf(filter.kind, CATALOG_KINDS, "a kind");
|
|
88
|
+
const supplier = partyThing(filter.supplier, "the supplier");
|
|
89
|
+
const archived = filter.archived === undefined ? null : filter.archived ? 1 : 0;
|
|
90
|
+
const words = filter.name === undefined ? [] : fold(filter.name).split(/\s+/).filter(Boolean);
|
|
91
|
+
const limit = limitOf(filter.limit);
|
|
92
|
+
return database
|
|
93
|
+
.prepare(`${PRODUCTS} WHERE c.deleted_at IS NULL AND (? IS NULL OR c.kind = ?) ` +
|
|
94
|
+
"AND (? IS NULL OR (c.supplier_type = ? AND c.supplier_id = ?)) " +
|
|
95
|
+
"AND (? IS NULL OR (c.archived_date IS NOT NULL) = ?) ORDER BY c.name_folded, c.id")
|
|
96
|
+
.all(kind, kind, supplier?.type ?? null, supplier?.type ?? null, supplier?.id ?? null, archived, archived)
|
|
97
|
+
.filter((row) => words.every((word) => String(row.name_folded).includes(word)))
|
|
98
|
+
.slice(0, limit)
|
|
99
|
+
.map(productOf);
|
|
100
|
+
},
|
|
101
|
+
update: (reference, input) => {
|
|
102
|
+
const row = rowOf(reference);
|
|
103
|
+
if (row.deleted_at != null)
|
|
104
|
+
refuse(`product:${row.id} is deleted`, "product.ref");
|
|
105
|
+
authorActor(context, input.by);
|
|
106
|
+
const name = input.name === undefined ? String(row.name) : text(input.name, "a product's name", 200);
|
|
107
|
+
const unit = input.unit === undefined ? row.unit : text(input.unit, "a product's unit", 40);
|
|
108
|
+
const description = input.description === undefined ? row.description : text(input.description, "a product's description", 2000);
|
|
109
|
+
const price = input.unitPrice === undefined && input.currency === undefined
|
|
110
|
+
? { units: row.unit_price, currency: row.currency }
|
|
111
|
+
: priceOf(input.unitPrice, input.currency);
|
|
112
|
+
const archived = input.archivedDate === undefined ? row.archived_date : isoDate(input.archivedDate, "the day it stopped selling");
|
|
113
|
+
write(database, () => {
|
|
114
|
+
database
|
|
115
|
+
.prepare("UPDATE catalog_items SET name = ?, name_folded = ?, unit = ?, unit_price = ?, currency = ?, description = ?, " +
|
|
116
|
+
"archived_date = ?, updated_at = ? WHERE id = ?")
|
|
117
|
+
.run(name, fold(name), unit, price.units, price.currency, description, archived, now(), Number(row.id));
|
|
118
|
+
});
|
|
119
|
+
return productOf(rowOf(`product:${row.id}`));
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
export const productsStoreOver = (context) => {
|
|
124
|
+
const operations = productOperationsOver(context);
|
|
125
|
+
return {
|
|
126
|
+
add: async (input) => operations.add(input),
|
|
127
|
+
get: async (reference) => operations.get(reference),
|
|
128
|
+
list: async (filter) => operations.list(filter),
|
|
129
|
+
update: async (reference, input) => operations.update(reference, input),
|
|
130
|
+
};
|
|
131
|
+
};
|
|
@@ -1,7 +1,32 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
2
|
import { fold } from "../../normalize.js";
|
|
3
|
+
import { isEntityType } from "../entity-types.js";
|
|
4
|
+
import { evidenceMetadata, evidenceSignature } from "../evidence-signature.js";
|
|
3
5
|
import { nextId } from "../memories.js";
|
|
4
6
|
import { thingOf } from "../things.js";
|
|
7
|
+
const metadataOf = (database, link, to) => {
|
|
8
|
+
const provenance = link.provenance === null ? {} : { provenance: link.provenance };
|
|
9
|
+
if (link.kind === "evidence" && to)
|
|
10
|
+
return JSON.stringify({
|
|
11
|
+
...provenance,
|
|
12
|
+
...evidenceMetadata(evidenceSignature(database, to), link.updated_at, undefined),
|
|
13
|
+
});
|
|
14
|
+
if (link.kind === "evidence")
|
|
15
|
+
return JSON.stringify({ ...provenance, ...goneEvidence(link) });
|
|
16
|
+
return link.provenance === null ? null : JSON.stringify(provenance);
|
|
17
|
+
};
|
|
18
|
+
/** Evidence whose source was deleted for good travels with only its old reference, and stays gone. */
|
|
19
|
+
const goneEvidence = (link) => {
|
|
20
|
+
const kind = link.target_text?.split(":")[0] ?? "";
|
|
21
|
+
return {
|
|
22
|
+
v: 1,
|
|
23
|
+
sig: "gone",
|
|
24
|
+
rev: null,
|
|
25
|
+
cited_rev: null,
|
|
26
|
+
checked_at: link.updated_at,
|
|
27
|
+
gone: isEntityType(kind) ? kind : "source",
|
|
28
|
+
};
|
|
29
|
+
};
|
|
5
30
|
export const restoreAnnotationRecords = (database, projectId, snapshot, bindings) => {
|
|
6
31
|
const mappings = { ...bindings };
|
|
7
32
|
const resolve = (ref) => {
|
|
@@ -46,7 +71,7 @@ export const restoreAnnotationRecords = (database, projectId, snapshot, bindings
|
|
|
46
71
|
// A relationship the store already holds under its unique key keeps that row: the snapshot maps onto it.
|
|
47
72
|
const made = database
|
|
48
73
|
.prepare("INSERT INTO links(from_type,from_id,to_type,to_id,kind,anchor,source,target_text,target_folded,role,evidence,metadata,confirmed,created_at,author,updated_at,starts_at,ends_at,deleted_at) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT DO NOTHING RETURNING id")
|
|
49
|
-
.get(from.type, from.id, to?.type ?? null, to?.id ?? null, link.kind, link.anchor, link.origin, link.target_text, link.target_text === null ? null : fold(link.target_text), link.role, link.evidence,
|
|
74
|
+
.get(from.type, from.id, to?.type ?? null, to?.id ?? null, link.kind, link.anchor, link.origin, link.target_text, link.target_text === null || (link.kind === "evidence" && to === null) ? null : fold(link.target_text), link.role, link.evidence, metadataOf(database, link, to), Number(link.confirmed), link.created_at, link.author, link.updated_at, link.starts_at ?? null, link.ends_at ?? null, link.deleted_at ?? null);
|
|
50
75
|
const row = made ??
|
|
51
76
|
database
|
|
52
77
|
.prepare("SELECT id FROM links WHERE kind=? AND from_type=? AND from_id=? AND to_type IS ? AND to_id IS ? " +
|
|
@@ -12,7 +12,9 @@ const flag = (value) => {
|
|
|
12
12
|
return unsupported();
|
|
13
13
|
return value === 1;
|
|
14
14
|
};
|
|
15
|
-
|
|
15
|
+
/** An evidence link's signature describes a row of this store, so it does not travel; the restore signs it anew. */
|
|
16
|
+
const SIGNATURE_KEYS = new Set(["v", "sig", "rev", "cited_rev", "quote", "checked_at", "gone"]);
|
|
17
|
+
const provenance = (raw, kind) => {
|
|
16
18
|
if (raw === null)
|
|
17
19
|
return null;
|
|
18
20
|
let value;
|
|
@@ -25,7 +27,7 @@ const provenance = (raw) => {
|
|
|
25
27
|
if (!value ||
|
|
26
28
|
typeof value !== "object" ||
|
|
27
29
|
Array.isArray(value) ||
|
|
28
|
-
Object.keys(value).some((key) => key !== "provenance"))
|
|
30
|
+
Object.keys(value).some((key) => key !== "provenance" && !(kind === "evidence" && SIGNATURE_KEYS.has(key))))
|
|
29
31
|
return unsupported();
|
|
30
32
|
const text = value.provenance;
|
|
31
33
|
if (text !== undefined && text !== null && typeof text !== "string")
|
|
@@ -175,7 +177,7 @@ export const projectAnnotationSnapshot = (database, projectId, budget) => read(d
|
|
|
175
177
|
role: nullableText(row.role),
|
|
176
178
|
evidence: nullableText(row.evidence),
|
|
177
179
|
confirmed: flag(row.confirmed),
|
|
178
|
-
provenance: provenance(row.metadata),
|
|
180
|
+
provenance: provenance(row.metadata, row.kind),
|
|
179
181
|
created_at: row.created_at,
|
|
180
182
|
updated_at: row.updated_at,
|
|
181
183
|
author: nullableText(row.author),
|
|
@@ -83,7 +83,7 @@ type ProposalOperations = Omit<{
|
|
|
83
83
|
/** Who proposed an action, for `mayDecide`. */
|
|
84
84
|
export declare const proposerOfAction: ({ proposedBy, payload }: ProposedAction) => Proposer;
|
|
85
85
|
export declare const proposalOperationsOver: (context: StoreContext) => ProposalOperations;
|
|
86
|
-
/** Refuses a
|
|
86
|
+
/** Refuses a dedicated kind on a generic action path: approving one as an action would skip its own checks. */
|
|
87
87
|
export declare const assertGenericAction: (kind: string) => void;
|
|
88
88
|
export declare const proposedActionsStoreOver: (context: StoreContext) => ProposedActionsStore;
|
|
89
89
|
export {};
|
|
@@ -3,6 +3,7 @@ import { MEETING_FINDINGS_KIND } from "../../meetings/index.js";
|
|
|
3
3
|
import { assertMayDecide, deciderAuthor } from "../decisions.js";
|
|
4
4
|
import { EVIDENCE_FINDINGS_KIND } from "../evidence-findings.js";
|
|
5
5
|
import { MAIL_FINDINGS_KIND } from "../mail-findings.js";
|
|
6
|
+
import { SOURCE_REVIEW_KIND } from "../source-review.js";
|
|
6
7
|
import { proposedBySql } from "./actors.js";
|
|
7
8
|
import { authorActor } from "./memories.js";
|
|
8
9
|
import { requiredThing } from "./notes.js";
|
|
@@ -164,17 +165,31 @@ export const proposalOperationsOver = (context) => {
|
|
|
164
165
|
},
|
|
165
166
|
};
|
|
166
167
|
};
|
|
167
|
-
|
|
168
|
+
const FINDINGS_COMMAND = "the dedicated findings command";
|
|
169
|
+
/** Stored kinds a generic action path never decides: each has its own review, which checks what it cites. */
|
|
168
170
|
const DEDICATED_REVIEW = {
|
|
169
|
-
[MEETING_FINDINGS_KIND]: {
|
|
170
|
-
|
|
171
|
-
|
|
171
|
+
[MEETING_FINDINGS_KIND]: {
|
|
172
|
+
what: "meeting findings",
|
|
173
|
+
reason: "meeting-findings.dedicated-review",
|
|
174
|
+
through: FINDINGS_COMMAND,
|
|
175
|
+
},
|
|
176
|
+
[EVIDENCE_FINDINGS_KIND]: {
|
|
177
|
+
what: "evidence findings",
|
|
178
|
+
reason: "evidence-findings.dedicated-review",
|
|
179
|
+
through: FINDINGS_COMMAND,
|
|
180
|
+
},
|
|
181
|
+
[MAIL_FINDINGS_KIND]: { what: "mail findings", reason: "mail-findings.dedicated-review", through: FINDINGS_COMMAND },
|
|
182
|
+
[SOURCE_REVIEW_KIND]: {
|
|
183
|
+
what: "a source review",
|
|
184
|
+
reason: "source-review.dedicated-review",
|
|
185
|
+
through: "the review of memories and facts, which signs their sources again",
|
|
186
|
+
},
|
|
172
187
|
};
|
|
173
|
-
/** Refuses a
|
|
188
|
+
/** Refuses a dedicated kind on a generic action path: approving one as an action would skip its own checks. */
|
|
174
189
|
export const assertGenericAction = (kind) => {
|
|
175
190
|
const dedicated = DEDICATED_REVIEW[kind];
|
|
176
191
|
if (dedicated)
|
|
177
|
-
throw new CliError("validation_error", `review ${dedicated.what} through
|
|
192
|
+
throw new CliError("validation_error", `review ${dedicated.what} through ${dedicated.through}`, {
|
|
178
193
|
reason: dedicated.reason,
|
|
179
194
|
});
|
|
180
195
|
};
|
|
@@ -189,7 +204,9 @@ export const proposedActionsStoreOver = (context) => {
|
|
|
189
204
|
propose: async (input) => {
|
|
190
205
|
const dedicated = DEDICATED_REVIEW[input.kind];
|
|
191
206
|
if (dedicated)
|
|
192
|
-
throw new CliError("validation_error", `stage ${dedicated.what} through
|
|
207
|
+
throw new CliError("validation_error", `stage ${dedicated.what} through ${dedicated.through}`, {
|
|
208
|
+
reason: dedicated.reason,
|
|
209
|
+
});
|
|
193
210
|
return operations.propose(input);
|
|
194
211
|
},
|
|
195
212
|
get: async (id) => operations.get(id),
|
|
@@ -5,8 +5,4 @@ export declare const PROVIDERS_SETTING = "providers";
|
|
|
5
5
|
export declare const storedProvidersOf: (database: CacheDatabase) => unknown[];
|
|
6
6
|
/** Called on every open: the module state follows the store the process opened last. */
|
|
7
7
|
export declare const loadProviders: (database: CacheDatabase) => void;
|
|
8
|
-
/**
|
|
9
|
-
* Writes `entry` over the stored one of its name and loads the result. Every run of every CLI calls it, so it
|
|
10
|
-
* writes only when the entry changed. Entries this build cannot read stay stored for the CLIs that can.
|
|
11
|
-
*/
|
|
12
8
|
export declare const registerProvider: (database: CacheDatabase, value: ProviderEntry, now: number) => void;
|