@wirecat/cli-core 0.606.0 → 0.608.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 +196 -0
- 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 +63 -4
- 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,295 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { TRANSACTION_STATUSES, transactionLine } from "../../domain/business.js";
|
|
3
|
+
import { FINANCE_REVIEWS, MONEY_DIRECTIONS } from "../../domain/finance.js";
|
|
4
|
+
import { formatDecimal, minorUnits, moneyLabel, moneyOf, scaleOf } from "../../domain/money.js";
|
|
5
|
+
import { fold } from "../normalize.js";
|
|
6
|
+
import { proposedBySql } from "./actors.js";
|
|
7
|
+
import { projectId } from "./memories.js";
|
|
8
|
+
import { requiredThing } from "./notes.js";
|
|
9
|
+
import { isoDate, limitOf, oneOf, refuse, reviewedRowsOver, text, } from "./reviewed.js";
|
|
10
|
+
import { write } from "./transaction.js";
|
|
11
|
+
const TRANSACTIONS = "SELECT t.*, w.name AS wallet_name, b.name AS author_name, b.kind AS author_kind, " +
|
|
12
|
+
"(SELECT coalesce(sum(amount), 0) FROM transaction_splits WHERE transaction_id = t.id) AS split " +
|
|
13
|
+
"FROM financial_transactions t JOIN financial_accounts w ON w.id = t.financial_account_id " +
|
|
14
|
+
"LEFT JOIN bots b ON t.author_type = 'bot' AND b.id = t.author_id";
|
|
15
|
+
const MAX_SPLITS = 100;
|
|
16
|
+
export const transactionOperationsOver = (context) => {
|
|
17
|
+
const { database, now } = context;
|
|
18
|
+
const { partyOf, partyThing, reviewedOf, written, keepEvidence, linkedId, decideRow, mayChangeParts } = reviewedRowsOver(context);
|
|
19
|
+
const splitsOf = (id, currency) => database
|
|
20
|
+
.prepare("SELECT * FROM transaction_splits WHERE transaction_id = ? ORDER BY position")
|
|
21
|
+
.all(id)
|
|
22
|
+
.map((row) => ({
|
|
23
|
+
position: Number(row.position),
|
|
24
|
+
category: String(row.category),
|
|
25
|
+
amount: formatDecimal(Number(row.amount), scaleOf(currency)),
|
|
26
|
+
project: row.project_id == null ? null : `project:${row.project_id}`,
|
|
27
|
+
note: row.note == null ? null : String(row.note),
|
|
28
|
+
}));
|
|
29
|
+
const transactionOf = (row) => {
|
|
30
|
+
const currency = String(row.currency);
|
|
31
|
+
const scale = scaleOf(currency);
|
|
32
|
+
const amount = Number(row.amount);
|
|
33
|
+
const fields = {
|
|
34
|
+
direction: row.direction,
|
|
35
|
+
amount,
|
|
36
|
+
currency,
|
|
37
|
+
label: (row.counterparty_name ?? row.description ?? null),
|
|
38
|
+
date: String(row.statement_date),
|
|
39
|
+
status: row.status,
|
|
40
|
+
review: row.review,
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
id: String(row.id),
|
|
44
|
+
ref: `transaction:${row.id}`,
|
|
45
|
+
title: `Transaction · ${transactionLine(fields)}`,
|
|
46
|
+
wallet: `wallet:${row.financial_account_id}`,
|
|
47
|
+
walletName: String(row.wallet_name),
|
|
48
|
+
direction: fields.direction,
|
|
49
|
+
amount: formatDecimal(amount, scale),
|
|
50
|
+
currency,
|
|
51
|
+
status: fields.status,
|
|
52
|
+
statementDate: fields.date,
|
|
53
|
+
bookedDate: row.booked_date == null ? null : String(row.booked_date),
|
|
54
|
+
description: row.description == null ? null : String(row.description),
|
|
55
|
+
counterpartyName: row.counterparty_name == null ? null : String(row.counterparty_name),
|
|
56
|
+
counterparty: partyOf(row.counterparty_type, row.counterparty_id),
|
|
57
|
+
reference: row.reference == null ? null : String(row.reference),
|
|
58
|
+
payment: row.payment_id == null ? null : `payment:${row.payment_id}`,
|
|
59
|
+
splits: splitsOf(Number(row.id), currency),
|
|
60
|
+
uncategorized: formatDecimal(amount - Number(row.split), scale),
|
|
61
|
+
...reviewedOf(row, "transaction"),
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
const rowOf = (reference) => {
|
|
65
|
+
const thing = requiredThing(database, reference.includes(":") ? reference : `transaction:${reference}`, "the transaction");
|
|
66
|
+
if (thing.type !== "transaction")
|
|
67
|
+
return refuse(`${reference} is not a transaction`, "transaction.ref");
|
|
68
|
+
return database.prepare(`${TRANSACTIONS} WHERE t.id = ?`).get(thing.id);
|
|
69
|
+
};
|
|
70
|
+
/** A statement line meets the settlement it records only when both say the same: currency, direction, amount. */
|
|
71
|
+
const paymentFor = (reference, moved, except) => {
|
|
72
|
+
if (reference == null)
|
|
73
|
+
return null;
|
|
74
|
+
const id = linkedId(reference, "payment", "transaction.payment");
|
|
75
|
+
const payment = database.prepare("SELECT * FROM payments WHERE id = ?").get(id);
|
|
76
|
+
const ref = `payment:${id}`;
|
|
77
|
+
if (payment.review === "rejected" || payment.deleted_at != null)
|
|
78
|
+
refuse(`${ref} is ${payment.review === "rejected" ? "rejected" : "deleted"}`, "transaction.payment");
|
|
79
|
+
if (payment.currency !== moved.currency ||
|
|
80
|
+
payment.direction !== moved.direction ||
|
|
81
|
+
Number(payment.amount) !== moved.amount)
|
|
82
|
+
refuse(`${ref} is ${moneyLabel(Number(payment.amount), String(payment.currency))} ${String(payment.direction)}; ` +
|
|
83
|
+
`the transaction is ${moneyLabel(moved.amount, moved.currency)} ${moved.direction}: they are not the same money`, "transaction.payment");
|
|
84
|
+
const taken = database
|
|
85
|
+
.prepare("SELECT id FROM financial_transactions WHERE payment_id = ? AND deleted_at IS NULL AND id IS NOT ?")
|
|
86
|
+
.get(id, except);
|
|
87
|
+
if (taken)
|
|
88
|
+
refuse(`${ref} is already the payment of transaction:${taken.id}`, "transaction.payment");
|
|
89
|
+
return id;
|
|
90
|
+
};
|
|
91
|
+
const splitRows = (splits, currency, amount, already) => {
|
|
92
|
+
if (!Array.isArray(splits) || splits.length > MAX_SPLITS)
|
|
93
|
+
refuse(`a transaction has at most ${MAX_SPLITS} splits`, "transaction.split");
|
|
94
|
+
const rows = splits.map((split, at) => {
|
|
95
|
+
const what = `split ${at + 1}`;
|
|
96
|
+
const units = minorUnits(split.amount, currency, `${what}'s amount`);
|
|
97
|
+
if (units <= 0)
|
|
98
|
+
refuse(`${what}'s amount is more than 0`, "money.amount");
|
|
99
|
+
return {
|
|
100
|
+
units,
|
|
101
|
+
category: text(split.category, `${what}'s category`, 200) ?? refuse(`${what} needs a category`, "transaction.split"),
|
|
102
|
+
project: split.project === undefined ? null : projectId(database, split.project),
|
|
103
|
+
note: text(split.note, `${what}'s note`, 2000),
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
const total = already + rows.reduce((sum, row) => sum + row.units, 0);
|
|
107
|
+
if (total > amount)
|
|
108
|
+
refuse(`the splits come to ${moneyLabel(total, currency)}, more than the transaction's ${moneyLabel(amount, currency)}`, "transaction.split");
|
|
109
|
+
return rows;
|
|
110
|
+
};
|
|
111
|
+
const insertSplits = (id, rows, from) => {
|
|
112
|
+
const at = now();
|
|
113
|
+
const insert = database.prepare("INSERT INTO transaction_splits (transaction_id, position, category, category_folded, amount, project_id, note, " +
|
|
114
|
+
"created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
|
115
|
+
for (const [index, row] of rows.entries())
|
|
116
|
+
insert.run(id, from + index, row.category, fold(row.category), row.units, row.project, row.note, at, at);
|
|
117
|
+
};
|
|
118
|
+
const mayChange = (row, by) => {
|
|
119
|
+
mayChangeParts(row, by, `only the owner changes transaction:${row.id}; a bot changes only a transaction it proposed that is still proposed`);
|
|
120
|
+
if (row.review === "rejected" || row.deleted_at != null)
|
|
121
|
+
refuse(`transaction:${row.id} is ${row.deleted_at != null ? "deleted" : "rejected"}`, "transaction.ref");
|
|
122
|
+
};
|
|
123
|
+
const bookingOf = (status, booked, date) => {
|
|
124
|
+
if (status === "pending" && booked != null)
|
|
125
|
+
refuse("a pending transaction has no booking day yet", "transaction.field");
|
|
126
|
+
return status === "pending" ? null : (isoDate(booked, "the booking day") ?? date);
|
|
127
|
+
};
|
|
128
|
+
const decide = (reference, by, reason, confirm) => {
|
|
129
|
+
const row = rowOf(reference);
|
|
130
|
+
decideRow("transaction", row, { by, reason, confirm });
|
|
131
|
+
return transactionOf(rowOf(`transaction:${row.id}`));
|
|
132
|
+
};
|
|
133
|
+
return {
|
|
134
|
+
add: (input) => {
|
|
135
|
+
const direction = oneOf(input.direction, MONEY_DIRECTIONS, "a transaction's direction");
|
|
136
|
+
const status = input.status === undefined ? "booked" : oneOf(input.status, TRANSACTION_STATUSES, "a status");
|
|
137
|
+
const date = isoDate(input.statementDate, "a transaction's day") ?? refuse("a transaction has a day", "transaction.field");
|
|
138
|
+
const bookedDate = bookingOf(status, input.bookedDate, date);
|
|
139
|
+
const id = write(database, () => {
|
|
140
|
+
const walletId = linkedId(input.wallet, "wallet", "transaction.ref");
|
|
141
|
+
const wallet = database.prepare("SELECT * FROM financial_accounts WHERE id = ?").get(walletId);
|
|
142
|
+
if (wallet.deleted_at != null)
|
|
143
|
+
refuse(`wallet:${walletId} is deleted`, "transaction.ref");
|
|
144
|
+
const currency = String(wallet.currency);
|
|
145
|
+
if (input.currency !== undefined && input.currency.trim().toUpperCase() !== currency)
|
|
146
|
+
refuse(`wallet:${walletId} is in ${currency}; a transaction is in its wallet's currency`, "money.currency");
|
|
147
|
+
const amount = minorUnits(input.amount, currency, "a transaction's amount");
|
|
148
|
+
if (amount <= 0)
|
|
149
|
+
refuse("a transaction's amount is more than 0", "money.amount");
|
|
150
|
+
const splits = splitRows(input.splits ?? [], currency, amount, 0);
|
|
151
|
+
const payment = paymentFor(input.payment, { direction, amount, currency }, null);
|
|
152
|
+
const counterparty = partyThing(input.counterparty, "the counterparty");
|
|
153
|
+
const by = written(input);
|
|
154
|
+
const at = now();
|
|
155
|
+
const made = Number(database
|
|
156
|
+
.prepare("INSERT INTO financial_transactions (financial_account_id, direction, amount, currency, status, statement_date, " +
|
|
157
|
+
"booked_date, description, counterparty_name, counterparty_type, counterparty_id, reference, payment_id, " +
|
|
158
|
+
"project_id, review, author_type, author_id, model, confirmed_by_type, confirmed_by_id, source, metadata, " +
|
|
159
|
+
"created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) " +
|
|
160
|
+
"RETURNING id")
|
|
161
|
+
.get(walletId, direction, amount, currency, status, date, bookedDate, text(input.description, "a transaction's description", 2000), text(input.counterpartyName, "the counterparty's name", 200), counterparty?.type ?? null, counterparty?.id ?? null, text(input.reference, "a transaction's reference", 500), payment, by.project, by.review, by.actor.type, by.actor.id, by.model, by.confirmedType, by.confirmedId, by.source, by.metadata, at, at)?.id);
|
|
162
|
+
insertSplits(made, splits, 1);
|
|
163
|
+
keepEvidence("transaction", made, by);
|
|
164
|
+
return made;
|
|
165
|
+
});
|
|
166
|
+
return transactionOf(rowOf(`transaction:${id}`));
|
|
167
|
+
},
|
|
168
|
+
get: (reference) => transactionOf(rowOf(reference)),
|
|
169
|
+
list: (filter = {}) => {
|
|
170
|
+
const wallet = filter.wallet === undefined ? null : linkedId(filter.wallet, "wallet", "transaction.ref");
|
|
171
|
+
const project = filter.project === undefined ? null : projectId(database, filter.project);
|
|
172
|
+
const direction = filter.direction === undefined ? null : oneOf(filter.direction, MONEY_DIRECTIONS, "a direction");
|
|
173
|
+
const status = filter.status === undefined ? null : oneOf(filter.status, TRANSACTION_STATUSES, "a status");
|
|
174
|
+
const review = filter.review === undefined ? null : oneOf(filter.review, FINANCE_REVIEWS, "a review");
|
|
175
|
+
const category = filter.category === undefined ? null : fold(filter.category.trim());
|
|
176
|
+
const by = proposedBySql("t.author_type", "t.author_id", filter.proposedBy);
|
|
177
|
+
return database
|
|
178
|
+
.prepare(`${TRANSACTIONS} WHERE t.deleted_at IS NULL AND (? IS NULL OR t.financial_account_id = ?) ` +
|
|
179
|
+
"AND (? IS NULL OR t.direction = ?) AND (? IS NULL OR t.status = ?) AND (? IS NULL OR t.review = ?) " +
|
|
180
|
+
"AND (? IS NULL OR t.project_id = ? OR EXISTS (SELECT 1 FROM transaction_splits s WHERE s.transaction_id = t.id " +
|
|
181
|
+
"AND s.project_id = ?)) AND (? IS NULL OR EXISTS (SELECT 1 FROM transaction_splits s " +
|
|
182
|
+
"WHERE s.transaction_id = t.id AND s.category_folded = ?)) " +
|
|
183
|
+
`${filter.uncategorized ? "AND split < t.amount " : ""}AND ${by.sql} ` +
|
|
184
|
+
"ORDER BY t.statement_date DESC, t.id DESC LIMIT ?")
|
|
185
|
+
.all(wallet, wallet, direction, direction, status, status, review, review, project, project, project, category, category, ...by.params, limitOf(filter.limit))
|
|
186
|
+
.map(transactionOf);
|
|
187
|
+
},
|
|
188
|
+
update: (reference, input) => {
|
|
189
|
+
const row = rowOf(reference);
|
|
190
|
+
const ref = `transaction:${row.id}`;
|
|
191
|
+
mayChange(row, input.by);
|
|
192
|
+
const currency = String(row.currency);
|
|
193
|
+
const direction = input.direction === undefined ? String(row.direction) : oneOf(input.direction, MONEY_DIRECTIONS, "a direction");
|
|
194
|
+
const amount = input.amount === undefined ? Number(row.amount) : minorUnits(input.amount, currency, "a transaction's amount");
|
|
195
|
+
if (amount <= 0)
|
|
196
|
+
refuse("a transaction's amount is more than 0", "money.amount");
|
|
197
|
+
if (amount < Number(row.split))
|
|
198
|
+
refuse(`the splits of ${ref} come to ${moneyLabel(Number(row.split), currency)}, more than ${moneyLabel(amount, currency)}`, "transaction.split");
|
|
199
|
+
const status = input.status === undefined
|
|
200
|
+
? row.status
|
|
201
|
+
: oneOf(input.status, TRANSACTION_STATUSES, "a status");
|
|
202
|
+
const date = input.statementDate === undefined
|
|
203
|
+
? String(row.statement_date)
|
|
204
|
+
: isoDate(input.statementDate, "a transaction's day");
|
|
205
|
+
const bookedDate = input.status === undefined && input.bookedDate === undefined
|
|
206
|
+
? row.booked_date
|
|
207
|
+
: bookingOf(status, input.bookedDate, date);
|
|
208
|
+
const kept = (given, column, what, limit) => given === undefined ? (row[column] ?? null) : text(given, what, limit);
|
|
209
|
+
write(database, () => {
|
|
210
|
+
const moved = { direction, amount, currency };
|
|
211
|
+
const linked = input.payment === undefined ? (row.payment_id == null ? null : `payment:${row.payment_id}`) : input.payment;
|
|
212
|
+
const payment = paymentFor(linked, moved, Number(row.id));
|
|
213
|
+
const counterparty = input.counterparty === undefined
|
|
214
|
+
? { type: row.counterparty_type, id: row.counterparty_id }
|
|
215
|
+
: input.counterparty === null
|
|
216
|
+
? { type: null, id: null }
|
|
217
|
+
: partyThing(input.counterparty, "the counterparty");
|
|
218
|
+
database
|
|
219
|
+
.prepare("UPDATE financial_transactions SET direction = ?, amount = ?, status = ?, statement_date = ?, booked_date = ?, " +
|
|
220
|
+
"description = ?, counterparty_name = ?, counterparty_type = ?, counterparty_id = ?, reference = ?, " +
|
|
221
|
+
"payment_id = ?, updated_at = ? WHERE id = ?")
|
|
222
|
+
.run(direction, amount, status, date, bookedDate, kept(input.description, "description", "a transaction's description", 2000), kept(input.counterpartyName, "counterparty_name", "the counterparty's name", 200), counterparty.type, counterparty.id, kept(input.reference, "reference", "a transaction's reference", 500), payment, now(), Number(row.id));
|
|
223
|
+
});
|
|
224
|
+
return transactionOf(rowOf(ref));
|
|
225
|
+
},
|
|
226
|
+
addSplit: (reference, { by, ...split }) => {
|
|
227
|
+
const row = rowOf(reference);
|
|
228
|
+
mayChange(row, by);
|
|
229
|
+
write(database, () => {
|
|
230
|
+
const rows = splitRows([split], String(row.currency), Number(row.amount), Number(row.split));
|
|
231
|
+
const last = Number(database
|
|
232
|
+
.prepare("SELECT coalesce(max(position), 0) AS n FROM transaction_splits WHERE transaction_id = ?")
|
|
233
|
+
.get(Number(row.id))?.n);
|
|
234
|
+
if (last >= MAX_SPLITS)
|
|
235
|
+
refuse(`a transaction has at most ${MAX_SPLITS} splits`, "transaction.split");
|
|
236
|
+
insertSplits(Number(row.id), rows, last + 1);
|
|
237
|
+
});
|
|
238
|
+
return transactionOf(rowOf(`transaction:${row.id}`));
|
|
239
|
+
},
|
|
240
|
+
removeSplit: (reference, { position, by }) => {
|
|
241
|
+
const row = rowOf(reference);
|
|
242
|
+
mayChange(row, by);
|
|
243
|
+
const gone = database
|
|
244
|
+
.prepare("DELETE FROM transaction_splits WHERE transaction_id = ? AND position = ?")
|
|
245
|
+
.run(Number(row.id), position);
|
|
246
|
+
if (gone.changes === 0)
|
|
247
|
+
throw new CliError("not_found", `transaction:${row.id} has no split ${position}`, {
|
|
248
|
+
reason: "transaction.no-split",
|
|
249
|
+
});
|
|
250
|
+
return transactionOf(rowOf(`transaction:${row.id}`));
|
|
251
|
+
},
|
|
252
|
+
categories: (filter = {}) => {
|
|
253
|
+
const wallet = filter.wallet === undefined ? null : linkedId(filter.wallet, "wallet", "transaction.ref");
|
|
254
|
+
const from = isoDate(filter.from, "the first day counted");
|
|
255
|
+
const until = isoDate(filter.until, "the last day counted");
|
|
256
|
+
const where = "t.review = 'confirmed' AND t.status = 'booked' AND t.deleted_at IS NULL AND (? IS NULL OR t.financial_account_id = ?) " +
|
|
257
|
+
"AND (? IS NULL OR t.booked_date >= ?) AND (? IS NULL OR t.booked_date <= ?)";
|
|
258
|
+
const params = [wallet, wallet, from, from, until, until];
|
|
259
|
+
const split = database
|
|
260
|
+
.prepare("SELECT min(s.category) AS category, s.category_folded, t.currency, sum(s.amount) AS units FROM transaction_splits s " +
|
|
261
|
+
`JOIN financial_transactions t ON t.id = s.transaction_id WHERE ${where} ` +
|
|
262
|
+
"GROUP BY s.category_folded, t.currency ORDER BY s.category_folded, t.currency")
|
|
263
|
+
.all(...params);
|
|
264
|
+
const rest = database
|
|
265
|
+
.prepare("SELECT t.currency, sum(t.amount - (SELECT coalesce(sum(amount), 0) FROM transaction_splits " +
|
|
266
|
+
`WHERE transaction_id = t.id)) AS units FROM financial_transactions t WHERE ${where} GROUP BY t.currency`)
|
|
267
|
+
.all(...params);
|
|
268
|
+
return [
|
|
269
|
+
...split.map((row) => ({
|
|
270
|
+
category: String(row.category),
|
|
271
|
+
...moneyOf(Number(row.units), String(row.currency)),
|
|
272
|
+
})),
|
|
273
|
+
...rest
|
|
274
|
+
.filter((row) => Number(row.units) > 0)
|
|
275
|
+
.map((row) => ({ category: null, ...moneyOf(Number(row.units), String(row.currency)) })),
|
|
276
|
+
];
|
|
277
|
+
},
|
|
278
|
+
confirm: (reference, by, reason) => decide(reference, by, reason, true),
|
|
279
|
+
reject: (reference, by, reason) => decide(reference, by, reason, false),
|
|
280
|
+
};
|
|
281
|
+
};
|
|
282
|
+
export const transactionsStoreOver = (context) => {
|
|
283
|
+
const operations = transactionOperationsOver(context);
|
|
284
|
+
return {
|
|
285
|
+
add: async (input) => operations.add(input),
|
|
286
|
+
get: async (reference) => operations.get(reference),
|
|
287
|
+
list: async (filter) => operations.list(filter),
|
|
288
|
+
update: async (reference, input) => operations.update(reference, input),
|
|
289
|
+
addSplit: async (reference, input) => operations.addSplit(reference, input),
|
|
290
|
+
removeSplit: async (reference, input) => operations.removeSplit(reference, input),
|
|
291
|
+
categories: async (filter) => operations.categories(filter),
|
|
292
|
+
confirm: async (reference, by, reason) => operations.confirm(reference, by, reason),
|
|
293
|
+
reject: async (reference, by, reason) => operations.reject(reference, by, reason),
|
|
294
|
+
};
|
|
295
|
+
};
|
|
@@ -2,6 +2,7 @@ import { type RelationshipColumns } from "../../domain/relations.js";
|
|
|
2
2
|
import type { Section } from "../../readers/types.js";
|
|
3
3
|
import { type Decider } from "../decisions.js";
|
|
4
4
|
import type { CacheDatabase } from "../driver.js";
|
|
5
|
+
import type { AddedSources } from "./added-sources.js";
|
|
5
6
|
import { type DocumentMetadata } from "./document-metadata.js";
|
|
6
7
|
import { type ImportedDocuments } from "./imported-documents.js";
|
|
7
8
|
import { LINK_KINDS, LINK_ORIGINS, type LINK_SOURCES } from "./link-kinds.js";
|
|
@@ -113,7 +114,7 @@ export interface MessageDocument {
|
|
|
113
114
|
reference: string;
|
|
114
115
|
fileId: string;
|
|
115
116
|
}
|
|
116
|
-
export interface NotesStore extends NoteSearch, ImportedDocuments {
|
|
117
|
+
export interface NotesStore extends NoteSearch, ImportedDocuments, AddedSources {
|
|
117
118
|
addFolder(input: {
|
|
118
119
|
name: string;
|
|
119
120
|
format?: NoteFolder["format"];
|
|
@@ -203,6 +204,7 @@ export declare const documentOf: (row: Row) => Note;
|
|
|
203
204
|
export declare const noteOf: (row: Row) => Note;
|
|
204
205
|
/** A document or note row by its typed reference, or `undefined`. */
|
|
205
206
|
export declare const noteRowOf: (database: CacheDatabase, reference: string) => Note | undefined;
|
|
207
|
+
export declare const thingRef: (database: CacheDatabase, type: unknown, id: unknown) => string | null;
|
|
206
208
|
export declare const linkOf: (database: CacheDatabase, row: Row) => Link;
|
|
207
209
|
/** The row a reference names, refused when the store holds none. */
|
|
208
210
|
export declare const requiredThing: (database: CacheDatabase, reference: string, what?: string) => Thing;
|
|
@@ -215,4 +217,11 @@ export declare const resolvePersonLinks: (database: CacheDatabase, names: (strin
|
|
|
215
217
|
export declare const purposeOf: (purpose: string) => string;
|
|
216
218
|
/** Writes one link; an identical resolved link is answered instead of doubled. */
|
|
217
219
|
export declare const insertLink: (context: StoreContext, input: LinkInput) => Link;
|
|
218
|
-
|
|
220
|
+
/** A folder's `accounts` row; its format lives in `settings`. */
|
|
221
|
+
export declare const folderOf: (row: Row) => NoteFolder;
|
|
222
|
+
export declare const pathParts: (path: string) => {
|
|
223
|
+
location: string | null;
|
|
224
|
+
fileName: string;
|
|
225
|
+
extension: string | null;
|
|
226
|
+
};
|
|
227
|
+
export declare const notesStoreOver: (context: StoreContext) => Omit<NotesStore, keyof NoteSearch | keyof AddedSources>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CliError } from "@wirecat/cli-base";
|
|
2
2
|
import { MAX_TEXT_CHARS } from "../../attachments/limits.js";
|
|
3
3
|
import { googleDocumentLinks } from "../../domain/google-documents.js";
|
|
4
|
-
import { DOCUMENT_ACCOUNT_PROVIDERS, filesAccountSql, folderProviderOf, folderProviderSql, isDocumentAccountProvider, } from "../../domain/providers.js";
|
|
4
|
+
import { ADDED_PROVIDER, DOCUMENT_ACCOUNT_PROVIDERS, filesAccountSql, folderProviderOf, folderProviderSql, isDocumentAccountProvider, notAdded, } from "../../domain/providers.js";
|
|
5
5
|
import { parseReference } from "../../domain/references.js";
|
|
6
6
|
import { isRelationKind } from "../../domain/relations.js";
|
|
7
7
|
import { normalizeTag } from "../../domain/tags.js";
|
|
@@ -10,6 +10,7 @@ import { fold } from "../normalize.js";
|
|
|
10
10
|
import { ulid } from "../ulid.js";
|
|
11
11
|
import { ownerPerson } from "./actors.js";
|
|
12
12
|
import { encodeDocumentMetadata, languageOf } from "./document-metadata.js";
|
|
13
|
+
import { deleteDocumentRow, forgetEvidenceToRow } from "./evidence-signature.js";
|
|
13
14
|
import { PREFERRED_FILE, syncFile } from "./files.js";
|
|
14
15
|
import { importedDocumentWhere } from "./imported-documents.js";
|
|
15
16
|
import { LINK_KINDS, LINK_ORIGINS, linkKind } from "./link-kinds.js";
|
|
@@ -17,23 +18,13 @@ import { reviewLink } from "./link-reviews.js";
|
|
|
17
18
|
import { dateFileNotes } from "./note-dates.js";
|
|
18
19
|
import { holdingText } from "./note-index.js";
|
|
19
20
|
import { addTags, ensureTag, labelsOf } from "./tags.js";
|
|
21
|
+
import { oneOf, textOf } from "./text-input.js";
|
|
20
22
|
import { referenceOfThing, storedThing, thingOf } from "./things.js";
|
|
21
23
|
import { write } from "./transaction.js";
|
|
22
24
|
export const NOTE_FORMATS = ["obsidian", "markdown"];
|
|
23
25
|
export { LINK_ORIGINS } from "./link-kinds.js";
|
|
24
26
|
const iso = (value) => new Date(Number(value)).toISOString();
|
|
25
27
|
const optionalIso = (value) => (value == null ? null : iso(value));
|
|
26
|
-
const textOf = (text, what, max = 100_000, allowEmpty = false) => {
|
|
27
|
-
const value = text.trim();
|
|
28
|
-
if ((!value && !allowEmpty) || value.length > max)
|
|
29
|
-
throw new CliError("validation_error", `${what} takes ${allowEmpty ? 0 : 1}–${max} characters`);
|
|
30
|
-
return value;
|
|
31
|
-
};
|
|
32
|
-
const oneOf = (value, allowed, what) => {
|
|
33
|
-
if (!allowed.includes(value))
|
|
34
|
-
throw new CliError("validation_error", `${what} is one of ${allowed.join(", ")}`);
|
|
35
|
-
return value;
|
|
36
|
-
};
|
|
37
28
|
/** What a link's written target is matched by: case, accents and a leading `@` do not count. */
|
|
38
29
|
export const targetKey = (text) => fold(text).trim().replace(/^@/, "");
|
|
39
30
|
export const documentOf = (row) => ({
|
|
@@ -79,7 +70,7 @@ export const noteRowOf = (database, reference) => {
|
|
|
79
70
|
return noteOf(database.prepare("SELECT * FROM notes WHERE id = ?").get(thing.id));
|
|
80
71
|
return undefined;
|
|
81
72
|
};
|
|
82
|
-
const thingRef = (database, type, id) => type == null || id == null ? null : (referenceOfThing(database, storedThing(type, id)) ?? null);
|
|
73
|
+
export const thingRef = (database, type, id) => type == null || id == null ? null : (referenceOfThing(database, storedThing(type, id)) ?? null);
|
|
83
74
|
export const linkOf = (database, row) => {
|
|
84
75
|
const metadata = row.metadata == null
|
|
85
76
|
? {}
|
|
@@ -218,9 +209,9 @@ export const insertLink = (context, input) => {
|
|
|
218
209
|
return linkOf(database, database.prepare("SELECT * FROM links WHERE id = ?").get(Number(row?.id)));
|
|
219
210
|
};
|
|
220
211
|
/** A folder's `accounts` row; its format lives in `settings`. */
|
|
221
|
-
const folderOf = (row) => {
|
|
212
|
+
export const folderOf = (row) => {
|
|
222
213
|
const settings = row.settings == null ? {} : JSON.parse(String(row.settings));
|
|
223
|
-
const folder = !isDocumentAccountProvider(String(row.provider));
|
|
214
|
+
const folder = !isDocumentAccountProvider(String(row.provider)) && row.provider !== ADDED_PROVIDER;
|
|
224
215
|
return {
|
|
225
216
|
id: String(row.id),
|
|
226
217
|
name: folder ? String(row.name) : `${String(row.provider)}:${String(row.external_id)}`,
|
|
@@ -247,7 +238,7 @@ const frontMatterOf = (metadata) => {
|
|
|
247
238
|
return null;
|
|
248
239
|
}
|
|
249
240
|
};
|
|
250
|
-
const pathParts = (path) => {
|
|
241
|
+
export const pathParts = (path) => {
|
|
251
242
|
const slash = path.lastIndexOf("/");
|
|
252
243
|
const fileName = slash < 0 ? path : path.slice(slash + 1);
|
|
253
244
|
const dot = fileName.lastIndexOf(".");
|
|
@@ -259,12 +250,15 @@ const pathParts = (path) => {
|
|
|
259
250
|
};
|
|
260
251
|
export const notesStoreOver = (context) => {
|
|
261
252
|
const { database, now } = context;
|
|
262
|
-
|
|
253
|
+
/** `synced`: an account a sync writes, which the added sources never are. */
|
|
254
|
+
const folderRow = (id, synced = false) => {
|
|
263
255
|
const row = /^\d+$/.test(id)
|
|
264
256
|
? database.prepare(`SELECT * FROM accounts WHERE id = ? AND ${filesAccountSql()}`).get(Number(id))
|
|
265
257
|
: undefined;
|
|
266
258
|
if (!row)
|
|
267
259
|
throw new CliError("not_found", `no notes folder ${id} in the local store`);
|
|
260
|
+
if (synced && row.provider === ADDED_PROVIDER)
|
|
261
|
+
throw new CliError("validation_error", `account ${id} holds the sources added by hand; no folder syncs it`);
|
|
268
262
|
return row;
|
|
269
263
|
};
|
|
270
264
|
const documentRow = (id) => {
|
|
@@ -313,12 +307,12 @@ export const notesStoreOver = (context) => {
|
|
|
313
307
|
if (!hash || !text.trim())
|
|
314
308
|
return [];
|
|
315
309
|
const live = database
|
|
316
|
-
.prepare(
|
|
310
|
+
.prepare(`SELECT id FROM documents d WHERE content_hash = ? AND deleted_at IS NULL AND ${notAdded} ORDER BY id`)
|
|
317
311
|
.all(hash);
|
|
318
312
|
if (live.length)
|
|
319
313
|
return live.map(({ id }) => Number(id));
|
|
320
314
|
return database
|
|
321
|
-
.prepare(
|
|
315
|
+
.prepare(`SELECT d.id FROM documents d WHERE d.content_hash = ? AND d.deleted_at IS NOT NULL AND ${notAdded} AND NOT EXISTS ` +
|
|
322
316
|
"(SELECT 1 FROM files f WHERE f.document_id = d.id AND f.deleted_at IS NULL) ORDER BY d.id")
|
|
323
317
|
.all(hash)
|
|
324
318
|
.map(({ id }) => Number(id));
|
|
@@ -347,7 +341,7 @@ export const notesStoreOver = (context) => {
|
|
|
347
341
|
if (blocking?.deleted_at == null && blocking !== undefined)
|
|
348
342
|
return;
|
|
349
343
|
if (blocking)
|
|
350
|
-
database
|
|
344
|
+
deleteDocumentRow(database, Number(blocking.id), at);
|
|
351
345
|
const { location, fileName, extension } = pathParts(path);
|
|
352
346
|
database
|
|
353
347
|
.prepare("UPDATE documents SET account_id = ?, external_id = ?, location = ?, file_name = ?, extension = ?, updated_at = ? WHERE id = ?")
|
|
@@ -387,7 +381,7 @@ export const notesStoreOver = (context) => {
|
|
|
387
381
|
.all(...DOCUMENT_ACCOUNT_PROVIDERS)
|
|
388
382
|
.map(folderOf),
|
|
389
383
|
saveFileNote: async (input) => write(database, () => {
|
|
390
|
-
const folder = Number(folderRow(input.folderId).id);
|
|
384
|
+
const folder = Number(folderRow(input.folderId, true).id);
|
|
391
385
|
const path = textOf(input.path, "a note's path", 4096);
|
|
392
386
|
const text = textOf(input.text, "a note's text", MAX_TEXT_CHARS, true);
|
|
393
387
|
const frontMatter = input.frontMatter == null ? null : JSON.stringify(input.frontMatter);
|
|
@@ -459,7 +453,7 @@ export const notesStoreOver = (context) => {
|
|
|
459
453
|
return { note: noteAt(id, folder, path), changed: !same };
|
|
460
454
|
}),
|
|
461
455
|
renameFileNote: async (folderId, from, to) => write(database, () => {
|
|
462
|
-
const folder = Number(folderRow(folderId).id);
|
|
456
|
+
const folder = Number(folderRow(folderId, true).id);
|
|
463
457
|
const target = textOf(to, "a note's path", 4096);
|
|
464
458
|
const at = now();
|
|
465
459
|
const place = fileAt(folder, from, true);
|
|
@@ -479,7 +473,7 @@ export const notesStoreOver = (context) => {
|
|
|
479
473
|
throw new CliError("validation_error", `a live note is already at ${target} in folder ${folderId}`);
|
|
480
474
|
// The gone note at the target goes, as it always did; its file row goes with it or on its own.
|
|
481
475
|
if (taken)
|
|
482
|
-
database
|
|
476
|
+
deleteDocumentRow(database, Number(taken.id), now());
|
|
483
477
|
if (takenFile)
|
|
484
478
|
database.prepare("DELETE FROM files WHERE id = ?").run(Number(takenFile.id));
|
|
485
479
|
if (place)
|
|
@@ -504,9 +498,9 @@ export const notesStoreOver = (context) => {
|
|
|
504
498
|
});
|
|
505
499
|
return noteAt(id, folder, target);
|
|
506
500
|
}),
|
|
507
|
-
dateFileNotes: async (folderId, dates) => write(database, () => dateFileNotes(database, Number(folderRow(folderId).id), dates)),
|
|
501
|
+
dateFileNotes: async (folderId, dates) => write(database, () => dateFileNotes(database, Number(folderRow(folderId, true).id), dates)),
|
|
508
502
|
deleteFileNotes: async (folderId, paths) => write(database, () => {
|
|
509
|
-
const folder = Number(folderRow(folderId).id);
|
|
503
|
+
const folder = Number(folderRow(folderId, true).id);
|
|
510
504
|
let deleted = 0;
|
|
511
505
|
for (const path of paths) {
|
|
512
506
|
const at = now();
|
|
@@ -630,6 +624,7 @@ export const notesStoreOver = (context) => {
|
|
|
630
624
|
throw new CliError("validation_error", "a file note goes when its file does; the next import notices");
|
|
631
625
|
const bare = id.replace(/^note:/, "");
|
|
632
626
|
noteRow(bare);
|
|
627
|
+
forgetEvidenceToRow(database, { type: "note", id: Number(bare) }, now());
|
|
633
628
|
database.prepare("DELETE FROM notes WHERE id = ?").run(Number(bare));
|
|
634
629
|
return { id, removed: true };
|
|
635
630
|
}),
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { type OrderDirection } from "../../domain/business.js";
|
|
1
2
|
import { type FinanceReview } from "../../domain/finance.js";
|
|
2
3
|
import { type OrderPurpose, type OrderStatus } from "../../domain/orders.js";
|
|
3
|
-
import {
|
|
4
|
+
import type { Decider } from "../decisions.js";
|
|
4
5
|
import { type ProposedByFilter } from "./actors.js";
|
|
5
|
-
import { type Party, type Reviewed, type Written } from "./finance.js";
|
|
6
6
|
import { type Author } from "./memories.js";
|
|
7
|
+
import { type MoneyLine, type MoneyLineInput } from "./money-lines.js";
|
|
7
8
|
import type { StoreContext } from "./open.js";
|
|
9
|
+
import { type Party, type Reviewed, type Written } from "./reviewed.js";
|
|
8
10
|
export interface Order extends Reviewed {
|
|
9
11
|
id: string;
|
|
10
12
|
ref: string;
|
|
@@ -28,15 +30,23 @@ export interface Order extends Reviewed {
|
|
|
28
30
|
payment: string | null;
|
|
29
31
|
/** The order's page at the shop, http or https. */
|
|
30
32
|
sourceUrl: string | null;
|
|
33
|
+
/** `sale` when the owner sells: he is then its supplier. */
|
|
34
|
+
direction: OrderDirection;
|
|
35
|
+
contract: string | null;
|
|
36
|
+
deal: string | null;
|
|
37
|
+
}
|
|
38
|
+
export interface OrderDetail extends Order {
|
|
39
|
+
lines: MoneyLine[];
|
|
31
40
|
}
|
|
32
41
|
export interface OrderInput extends Written {
|
|
33
42
|
/** `person:` or `organization:`; the owner's own person when left out. */
|
|
34
43
|
customer?: string;
|
|
35
44
|
purpose: OrderPurpose;
|
|
36
45
|
supplier?: string;
|
|
37
|
-
|
|
46
|
+
/** Required for a purchase; a sale takes the supplier's name, the owner's by default. */
|
|
47
|
+
supplierName?: string;
|
|
38
48
|
number?: string;
|
|
39
|
-
/** A decimal string in `currency`; give both or neither. */
|
|
49
|
+
/** A decimal string in `currency`; give both or neither. With lines it is their sum, refused when it differs. */
|
|
40
50
|
total?: string;
|
|
41
51
|
currency?: string;
|
|
42
52
|
/** `placed` when left out. */
|
|
@@ -47,6 +57,13 @@ export interface OrderInput extends Written {
|
|
|
47
57
|
payment?: string;
|
|
48
58
|
/** The order's page at the shop: an http or https address. */
|
|
49
59
|
sourceUrl?: string;
|
|
60
|
+
/** `purchase` when left out. A sale names its customer, and its supplier is the owner's own person by default. */
|
|
61
|
+
direction?: OrderDirection;
|
|
62
|
+
contract?: string;
|
|
63
|
+
/** The deal it came from, when no contract sits between. */
|
|
64
|
+
deal?: string;
|
|
65
|
+
/** With lines the order has a currency and totals their sum. */
|
|
66
|
+
lines?: MoneyLineInput[];
|
|
50
67
|
}
|
|
51
68
|
export interface OrderFilter {
|
|
52
69
|
status?: OrderStatus;
|
|
@@ -66,8 +83,8 @@ export interface OrderFilter {
|
|
|
66
83
|
* confirmed at once; an agent's waits as proposed for him, and only he decides it.
|
|
67
84
|
*/
|
|
68
85
|
export interface OrdersStore {
|
|
69
|
-
add(input: OrderInput): Promise<
|
|
70
|
-
get(reference: string): Promise<
|
|
86
|
+
add(input: OrderInput): Promise<OrderDetail>;
|
|
87
|
+
get(reference: string): Promise<OrderDetail>;
|
|
71
88
|
list(filter?: OrderFilter): Promise<Order[]>;
|
|
72
89
|
/** The owner moves an order on, such as a later mail saying it shipped; `date` is the delivery day. */
|
|
73
90
|
setStatus(reference: string, input: {
|
|
@@ -77,6 +94,19 @@ export interface OrdersStore {
|
|
|
77
94
|
}): Promise<Order>;
|
|
78
95
|
confirm(reference: string, by: Decider, reason?: string): Promise<Order>;
|
|
79
96
|
reject(reference: string, by: Decider, reason?: string): Promise<Order>;
|
|
97
|
+
/**
|
|
98
|
+
* Everything an add sets, again: the owner on any order, a bot only on one it proposed that is still proposed, as
|
|
99
|
+
* an import does when its source changed the order. Its lines are replaced; who wrote it and its review stay.
|
|
100
|
+
*/
|
|
101
|
+
revise(reference: string, input: OrderInput): Promise<OrderDetail>;
|
|
102
|
+
/** A line at the end; the owner changes any order's lines, a bot only those of an order it proposed still proposed. */
|
|
103
|
+
addLine(reference: string, input: MoneyLineInput & {
|
|
104
|
+
by: Author;
|
|
105
|
+
}): Promise<OrderDetail>;
|
|
106
|
+
removeLine(reference: string, input: {
|
|
107
|
+
position: number;
|
|
108
|
+
by: Author;
|
|
109
|
+
}): Promise<OrderDetail>;
|
|
80
110
|
}
|
|
81
111
|
type OrderOperations = {
|
|
82
112
|
[K in keyof OrdersStore]: OrdersStore[K] extends (...args: infer A) => Promise<infer R> ? (...args: A) => R : never;
|