@wirecat/cli-core 0.605.0 → 0.607.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +284 -1
- package/dist/calendar/index.d.ts +1 -0
- package/dist/calendar/index.js +1 -0
- package/dist/calendar/reads.js +5 -0
- package/dist/cli/app-mcp-command.d.ts +6 -2
- package/dist/cli/app-mcp-command.js +11 -2
- package/dist/cli/csv.d.ts +10 -0
- package/dist/cli/csv.js +22 -0
- package/dist/cli/early-refusal.d.ts +10 -0
- package/dist/cli/early-refusal.js +19 -0
- package/dist/cli/index.d.ts +4 -1
- package/dist/cli/index.js +4 -0
- package/dist/cli/profile-token.d.ts +29 -0
- package/dist/cli/profile-token.js +65 -0
- package/dist/cli/program.js +4 -2
- package/dist/cli/store-settings.js +38 -4
- package/dist/crm/cli/csv.d.ts +23 -0
- package/dist/crm/cli/csv.js +135 -0
- package/dist/crm/cli/index.d.ts +1 -1
- package/dist/crm/cli/index.js +36 -22
- package/dist/crm/cli/pipeline-view.d.ts +3 -0
- package/dist/crm/cli/pipeline-view.js +36 -0
- package/dist/crm/index.d.ts +2 -2
- package/dist/crm/index.js +1 -1
- package/dist/crm/source.d.ts +35 -5
- package/dist/crm/sync.d.ts +15 -7
- package/dist/crm/sync.js +169 -11
- package/dist/crm/writes.d.ts +6 -4
- package/dist/crm/writes.js +17 -13
- package/dist/documents.d.ts +2 -0
- package/dist/documents.js +2 -0
- package/dist/domain/business.d.ts +79 -0
- package/dist/domain/business.js +42 -0
- package/dist/domain/crm.d.ts +41 -2
- package/dist/domain/deals.d.ts +5 -2
- package/dist/domain/deals.js +5 -2
- package/dist/domain/finance.d.ts +2 -0
- package/dist/domain/finance.js +2 -2
- package/dist/domain/google-documents.d.ts +2 -0
- package/dist/domain/google-documents.js +3 -1
- package/dist/domain/money.d.ts +2 -0
- package/dist/domain/money.js +11 -3
- package/dist/domain/providers.d.ts +36 -1
- package/dist/domain/providers.js +69 -7
- package/dist/domain/record-fields.d.ts +42 -0
- package/dist/domain/record-fields.js +122 -0
- package/dist/domain/record-types.d.ts +489 -16
- package/dist/domain/record-types.js +374 -7
- package/dist/domain/records.d.ts +96 -5
- package/dist/domain/records.js +178 -32
- package/dist/domain/ref-summaries.js +5 -0
- package/dist/domain/references.d.ts +1 -1
- package/dist/domain/references.js +5 -0
- package/dist/domain/relations.d.ts +125 -0
- package/dist/domain/relations.js +107 -0
- package/dist/domain/vcard.d.ts +25 -0
- package/dist/domain/vcard.js +267 -0
- package/dist/entities/deal.d.ts +16 -2
- package/dist/entities/deal.js +5 -1
- package/dist/entities/index.d.ts +29 -13
- package/dist/entities/relationship.d.ts +8 -2
- package/dist/entities/relationship.js +8 -2
- package/dist/entities/task-results.d.ts +10 -10
- package/dist/errors/reasons.js +73 -2
- package/dist/fetch/addresses.d.ts +5 -0
- package/dist/fetch/addresses.js +76 -0
- package/dist/fetch/page.d.ts +64 -0
- package/dist/fetch/page.js +256 -0
- package/dist/index.d.ts +7 -4
- package/dist/index.js +6 -3
- package/dist/mail/cli/review.js +20 -6
- package/dist/mail/index.d.ts +1 -1
- package/dist/mail/marks.d.ts +10 -5
- package/dist/mail/marks.js +16 -8
- package/dist/mail/review.d.ts +5 -0
- package/dist/meetings/calendar-text.d.ts +1 -1
- package/dist/meetings/calendar-text.js +4 -0
- package/dist/meetings/event-store.d.ts +2 -2
- package/dist/search/account-list.d.ts +4 -0
- package/dist/search/account-list.js +19 -8
- package/dist/search/query.js +2 -2
- package/dist/services/events-search.js +2 -2
- package/dist/services/index.d.ts +1 -1
- package/dist/services/index.js +1 -1
- package/dist/services/ingest.d.ts +56 -8
- package/dist/services/ingest.js +95 -61
- package/dist/services/link-scorers/scan.js +2 -1
- package/dist/services/link-scorers/shared.js +2 -1
- package/dist/services/messages-search.js +3 -2
- package/dist/services/metadata.d.ts +1 -1
- package/dist/services/notes-search.js +1 -0
- package/dist/services/project-status/index.js +2 -1
- package/dist/services/proposals/kinds/contracts.d.ts +4 -0
- package/dist/services/proposals/kinds/contracts.js +9 -0
- package/dist/services/proposals/kinds/finance.d.ts +4 -0
- package/dist/services/proposals/kinds/finance.js +14 -35
- package/dist/services/proposals/kinds/orders.js +7 -30
- package/dist/services/proposals/kinds/shared.d.ts +27 -1
- package/dist/services/proposals/kinds/shared.js +34 -0
- package/dist/services/proposals/kinds/source-review.d.ts +4 -0
- package/dist/services/proposals/kinds/source-review.js +45 -0
- package/dist/services/proposals/kinds/subscriptions.d.ts +9 -0
- package/dist/services/proposals/kinds/subscriptions.js +24 -0
- package/dist/services/proposals/kinds/transactions.d.ts +4 -0
- package/dist/services/proposals/kinds/transactions.js +10 -0
- package/dist/services/proposals/registry.js +9 -0
- package/dist/services/search-all-meetings.js +2 -2
- package/dist/services/search-all.js +4 -3
- package/dist/services/search-kind.js +3 -3
- package/dist/services/stored-project-gathering.js +4 -3
- package/dist/store/decisions.js +5 -1
- package/dist/store/exports.d.ts +1 -0
- package/dist/store/exports.js +1 -0
- package/dist/store/index.d.ts +20 -4
- package/dist/store/index.js +4 -0
- package/dist/store/people.js +1 -1
- package/dist/store/project-transfer/request.d.ts +4 -1
- package/dist/store/project-transfer/schema.d.ts +9 -2
- package/dist/store/project-transfer/schema.js +4 -0
- package/dist/store/project-transfer/snapshot-model.d.ts +8 -2
- package/dist/store/source-review.d.ts +64 -0
- package/dist/store/source-review.js +7 -0
- package/dist/store/sqlite/accounts.d.ts +1 -1
- package/dist/store/sqlite/accounts.js +24 -1
- package/dist/store/sqlite/added-sources.d.ts +82 -0
- package/dist/store/sqlite/added-sources.js +237 -0
- package/dist/store/sqlite/business-contracts.d.ts +90 -0
- package/dist/store/sqlite/business-contracts.js +182 -0
- package/dist/store/sqlite/calendar-events.js +7 -3
- package/dist/store/sqlite/calendar-forget.js +3 -0
- package/dist/store/sqlite/calendar.d.ts +21 -1
- package/dist/store/sqlite/calendar.js +27 -7
- package/dist/store/sqlite/chat-rekey.js +7 -0
- package/dist/store/sqlite/chats.d.ts +3 -1
- package/dist/store/sqlite/chats.js +15 -1
- package/dist/store/sqlite/crm-rows.d.ts +44 -0
- package/dist/store/sqlite/crm-rows.js +195 -0
- package/dist/store/sqlite/crm-settings.d.ts +28 -0
- package/dist/store/sqlite/crm-settings.js +80 -0
- package/dist/store/sqlite/crm.d.ts +42 -11
- package/dist/store/sqlite/crm.js +402 -196
- package/dist/store/sqlite/deals.d.ts +4 -8
- package/dist/store/sqlite/deals.js +8 -5
- package/dist/store/sqlite/document-transfer/snapshot.js +2 -2
- package/dist/store/sqlite/emails.js +2 -0
- package/dist/store/sqlite/entity-types.d.ts +14 -9
- package/dist/store/sqlite/entity-types.js +4 -10
- package/dist/store/sqlite/evidence-signature.d.ts +114 -0
- package/dist/store/sqlite/evidence-signature.js +349 -0
- package/dist/store/sqlite/evidence-sources.d.ts +2 -1
- package/dist/store/sqlite/evidence-sources.js +7 -3
- package/dist/store/sqlite/facts.d.ts +7 -2
- package/dist/store/sqlite/facts.js +21 -5
- package/dist/store/sqlite/finance.d.ts +18 -105
- package/dist/store/sqlite/finance.js +100 -219
- package/dist/store/sqlite/involvement-queue.js +4 -3
- package/dist/store/sqlite/knowledge.d.ts +14 -4
- package/dist/store/sqlite/knowledge.js +108 -102
- package/dist/store/sqlite/labelled.d.ts +3 -0
- package/dist/store/sqlite/labelled.js +11 -0
- package/dist/store/sqlite/link-kinds.d.ts +4 -3
- package/dist/store/sqlite/link-kinds.js +6 -2
- package/dist/store/sqlite/manifest.js +7 -0
- package/dist/store/sqlite/memories.d.ts +24 -10
- package/dist/store/sqlite/memories.js +73 -19
- package/dist/store/sqlite/messages.js +2 -0
- package/dist/store/sqlite/migrations.generated.js +134 -0
- package/dist/store/sqlite/money-lines.d.ts +48 -0
- package/dist/store/sqlite/money-lines.js +76 -0
- package/dist/store/sqlite/money-transactions.d.ts +132 -0
- package/dist/store/sqlite/money-transactions.js +295 -0
- package/dist/store/sqlite/notes.d.ts +14 -4
- package/dist/store/sqlite/notes.js +43 -31
- package/dist/store/sqlite/orders.d.ts +36 -6
- package/dist/store/sqlite/orders.js +126 -46
- package/dist/store/sqlite/organizations.d.ts +8 -0
- package/dist/store/sqlite/organizations.js +14 -0
- package/dist/store/sqlite/payment-facts.js +1 -1
- package/dist/store/sqlite/person-preparation.d.ts +1 -1
- package/dist/store/sqlite/person-preparation.js +3 -2
- package/dist/store/sqlite/private-people.js +2 -0
- package/dist/store/sqlite/products.d.ts +78 -0
- package/dist/store/sqlite/products.js +131 -0
- package/dist/store/sqlite/project-transfer/records.js +39 -3
- package/dist/store/sqlite/project-transfer/snapshot.d.ts +4 -1
- package/dist/store/sqlite/project-transfer/snapshot.js +11 -3
- package/dist/store/sqlite/proposed-actions.d.ts +1 -1
- package/dist/store/sqlite/proposed-actions.js +24 -7
- package/dist/store/sqlite/providers.d.ts +0 -4
- package/dist/store/sqlite/providers.js +20 -2
- package/dist/store/sqlite/record-matching.d.ts +80 -0
- package/dist/store/sqlite/record-matching.js +264 -0
- package/dist/store/sqlite/record-projection.d.ts +89 -0
- package/dist/store/sqlite/record-projection.js +50 -0
- package/dist/store/sqlite/record-projectors-direct.d.ts +17 -0
- package/dist/store/sqlite/record-projectors-direct.js +125 -0
- package/dist/store/sqlite/record-projectors-money.d.ts +26 -0
- package/dist/store/sqlite/record-projectors-money.js +214 -0
- package/dist/store/sqlite/record-projectors-phone.d.ts +28 -0
- package/dist/store/sqlite/record-projectors-phone.js +212 -0
- package/dist/store/sqlite/record-projectors.d.ts +3 -44
- package/dist/store/sqlite/record-projectors.js +7 -67
- package/dist/store/sqlite/record-relations.d.ts +24 -0
- package/dist/store/sqlite/record-relations.js +92 -0
- package/dist/store/sqlite/record-remap.d.ts +12 -0
- package/dist/store/sqlite/record-remap.js +92 -0
- package/dist/store/sqlite/record-run.d.ts +71 -0
- package/dist/store/sqlite/record-run.js +179 -0
- package/dist/store/sqlite/record-settling.d.ts +9 -0
- package/dist/store/sqlite/record-settling.js +74 -0
- package/dist/store/sqlite/record-stamps.d.ts +13 -0
- package/dist/store/sqlite/record-stamps.js +30 -0
- package/dist/store/sqlite/record-writes.d.ts +43 -0
- package/dist/store/sqlite/record-writes.js +307 -0
- package/dist/store/sqlite/records.d.ts +52 -42
- package/dist/store/sqlite/records.js +123 -353
- package/dist/store/sqlite/ref-summaries.d.ts +6 -5
- package/dist/store/sqlite/ref-summaries.js +60 -8
- package/dist/store/sqlite/refunds.d.ts +31 -0
- package/dist/store/sqlite/refunds.js +112 -0
- package/dist/store/sqlite/relation-list.d.ts +43 -0
- package/dist/store/sqlite/relation-list.js +133 -0
- package/dist/store/sqlite/relation-proposals.d.ts +28 -6
- package/dist/store/sqlite/relation-proposals.js +134 -10
- package/dist/store/sqlite/relation-reads.d.ts +8 -0
- package/dist/store/sqlite/relation-reads.js +7 -0
- package/dist/store/sqlite/relation-roles.d.ts +26 -0
- package/dist/store/sqlite/relation-roles.js +91 -0
- package/dist/store/sqlite/reported-relations.d.ts +63 -0
- package/dist/store/sqlite/reported-relations.js +132 -0
- package/dist/store/sqlite/reviewed.d.ts +109 -0
- package/dist/store/sqlite/reviewed.js +173 -0
- package/dist/store/sqlite/schema/link-revisions.d.ts +136 -0
- package/dist/store/sqlite/schema/link-revisions.js +11 -0
- package/dist/store/sqlite/schema/links.d.ts +360 -0
- package/dist/store/sqlite/schema/links.js +32 -0
- package/dist/store/sqlite/schema.d.ts +2769 -306
- package/dist/store/sqlite/schema.js +258 -25
- package/dist/store/sqlite/source-review-marks.d.ts +54 -0
- package/dist/store/sqlite/source-review-marks.js +143 -0
- package/dist/store/sqlite/source-review.d.ts +54 -0
- package/dist/store/sqlite/source-review.js +221 -0
- package/dist/store/sqlite/stage-amounts.d.ts +14 -0
- package/dist/store/sqlite/stage-amounts.js +23 -0
- package/dist/store/sqlite/subscriptions.d.ts +135 -0
- package/dist/store/sqlite/subscriptions.js +294 -0
- package/dist/store/sqlite/text-input.d.ts +3 -0
- package/dist/store/sqlite/text-input.js +13 -0
- package/dist/store/sqlite/things.js +2 -9
- package/dist/store/sqlite/transaction.d.ts +15 -0
- package/dist/store/sqlite/transaction.js +57 -0
- package/dist/store/sqlite/wallets.d.ts +88 -0
- package/dist/store/sqlite/wallets.js +143 -0
- package/dist/store/store-errors.d.ts +1 -0
- package/dist/store/store-errors.js +45 -0
- package/dist/store/store.d.ts +19 -1
- package/dist/store/store.js +35 -6
- package/package.json +6 -3
- package/schemas/examples/records-business.jsonl +11 -0
- package/schemas/records-envelope.v1.schema.json +1160 -5
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { CliError } from "@wirecat/cli-base";
|
|
2
|
+
import { assertMayDecide, deciderAuthor } from "../decisions.js";
|
|
3
|
+
import { SOURCE_REVIEW_KIND } from "../source-review.js";
|
|
4
|
+
import { evidenceSignature, resignEvidence, sourceHolds, storedEvidenceMetadata, } from "./evidence-signature.js";
|
|
5
|
+
import { linkKind } from "./link-kinds.js";
|
|
6
|
+
import { authorActor } from "./memories.js";
|
|
7
|
+
import { requiredThing } from "./notes.js";
|
|
8
|
+
import { proposalOperationsOver, proposerOfAction } from "./proposed-actions.js";
|
|
9
|
+
import { asReviewTarget, closeSourceReviews, holdingSql, holds, raiseSourceReview, sourceChecker, } from "./source-review-marks.js";
|
|
10
|
+
import { referenceOfThing, storedThing } from "./things.js";
|
|
11
|
+
import { write } from "./transaction.js";
|
|
12
|
+
const TABLES = { memory: "memories", fact: "facts" };
|
|
13
|
+
const RETIRED = new Set(["superseded", "reversed", "rejected", "stale"]);
|
|
14
|
+
const DEFAULT_LIMIT = 500;
|
|
15
|
+
const CURSOR = "source-check.cursor";
|
|
16
|
+
const SCAN_FACTOR = 20;
|
|
17
|
+
/**
|
|
18
|
+
* Evidence links of memories and facts in id order, from a cursor. The unary `+` keeps SQLite off the `from_type`
|
|
19
|
+
* index, which would read every memory's and fact's links and sort them; the primary key stops at `limit`.
|
|
20
|
+
*/
|
|
21
|
+
export const SOURCE_WALK = "SELECT id, from_type, from_id, to_type, to_id, metadata, created_at FROM links WHERE id > ? AND id <= ? " +
|
|
22
|
+
`AND kind = ${linkKind("evidence")} AND +from_type IN ('memory', 'fact') AND to_id IS NOT NULL ORDER BY id LIMIT ?`;
|
|
23
|
+
const iso = (at) => new Date(at).toISOString();
|
|
24
|
+
const textLimit = (value, what, reason) => {
|
|
25
|
+
const text = typeof value === "string" ? value.trim() : "";
|
|
26
|
+
if (!text || text.length > 2000)
|
|
27
|
+
throw new CliError("validation_error", `${what} takes 1–2000 characters`, { reason });
|
|
28
|
+
return text;
|
|
29
|
+
};
|
|
30
|
+
export const sourceReviewOver = (context) => {
|
|
31
|
+
const { database, now } = context;
|
|
32
|
+
const operations = proposalOperationsOver(context);
|
|
33
|
+
const statusOf = (target) => {
|
|
34
|
+
const row = database.prepare(`SELECT status, deleted_at FROM ${TABLES[target.type]} WHERE id = ?`).get(target.id);
|
|
35
|
+
return row && row.deleted_at == null ? String(row.status) : undefined;
|
|
36
|
+
};
|
|
37
|
+
/** The newest fact or memory that replaced this one, following `supersedes` while each replacement holds. */
|
|
38
|
+
const replacementOf = (target) => {
|
|
39
|
+
const next = database.prepare(`SELECT id FROM ${TABLES[target.type]} t WHERE t.supersedes_id = ? AND ${holdingSql("t", target.type)} ` +
|
|
40
|
+
"ORDER BY t.id DESC LIMIT 1");
|
|
41
|
+
let current = target.id;
|
|
42
|
+
for (let hop = 0; hop < 20; hop++) {
|
|
43
|
+
const found = next.get(current);
|
|
44
|
+
if (!found)
|
|
45
|
+
break;
|
|
46
|
+
current = Number(found.id);
|
|
47
|
+
}
|
|
48
|
+
return current === target.id ? undefined : `${target.type}:${current}`;
|
|
49
|
+
};
|
|
50
|
+
/** What one evidence link says now: nothing new, a silent signature, or a cause for review. */
|
|
51
|
+
const judged = (link, thing, now) => {
|
|
52
|
+
const stored = storedEvidenceMetadata(link.metadata);
|
|
53
|
+
const cited = asReviewTarget(thing.type, thing.id);
|
|
54
|
+
const raise = (cause) => {
|
|
55
|
+
const replacement = cause === "outdated" && cited ? replacementOf(cited) : undefined;
|
|
56
|
+
return {
|
|
57
|
+
outcome: "raised",
|
|
58
|
+
source: {
|
|
59
|
+
link: Number(link.id),
|
|
60
|
+
ref: referenceOfThing(database, thing) ?? null,
|
|
61
|
+
cause,
|
|
62
|
+
citedRev: stored?.cited_rev ?? null,
|
|
63
|
+
...(replacement === undefined ? {} : { replacement }),
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
const retired = cited !== undefined && RETIRED.has(statusOf(cited) ?? "");
|
|
68
|
+
if (stored === undefined) {
|
|
69
|
+
const since = (at) => at !== null && at > Number(link.created_at);
|
|
70
|
+
if (now.deleted && (now.changedAt === null || since(now.changedAt)))
|
|
71
|
+
return raise("deleted");
|
|
72
|
+
if (since(now.changedAt)) {
|
|
73
|
+
if (cited === undefined)
|
|
74
|
+
return raise("changed");
|
|
75
|
+
if (retired)
|
|
76
|
+
return raise("outdated");
|
|
77
|
+
}
|
|
78
|
+
return { outcome: "baselined" };
|
|
79
|
+
}
|
|
80
|
+
if (stored.sig === now.sig)
|
|
81
|
+
return { outcome: "same" };
|
|
82
|
+
if (now.deleted)
|
|
83
|
+
return raise("deleted");
|
|
84
|
+
if (cited !== undefined)
|
|
85
|
+
return retired ? raise("outdated") : { outcome: "refreshed" };
|
|
86
|
+
if (stored.quote !== undefined && sourceHolds(database, thing, stored.quote) === true)
|
|
87
|
+
return { outcome: "refreshed" };
|
|
88
|
+
return raise("changed");
|
|
89
|
+
};
|
|
90
|
+
const evidenceLinksOf = (target) => database
|
|
91
|
+
.prepare(`SELECT id, to_type, to_id, metadata FROM links WHERE from_type = ? AND from_id = ? AND kind = ${linkKind("evidence")} ` +
|
|
92
|
+
"AND to_id IS NOT NULL")
|
|
93
|
+
.all(target.type, target.id);
|
|
94
|
+
const rebaseline = (target, at) => {
|
|
95
|
+
for (const link of evidenceLinksOf(target))
|
|
96
|
+
resignEvidence(database, Number(link.id), evidenceSignature(database, storedThing(link.to_type, link.to_id)), at, storedEvidenceMetadata(link.metadata));
|
|
97
|
+
};
|
|
98
|
+
const cursor = () => Number(database.prepare("SELECT value FROM store_settings WHERE key = ?").get(CURSOR)?.value ?? 0);
|
|
99
|
+
/**
|
|
100
|
+
* The next evidence links after the cursor, at most `limit`, read from a window of `SCAN_FACTOR * limit` link ids
|
|
101
|
+
* so a store with few evidence links among many others holds the write lock for a bounded time. Returns the cursor
|
|
102
|
+
* the next run starts from: after the last link taken, the window's end, or `0` once the window passed the last link.
|
|
103
|
+
*/
|
|
104
|
+
const nextLinks = (limit) => {
|
|
105
|
+
const from = cursor();
|
|
106
|
+
const to = from + SCAN_FACTOR * limit;
|
|
107
|
+
const links = database.prepare(SOURCE_WALK).all(from, to, limit);
|
|
108
|
+
if (links.length === limit)
|
|
109
|
+
return { links, next: Number(links.at(-1)?.id) };
|
|
110
|
+
const highest = Number(database.prepare("SELECT max(id) AS id FROM links").get()?.id ?? 0);
|
|
111
|
+
return { links, next: to >= highest ? 0 : to };
|
|
112
|
+
};
|
|
113
|
+
return {
|
|
114
|
+
checkSources: ({ limit = DEFAULT_LIMIT } = {}) => {
|
|
115
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 10_000)
|
|
116
|
+
throw new CliError("validation_error", "a source check takes 1–10000 evidence links");
|
|
117
|
+
return write(database, () => {
|
|
118
|
+
const at = now();
|
|
119
|
+
const counts = { checked: 0, baselined: 0, raised: 0, refreshed: 0, closed: 0 };
|
|
120
|
+
const raised = new Map();
|
|
121
|
+
const retired = new Map();
|
|
122
|
+
const { links, next } = nextLinks(limit);
|
|
123
|
+
for (const link of links) {
|
|
124
|
+
counts.checked++;
|
|
125
|
+
const target = asReviewTarget(link.from_type, link.from_id);
|
|
126
|
+
const key = `${target.type}:${target.id}`;
|
|
127
|
+
if (!holds(database, target)) {
|
|
128
|
+
retired.set(key, target);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const thing = storedThing(link.to_type, link.to_id);
|
|
132
|
+
const signature = evidenceSignature(database, thing);
|
|
133
|
+
const verdict = judged(link, thing, signature);
|
|
134
|
+
if (verdict.outcome === "same")
|
|
135
|
+
continue;
|
|
136
|
+
resignEvidence(database, Number(link.id), signature, at, storedEvidenceMetadata(link.metadata));
|
|
137
|
+
if (verdict.outcome === "baselined")
|
|
138
|
+
counts.baselined++;
|
|
139
|
+
else if (verdict.outcome === "refreshed")
|
|
140
|
+
counts.refreshed++;
|
|
141
|
+
else if (verdict.outcome === "raised") {
|
|
142
|
+
const entry = raised.get(key) ?? { target, sources: [] };
|
|
143
|
+
entry.sources.push(verdict.source);
|
|
144
|
+
raised.set(key, entry);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
const proposer = sourceChecker(database, at);
|
|
148
|
+
for (const { target, sources } of raised.values())
|
|
149
|
+
if (raiseSourceReview(database, target, { sources, proposer, at }).changed)
|
|
150
|
+
counts.raised++;
|
|
151
|
+
for (const target of retired.values())
|
|
152
|
+
counts.closed += closeSourceReviews(database, target, { actor: proposer, at, why: "no-longer-holds" });
|
|
153
|
+
database
|
|
154
|
+
.prepare("INSERT OR REPLACE INTO store_settings (key, value, at) VALUES (?, ?, ?)")
|
|
155
|
+
.run(CURSOR, String(next), at);
|
|
156
|
+
return counts;
|
|
157
|
+
});
|
|
158
|
+
},
|
|
159
|
+
challenge: (reference, input) => {
|
|
160
|
+
const reason = textLimit(input.reason, "a challenge's reason", "source-review.reason-required");
|
|
161
|
+
const suggestion = input.suggestion === undefined
|
|
162
|
+
? undefined
|
|
163
|
+
: textLimit(input.suggestion, "a suggestion", "source-review.suggestion-length");
|
|
164
|
+
if (!Array.isArray(input.evidence) || input.evidence.length === 0 || input.evidence.length > 20)
|
|
165
|
+
throw new CliError("validation_error", "a challenge cites 1–20 references that contradict it", {
|
|
166
|
+
reason: "source-review.evidence-required",
|
|
167
|
+
});
|
|
168
|
+
return write(database, () => {
|
|
169
|
+
const thing = requiredThing(database, reference, "the memory or fact");
|
|
170
|
+
const target = asReviewTarget(thing.type, thing.id);
|
|
171
|
+
if (!target)
|
|
172
|
+
throw new CliError("validation_error", `${reference} is not a memory or a fact`);
|
|
173
|
+
if (!holds(database, target))
|
|
174
|
+
throw new CliError("validation_error", `${reference} no longer holds; there is nothing to challenge`, {
|
|
175
|
+
reason: "source-review.not-holding",
|
|
176
|
+
});
|
|
177
|
+
const evidence = input.evidence.map((ref) => referenceOfThing(database, requiredThing(database, ref, "the evidence")) ?? ref);
|
|
178
|
+
const at = now();
|
|
179
|
+
const author = input.by === "owner" ? "owner" : input.by.bot;
|
|
180
|
+
const { id } = raiseSourceReview(database, target, {
|
|
181
|
+
challenge: { evidence, reason, by: author, at: iso(at) },
|
|
182
|
+
...(suggestion === undefined ? {} : { suggestion: { author, body: suggestion, evidence, at: iso(at) } }),
|
|
183
|
+
proposer: authorActor(context, input.by).actor,
|
|
184
|
+
at,
|
|
185
|
+
});
|
|
186
|
+
return operations.get(id);
|
|
187
|
+
});
|
|
188
|
+
},
|
|
189
|
+
decide: (id, decision) => write(database, () => {
|
|
190
|
+
const proposal = operations.get(id);
|
|
191
|
+
if (proposal.kind !== SOURCE_REVIEW_KIND)
|
|
192
|
+
throw new CliError("validation_error", `${id} is a ${proposal.kind} proposal, not a source review`);
|
|
193
|
+
assertMayDecide({ id, kind: SOURCE_REVIEW_KIND, ownerOnly: true, proposedBy: proposerOfAction(proposal) }, decision.by, context.decisions);
|
|
194
|
+
const row = database.prepare("SELECT target_type, target_id FROM proposed_actions WHERE id = ?").get(Number(id));
|
|
195
|
+
const target = row && asReviewTarget(row.target_type, row.target_id);
|
|
196
|
+
const author = deciderAuthor(decision.by);
|
|
197
|
+
const reason = decision.reason?.trim() || undefined;
|
|
198
|
+
const result = { ...(reason === undefined ? {} : { reason }) };
|
|
199
|
+
const decided = decision.confirm
|
|
200
|
+
? operations.approve(id, { stillTrue: true, ...result }, author)
|
|
201
|
+
: operations.reject(id, author, { dismissed: true, ...result });
|
|
202
|
+
if (target) {
|
|
203
|
+
const at = now();
|
|
204
|
+
rebaseline(target, at);
|
|
205
|
+
if (decision.confirm && target.type === "memory")
|
|
206
|
+
database
|
|
207
|
+
.prepare("UPDATE memories SET last_verified_at = ?, updated_at = ? WHERE id = ?")
|
|
208
|
+
.run(at, at, target.id);
|
|
209
|
+
}
|
|
210
|
+
return decided;
|
|
211
|
+
}),
|
|
212
|
+
};
|
|
213
|
+
};
|
|
214
|
+
export const sourceReviewStoreOver = (context) => {
|
|
215
|
+
const sourceReview = sourceReviewOver(context);
|
|
216
|
+
return {
|
|
217
|
+
checkSources: async (options) => sourceReview.checkSources(options),
|
|
218
|
+
challenge: async (reference, input) => sourceReview.challenge(reference, input),
|
|
219
|
+
decideSourceReview: async (id, decision) => sourceReview.decide(id, decision),
|
|
220
|
+
};
|
|
221
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { DealKind } from "../../domain/deals.js";
|
|
2
|
+
import { type Money } from "../../domain/money.js";
|
|
3
|
+
import type { StoreContext } from "./open.js";
|
|
4
|
+
export interface StageAmount extends Money {
|
|
5
|
+
items: number;
|
|
6
|
+
}
|
|
7
|
+
export interface StageAmounts {
|
|
8
|
+
/** Every deal or lead standing in it now, with an amount or without. */
|
|
9
|
+
items: number;
|
|
10
|
+
/** One per currency, never converted; an item with no amount adds none. */
|
|
11
|
+
amounts: StageAmount[];
|
|
12
|
+
}
|
|
13
|
+
/** What stands in each stage of `stageIds`, in that order: `cutoff` (ms) keeps only rows made since then. */
|
|
14
|
+
export declare const stageAmountsOf: ({ database }: StoreContext, kind: DealKind, stageIds: number[], cutoff: number | null) => StageAmounts[];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { formatDecimal, scaleOf } from "../../domain/money.js";
|
|
2
|
+
const TABLES = { deal: "deals", lead: "leads" };
|
|
3
|
+
/** What stands in each stage of `stageIds`, in that order: `cutoff` (ms) keeps only rows made since then. */
|
|
4
|
+
export const stageAmountsOf = ({ database }, kind, stageIds, cutoff) => {
|
|
5
|
+
const rows = database
|
|
6
|
+
.prepare("SELECT stage_id, currency, count(*) AS items, coalesce(sum(amount), 0) AS units " +
|
|
7
|
+
`FROM ${TABLES[kind]} WHERE stage_id IN (${stageIds.map(() => "?").join(", ") || "NULL"}) ` +
|
|
8
|
+
"AND deleted_at IS NULL AND (? IS NULL OR created_at >= ?) GROUP BY stage_id, currency ORDER BY currency")
|
|
9
|
+
.all(...stageIds, cutoff, cutoff);
|
|
10
|
+
return stageIds.map((stage) => {
|
|
11
|
+
const within = rows.filter((row) => Number(row.stage_id) === stage);
|
|
12
|
+
return {
|
|
13
|
+
items: within.reduce((all, row) => all + Number(row.items), 0),
|
|
14
|
+
amounts: within
|
|
15
|
+
.filter((row) => row.currency != null)
|
|
16
|
+
.map((row) => ({
|
|
17
|
+
amount: formatDecimal(BigInt(row.units), scaleOf(String(row.currency))),
|
|
18
|
+
currency: String(row.currency),
|
|
19
|
+
items: Number(row.items),
|
|
20
|
+
})),
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { type Interval, type SubscriptionStatus } from "../../domain/business.js";
|
|
2
|
+
import { type FinanceReview, type InvoiceDirection } from "../../domain/finance.js";
|
|
3
|
+
import { type Money } from "../../domain/money.js";
|
|
4
|
+
import type { Decider } from "../decisions.js";
|
|
5
|
+
import { type ProposedByFilter } from "./actors.js";
|
|
6
|
+
import { type Author } from "./memories.js";
|
|
7
|
+
import type { StoreContext } from "./open.js";
|
|
8
|
+
import { type AuthorInfo, type Party, type Reviewed, type Written } from "./reviewed.js";
|
|
9
|
+
export interface SubscriptionPrice extends Money {
|
|
10
|
+
id: string;
|
|
11
|
+
/** `subscription_price:<id>`: its own proposal, never changed once written. */
|
|
12
|
+
ref: string;
|
|
13
|
+
/** One line, as the review inbox prints it. */
|
|
14
|
+
title: string;
|
|
15
|
+
subscription: string;
|
|
16
|
+
/** `YYYY-MM-DD`: the first day it is billed. */
|
|
17
|
+
effectiveFrom: string;
|
|
18
|
+
review: FinanceReview;
|
|
19
|
+
author: AuthorInfo;
|
|
20
|
+
reason: string | null;
|
|
21
|
+
/** Always `null`: a price belongs to its subscription's project. */
|
|
22
|
+
project: null;
|
|
23
|
+
createdAt: string;
|
|
24
|
+
}
|
|
25
|
+
export interface Subscription extends Reviewed {
|
|
26
|
+
id: string;
|
|
27
|
+
ref: string;
|
|
28
|
+
/** One line, as a list or the review inbox prints it. */
|
|
29
|
+
title: string;
|
|
30
|
+
name: string;
|
|
31
|
+
/** `receivable` when the owner bills it, `payable` when he pays it. */
|
|
32
|
+
direction: InvoiceDirection;
|
|
33
|
+
supplier: Party;
|
|
34
|
+
customer: Party;
|
|
35
|
+
product: string | null;
|
|
36
|
+
contract: string | null;
|
|
37
|
+
status: SubscriptionStatus;
|
|
38
|
+
interval: Interval;
|
|
39
|
+
/** `YYYY-MM-DD`. */
|
|
40
|
+
startDate: string;
|
|
41
|
+
trialEndDate: string | null;
|
|
42
|
+
nextRenewalDate: string | null;
|
|
43
|
+
cancelledDate: string | null;
|
|
44
|
+
endDate: string | null;
|
|
45
|
+
/** The confirmed price with the latest start up to today; `null` before one is confirmed. */
|
|
46
|
+
currentPrice: SubscriptionPrice | null;
|
|
47
|
+
}
|
|
48
|
+
export interface SubscriptionDetail extends Subscription {
|
|
49
|
+
/** Every price, oldest start first, rejected ones too: history is never overwritten. */
|
|
50
|
+
prices: SubscriptionPrice[];
|
|
51
|
+
}
|
|
52
|
+
export interface PriceInput {
|
|
53
|
+
/** A decimal string in `currency`. */
|
|
54
|
+
amount: string;
|
|
55
|
+
currency: string;
|
|
56
|
+
/** `YYYY-MM-DD`; the subscription's start when left out. */
|
|
57
|
+
effectiveFrom?: string;
|
|
58
|
+
}
|
|
59
|
+
export interface SubscriptionFields {
|
|
60
|
+
name?: string;
|
|
61
|
+
direction?: InvoiceDirection;
|
|
62
|
+
/** The owner's own person when left out of a receivable one. */
|
|
63
|
+
supplier?: string;
|
|
64
|
+
/** The owner's own person when left out of a payable one. */
|
|
65
|
+
customer?: string;
|
|
66
|
+
/** `null` takes it off. */
|
|
67
|
+
product?: string | null;
|
|
68
|
+
contract?: string | null;
|
|
69
|
+
/** `trial` when a trial end is given, else `active`. */
|
|
70
|
+
status?: SubscriptionStatus;
|
|
71
|
+
/** A count and a unit, such as 1-month, as `--every` takes it. */
|
|
72
|
+
every?: string;
|
|
73
|
+
startDate?: string;
|
|
74
|
+
trialEndDate?: string | null;
|
|
75
|
+
nextRenewalDate?: string | null;
|
|
76
|
+
/** Today when it is cancelled and no day is given. */
|
|
77
|
+
cancelledDate?: string | null;
|
|
78
|
+
endDate?: string | null;
|
|
79
|
+
}
|
|
80
|
+
export interface SubscriptionInput extends Written, SubscriptionFields {
|
|
81
|
+
direction: InvoiceDirection;
|
|
82
|
+
every: string;
|
|
83
|
+
startDate: string;
|
|
84
|
+
/** Its prices, proposed or confirmed with the subscription. */
|
|
85
|
+
prices?: PriceInput[];
|
|
86
|
+
}
|
|
87
|
+
export interface SubscriptionUpdate extends SubscriptionFields {
|
|
88
|
+
by: Author;
|
|
89
|
+
}
|
|
90
|
+
export interface SubscriptionFilter {
|
|
91
|
+
status?: SubscriptionStatus;
|
|
92
|
+
direction?: InvoiceDirection;
|
|
93
|
+
supplier?: string;
|
|
94
|
+
customer?: string;
|
|
95
|
+
project?: string;
|
|
96
|
+
review?: FinanceReview;
|
|
97
|
+
proposedBy?: ProposedByFilter;
|
|
98
|
+
/** `YYYY-MM-DD`: only those that renew on or before it. */
|
|
99
|
+
renewsBy?: string;
|
|
100
|
+
/** 1–500, 100 by default; newest written first. */
|
|
101
|
+
limit?: number;
|
|
102
|
+
}
|
|
103
|
+
export interface PriceFilter {
|
|
104
|
+
review?: FinanceReview;
|
|
105
|
+
proposedBy?: ProposedByFilter;
|
|
106
|
+
/** 1–500, 100 by default; newest written first. */
|
|
107
|
+
limit?: number;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Subscriptions the owner bills or pays, and their prices. A price is a row of its own and never changes. Confirming a
|
|
111
|
+
* subscription confirms the prices proposed with it; a later price is a proposal of its own, and the subscription
|
|
112
|
+
* stays confirmed. A confirmed price is never rejected, and a day holds one live price, so a wrong confirmed price is
|
|
113
|
+
* corrected by a new price from a later day.
|
|
114
|
+
*/
|
|
115
|
+
export interface SubscriptionsStore {
|
|
116
|
+
add(input: SubscriptionInput): Promise<SubscriptionDetail>;
|
|
117
|
+
get(reference: string): Promise<SubscriptionDetail>;
|
|
118
|
+
list(filter?: SubscriptionFilter): Promise<Subscription[]>;
|
|
119
|
+
/** The owner changes any; a bot only one it proposed that is still proposed. */
|
|
120
|
+
update(reference: string, input: SubscriptionUpdate): Promise<SubscriptionDetail>;
|
|
121
|
+
addPrice(reference: string, input: PriceInput & Omit<Written, "project" | "evidence">): Promise<SubscriptionPrice>;
|
|
122
|
+
/** `subscription:<id>` or `subscription_price:<id>`. */
|
|
123
|
+
confirm(reference: string, by: Decider, reason?: string): Promise<SubscriptionDetail>;
|
|
124
|
+
reject(reference: string, by: Decider, reason?: string): Promise<SubscriptionDetail>;
|
|
125
|
+
/** `subscription_price:<id>`. */
|
|
126
|
+
price(reference: string): Promise<SubscriptionPrice>;
|
|
127
|
+
/** Proposed or decided prices, newest written first, as the review inbox lists them. */
|
|
128
|
+
prices(filter?: PriceFilter): Promise<SubscriptionPrice[]>;
|
|
129
|
+
}
|
|
130
|
+
type SubscriptionOperations = {
|
|
131
|
+
[K in keyof SubscriptionsStore]: SubscriptionsStore[K] extends (...args: infer A) => Promise<infer R> ? (...args: A) => R : never;
|
|
132
|
+
};
|
|
133
|
+
export declare const subscriptionOperationsOver: (context: StoreContext) => SubscriptionOperations;
|
|
134
|
+
export declare const subscriptionsStoreOver: (context: StoreContext) => SubscriptionsStore;
|
|
135
|
+
export {};
|