@dench.com/cli 2.9.4-staging.231 → 2.9.5-staging.239
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/crm-analytics.ts +456 -0
- package/crm.ts +62 -0
- package/dench.ts +1 -1
- package/lib/api-schemas.ts +603 -0
- package/lib/command-registry.ts +170 -0
- package/notifications.ts +7 -6
- package/package.json +2 -1
package/crm-analytics.ts
ADDED
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `dench crm analytics …`
|
|
3
|
+
*
|
|
4
|
+
* Thin dispatcher over the Convex functions the analytics strip itself
|
|
5
|
+
* uses (`functions/crm/analytics`), so the CLI, the `/api/v1` surface and
|
|
6
|
+
* the UI share one contract: `computePanels` is the only chart math, and a
|
|
7
|
+
* dashboard belongs to the object or to ONE saved view (`--view`).
|
|
8
|
+
*
|
|
9
|
+
* get [--view V] read config (+ the view's own dashboard)
|
|
10
|
+
* compute [--view V] [--period P] [--panel ID ...] [--panels JSON] [--filters JSON]
|
|
11
|
+
* save <panelId> --panel JSON [--view V] create / replace one panel
|
|
12
|
+
* update <panelId> --patch JSON [--view V] edit part of one panel (null clears)
|
|
13
|
+
* hide <panelId> [--view V] keep it off the strip (full view only)
|
|
14
|
+
* show <panelId> [--view V] back onto the strip
|
|
15
|
+
* resize <panelId> <third|half|two-thirds|full|default> [--view V]
|
|
16
|
+
* delete <panelId> [--view V]
|
|
17
|
+
* reorder <id1> <id2> … [--view V] full order
|
|
18
|
+
* roles --roles JSON [--merge] field roles shared by every chart + view
|
|
19
|
+
* collapse | expand the strip above the table
|
|
20
|
+
* scope <view> --mode own|shared view-owned vs shared dashboard
|
|
21
|
+
*
|
|
22
|
+
* `--period` is expanded SERVER-SIDE (`computePanels.period`) into the same
|
|
23
|
+
* extra + compare filters the strip's pill builds, so a CLI number and the
|
|
24
|
+
* UI number for "this month" always agree. Kept out of `crm.ts` for size.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { makeFunctionReference } from "convex/server";
|
|
28
|
+
import {
|
|
29
|
+
assertNoUnknownFlags,
|
|
30
|
+
type CrmCliContext,
|
|
31
|
+
CrmCliError,
|
|
32
|
+
callMutation,
|
|
33
|
+
callQuery,
|
|
34
|
+
out,
|
|
35
|
+
shift,
|
|
36
|
+
} from "./crm";
|
|
37
|
+
import { getFlag, hasFlag, parseJson } from "./lib/cli-args";
|
|
38
|
+
|
|
39
|
+
const api = {
|
|
40
|
+
getConfig: makeFunctionReference<"query">(
|
|
41
|
+
"functions/crm/analytics:getAnalyticsConfig",
|
|
42
|
+
),
|
|
43
|
+
compute: makeFunctionReference<"query">(
|
|
44
|
+
"functions/crm/analytics:computePanels",
|
|
45
|
+
),
|
|
46
|
+
savePanel: makeFunctionReference<"mutation">(
|
|
47
|
+
"functions/crm/analytics:saveAnalyticsPanel",
|
|
48
|
+
),
|
|
49
|
+
updatePanel: makeFunctionReference<"mutation">(
|
|
50
|
+
"functions/crm/analytics:updateAnalyticsPanel",
|
|
51
|
+
),
|
|
52
|
+
deletePanel: makeFunctionReference<"mutation">(
|
|
53
|
+
"functions/crm/analytics:deleteAnalyticsPanel",
|
|
54
|
+
),
|
|
55
|
+
reorder: makeFunctionReference<"mutation">(
|
|
56
|
+
"functions/crm/analytics:reorderAnalyticsPanels",
|
|
57
|
+
),
|
|
58
|
+
setRoles: makeFunctionReference<"mutation">(
|
|
59
|
+
"functions/crm/analytics:setAnalyticsFieldRoles",
|
|
60
|
+
),
|
|
61
|
+
setCollapsed: makeFunctionReference<"mutation">(
|
|
62
|
+
"functions/crm/analytics:setAnalyticsStripCollapsed",
|
|
63
|
+
),
|
|
64
|
+
setViewMode: makeFunctionReference<"mutation">(
|
|
65
|
+
"functions/crm/analytics:setViewAnalyticsMode",
|
|
66
|
+
),
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
/** Same ids as ANALYTICS_PERIODS in convex/lib/crm/analyticsPeriod.ts. */
|
|
70
|
+
export const ANALYTICS_CLI_PERIODS = [
|
|
71
|
+
"all",
|
|
72
|
+
"this_week",
|
|
73
|
+
"this_month",
|
|
74
|
+
"last_month",
|
|
75
|
+
"this_quarter",
|
|
76
|
+
"last_quarter",
|
|
77
|
+
"this_year",
|
|
78
|
+
] as const;
|
|
79
|
+
|
|
80
|
+
const PANEL_TYPES = [
|
|
81
|
+
"stat",
|
|
82
|
+
"bar",
|
|
83
|
+
"line",
|
|
84
|
+
"area",
|
|
85
|
+
"donut",
|
|
86
|
+
"funnel",
|
|
87
|
+
"leaderboard",
|
|
88
|
+
"forecast",
|
|
89
|
+
"map",
|
|
90
|
+
] as const;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* `resize` widths: tracks of the 6-track grid, by number or by name.
|
|
94
|
+
* `default` clears the stored span so the type's default applies again.
|
|
95
|
+
*/
|
|
96
|
+
export const ANALYTICS_CLI_WIDTHS: Record<string, 2 | 3 | 4 | 6 | null> = {
|
|
97
|
+
"2": 2,
|
|
98
|
+
"3": 3,
|
|
99
|
+
"4": 4,
|
|
100
|
+
"6": 6,
|
|
101
|
+
third: 2,
|
|
102
|
+
"1/3": 2,
|
|
103
|
+
half: 3,
|
|
104
|
+
"1/2": 3,
|
|
105
|
+
"two-thirds": 4,
|
|
106
|
+
"2/3": 4,
|
|
107
|
+
full: 6,
|
|
108
|
+
default: null,
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export function parseWidth(raw: string): 2 | 3 | 4 | 6 | null {
|
|
112
|
+
const key = raw.trim().toLowerCase();
|
|
113
|
+
if (!(key in ANALYTICS_CLI_WIDTHS)) {
|
|
114
|
+
throw new CrmCliError(
|
|
115
|
+
`Width must be one of third | half | two-thirds | full | default (or 2 | 3 | 4 | 6 tracks), got "${raw}".`,
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
return ANALYTICS_CLI_WIDTHS[key];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function viewFlag(args: string[]): { viewName: string } | Record<never, never> {
|
|
122
|
+
const view = getFlag(args, "--view");
|
|
123
|
+
return view?.trim() ? { viewName: view.trim() } : {};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function parsePeriod(raw: string | undefined): string | undefined {
|
|
127
|
+
if (raw === undefined) return undefined;
|
|
128
|
+
const period = raw.trim();
|
|
129
|
+
if (!(ANALYTICS_CLI_PERIODS as readonly string[]).includes(period)) {
|
|
130
|
+
throw new CrmCliError(
|
|
131
|
+
`--period must be one of ${ANALYTICS_CLI_PERIODS.join(" | ")}, got "${raw}".`,
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
return period === "all" ? undefined : period;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function parseObjectFlag(
|
|
138
|
+
args: string[],
|
|
139
|
+
name: string,
|
|
140
|
+
): Record<string, unknown> | undefined {
|
|
141
|
+
const raw = getFlag(args, name);
|
|
142
|
+
if (raw === undefined) return undefined;
|
|
143
|
+
const parsed = parseJson(raw);
|
|
144
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
145
|
+
throw new CrmCliError(`${name} must be a JSON object.`);
|
|
146
|
+
}
|
|
147
|
+
return parsed as Record<string, unknown>;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function parseArrayFlag(
|
|
151
|
+
args: string[],
|
|
152
|
+
name: string,
|
|
153
|
+
): Array<Record<string, unknown>> | undefined {
|
|
154
|
+
const raw = getFlag(args, name);
|
|
155
|
+
if (raw === undefined) return undefined;
|
|
156
|
+
const parsed = parseJson(raw);
|
|
157
|
+
if (!Array.isArray(parsed)) {
|
|
158
|
+
throw new CrmCliError(`${name} must be a JSON array.`);
|
|
159
|
+
}
|
|
160
|
+
return parsed as Array<Record<string, unknown>>;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Repeated `--panel <id>` flags (compute) → panelIds. */
|
|
164
|
+
function collectPanelIds(args: string[]): string[] {
|
|
165
|
+
const ids: string[] = [];
|
|
166
|
+
for (;;) {
|
|
167
|
+
const value = getFlag(args, "--panel");
|
|
168
|
+
if (value === undefined) break;
|
|
169
|
+
if (value.trim()) ids.push(value.trim());
|
|
170
|
+
}
|
|
171
|
+
return ids;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Normalize an ad-hoc panel from the CLI: fill `id`, default `sortOrder`
|
|
176
|
+
* and `metric` so a quick `--panel '{"title":"Leads by source","type":
|
|
177
|
+
* "donut","groupBy":"Source"}'` is enough. Validation proper happens in
|
|
178
|
+
* the Convex validator.
|
|
179
|
+
*/
|
|
180
|
+
export function normalizePanelInput(
|
|
181
|
+
raw: Record<string, unknown>,
|
|
182
|
+
opts: { id?: string; index?: number } = {},
|
|
183
|
+
): Record<string, unknown> {
|
|
184
|
+
const id =
|
|
185
|
+
opts.id ??
|
|
186
|
+
(typeof raw.id === "string" && raw.id.trim()
|
|
187
|
+
? raw.id.trim()
|
|
188
|
+
: `adhoc:${String(raw.title ?? raw.type ?? "panel")
|
|
189
|
+
.toLowerCase()
|
|
190
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
191
|
+
.replace(
|
|
192
|
+
/(^-|-$)/g,
|
|
193
|
+
"",
|
|
194
|
+
)}${opts.index !== undefined ? `-${opts.index}` : ""}`);
|
|
195
|
+
const type = typeof raw.type === "string" ? raw.type : "stat";
|
|
196
|
+
if (!(PANEL_TYPES as readonly string[]).includes(type)) {
|
|
197
|
+
throw new CrmCliError(
|
|
198
|
+
`Panel type must be one of ${PANEL_TYPES.join(" | ")}, got "${type}".`,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
...raw,
|
|
203
|
+
id,
|
|
204
|
+
type,
|
|
205
|
+
title: typeof raw.title === "string" ? raw.title : id,
|
|
206
|
+
metric: typeof raw.metric === "string" ? raw.metric : "count",
|
|
207
|
+
sortOrder:
|
|
208
|
+
typeof raw.sortOrder === "number" ? raw.sortOrder : (opts.index ?? 0),
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Relative deep link to the dashboard the result came from — the same
|
|
214
|
+
* link the in-chat card's Open uses (`buildAnalyticsLink` in the web app).
|
|
215
|
+
*/
|
|
216
|
+
export function analyticsDeepLink(args: {
|
|
217
|
+
objectName: string;
|
|
218
|
+
viewName?: string;
|
|
219
|
+
period?: string;
|
|
220
|
+
host?: string;
|
|
221
|
+
workspaceSlug?: string;
|
|
222
|
+
}): { href: string; url?: string } {
|
|
223
|
+
const params = new URLSearchParams();
|
|
224
|
+
params.set("path", args.objectName);
|
|
225
|
+
if (args.viewName) params.set("view", args.viewName);
|
|
226
|
+
params.set("analytics", "1");
|
|
227
|
+
if (args.period && args.period !== "all")
|
|
228
|
+
params.set("analyticsPeriod", args.period);
|
|
229
|
+
const href = `/?${params.toString()}`;
|
|
230
|
+
const slug = args.workspaceSlug?.trim();
|
|
231
|
+
const host = args.host?.trim();
|
|
232
|
+
if (!slug || !host) return { href };
|
|
233
|
+
const withProtocol = /^https?:\/\//.test(host) ? host : `https://${host}`;
|
|
234
|
+
try {
|
|
235
|
+
return {
|
|
236
|
+
href,
|
|
237
|
+
url: `${new URL(withProtocol).origin}/${encodeURIComponent(slug)}?${params.toString()}`,
|
|
238
|
+
};
|
|
239
|
+
} catch {
|
|
240
|
+
return { href };
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export async function runAnalyticsCommand(ctx: CrmCliContext): Promise<void> {
|
|
245
|
+
const verb = ctx.args.shift();
|
|
246
|
+
switch (verb) {
|
|
247
|
+
case "get": {
|
|
248
|
+
const objectName = shift(ctx.args, "object name");
|
|
249
|
+
const view = viewFlag(ctx.args);
|
|
250
|
+
assertNoUnknownFlags(ctx.args, "crm analytics get");
|
|
251
|
+
out(ctx, await callQuery(ctx, api.getConfig, { objectName, ...view }));
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
case "compute": {
|
|
255
|
+
const objectName = shift(ctx.args, "object name");
|
|
256
|
+
const view = viewFlag(ctx.args);
|
|
257
|
+
const period = parsePeriod(getFlag(ctx.args, "--period"));
|
|
258
|
+
const panelIds = collectPanelIds(ctx.args);
|
|
259
|
+
const adhoc = parseArrayFlag(ctx.args, "--panels");
|
|
260
|
+
const extraFilters = parseObjectFlag(ctx.args, "--filters");
|
|
261
|
+
const compareFilters = parseObjectFlag(ctx.args, "--compare-filters");
|
|
262
|
+
const rolesOverride = parseObjectFlag(ctx.args, "--roles");
|
|
263
|
+
assertNoUnknownFlags(ctx.args, "crm analytics compute");
|
|
264
|
+
if (panelIds.length > 0 && adhoc) {
|
|
265
|
+
throw new CrmCliError(
|
|
266
|
+
"Pass either --panel <id> (stored panels) or --panels <JSON> (ad-hoc), not both.",
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
const result = (await callQuery(ctx, api.compute, {
|
|
270
|
+
objectName,
|
|
271
|
+
...view,
|
|
272
|
+
...(panelIds.length > 0 ? { panelIds } : {}),
|
|
273
|
+
...(adhoc
|
|
274
|
+
? {
|
|
275
|
+
panels: adhoc.map((p, i) => normalizePanelInput(p, { index: i })),
|
|
276
|
+
}
|
|
277
|
+
: {}),
|
|
278
|
+
...(period ? { period } : {}),
|
|
279
|
+
...(extraFilters ? { extraFilters } : {}),
|
|
280
|
+
...(compareFilters ? { compareFilters } : {}),
|
|
281
|
+
...(rolesOverride ? { fieldRolesOverride: rolesOverride } : {}),
|
|
282
|
+
})) as Record<string, unknown>;
|
|
283
|
+
const link = analyticsDeepLink({
|
|
284
|
+
objectName,
|
|
285
|
+
viewName: "viewName" in view ? view.viewName : undefined,
|
|
286
|
+
period,
|
|
287
|
+
host: ctx.host,
|
|
288
|
+
workspaceSlug: ctx.workspaceSlug,
|
|
289
|
+
});
|
|
290
|
+
out(ctx, {
|
|
291
|
+
...result,
|
|
292
|
+
...(period ? { period } : {}),
|
|
293
|
+
link: link.url ?? link.href,
|
|
294
|
+
});
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
case "save": {
|
|
298
|
+
const objectName = shift(ctx.args, "object name");
|
|
299
|
+
const panelId = shift(ctx.args, "panel id");
|
|
300
|
+
const raw = parseObjectFlag(ctx.args, "--panel");
|
|
301
|
+
const view = viewFlag(ctx.args);
|
|
302
|
+
assertNoUnknownFlags(ctx.args, "crm analytics save");
|
|
303
|
+
if (!raw) throw new CrmCliError("--panel <JSON> is required.");
|
|
304
|
+
out(
|
|
305
|
+
ctx,
|
|
306
|
+
await callMutation(ctx, api.savePanel, {
|
|
307
|
+
objectName,
|
|
308
|
+
panel: normalizePanelInput(raw, { id: panelId }),
|
|
309
|
+
...view,
|
|
310
|
+
}),
|
|
311
|
+
);
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
case "update": {
|
|
315
|
+
const objectName = shift(ctx.args, "object name");
|
|
316
|
+
const panelId = shift(ctx.args, "panel id");
|
|
317
|
+
const patch = parseObjectFlag(ctx.args, "--patch");
|
|
318
|
+
const view = viewFlag(ctx.args);
|
|
319
|
+
assertNoUnknownFlags(ctx.args, "crm analytics update");
|
|
320
|
+
if (!patch || Object.keys(patch).length === 0) {
|
|
321
|
+
throw new CrmCliError(
|
|
322
|
+
'--patch <JSON> is required, e.g. --patch \'{"title":"Won by rep","span":2,"hidden":false}\' (null clears a key).',
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
out(
|
|
326
|
+
ctx,
|
|
327
|
+
await callMutation(ctx, api.updatePanel, {
|
|
328
|
+
objectName,
|
|
329
|
+
panelId,
|
|
330
|
+
patch,
|
|
331
|
+
...view,
|
|
332
|
+
}),
|
|
333
|
+
);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
case "hide":
|
|
337
|
+
case "show": {
|
|
338
|
+
const objectName = shift(ctx.args, "object name");
|
|
339
|
+
const panelId = shift(ctx.args, "panel id");
|
|
340
|
+
const view = viewFlag(ctx.args);
|
|
341
|
+
assertNoUnknownFlags(ctx.args, `crm analytics ${verb}`);
|
|
342
|
+
out(
|
|
343
|
+
ctx,
|
|
344
|
+
await callMutation(ctx, api.updatePanel, {
|
|
345
|
+
objectName,
|
|
346
|
+
panelId,
|
|
347
|
+
patch: { hidden: verb === "hide" },
|
|
348
|
+
...view,
|
|
349
|
+
}),
|
|
350
|
+
);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
case "resize": {
|
|
354
|
+
const objectName = shift(ctx.args, "object name");
|
|
355
|
+
const panelId = shift(ctx.args, "panel id");
|
|
356
|
+
const span = parseWidth(shift(ctx.args, "width"));
|
|
357
|
+
const view = viewFlag(ctx.args);
|
|
358
|
+
assertNoUnknownFlags(ctx.args, "crm analytics resize");
|
|
359
|
+
out(
|
|
360
|
+
ctx,
|
|
361
|
+
await callMutation(ctx, api.updatePanel, {
|
|
362
|
+
objectName,
|
|
363
|
+
panelId,
|
|
364
|
+
patch: { span },
|
|
365
|
+
...view,
|
|
366
|
+
}),
|
|
367
|
+
);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
case "delete": {
|
|
371
|
+
const objectName = shift(ctx.args, "object name");
|
|
372
|
+
const panelId = shift(ctx.args, "panel id");
|
|
373
|
+
const view = viewFlag(ctx.args);
|
|
374
|
+
assertNoUnknownFlags(ctx.args, "crm analytics delete");
|
|
375
|
+
out(
|
|
376
|
+
ctx,
|
|
377
|
+
await callMutation(ctx, api.deletePanel, {
|
|
378
|
+
objectName,
|
|
379
|
+
panelId,
|
|
380
|
+
...view,
|
|
381
|
+
}),
|
|
382
|
+
);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
case "reorder": {
|
|
386
|
+
const objectName = shift(ctx.args, "object name");
|
|
387
|
+
const view = viewFlag(ctx.args);
|
|
388
|
+
assertNoUnknownFlags(ctx.args, "crm analytics reorder");
|
|
389
|
+
const panelIds = ctx.args.splice(0).filter((id) => id.trim());
|
|
390
|
+
if (panelIds.length === 0) {
|
|
391
|
+
throw new CrmCliError(
|
|
392
|
+
"Pass every panel id in the new order: dench crm analytics reorder <object> <id1> <id2> …",
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
out(
|
|
396
|
+
ctx,
|
|
397
|
+
await callMutation(ctx, api.reorder, { objectName, panelIds, ...view }),
|
|
398
|
+
);
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
case "roles": {
|
|
402
|
+
const objectName = shift(ctx.args, "object name");
|
|
403
|
+
const fieldRoles = parseObjectFlag(ctx.args, "--roles");
|
|
404
|
+
const merge = hasFlag(ctx.args, "--merge");
|
|
405
|
+
assertNoUnknownFlags(ctx.args, "crm analytics roles");
|
|
406
|
+
if (!fieldRoles) throw new CrmCliError("--roles <JSON> is required.");
|
|
407
|
+
out(
|
|
408
|
+
ctx,
|
|
409
|
+
await callMutation(ctx, api.setRoles, {
|
|
410
|
+
objectName,
|
|
411
|
+
fieldRoles,
|
|
412
|
+
...(merge ? { merge: true } : {}),
|
|
413
|
+
}),
|
|
414
|
+
);
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
case "collapse":
|
|
418
|
+
case "expand": {
|
|
419
|
+
const objectName = shift(ctx.args, "object name");
|
|
420
|
+
assertNoUnknownFlags(ctx.args, `crm analytics ${verb}`);
|
|
421
|
+
out(
|
|
422
|
+
ctx,
|
|
423
|
+
await callMutation(ctx, api.setCollapsed, {
|
|
424
|
+
objectName,
|
|
425
|
+
collapsed: verb === "collapse",
|
|
426
|
+
}),
|
|
427
|
+
);
|
|
428
|
+
return;
|
|
429
|
+
}
|
|
430
|
+
case "scope": {
|
|
431
|
+
const objectName = shift(ctx.args, "object name");
|
|
432
|
+
const viewName = shift(ctx.args, "view name");
|
|
433
|
+
const mode = getFlag(ctx.args, "--mode");
|
|
434
|
+
const own = hasFlag(ctx.args, "--own");
|
|
435
|
+
const shared = hasFlag(ctx.args, "--shared");
|
|
436
|
+
assertNoUnknownFlags(ctx.args, "crm analytics scope");
|
|
437
|
+
const resolved = mode ?? (own ? "own" : shared ? "shared" : undefined);
|
|
438
|
+
if (resolved !== "own" && resolved !== "shared") {
|
|
439
|
+
throw new CrmCliError("--mode own|shared is required.");
|
|
440
|
+
}
|
|
441
|
+
out(
|
|
442
|
+
ctx,
|
|
443
|
+
await callMutation(ctx, api.setViewMode, {
|
|
444
|
+
objectName,
|
|
445
|
+
viewName,
|
|
446
|
+
mode: resolved,
|
|
447
|
+
}),
|
|
448
|
+
);
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
default:
|
|
452
|
+
throw new CrmCliError(
|
|
453
|
+
`Unknown analytics verb: ${verb ?? "(none)"}. Use get | compute | save | update | hide | show | resize | delete | reorder | roles | collapse | expand | scope.`,
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
}
|
package/crm.ts
CHANGED
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
*/
|
|
30
30
|
import type { ConvexHttpClient } from "convex/browser";
|
|
31
31
|
import { makeFunctionReference } from "convex/server";
|
|
32
|
+
import { runAnalyticsCommand } from "./crm-analytics";
|
|
32
33
|
import {
|
|
33
34
|
runCommentsCommand,
|
|
34
35
|
runTasksCommand,
|
|
@@ -1053,6 +1054,8 @@ export async function runCrmCommand(opts: {
|
|
|
1053
1054
|
return await runEnrichCommand(ctx);
|
|
1054
1055
|
case "reports":
|
|
1055
1056
|
return await runReportsCommand(ctx);
|
|
1057
|
+
case "analytics":
|
|
1058
|
+
return await runAnalyticsCommand(ctx);
|
|
1056
1059
|
case "docs":
|
|
1057
1060
|
case "documents":
|
|
1058
1061
|
return await runDocsCommand(ctx);
|
|
@@ -1104,6 +1107,7 @@ const CRM_SUBCOMMANDS = [
|
|
|
1104
1107
|
"companies",
|
|
1105
1108
|
"enrich",
|
|
1106
1109
|
"reports",
|
|
1110
|
+
"analytics",
|
|
1107
1111
|
"docs",
|
|
1108
1112
|
"documents",
|
|
1109
1113
|
"actions",
|
|
@@ -4550,6 +4554,64 @@ Cell / row / object enrichment:
|
|
|
4550
4554
|
Reports (chart-ready JSON):
|
|
4551
4555
|
dench crm reports generate '<name>' --object <object> --type pie|bar|line|table --group-by <field> [--metric count|sum --metric-field <field>]
|
|
4552
4556
|
|
|
4557
|
+
Analytics (the dashboard strip above every object table — same math the UI draws):
|
|
4558
|
+
dench crm analytics get <object> [--view <view-name>] [--json]
|
|
4559
|
+
Panels, field roles, the strip's collapsed flag, the fields panels may
|
|
4560
|
+
bucket on (synthetic $createdAt / $location last), and — with --view —
|
|
4561
|
+
that saved view's own dashboard (viewAnalytics; null when it inherits).
|
|
4562
|
+
dench crm analytics compute <object> [--view <view-name>] [--period this_week|this_month|last_month|this_quarter|last_quarter|this_year]
|
|
4563
|
+
[--panel <id> ...] [--panels '<JSON array of panel defs>'] [--filters '<FilterGroup JSON>'] [--json]
|
|
4564
|
+
Chart-ready buckets / series / values for the stored dashboard, a
|
|
4565
|
+
subset of its panels (--panel), or ad-hoc definitions (--panels; nothing
|
|
4566
|
+
is persisted). --period scopes every panel AND fills previousValue from
|
|
4567
|
+
the previous interval, exactly like the strip's period pill. The
|
|
4568
|
+
response carries a "link" that opens the dashboard in the app. Prefer
|
|
4569
|
+
this over "reports generate" and over aggregating query rows yourself.
|
|
4570
|
+
Panel def (every key the chart editor has):
|
|
4571
|
+
{"title","type":stat|bar|line|area|donut|funnel|leaderboard|forecast|map,
|
|
4572
|
+
"metric":count|sum|avg|min|max,"metricField"?,
|
|
4573
|
+
"groupBy"? breakdown field; $location for a map's place field;
|
|
4574
|
+
on line/area/forecast it splits the series into one line per value
|
|
4575
|
+
"dateField"?,"dateBucket"?:day|week|month|quarter time axis ($createdAt works)
|
|
4576
|
+
"filters"? panel-scoped FilterGroup (saved-view shape)
|
|
4577
|
+
"derived"?:conversion_rate|win_rate|won_amount|lost_amount|open_pipeline|pipeline_forecast|mrr|arr
|
|
4578
|
+
role-driven metric — ignores metric/metricField
|
|
4579
|
+
"span"?:2|3|4|6 width: third | half | two-thirds | full (default by chart kind)
|
|
4580
|
+
"hidden"?:true off the strip; only in the full analytics view
|
|
4581
|
+
"sortOrder"?}
|
|
4582
|
+
dench crm analytics save <object> <panelId> --panel '<panel JSON>' [--view <view-name>]
|
|
4583
|
+
Create or replace one panel (whole definition). With --view the write
|
|
4584
|
+
lands on that view's own dashboard (forking a copy if it was still
|
|
4585
|
+
inheriting the object's).
|
|
4586
|
+
dench crm analytics update <object> <panelId> --patch '<partial panel JSON>' [--view <view-name>]
|
|
4587
|
+
Edit part of a stored panel: only the keys given change; null clears
|
|
4588
|
+
one ("span":null → default width, "filters":null → no filter).
|
|
4589
|
+
dench crm analytics hide <object> <panelId> [--view <view-name>]
|
|
4590
|
+
dench crm analytics show <object> <panelId> [--view <view-name>]
|
|
4591
|
+
Take a chart off the strip above the table (it stays in the full
|
|
4592
|
+
analytics view, "+N more") or bring it back — the card's ⋮ menu.
|
|
4593
|
+
dench crm analytics resize <object> <panelId> third|half|two-thirds|full|default [--view <view-name>]
|
|
4594
|
+
Card width on the 6-track grid (2|3|4|6 also accepted); default clears
|
|
4595
|
+
it so the chart kind's default applies again.
|
|
4596
|
+
dench crm analytics delete <object> <panelId> [--view <view-name>]
|
|
4597
|
+
Idempotent; template panels (tpl:…) are tombstoned so a re-install
|
|
4598
|
+
never brings them back.
|
|
4599
|
+
dench crm analytics reorder <object> <id1> <id2> ... [--view <view-name>]
|
|
4600
|
+
Must name EVERY stored panel of the targeted dashboard (stats too).
|
|
4601
|
+
dench crm analytics roles <object> --roles '{"amount":"Amount","stage":"Stage","wonValues":["Closed Won"],"lostValues":["Closed Lost"],"convertedValues":["Qualified"],"closeDate":"Close Date","owner":"Owner","location":"Account.HQ Location","recurrence":"annual","stageProbabilities":{"Discovery":0.2,"Proposal":0.5}}' [--merge]
|
|
4602
|
+
The analytics setup shared by EVERY chart and view of the object —
|
|
4603
|
+
what the strip's ⚙ sheet edits. Replaces the mapping; --merge changes
|
|
4604
|
+
only the keys given (null clears one). "location" may be a relation
|
|
4605
|
+
path (field on the linked record); "recurrence" is monthly|annual|one_time;
|
|
4606
|
+
"stageProbabilities" are 0..1 per open stage for the weighted forecast.
|
|
4607
|
+
dench crm analytics collapse <object> | expand <object>
|
|
4608
|
+
Team-shared collapsed state of the strip (like saved views).
|
|
4609
|
+
dench crm analytics scope <object> <view-name> --mode own|shared
|
|
4610
|
+
own: the view gets its own dashboard (a copy of the object's panels);
|
|
4611
|
+
shared: back to the object's. Any active view follows its filters by
|
|
4612
|
+
default either way.
|
|
4613
|
+
Deep link: /?path=<object>&analytics=1[&view=<view>][&analyticsPeriod=<period>]
|
|
4614
|
+
|
|
4553
4615
|
Docs:
|
|
4554
4616
|
dench crm docs list
|
|
4555
4617
|
dench crm docs create <path> [--title ...] [--icon ...] [--link-entry <id>]
|
package/dench.ts
CHANGED
|
@@ -552,7 +552,7 @@ Meetings (CRM \`meeting\` records + their transcript, action items, attendance):
|
|
|
552
552
|
Help: dench meetings help
|
|
553
553
|
|
|
554
554
|
Notifications (the bell's inbox):
|
|
555
|
-
dench notifications list [--filter inbox|unread|archived|all] [--limit N] [--json]
|
|
555
|
+
dench notifications list [--filter inbox|unread|archived|all|mentions] [--limit N] [--json]
|
|
556
556
|
dench notifications count | read <id> [--unread] | read-all | archive <id>
|
|
557
557
|
dench notifications prefs [--set emailEnabled=false] [--set <kind>.email=false]
|
|
558
558
|
Help: dench notifications help
|