@velaro/cli 1.4.209 → 1.4.210
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/sign.js +57 -1
- package/lib/sign-fields.js +49 -0
- package/lib/sign-fields.test.js +27 -0
- package/package.json +1 -1
package/lib/commands/sign.js
CHANGED
|
@@ -2,6 +2,7 @@ import { readFileSync, writeFileSync } from 'node:fs';
|
|
|
2
2
|
import { basename } from 'node:path';
|
|
3
3
|
import { get, post, put, postForm, getBinary, getCredentials } from '../api.js';
|
|
4
4
|
import { runCommand } from '../run.js';
|
|
5
|
+
import { fieldsPath, FROM_SIGNUP_PATH, buildFieldsBody, describeFields, buildFromSignupBody } from '../sign-fields.js';
|
|
5
6
|
import { versionsPath, diffPath, uploadPath, describeVersions, describeDiff, buildUploadForm, OWNER_ONLY_RULE } from '../sign-versions.js';
|
|
6
7
|
|
|
7
8
|
// velaro sign -- Velaro Sign routing, reminders, final PDF download, file verification.
|
|
@@ -118,6 +119,35 @@ async function uploadVersion(id, file, note) {
|
|
|
118
119
|
console.log(describeVersions(await get(versionsPath(id))));
|
|
119
120
|
}
|
|
120
121
|
|
|
122
|
+
async function fieldsGet(id, json) {
|
|
123
|
+
const res = await get(fieldsPath(id));
|
|
124
|
+
console.log(json ? JSON.stringify(res, null, 2) : describeFields(res));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function fieldsPut(id, file, initialsEveryPage) {
|
|
128
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8'));
|
|
129
|
+
const list = Array.isArray(parsed) ? parsed : parsed.fields;
|
|
130
|
+
const res = await put(fieldsPath(id), buildFieldsBody(list, initialsEveryPage ?? parsed.initialsEveryPage));
|
|
131
|
+
console.log(`Saved fields for contract ${id}.`);
|
|
132
|
+
console.log(JSON.stringify(res, null, 2));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Service-to-service endpoint: needs the internal secret (env VELARO_INTERNAL_SECRET) and Features:SignupContracts on the server.
|
|
136
|
+
async function fromSignup(a) {
|
|
137
|
+
const secret = process.env.VELARO_INTERNAL_SECRET;
|
|
138
|
+
if (!secret) throw new Error('Set VELARO_INTERNAL_SECRET (Key Vault: VelaroInternalSecret) to call from-signup.');
|
|
139
|
+
const creds = await getCredentials();
|
|
140
|
+
const base = String(creds.adminApiBase).replace(/\/+$/, '');
|
|
141
|
+
const res = await fetch(`${base}${FROM_SIGNUP_PATH}`, {
|
|
142
|
+
method: 'POST',
|
|
143
|
+
headers: { 'Content-Type': 'application/json', 'X-Velaro-Internal-Secret': secret },
|
|
144
|
+
body: JSON.stringify(buildFromSignupBody(a)),
|
|
145
|
+
});
|
|
146
|
+
const text = await res.text();
|
|
147
|
+
if (!res.ok) throw new Error(`POST ${FROM_SIGNUP_PATH} -> ${res.status}: ${text.slice(0, 300)}`);
|
|
148
|
+
console.log(text);
|
|
149
|
+
}
|
|
150
|
+
|
|
121
151
|
export const signCommand = {
|
|
122
152
|
command: 'sign <subcommand>',
|
|
123
153
|
describe: 'Velaro Sign -- signer routing, reminders, final PDF, file verification',
|
|
@@ -191,7 +221,33 @@ export const signCommand = {
|
|
|
191
221
|
.option('note', { type: 'string', describe: 'Change note stored with the version' }),
|
|
192
222
|
handler: runCommand((a) => uploadVersion(a.contractId, a.file, a.note)),
|
|
193
223
|
})
|
|
194
|
-
.
|
|
224
|
+
.command({
|
|
225
|
+
command: 'fields <action> <contractId> [file]',
|
|
226
|
+
describe: 'Signing fields: get | put <file.json> (full set; types incl. fullname, email, company, title, dropdown; prefillKey)',
|
|
227
|
+
builder: (y2) => y2
|
|
228
|
+
.positional('action', { type: 'string', choices: ['get', 'put'] })
|
|
229
|
+
.positional('contractId', { type: 'number' })
|
|
230
|
+
.positional('file', { type: 'string' })
|
|
231
|
+
.option('json', { type: 'boolean', default: false })
|
|
232
|
+
.option('initials-every-page', { type: 'boolean', describe: 'Add an initials box to every page' }),
|
|
233
|
+
handler: runCommand((a) => {
|
|
234
|
+
if (a.action === 'get') return fieldsGet(a.contractId, a.json);
|
|
235
|
+
if (!a.file) throw new Error('put needs a JSON file: velaro sign fields put <contractId> <file.json>');
|
|
236
|
+
return fieldsPut(a.contractId, a.file, a.initialsEveryPage);
|
|
237
|
+
}),
|
|
238
|
+
})
|
|
239
|
+
.command({
|
|
240
|
+
command: 'from-signup',
|
|
241
|
+
describe: 'Create and send a contract from a signup flow, returns the signing link (needs VELARO_INTERNAL_SECRET)',
|
|
242
|
+
builder: (y2) => y2
|
|
243
|
+
.option('siteId', { type: 'number', demandOption: true }).option('title', { type: 'string', demandOption: true })
|
|
244
|
+
.option('customerEmail', { type: 'string', demandOption: true }).option('customerName', { type: 'string' })
|
|
245
|
+
.option('customerCompany', { type: 'string' }).option('packageName', { type: 'string' })
|
|
246
|
+
.option('totalMonthlyValue', { type: 'number' }).option('setupFee', { type: 'number' })
|
|
247
|
+
.option('contractTemplateId', { type: 'number' }).option('sendEmail', { type: 'boolean', default: false }),
|
|
248
|
+
handler: runCommand((a) => fromSignup(a)),
|
|
249
|
+
})
|
|
250
|
+
.demandCommand(1, 'Specify: fields | from-signup | signers | reassign | reminders | download | verify | versions | diff | upload-version')
|
|
195
251
|
.strict(),
|
|
196
252
|
handler: () => {},
|
|
197
253
|
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Pure helpers for Velaro Sign field surfaces (`velaro sign fields get|put`, `velaro sign from-signup`, MCP sign_fields_get/put).
|
|
2
|
+
// REST: GET/PUT /api/Contract/{id}/fields, POST /api/Contract/from-signup (internal secret, Features:SignupContracts).
|
|
3
|
+
export const FIELD_TYPES = ['signature', 'initials', 'date', 'text', 'checkbox', 'fullname', 'email', 'company', 'title', 'dropdown'];
|
|
4
|
+
export const SIGNER_ROLES = ['customer', 'velaro'];
|
|
5
|
+
|
|
6
|
+
export const fieldsPath = (id) => `/api/Contract/${Number(id)}/fields`;
|
|
7
|
+
export const FROM_SIGNUP_PATH = '/api/Contract/from-signup';
|
|
8
|
+
|
|
9
|
+
export function validateFields(fields) {
|
|
10
|
+
if (!Array.isArray(fields)) throw new Error('fields must be an array');
|
|
11
|
+
fields.forEach((f, i) => {
|
|
12
|
+
const at = `field ${i + 1}`;
|
|
13
|
+
if (!FIELD_TYPES.includes(f.fieldType)) throw new Error(`${at}: fieldType must be one of ${FIELD_TYPES.join(', ')}`);
|
|
14
|
+
if (f.signerRole && !SIGNER_ROLES.includes(f.signerRole)) throw new Error(`${at}: signerRole must be customer or velaro`);
|
|
15
|
+
if (!Number.isInteger(f.page) || f.page < 1) throw new Error(`${at}: page must be 1 or more`);
|
|
16
|
+
for (const k of ['xPct', 'yPct', 'widthPct', 'heightPct']) {
|
|
17
|
+
if (typeof f[k] !== 'number') throw new Error(`${at}: ${k} must be a number (percent of the page)`);
|
|
18
|
+
}
|
|
19
|
+
if (f.fieldType === 'dropdown' && !String(f.options || '').trim()) throw new Error(`${at}: a dropdown needs options (one per line)`);
|
|
20
|
+
});
|
|
21
|
+
return fields;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Body for PUT /fields. The server replaces the whole field set, so callers pass the full list.
|
|
25
|
+
export function buildFieldsBody(fields, initialsEveryPage = false) {
|
|
26
|
+
return { initialsEveryPage: !!initialsEveryPage, fields: validateFields(fields) };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function describeFields(res) {
|
|
30
|
+
if (!res?.hasDocument) return 'No document uploaded yet; upload a PDF before placing fields.';
|
|
31
|
+
const rows = res.fields ?? [];
|
|
32
|
+
const out = [`${res.fileName || 'document'}: ${res.pageCount ?? '?'} page(s), ${rows.length} field(s)${res.initialsEveryPage ? ', initials on every page' : ''}`];
|
|
33
|
+
for (const f of rows) {
|
|
34
|
+
out.push(` #${f.id} p${f.page} ${f.fieldType} (${f.signerRole})${f.required ? ' required' : ''}${f.label ? ` "${f.label}"` : ''}${f.prefillKey ? ` prefill=${f.prefillKey}` : ''}${f.options ? ` options=${String(f.options).split('\n').length}` : ''}`);
|
|
35
|
+
}
|
|
36
|
+
return out.join('\n');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function buildFromSignupBody(a) {
|
|
40
|
+
const siteId = Number(a.siteId);
|
|
41
|
+
if (!Number.isInteger(siteId) || siteId <= 0) throw new Error('siteId is required');
|
|
42
|
+
if (!a.title || !String(a.title).trim()) throw new Error('title is required');
|
|
43
|
+
if (!a.customerEmail || !String(a.customerEmail).includes('@')) throw new Error('a valid customerEmail is required');
|
|
44
|
+
const body = { siteId, title: String(a.title).trim(), customerEmail: String(a.customerEmail).trim(), sendEmail: !!a.sendEmail };
|
|
45
|
+
for (const k of ['customerName', 'customerCompany', 'packageName', 'totalMonthlyValue', 'setupFee', 'contractTemplateId']) {
|
|
46
|
+
if (a[k] !== undefined && a[k] !== null && a[k] !== '') body[k] = a[k];
|
|
47
|
+
}
|
|
48
|
+
return body;
|
|
49
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { fieldsPath, buildFieldsBody, describeFields, buildFromSignupBody } from './sign-fields.js';
|
|
4
|
+
|
|
5
|
+
const ok = { fieldType: 'fullname', page: 1, xPct: 10, yPct: 10, widthPct: 20, heightPct: 5, prefillKey: 'name' };
|
|
6
|
+
|
|
7
|
+
test('fieldsPath', () => assert.equal(fieldsPath('7'), '/api/Contract/7/fields'));
|
|
8
|
+
|
|
9
|
+
test('buildFieldsBody accepts new types and rejects bad ones', () => {
|
|
10
|
+
assert.equal(buildFieldsBody([ok]).fields.length, 1);
|
|
11
|
+
assert.throws(() => buildFieldsBody([{ ...ok, fieldType: 'nope' }]), /fieldType/);
|
|
12
|
+
assert.throws(() => buildFieldsBody([{ ...ok, fieldType: 'dropdown' }]), /options/);
|
|
13
|
+
assert.throws(() => buildFieldsBody([{ ...ok, page: 0 }]), /page/);
|
|
14
|
+
assert.throws(() => buildFieldsBody([{ ...ok, signerRole: 'x' }]), /signerRole/);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test('describeFields shows prefill key', () => {
|
|
18
|
+
const out = describeFields({ hasDocument: true, fileName: 'a.pdf', pageCount: 2, fields: [{ id: 3, page: 1, fieldType: 'email', signerRole: 'customer', required: true, prefillKey: 'email' }] });
|
|
19
|
+
assert.match(out, /prefill=email/);
|
|
20
|
+
assert.match(describeFields({ hasDocument: false }), /No document/);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('buildFromSignupBody validates', () => {
|
|
24
|
+
assert.throws(() => buildFromSignupBody({ title: 't', customerEmail: 'a@b.c' }), /siteId/);
|
|
25
|
+
assert.throws(() => buildFromSignupBody({ siteId: 1, title: 't', customerEmail: 'x' }), /customerEmail/);
|
|
26
|
+
assert.deepEqual(buildFromSignupBody({ siteId: 1, title: ' t ', customerEmail: 'a@b.c', sendEmail: true }), { siteId: 1, title: 't', customerEmail: 'a@b.c', sendEmail: true });
|
|
27
|
+
});
|
package/package.json
CHANGED