@velaro/cli 1.4.171 → 1.4.172

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/bin/velaro.js CHANGED
@@ -103,6 +103,7 @@ import { authorityCommand } from '../lib/commands/authority.js';
103
103
  import { caseCommand } from '../lib/commands/case.js';
104
104
  import { dealCommand } from '../lib/commands/deal.js';
105
105
  import { ticketCommand } from '../lib/commands/ticket.js';
106
+ import { crmCommand } from '../lib/commands/crm-studio.js';
106
107
  import { emailRouteCommand } from '../lib/commands/email-route.js';
107
108
  import { emailRouteOverrideCommand } from '../lib/commands/email-route-override.js';
108
109
  import { pickupCommand } from '../lib/commands/pickup.js';
@@ -260,6 +261,7 @@ await (registerStaffCommands ? registerStaffCommands(baseYargs) : baseYargs)
260
261
  .command(caseCommand)
261
262
  .command(dealCommand)
262
263
  .command(ticketCommand)
264
+ .command(crmCommand)
263
265
  .command(emailRouteCommand)
264
266
  .command(emailRouteOverrideCommand)
265
267
  .command(pickupCommand)
@@ -0,0 +1,183 @@
1
+ import { messagingGet, messagingPost, messagingPut, messagingDel } from '../api.js';
2
+ import { runCommand } from '../run.js';
3
+
4
+ // -- velaro crm ---------------------------------------------------------------
5
+ // CLI for CRM Studio (docs/architecture/crm-studio-program.md):
6
+ // field-* CrmCustomFieldsController /CrmCustomFields
7
+ // layout-* CrmFormLayoutsController /CrmFormLayouts
8
+ // The site always comes from the signed-in credential; no command takes a site id.
9
+ // MCP parity: mcp/parity/crm-studio.customer.js (crm_* tools).
10
+
11
+ const BOLD = '\x1b[1m';
12
+ const DIM = '\x1b[2m';
13
+ const RST = '\x1b[0m';
14
+
15
+ const ENTITIES = ['deal', 'contact', 'company', 'ticket'];
16
+ const FIELD_TYPES = [
17
+ 'Text', 'Number', 'Date', 'Dropdown', 'Checkbox', 'Currency',
18
+ 'MultiSelect', 'LongText', 'Url', 'Email', 'Phone', 'Percent', 'DateTime', 'Lookup',
19
+ ];
20
+
21
+ const data = (r) => (r && typeof r === 'object' && 'data' in r ? r.data : r);
22
+ const printJson = (v) => console.log(JSON.stringify(v, null, 2));
23
+ const csv = (s) => (s ? String(s).split(',').map((x) => x.trim()).filter(Boolean) : undefined);
24
+ const q = (o) => {
25
+ const p = Object.entries(o).filter(([, v]) => v !== undefined && v !== null && v !== '').map(([k, v]) => `${k}=${encodeURIComponent(v)}`);
26
+ return p.length ? `?${p.join('&')}` : '';
27
+ };
28
+
29
+ const json = (y) => y.option('json', { type: 'boolean', default: false, describe: 'Print raw JSON' });
30
+ const entity = (y) => y.option('entity', { choices: ENTITIES, demandOption: true, describe: 'Record type' });
31
+
32
+ async function fieldListHandler(argv) {
33
+ const v = data(await messagingGet(`/CrmCustomFields${q({ entity: argv.entity, pipelineId: argv.pipelineId })}`));
34
+ const list = Array.isArray(v) ? v : v?.fields ?? [];
35
+ if (argv.json) return printJson(list);
36
+ console.log(`\n${BOLD}${argv.entity} fields${RST} (${list.length})`);
37
+ for (const f of list) console.log(` #${f.id} ${f.label} ${DIM}${f.fieldType}${f.isRequired ? ', required' : ''}${RST}`);
38
+ console.log('');
39
+ }
40
+
41
+ // Update semantics (server): a key that is absent or null leaves the stored value alone; an empty string clears it.
42
+ // So only keys the caller actually passed are sent, and `--read-roles ""` clears the restriction.
43
+ const listArg = (v) => (v === undefined ? undefined : v === '' ? '' : JSON.stringify(csv(v)));
44
+
45
+ async function fieldDefineHandler(argv) {
46
+ if (!argv.id && !argv.label) throw new Error('[CRM] --label is required when creating a field.');
47
+ const body = { entity: argv.entity };
48
+ const set = (k, v) => { if (v !== undefined) body[k] = v; };
49
+ set('label', argv.label);
50
+ set('fieldType', argv.type ?? (argv.id ? undefined : 'Text'));
51
+ set('optionsJson', listArg(argv.options));
52
+ set('isRequired', argv.required);
53
+ set('pipelineId', argv.pipelineId);
54
+ set('sortOrder', argv.sortOrder);
55
+ set('helpText', argv.helpText);
56
+ set('defaultValue', argv.default);
57
+ set('validationJson', argv.validation);
58
+ set('readRolesJson', listArg(argv.readRoles));
59
+ set('writeRolesJson', listArg(argv.writeRoles));
60
+ set('visibleWhenJson', argv.visibleWhen);
61
+ const r = data(argv.id ? await messagingPut(`/CrmCustomFields/${argv.id}`, body) : await messagingPost('/CrmCustomFields', body));
62
+ if (argv.json) return printJson(r);
63
+ console.log(`\n Field saved: ${argv.label}${r?.id ? ` (#${r.id})` : ''}.\n`);
64
+ }
65
+
66
+ async function fieldArchiveHandler(argv) {
67
+ await messagingDel(`/CrmCustomFields/${argv.id}`);
68
+ console.log(`\n Field ${argv.id} archived. Saved values are kept.\n`);
69
+ }
70
+
71
+ async function layoutListHandler(argv) {
72
+ const v = data(await messagingGet(`/CrmFormLayouts${q({ entity: argv.entity, pipelineId: argv.pipelineId })}`));
73
+ const list = Array.isArray(v) ? v : v?.layouts ?? [];
74
+ if (argv.json) return printJson(list);
75
+ console.log(`\n${BOLD}${argv.entity} form layouts${RST} (${list.length})`);
76
+ for (const l of list) console.log(` #${l.id} ${l.name}${l.isDefault ? ' (default)' : ''} ${DIM}v${l.version ?? 1}${RST}`);
77
+ console.log('');
78
+ }
79
+
80
+ async function layoutResolveHandler(argv) {
81
+ printJson(data(await messagingGet(`/CrmFormLayouts/resolve${q({ entity: argv.entity, pipelineId: argv.pipelineId, stageId: argv.stageId })}`)));
82
+ }
83
+
84
+ async function layoutSaveHandler(argv) {
85
+ const fs = await import('node:fs');
86
+ const layout = JSON.parse(fs.readFileSync(argv.file, 'utf8'));
87
+ const body = {
88
+ entity: argv.entity,
89
+ name: argv.name,
90
+ pipelineId: argv.pipelineId ?? null,
91
+ stageId: argv.stageId ?? null,
92
+ roles: csv(argv.roles) ?? [],
93
+ isDefault: !!argv.default,
94
+ layout,
95
+ };
96
+ // Optimistic concurrency: on update the server rejects a stale save (409) when the version no longer matches.
97
+ if (argv.id) {
98
+ const current = data(await messagingGet(`/CrmFormLayouts/${argv.id}`));
99
+ body.expectedVersion = argv.expectedVersion ?? current?.version;
100
+ }
101
+ const r = data(argv.id ? await messagingPut(`/CrmFormLayouts/${argv.id}`, body) : await messagingPost('/CrmFormLayouts', body));
102
+ if (argv.json) return printJson(r);
103
+ console.log(`\n Layout saved: ${argv.name}${r?.id ? ` (#${r.id})` : ''}.\n`);
104
+ }
105
+
106
+ async function layoutDeleteHandler(argv) {
107
+ await messagingDel(`/CrmFormLayouts/${argv.id}`);
108
+ console.log(`\n Layout ${argv.id} deleted. Field values were not touched.\n`);
109
+ }
110
+
111
+ async function layoutVersionsHandler(argv) {
112
+ printJson(data(await messagingGet(`/CrmFormLayouts/${argv.id}/versions`)));
113
+ }
114
+
115
+ async function layoutRestoreHandler(argv) {
116
+ await messagingPost(`/CrmFormLayouts/${argv.id}/versions/${argv.version}/restore`, {});
117
+ console.log(`\n Layout ${argv.id} restored to version ${argv.version}.\n`);
118
+ }
119
+
120
+ export const crmCommand = {
121
+ command: 'crm <subcommand>',
122
+ describe: 'CRM Studio: custom fields and form layouts (Form Designer)',
123
+ builder: (y) => y
124
+ .command({
125
+ command: 'field-list',
126
+ describe: 'List CRM custom fields for a record type',
127
+ builder: (yy) => json(entity(yy).option('pipeline-id', { type: 'number', alias: 'pipelineId' })),
128
+ handler: runCommand(fieldListHandler),
129
+ })
130
+ .command({
131
+ command: 'field-define',
132
+ describe: 'Create or update a CRM custom field (Administrator)',
133
+ builder: (yy) => json(entity(yy)
134
+ .option('id', { type: 'number', describe: 'Existing field id to update' })
135
+ .option('label', { type: 'string', describe: 'Required when creating' })
136
+ .option('type', { choices: FIELD_TYPES, describe: 'Defaults to Text when creating; unchanged on update' })
137
+ .option('options', { type: 'string', describe: 'Comma-separated choices for Dropdown and MultiSelect' })
138
+ .option('required', { type: 'boolean' })
139
+ .option('pipeline-id', { type: 'number', alias: 'pipelineId' })
140
+ .option('sort-order', { type: 'number', alias: 'sortOrder' })
141
+ .option('help-text', { type: 'string', alias: 'helpText', describe: 'Help text shown under the field' })
142
+ .option('default', { type: 'string', describe: 'Default value' })
143
+ .option('validation', { type: 'string', describe: 'JSON, for example {"min":0,"max":100}' })
144
+ .option('read-roles', { type: 'string', alias: 'readRoles', describe: 'Comma-separated role names; empty means everyone' })
145
+ .option('write-roles', { type: 'string', alias: 'writeRoles', describe: 'Comma-separated role names; empty means everyone' })
146
+ .option('visible-when', { type: 'string', alias: 'visibleWhen', describe: 'JSON rule' })),
147
+ handler: runCommand(fieldDefineHandler),
148
+ })
149
+ .command({ command: 'field-archive <id>', describe: 'Archive a CRM custom field (Administrator)', handler: runCommand(fieldArchiveHandler) })
150
+ .command({
151
+ command: 'layout-list',
152
+ describe: 'List form layouts for a record type',
153
+ builder: (yy) => json(entity(yy).option('pipeline-id', { type: 'number', alias: 'pipelineId' })),
154
+ handler: runCommand(layoutListHandler),
155
+ })
156
+ .command({
157
+ command: 'layout-resolve',
158
+ describe: 'Show the layout the signed-in user gets for a record type, pipeline and stage',
159
+ builder: (yy) => entity(yy)
160
+ .option('pipeline-id', { type: 'number', alias: 'pipelineId' })
161
+ .option('stage-id', { type: 'string', alias: 'stageId' }),
162
+ handler: runCommand(layoutResolveHandler),
163
+ })
164
+ .command({
165
+ command: 'layout-save',
166
+ describe: 'Create or update a form layout from a JSON file (Administrator)',
167
+ builder: (yy) => json(entity(yy)
168
+ .option('id', { type: 'number', describe: 'Existing layout id to update' })
169
+ .option('name', { type: 'string', demandOption: true })
170
+ .option('file', { type: 'string', demandOption: true, describe: 'Path to the layout JSON ({ "sections": [...] })' })
171
+ .option('pipeline-id', { type: 'number', alias: 'pipelineId' })
172
+ .option('stage-id', { type: 'string', alias: 'stageId' })
173
+ .option('roles', { type: 'string', describe: 'Comma-separated roles (Admin, Manager, Agent, Viewer, Billing); empty means everyone' })
174
+ .option('expected-version', { type: 'number', alias: 'expectedVersion', describe: 'Version you based the edit on; defaults to the current one. A stale value fails with a conflict' })
175
+ .option('default', { type: 'boolean', describe: 'Make this the default layout' })),
176
+ handler: runCommand(layoutSaveHandler),
177
+ })
178
+ .command({ command: 'layout-delete <id>', describe: 'Delete a form layout (Administrator)', handler: runCommand(layoutDeleteHandler) })
179
+ .command({ command: 'layout-versions <id>', describe: 'List saved versions of a form layout', handler: runCommand(layoutVersionsHandler) })
180
+ .command({ command: 'layout-restore <id> <version>', describe: 'Restore a saved layout version (Administrator)', handler: runCommand(layoutRestoreHandler) })
181
+ .demandCommand(1),
182
+ handler: () => {},
183
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@velaro/cli",
3
- "version": "1.4.171",
3
+ "version": "1.4.172",
4
4
  "description": "Velaro Workspace v20 — command-line interface for managing bots, knowledge base ingestion, MCP API keys, search indexes, and ops health.",
5
5
  "type": "module",
6
6
  "bin": {