@wirecat/cli-core 0.675.0 → 0.676.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 +88 -0
- package/dist/background/daily-archive-timer.d.ts +1 -1
- package/dist/calendar/mcp/index.d.ts +12 -12
- package/dist/cli/documents/words.d.ts +1 -1
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/list-query.d.ts +3 -2
- package/dist/cli/list-query.js +5 -2
- package/dist/cli/setup-command.js +9 -3
- package/dist/crm/cli/index.d.ts +7 -1
- package/dist/crm/cli/index.js +104 -20
- package/dist/crm/cli/pipeline-view.js +15 -1
- package/dist/crm/cli/records.js +11 -3
- package/dist/crm/cli/search-all.d.ts +5 -2
- package/dist/crm/cli/search-all.js +15 -3
- package/dist/crm/source.d.ts +3 -2
- package/dist/crm/stage-tasks.js +1 -1
- package/dist/crm/sync.js +13 -11
- package/dist/crm/testing/write-contract.js +5 -1
- package/dist/crm/viewer.d.ts +28 -0
- package/dist/crm/viewer.js +63 -0
- package/dist/crm/writer.d.ts +3 -0
- package/dist/crm/writes.d.ts +7 -2
- package/dist/crm/writes.js +24 -14
- package/dist/domain/crm.d.ts +9 -9
- package/dist/domain/moment.d.ts +2 -0
- package/dist/domain/moment.js +2 -0
- package/dist/entities/business-context.js +1 -1
- package/dist/entities/deal.d.ts +86 -39
- package/dist/entities/deal.js +45 -4
- package/dist/entities/index.d.ts +20 -32
- package/dist/errors/reasons.js +1 -0
- package/dist/meetings/mcp/index.d.ts +15 -15
- package/dist/services/search-all.d.ts +3 -1
- package/dist/services/search-all.js +1 -0
- package/dist/setup/define.d.ts +17 -2
- package/dist/store/exports.d.ts +1 -0
- package/dist/store/exports.js +1 -0
- package/dist/store/sqlite/copy-viewer.d.ts +16 -0
- package/dist/store/sqlite/copy-viewer.js +11 -0
- package/dist/store/sqlite/crm-activities.d.ts +7 -1
- package/dist/store/sqlite/crm-activities.js +32 -5
- package/dist/store/sqlite/crm-contacts.d.ts +5 -2
- package/dist/store/sqlite/crm-contacts.js +30 -14
- package/dist/store/sqlite/crm-rows.d.ts +7 -0
- package/dist/store/sqlite/crm-rows.js +37 -5
- package/dist/store/sqlite/crm-settings.d.ts +2 -1
- package/dist/store/sqlite/crm-settings.js +3 -2
- package/dist/store/sqlite/crm-stage-entries.d.ts +24 -4
- package/dist/store/sqlite/crm-stage-entries.js +77 -18
- package/dist/store/sqlite/crm.d.ts +14 -4
- package/dist/store/sqlite/crm.js +59 -7
- package/dist/store/sqlite/deals.d.ts +47 -29
- package/dist/store/sqlite/deals.js +64 -76
- package/dist/store/sqlite/pipeline-period.d.ts +85 -0
- package/dist/store/sqlite/pipeline-period.js +199 -0
- package/dist/store/sqlite/stage-amounts.d.ts +6 -2
- package/dist/store/sqlite/stage-amounts.js +8 -5
- package/package.json +3 -3
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { zonedMoment } from "@wirecat/cli-base/query";
|
|
2
|
+
import { dayIn } from "../../domain/moment.js";
|
|
3
|
+
import { formatDecimal, scaleOf } from "../../domain/money.js";
|
|
4
|
+
import { viewerSql } from "./copy-viewer.js";
|
|
5
|
+
import { stageEntriesOf } from "./crm-stage-entries.js";
|
|
6
|
+
import { refuse } from "./reviewed.js";
|
|
7
|
+
import { stageAmountsOf } from "./stage-amounts.js";
|
|
8
|
+
const TABLES = { deal: "deals", lead: "leads" };
|
|
9
|
+
const DAY = 86_400_000;
|
|
10
|
+
const LOST_REASONS = 10;
|
|
11
|
+
/** A column between the bounds, each inclusive; no bound, no condition. */
|
|
12
|
+
export const bounded = (column, bounds) => [
|
|
13
|
+
...(bounds?.since === undefined ? [] : [{ sql: `${column} >= ?`, params: [bounds.since] }]),
|
|
14
|
+
...(bounds?.until === undefined ? [] : [{ sql: `${column} <= ?`, params: [bounds.until] }]),
|
|
15
|
+
];
|
|
16
|
+
/** A window's bound, a `YYYY-MM-DD` day in `zone` (its start, or its end for `end`) or an ISO time, as epoch ms. */
|
|
17
|
+
export const boundOf = (value, what, zone, end = false) => {
|
|
18
|
+
if (value === undefined)
|
|
19
|
+
return null;
|
|
20
|
+
const typed = value.trim();
|
|
21
|
+
try {
|
|
22
|
+
return zonedMoment(typed, zone, { start: 0, end: typed.length }, { end });
|
|
23
|
+
}
|
|
24
|
+
catch {
|
|
25
|
+
return refuse(`${what} is a YYYY-MM-DD day or an ISO time, not ${value}`, "deal.field");
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
/** A stage the CRM removed stays in a pipeline only while a row stands in it (BUG-7). */
|
|
29
|
+
export const SHOWN_STAGE = "(s.deleted_at IS NULL OR EXISTS (SELECT 1 FROM deals d WHERE d.stage_id = s.id AND d.deleted_at IS NULL) " +
|
|
30
|
+
"OR EXISTS (SELECT 1 FROM leads l WHERE l.stage_id = s.id AND l.deleted_at IS NULL))";
|
|
31
|
+
/** Each stage of these pipelines that is shown, with how many open deals and leads the viewer may read stand in it. */
|
|
32
|
+
export const openPerStage = (database, pipelineIds, viewer) => {
|
|
33
|
+
const deals = viewerSql("d", viewer);
|
|
34
|
+
const leads = viewerSql("l", viewer);
|
|
35
|
+
return database
|
|
36
|
+
.prepare(`SELECT s.*, (SELECT count(*) FROM deals d WHERE d.stage_id = s.id AND d.status = 'open' AND d.deleted_at IS NULL AND ${deals.sql}) + ` +
|
|
37
|
+
`(SELECT count(*) FROM leads l WHERE l.stage_id = s.id AND l.status = 'open' AND l.deleted_at IS NULL AND ${leads.sql}) AS open ` +
|
|
38
|
+
`FROM pipeline_stages s WHERE s.pipeline_id IN (${pipelineIds.map(() => "?").join(", ") || "NULL"}) ` +
|
|
39
|
+
`AND ${SHOWN_STAGE} ORDER BY s.pipeline_id, s.position, s.id`)
|
|
40
|
+
.all(...deals.params, ...leads.params, ...pipelineIds);
|
|
41
|
+
};
|
|
42
|
+
const share = (part, whole) => (whole === 0 ? null : Math.round((part / whole) * 1000) / 1000);
|
|
43
|
+
const median = (values) => {
|
|
44
|
+
if (values.length === 0)
|
|
45
|
+
return null;
|
|
46
|
+
const sorted = [...values].sort((a, b) => a - b);
|
|
47
|
+
const middle = Math.floor(sorted.length / 2);
|
|
48
|
+
const value = sorted.length % 2 ? sorted[middle] : (sorted[middle - 1] + sorted[middle]) / 2;
|
|
49
|
+
return Math.round((value / DAY) * 10) / 10;
|
|
50
|
+
};
|
|
51
|
+
const rate = (won, lost) => share(won, won + lost);
|
|
52
|
+
const itemsOf = (database, kind, pipelineId, viewer) => {
|
|
53
|
+
const seen = viewerSql("d", viewer);
|
|
54
|
+
return database
|
|
55
|
+
.prepare("SELECT d.id, d.status, d.stage_id, d.created_at, d.closed_date, d.currency, d.amount, " +
|
|
56
|
+
"json_extract(d.metadata, '$.assignee') AS assignee, json_extract(d.metadata, '$.closed_reason') AS reason " +
|
|
57
|
+
`FROM ${TABLES[kind]} d JOIN pipeline_stages s ON s.id = d.stage_id WHERE s.pipeline_id = ? AND d.deleted_at IS NULL AND ${seen.sql}`)
|
|
58
|
+
.all(pipelineId, ...seen.params)
|
|
59
|
+
.map((row) => ({
|
|
60
|
+
id: Number(row.id),
|
|
61
|
+
status: String(row.status),
|
|
62
|
+
stageId: Number(row.stage_id),
|
|
63
|
+
createdAt: Number(row.created_at),
|
|
64
|
+
closedDate: row.closed_date == null ? null : String(row.closed_date),
|
|
65
|
+
assignee: row.assignee == null ? null : String(row.assignee),
|
|
66
|
+
reason: row.reason == null ? null : String(row.reason),
|
|
67
|
+
currency: row.currency == null ? null : String(row.currency),
|
|
68
|
+
amount: row.amount ?? null,
|
|
69
|
+
}));
|
|
70
|
+
};
|
|
71
|
+
/** The CRM user's name as its identity holds it; its ids are `<account>/user/<id>`. */
|
|
72
|
+
const userName = (database, provider, assignee) => {
|
|
73
|
+
if (provider === null || assignee === null)
|
|
74
|
+
return null;
|
|
75
|
+
const row = database
|
|
76
|
+
.prepare("SELECT coalesce(p.name, i.name) AS name FROM identities i LEFT JOIN identity_links l ON l.identity_id = i.id " +
|
|
77
|
+
"LEFT JOIN persons p ON p.id = l.person_id WHERE i.provider = ? AND i.external_id = ?")
|
|
78
|
+
.get(provider, assignee);
|
|
79
|
+
return row?.name == null ? null : String(row.name);
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* One pipeline's funnel and outcomes over a window: the funnel counts the cohort made in it, `closed`, `cycle` and
|
|
83
|
+
* `lostReasons` the rows closed in it. Each row's stages come from the CRM's history where it has one, else the copy's.
|
|
84
|
+
*/
|
|
85
|
+
export const pipelinePeriod = (context, pipeline, window, zone, viewer) => {
|
|
86
|
+
const { database } = context;
|
|
87
|
+
const { kind } = pipeline;
|
|
88
|
+
const pipelineId = Number(pipeline.id);
|
|
89
|
+
const made = (at) => (window.since === null || at >= window.since) && (window.until === null || at <= window.until);
|
|
90
|
+
const days = {
|
|
91
|
+
since: window.since === null ? null : dayIn(window.since, zone),
|
|
92
|
+
until: window.until === null ? null : dayIn(window.until, zone),
|
|
93
|
+
};
|
|
94
|
+
const closedIn = (day) => day !== null && (days.since === null || day >= days.since) && (days.until === null || day <= days.until);
|
|
95
|
+
const all = itemsOf(database, kind, pipelineId, viewer);
|
|
96
|
+
const cohort = all.filter((item) => made(item.createdAt));
|
|
97
|
+
const stages = database
|
|
98
|
+
.prepare(`SELECT s.id, s.position, s.outcome FROM pipeline_stages s WHERE s.pipeline_id = ? AND ${SHOWN_STAGE} ` +
|
|
99
|
+
"ORDER BY s.position, s.id")
|
|
100
|
+
.all(pipelineId)
|
|
101
|
+
.map((row) => ({ id: Number(row.id), outcome: row.outcome }));
|
|
102
|
+
const order = new Map(stages.map((stage, at) => [stage.id, at]));
|
|
103
|
+
const lostStage = new Set(stages.filter((stage) => stage.outcome === "lost").map(({ id }) => id));
|
|
104
|
+
const wonStage = new Set(stages.filter((stage) => stage.outcome === "won").map(({ id }) => id));
|
|
105
|
+
const entries = new Map();
|
|
106
|
+
for (const entry of stageEntriesOf(database, kind, { pipeline: pipelineId })) {
|
|
107
|
+
const own = entries.get(entry.itemId) ?? [];
|
|
108
|
+
own.push(entry);
|
|
109
|
+
entries.set(entry.itemId, own);
|
|
110
|
+
}
|
|
111
|
+
const furthest = cohort.map((item) => {
|
|
112
|
+
if (item.status === "won")
|
|
113
|
+
return Number.POSITIVE_INFINITY;
|
|
114
|
+
const reached = [...(entries.get(item.id) ?? []).map(({ toStageId }) => toStageId), item.stageId]
|
|
115
|
+
.filter((stage) => !lostStage.has(stage))
|
|
116
|
+
.map((stage) => order.get(stage) ?? -1);
|
|
117
|
+
// A row first seen closed has no entries: it got at least as far as the first stage.
|
|
118
|
+
return reached.reduce((far, at) => Math.max(far, at), 0);
|
|
119
|
+
});
|
|
120
|
+
const lost = cohort.filter((item) => item.status === "lost").length;
|
|
121
|
+
const won = cohort.filter((item) => item.status === "won").length;
|
|
122
|
+
const reachedAt = (at) => lostStage.has(stages[at]?.id) ? lost : furthest.filter((far) => far >= at).length;
|
|
123
|
+
const stays = new Map();
|
|
124
|
+
for (const item of cohort) {
|
|
125
|
+
const own = entries.get(item.id) ?? [];
|
|
126
|
+
own.slice(0, -1).forEach((entry, at) => {
|
|
127
|
+
const next = own[at + 1];
|
|
128
|
+
const days = stays.get(entry.toStageId) ?? [];
|
|
129
|
+
days.push(next.at - entry.at);
|
|
130
|
+
stays.set(entry.toStageId, days);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
const standing = stageAmountsOf(context, kind, stages.map(({ id }) => id), window, viewer);
|
|
134
|
+
const funnel = stages.map((stage, at) => {
|
|
135
|
+
const reached = reachedAt(at);
|
|
136
|
+
const next = stages.findIndex((one, after) => after > at && !lostStage.has(one.id));
|
|
137
|
+
const conversion = lostStage.has(stage.id) || next === -1 ? null : share(reachedAt(next), reached);
|
|
138
|
+
return { reached, conversion, medianDays: median(stays.get(stage.id) ?? []), ...standing[at] };
|
|
139
|
+
});
|
|
140
|
+
const closedWon = all.filter((item) => item.status === "won" && closedIn(item.closedDate));
|
|
141
|
+
const closedLost = all.filter((item) => item.status === "lost" && closedIn(item.closedDate));
|
|
142
|
+
const sums = new Map();
|
|
143
|
+
for (const item of closedWon)
|
|
144
|
+
if (item.currency !== null) {
|
|
145
|
+
const held = sums.get(item.currency) ?? { units: 0n, items: 0 };
|
|
146
|
+
sums.set(item.currency, { units: held.units + BigInt(item.amount ?? 0), items: held.items + 1 });
|
|
147
|
+
}
|
|
148
|
+
const wonAt = (item) => (entries.get(item.id) ?? []).filter((entry) => entry.exact && wonStage.has(entry.toStageId)).at(-1)?.at ??
|
|
149
|
+
(item.closedDate === null ? null : Date.parse(`${item.closedDate}T00:00:00Z`));
|
|
150
|
+
const cycles = closedWon.flatMap((item) => {
|
|
151
|
+
const at = wonAt(item);
|
|
152
|
+
return at === null ? [] : [Math.max(0, at - item.createdAt)];
|
|
153
|
+
});
|
|
154
|
+
const owners = new Map();
|
|
155
|
+
for (const item of cohort) {
|
|
156
|
+
const own = owners.get(item.assignee) ?? [];
|
|
157
|
+
own.push(item);
|
|
158
|
+
owners.set(item.assignee, own);
|
|
159
|
+
}
|
|
160
|
+
const reasons = new Map();
|
|
161
|
+
for (const item of closedLost)
|
|
162
|
+
if (item.reason)
|
|
163
|
+
reasons.set(item.reason, (reasons.get(item.reason) ?? 0) + 1);
|
|
164
|
+
const exact = cohort.filter((item) => entries.get(item.id)?.[0]?.exact === true).length;
|
|
165
|
+
return {
|
|
166
|
+
stages: funnel,
|
|
167
|
+
totals: { items: cohort.length, open: cohort.length - won - lost, won, lost, winRate: rate(won, lost) },
|
|
168
|
+
closed: {
|
|
169
|
+
won: closedWon.length,
|
|
170
|
+
lost: closedLost.length,
|
|
171
|
+
winRate: rate(closedWon.length, closedLost.length),
|
|
172
|
+
wonAmounts: [...sums]
|
|
173
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
174
|
+
.map(([currency, { units, items }]) => ({
|
|
175
|
+
amount: formatDecimal(units, scaleOf(currency)),
|
|
176
|
+
currency,
|
|
177
|
+
items,
|
|
178
|
+
})),
|
|
179
|
+
},
|
|
180
|
+
cycle: { medianDays: median(cycles), items: cycles.length },
|
|
181
|
+
owners: [...owners].map(([assignee, items]) => {
|
|
182
|
+
const ownWon = items.filter((item) => item.status === "won").length;
|
|
183
|
+
const ownLost = items.filter((item) => item.status === "lost").length;
|
|
184
|
+
return {
|
|
185
|
+
assignee,
|
|
186
|
+
name: userName(database, pipeline.account?.provider ?? null, assignee),
|
|
187
|
+
open: items.length - ownWon - ownLost,
|
|
188
|
+
won: ownWon,
|
|
189
|
+
lost: ownLost,
|
|
190
|
+
winRate: rate(ownWon, ownLost),
|
|
191
|
+
};
|
|
192
|
+
}),
|
|
193
|
+
lostReasons: [...reasons]
|
|
194
|
+
.sort(([a, x], [b, y]) => y - x || a.localeCompare(b))
|
|
195
|
+
.slice(0, LOST_REASONS)
|
|
196
|
+
.map(([reason, items]) => ({ reason, items })),
|
|
197
|
+
history: { exact, copy: cohort.length - exact },
|
|
198
|
+
};
|
|
199
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { DealKind } from "../../domain/deals.js";
|
|
2
2
|
import { type Money } from "../../domain/money.js";
|
|
3
|
+
import { type CopyViewer } from "./copy-viewer.js";
|
|
3
4
|
import type { StoreContext } from "./open.js";
|
|
4
5
|
export interface StageAmount extends Money {
|
|
5
6
|
items: number;
|
|
@@ -10,5 +11,8 @@ export interface StageAmounts {
|
|
|
10
11
|
/** One per currency, never converted; an item with no amount adds none. */
|
|
11
12
|
amounts: StageAmount[];
|
|
12
13
|
}
|
|
13
|
-
/** What stands in each stage of `stageIds`, in that order
|
|
14
|
-
export declare const stageAmountsOf: ({ database }: StoreContext, kind: DealKind, stageIds: number[],
|
|
14
|
+
/** What stands in each stage of `stageIds`, in that order, of the rows made in the window (epoch ms, inclusive). */
|
|
15
|
+
export declare const stageAmountsOf: ({ database }: StoreContext, kind: DealKind, stageIds: number[], { since, until }: {
|
|
16
|
+
since: number | null;
|
|
17
|
+
until: number | null;
|
|
18
|
+
}, viewer?: CopyViewer) => StageAmounts[];
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { formatDecimal, scaleOf } from "../../domain/money.js";
|
|
2
|
+
import { viewerSql } from "./copy-viewer.js";
|
|
2
3
|
const TABLES = { deal: "deals", lead: "leads" };
|
|
3
|
-
/** What stands in each stage of `stageIds`, in that order
|
|
4
|
-
export const stageAmountsOf = ({ database }, kind, stageIds,
|
|
4
|
+
/** What stands in each stage of `stageIds`, in that order, of the rows made in the window (epoch ms, inclusive). */
|
|
5
|
+
export const stageAmountsOf = ({ database }, kind, stageIds, { since, until }, viewer) => {
|
|
6
|
+
const seen = viewerSql("d", viewer);
|
|
5
7
|
const rows = database
|
|
6
8
|
.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
|
-
|
|
9
|
-
|
|
9
|
+
`FROM ${TABLES[kind]} d WHERE stage_id IN (${stageIds.map(() => "?").join(", ") || "NULL"}) ` +
|
|
10
|
+
`AND deleted_at IS NULL AND (? IS NULL OR created_at >= ?) AND (? IS NULL OR created_at <= ?) AND ${seen.sql} ` +
|
|
11
|
+
"GROUP BY stage_id, currency ORDER BY currency")
|
|
12
|
+
.all(...stageIds, since, since, until, until, ...seen.params);
|
|
10
13
|
return stageIds.map((stage) => {
|
|
11
14
|
const within = rows.filter((row) => Number(row.stage_id) === stage);
|
|
12
15
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wirecat/cli-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.676.0",
|
|
4
4
|
"description": "The messenger-neutral half of a messaging CLI: domain model, message locators, rendering, name resolution, the SQLite seam, the command skeleton and shared read commands, the send guard, run records and the message store",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Viacheslav Ptsarev",
|
|
@@ -300,7 +300,7 @@
|
|
|
300
300
|
"@types/pngjs": "6.0.5",
|
|
301
301
|
"@types/ssh2": "1.15.5",
|
|
302
302
|
"@vitest/coverage-v8": "^5.0.5",
|
|
303
|
-
"@wirecat/cli-base": "0.
|
|
303
|
+
"@wirecat/cli-base": "0.103.0",
|
|
304
304
|
"@wirecat/cli-core-legacy-tags": "npm:@wirecat/cli-core@0.414.0",
|
|
305
305
|
"ajv": "8.17.1",
|
|
306
306
|
"commander": "15.0.0",
|
|
@@ -324,7 +324,7 @@
|
|
|
324
324
|
},
|
|
325
325
|
"peerDependencies": {
|
|
326
326
|
"@napi-rs/canvas": ">=0.1.69 <2",
|
|
327
|
-
"@wirecat/cli-base": ">=0.
|
|
327
|
+
"@wirecat/cli-base": ">=0.103.0 <1.0.0",
|
|
328
328
|
"commander": ">=15",
|
|
329
329
|
"mysql2": "^3.24.6",
|
|
330
330
|
"pg": "^8.23.1",
|