@velaro/cli 1.4.173 → 1.4.174
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/lib/commands/crm-studio.js +59 -1
- package/package.json +1 -1
|
@@ -5,6 +5,7 @@ import { runCommand } from '../run.js';
|
|
|
5
5
|
// CLI for CRM Studio (docs/architecture/crm-studio-program.md):
|
|
6
6
|
// field-* CrmCustomFieldsController /CrmCustomFields
|
|
7
7
|
// layout-* CrmFormLayoutsController /CrmFormLayouts
|
|
8
|
+
// formula-test POST /CrmCustomFields/formula/test (dry run of a calculated field or rollup; writes nothing)
|
|
8
9
|
// The site always comes from the signed-in credential; no command takes a site id.
|
|
9
10
|
// MCP parity: mcp/parity/crm-studio.customer.js (crm_* tools).
|
|
10
11
|
|
|
@@ -26,6 +27,29 @@ const q = (o) => {
|
|
|
26
27
|
return p.length ? `?${p.join('&')}` : '';
|
|
27
28
|
};
|
|
28
29
|
|
|
30
|
+
// Calculated fields (CRM Studio): a field whose FormulaJson is { kind: 'formula'|'rollup', expr, resultType, when|where }.
|
|
31
|
+
// Expressions: + - * / %, comparisons, && || !, IF ROUND MIN MAX ABS CONCAT COALESCE TODAY DAYS_BETWEEN; refs are built-in field
|
|
32
|
+
// names or cf:<id>. Rollups: SUM|COUNT|MIN|MAX|AVG of lineItems (deal), deals (company, contact) or tickets (contact).
|
|
33
|
+
const RESULT_TYPES = ['number', 'text', 'date', 'boolean'];
|
|
34
|
+
const RESULT_FOR_FIELD = { Number: 'number', Currency: 'number', Percent: 'number', Text: 'text', LongText: 'text', Date: 'date', Checkbox: 'boolean' };
|
|
35
|
+
const parseJsonOpt = (v, name) => {
|
|
36
|
+
try { return JSON.parse(v); } catch { throw new Error(`[CRM] --${name} must be valid JSON.`); }
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// --formula takes the whole spec as JSON ("" clears it); --expr builds one from the pieces.
|
|
40
|
+
function formulaArg(argv) {
|
|
41
|
+
if (argv.formula !== undefined) return argv.formula;
|
|
42
|
+
if (argv.expr === undefined) return undefined;
|
|
43
|
+
const kind = argv.kind ?? 'formula';
|
|
44
|
+
const spec = { kind, expr: argv.expr };
|
|
45
|
+
const resultType = argv.resultType ?? (kind === 'rollup' ? 'number' : RESULT_FOR_FIELD[argv.type]);
|
|
46
|
+
if (!resultType) throw new Error('[CRM] --result-type is required (number, text, date or boolean) when it cannot be taken from --type.');
|
|
47
|
+
spec.resultType = resultType;
|
|
48
|
+
if (argv.when !== undefined) spec.when = parseJsonOpt(argv.when, 'when');
|
|
49
|
+
if (argv.where !== undefined) spec.where = parseJsonOpt(argv.where, 'where');
|
|
50
|
+
return JSON.stringify(spec);
|
|
51
|
+
}
|
|
52
|
+
|
|
29
53
|
const json = (y) => y.option('json', { type: 'boolean', default: false, describe: 'Print raw JSON' });
|
|
30
54
|
const entity = (y) => y.option('entity', { choices: ENTITIES, demandOption: true, describe: 'Record type' });
|
|
31
55
|
|
|
@@ -58,11 +82,27 @@ async function fieldDefineHandler(argv) {
|
|
|
58
82
|
set('readRolesJson', listArg(argv.readRoles));
|
|
59
83
|
set('writeRolesJson', listArg(argv.writeRoles));
|
|
60
84
|
set('visibleWhenJson', argv.visibleWhen);
|
|
85
|
+
set('formulaJson', formulaArg(argv));
|
|
61
86
|
const r = data(argv.id ? await messagingPut(`/CrmCustomFields/${argv.id}`, body) : await messagingPost('/CrmCustomFields', body));
|
|
62
87
|
if (argv.json) return printJson(r);
|
|
63
88
|
console.log(`\n Field saved: ${argv.label}${r?.id ? ` (#${r.id})` : ''}.\n`);
|
|
64
89
|
}
|
|
65
90
|
|
|
91
|
+
async function formulaTestHandler(argv) {
|
|
92
|
+
const body = { entity: argv.entity };
|
|
93
|
+
if (argv.formula !== undefined) body.formulaJson = argv.formula;
|
|
94
|
+
else {
|
|
95
|
+
body.expr = argv.expr;
|
|
96
|
+
body.resultType = argv.resultType ?? 'number';
|
|
97
|
+
}
|
|
98
|
+
if (argv.sample !== undefined) body.sampleValues = parseJsonOpt(argv.sample, 'sample');
|
|
99
|
+
if (argv.recordId !== undefined) body.recordId = argv.recordId;
|
|
100
|
+
const r = data(await messagingPost('/CrmCustomFields/formula/test', body));
|
|
101
|
+
if (argv.json) return printJson(r);
|
|
102
|
+
if (r?.error) console.log(`\n ${BOLD}Could not calculate:${RST} ${r.error}\n`);
|
|
103
|
+
else console.log(`\n ${BOLD}Result${RST} (${r?.resultType}): ${r?.value === null || r?.value === undefined ? 'no value' : r.value}\n`);
|
|
104
|
+
}
|
|
105
|
+
|
|
66
106
|
async function fieldArchiveHandler(argv) {
|
|
67
107
|
await messagingDel(`/CrmCustomFields/${argv.id}`);
|
|
68
108
|
console.log(`\n Field ${argv.id} archived. Saved values are kept.\n`);
|
|
@@ -143,9 +183,27 @@ export const crmCommand = {
|
|
|
143
183
|
.option('validation', { type: 'string', describe: 'JSON, for example {"min":0,"max":100}' })
|
|
144
184
|
.option('read-roles', { type: 'string', alias: 'readRoles', describe: 'Comma-separated role names; empty means everyone' })
|
|
145
185
|
.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' })
|
|
186
|
+
.option('visible-when', { type: 'string', alias: 'visibleWhen', describe: 'JSON rule' })
|
|
187
|
+
.option('formula', { type: 'string', describe: 'Calculated field spec as JSON, for example {"kind":"formula","expr":"amount * 2","resultType":"number"}; empty clears it' })
|
|
188
|
+
.option('expr', { type: 'string', describe: 'Calculated field expression, for example "ROUND(amount * probability / 100, 2)" or "SUM(lineItems.total)" (builds --formula)' })
|
|
189
|
+
.option('kind', { choices: ['formula', 'rollup'], describe: 'With --expr: a formula (default) or a rollup over related records' })
|
|
190
|
+
.option('result-type', { choices: RESULT_TYPES, alias: 'resultType', describe: 'With --expr: defaults from --type (Number, Currency, Percent = number); rollups are numbers' })
|
|
191
|
+
.option('when', { type: 'string', describe: 'With --expr (formula): JSON rule; the field has a value only when it is true' })
|
|
192
|
+
.option('where', { type: 'string', describe: 'With --expr (rollup): JSON rule over the related rows; only matching rows are counted' })),
|
|
147
193
|
handler: runCommand(fieldDefineHandler),
|
|
148
194
|
})
|
|
195
|
+
.command({
|
|
196
|
+
command: 'formula-test',
|
|
197
|
+
describe: 'Dry-run a calculated field expression or rollup against sample values or a real record (Administrator; writes nothing)',
|
|
198
|
+
builder: (yy) => json(entity(yy)
|
|
199
|
+
.option('expr', { type: 'string', describe: 'Expression, for example "amount * 2"' })
|
|
200
|
+
.option('formula', { type: 'string', describe: 'Whole spec as JSON instead of --expr (use for rollups)' })
|
|
201
|
+
.option('result-type', { choices: RESULT_TYPES, alias: 'resultType', describe: 'Defaults to number' })
|
|
202
|
+
.option('sample', { type: 'string', describe: 'JSON object of sample values, for example {"amount":100,"cf:12":"Gold"}' })
|
|
203
|
+
.option('record-id', { type: 'number', alias: 'recordId', describe: "Use this record's real values underneath the samples (required to test a rollup)" })
|
|
204
|
+
.check((a) => (a.expr || a.formula !== undefined) ? true : '[CRM] Pass --expr or --formula.')),
|
|
205
|
+
handler: runCommand(formulaTestHandler),
|
|
206
|
+
})
|
|
149
207
|
.command({ command: 'field-archive <id>', describe: 'Archive a CRM custom field (Administrator)', handler: runCommand(fieldArchiveHandler) })
|
|
150
208
|
.command({
|
|
151
209
|
command: 'layout-list',
|
package/package.json
CHANGED