@dench.com/cli 2.9.4 → 2.9.6-staging.240
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/app-sdk.d.ts +7027 -0
- package/apps.ts +206 -0
- package/crm-analytics.ts +456 -0
- package/crm.ts +62 -0
- package/dench.ts +1 -1
- package/lib/api-schemas.ts +646 -0
- package/lib/app-sdk-catalog.generated.ts +581 -0
- package/lib/app-sdk-surface.ts +1376 -0
- package/lib/apps-validate.ts +469 -0
- package/lib/command-registry.ts +243 -0
- package/notifications.ts +7 -6
- package/package.json +10 -1
package/apps.ts
CHANGED
|
@@ -8,8 +8,15 @@
|
|
|
8
8
|
* the server's requireCrmAccess accepts (and appStore forwards to the file
|
|
9
9
|
* functions as an apiKey for publish/install/create).
|
|
10
10
|
*/
|
|
11
|
+
import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
|
|
12
|
+
import { join, relative, resolve } from "node:path";
|
|
11
13
|
import type { ConvexHttpClient } from "convex/browser";
|
|
12
14
|
import { makeFunctionReference } from "convex/server";
|
|
15
|
+
import {
|
|
16
|
+
type AppFile,
|
|
17
|
+
formatValidation,
|
|
18
|
+
validateApp,
|
|
19
|
+
} from "./lib/apps-validate";
|
|
13
20
|
import { closestMatch } from "./lib/levenshtein";
|
|
14
21
|
|
|
15
22
|
const api = {
|
|
@@ -28,6 +35,18 @@ const api = {
|
|
|
28
35
|
"functions/appStore:setEnabled",
|
|
29
36
|
),
|
|
30
37
|
setPinned: makeFunctionReference<"mutation">("functions/appStore:setPinned"),
|
|
38
|
+
listTree: makeFunctionReference<"query">("functions/files:listTree"),
|
|
39
|
+
getFileContent: makeFunctionReference<"query">(
|
|
40
|
+
"functions/files:getFileContent",
|
|
41
|
+
),
|
|
42
|
+
webhooksRegister: makeFunctionReference<"mutation">(
|
|
43
|
+
"functions/appWebhooks:register",
|
|
44
|
+
),
|
|
45
|
+
webhooksList: makeFunctionReference<"query">("functions/appWebhooks:list"),
|
|
46
|
+
webhooksPoll: makeFunctionReference<"query">("functions/appWebhooks:poll"),
|
|
47
|
+
webhooksDelete: makeFunctionReference<"mutation">(
|
|
48
|
+
"functions/appWebhooks:remove",
|
|
49
|
+
),
|
|
31
50
|
};
|
|
32
51
|
|
|
33
52
|
class AppsCliError extends Error {}
|
|
@@ -87,9 +106,188 @@ function appsHelp() {
|
|
|
87
106
|
dench apps uninstall <slug> Remove an installed app
|
|
88
107
|
dench apps enable | disable <slug> Toggle whether the app loads
|
|
89
108
|
dench apps pin | unpin <slug> Toggle the rail pin
|
|
109
|
+
dench apps validate <appPath> [--json] Check the manifest, permissions and every window.dench call
|
|
110
|
+
(local folder, or a workspace path like /apps/x.dench.app)
|
|
111
|
+
dench apps webhooks register <slug> <hookName> [--rotate]
|
|
112
|
+
Inbound webhook URL + signing secret (secret shown once)
|
|
113
|
+
dench apps webhooks list <slug>
|
|
114
|
+
dench apps webhooks poll <slug> <hookName> [--since <ms>] [--limit <n>]
|
|
115
|
+
dench apps webhooks delete <slug> <hookName>
|
|
90
116
|
`);
|
|
91
117
|
}
|
|
92
118
|
|
|
119
|
+
const TEXT_FILE = /\.(html?|js|mjs|cjs|ts|jsx|tsx|css|json|ya?ml|md|txt|svg)$/i;
|
|
120
|
+
|
|
121
|
+
function readLocalAppFolder(root: string): AppFile[] {
|
|
122
|
+
const files: AppFile[] = [];
|
|
123
|
+
const walk = (dir: string) => {
|
|
124
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
125
|
+
const abs = join(dir, entry.name);
|
|
126
|
+
if (entry.isDirectory()) {
|
|
127
|
+
if (entry.name !== "node_modules" && entry.name !== ".git") walk(abs);
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
if (!entry.isFile()) continue;
|
|
131
|
+
const path = relative(root, abs).replace(/\\/g, "/");
|
|
132
|
+
if (path !== ".dench.yaml" && !TEXT_FILE.test(path)) continue;
|
|
133
|
+
files.push({ path, content: readFileSync(abs, "utf8") });
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
walk(root);
|
|
137
|
+
return files;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** Local folder → disk; otherwise read the workspace tree from Convex. */
|
|
141
|
+
async function loadAppFolder(
|
|
142
|
+
ctx: Ctx,
|
|
143
|
+
appPath: string,
|
|
144
|
+
): Promise<{ files: AppFile[]; source: "disk" | "workspace" }> {
|
|
145
|
+
const workspaceRoot =
|
|
146
|
+
process.env.DENCH_WORKSPACE_ROOT?.trim() || "/workspace";
|
|
147
|
+
const candidates = [
|
|
148
|
+
resolve(appPath),
|
|
149
|
+
join(workspaceRoot, appPath),
|
|
150
|
+
join(process.cwd(), appPath.replace(/^\/+/, "")),
|
|
151
|
+
];
|
|
152
|
+
for (const candidate of candidates) {
|
|
153
|
+
if (existsSync(candidate) && statSync(candidate).isDirectory()) {
|
|
154
|
+
return { files: readLocalAppFolder(candidate), source: "disk" };
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const prefix = appPath.startsWith("/") ? appPath : `/${appPath}`;
|
|
158
|
+
const rows: AppFile[] = [];
|
|
159
|
+
const walk = async (dir: string) => {
|
|
160
|
+
const entries = (await ctx.convex.query(
|
|
161
|
+
api.listTree,
|
|
162
|
+
withAuth(ctx, { prefix: dir }),
|
|
163
|
+
)) as Array<{ path: string; isDir: boolean }>;
|
|
164
|
+
for (const entry of entries) {
|
|
165
|
+
if (entry.isDir) {
|
|
166
|
+
await walk(entry.path);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
const rel = entry.path.slice(prefix.length).replace(/^\/+/, "");
|
|
170
|
+
if (rel !== ".dench.yaml" && !TEXT_FILE.test(rel)) continue;
|
|
171
|
+
const content = (await ctx.convex.query(
|
|
172
|
+
api.getFileContent,
|
|
173
|
+
withAuth(ctx, { path: entry.path }),
|
|
174
|
+
)) as { content?: string | null } | null;
|
|
175
|
+
if (typeof content?.content === "string") {
|
|
176
|
+
rows.push({ path: rel, content: content.content });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
await walk(prefix);
|
|
181
|
+
if (rows.length === 0) {
|
|
182
|
+
throw new AppsCliError(
|
|
183
|
+
`No app folder at ${appPath} (looked on disk and in the workspace).`,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
return { files: rows, source: "workspace" };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async function runValidate(ctx: Ctx) {
|
|
190
|
+
const appPath = firstPositional(ctx.args);
|
|
191
|
+
if (!appPath) {
|
|
192
|
+
throw new AppsCliError("Usage: dench apps validate <appPath> [--json]");
|
|
193
|
+
}
|
|
194
|
+
const { files, source } = await loadAppFolder(ctx, appPath);
|
|
195
|
+
const manifest = files.find((f) => f.path === ".dench.yaml");
|
|
196
|
+
const result = validateApp({
|
|
197
|
+
manifestYaml: manifest?.content ?? null,
|
|
198
|
+
files: files.filter((f) => f.path !== ".dench.yaml"),
|
|
199
|
+
});
|
|
200
|
+
if (ctx.jsonOutput) {
|
|
201
|
+
out(ctx, { appPath, source, ...result });
|
|
202
|
+
} else {
|
|
203
|
+
console.log(formatValidation(result, appPath));
|
|
204
|
+
}
|
|
205
|
+
if (!result.ok) process.exitCode = 1;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async function runWebhooks(ctx: Ctx) {
|
|
209
|
+
const verb = ctx.args.shift();
|
|
210
|
+
const usage =
|
|
211
|
+
"Usage: dench apps webhooks register|list|poll|delete <slug> [hookName]";
|
|
212
|
+
switch (verb) {
|
|
213
|
+
case "register": {
|
|
214
|
+
const rotateIndex = ctx.args.indexOf("--rotate");
|
|
215
|
+
const rotate = rotateIndex !== -1;
|
|
216
|
+
if (rotate) ctx.args.splice(rotateIndex, 1);
|
|
217
|
+
const slug = shift(ctx.args, "app slug");
|
|
218
|
+
const hookName = shift(ctx.args, "hook name");
|
|
219
|
+
const result = (await ctx.convex.mutation(
|
|
220
|
+
api.webhooksRegister,
|
|
221
|
+
withAuth(ctx, { slug, hookName, rotateSecret: rotate }),
|
|
222
|
+
)) as {
|
|
223
|
+
url: string | null;
|
|
224
|
+
path: string;
|
|
225
|
+
secret: string | null;
|
|
226
|
+
created: boolean;
|
|
227
|
+
};
|
|
228
|
+
const base = process.env.DENCH_API_URL?.trim().replace(/\/+$/, "");
|
|
229
|
+
const url = result.url ?? (base ? `${base}${result.path}` : result.path);
|
|
230
|
+
if (ctx.jsonOutput) {
|
|
231
|
+
out(ctx, { ...result, url });
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
console.log(
|
|
235
|
+
`${result.created ? "Registered" : "Existing"} webhook "${hookName}" for ${slug}`,
|
|
236
|
+
);
|
|
237
|
+
console.log(` URL: ${url}`);
|
|
238
|
+
if (result.secret) {
|
|
239
|
+
console.log(
|
|
240
|
+
` Secret: ${result.secret} (shown once — store it with the sender)`,
|
|
241
|
+
);
|
|
242
|
+
} else {
|
|
243
|
+
console.log(" Secret: (unchanged; pass --rotate to mint a new one)");
|
|
244
|
+
}
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
case "list": {
|
|
248
|
+
const slug = shift(ctx.args, "app slug");
|
|
249
|
+
out(
|
|
250
|
+
ctx,
|
|
251
|
+
await ctx.convex.query(api.webhooksList, withAuth(ctx, { slug })),
|
|
252
|
+
);
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
case "poll": {
|
|
256
|
+
const since = takeFlag(ctx.args, "--since");
|
|
257
|
+
const limit = takeFlag(ctx.args, "--limit");
|
|
258
|
+
const slug = shift(ctx.args, "app slug");
|
|
259
|
+
const hookName = shift(ctx.args, "hook name");
|
|
260
|
+
out(
|
|
261
|
+
ctx,
|
|
262
|
+
await ctx.convex.query(
|
|
263
|
+
api.webhooksPoll,
|
|
264
|
+
withAuth(ctx, {
|
|
265
|
+
slug,
|
|
266
|
+
hookName,
|
|
267
|
+
...(since ? { since: Number(since) } : {}),
|
|
268
|
+
...(limit ? { limit: Number(limit) } : {}),
|
|
269
|
+
}),
|
|
270
|
+
),
|
|
271
|
+
);
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
case "delete": {
|
|
275
|
+
const slug = shift(ctx.args, "app slug");
|
|
276
|
+
const hookName = shift(ctx.args, "hook name");
|
|
277
|
+
out(
|
|
278
|
+
ctx,
|
|
279
|
+
await ctx.convex.mutation(
|
|
280
|
+
api.webhooksDelete,
|
|
281
|
+
withAuth(ctx, { slug, hookName }),
|
|
282
|
+
),
|
|
283
|
+
);
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
default:
|
|
287
|
+
throw new AppsCliError(usage);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
93
291
|
export async function runAppsCommand(opts: {
|
|
94
292
|
convex: ConvexHttpClient;
|
|
95
293
|
args: string[];
|
|
@@ -211,6 +409,12 @@ export async function runAppsCommand(opts: {
|
|
|
211
409
|
out(ctx, { ok: true, slug, enabled: sub === "enable" });
|
|
212
410
|
return;
|
|
213
411
|
}
|
|
412
|
+
case "validate":
|
|
413
|
+
await runValidate(ctx);
|
|
414
|
+
return;
|
|
415
|
+
case "webhooks":
|
|
416
|
+
await runWebhooks(ctx);
|
|
417
|
+
return;
|
|
214
418
|
case "pin":
|
|
215
419
|
case "unpin": {
|
|
216
420
|
const slug = ctx.args.shift();
|
|
@@ -250,5 +454,7 @@ const APPS_SUBCOMMANDS = [
|
|
|
250
454
|
"disable",
|
|
251
455
|
"pin",
|
|
252
456
|
"unpin",
|
|
457
|
+
"validate",
|
|
458
|
+
"webhooks",
|
|
253
459
|
"help",
|
|
254
460
|
] as const;
|
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
|
+
}
|