Repository navigation
Expand file tree
/
Copy pathbulk-send-csv.mjs
More file actions
69 lines (66 loc) · 2.45 KB
/
Copy pathbulk-send-csv.mjs
File metadata and controls
69 lines (66 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Send one envelope per row of a CSV file (columns: name,email,customer_ref).
// Usage: node bulk-send-csv.mjs signers.csv
import { readFile } from 'node:fs/promises';
import { api, buildServiceAgreement, fail, getConfig, isMain } from './lib.mjs';
/** Minimal CSV parser: header row, comma separated, double quotes for values with commas. */
export function parseCsv(text) {
const rows = [];
for (const line of text.split(/\r?\n/)) {
if (!line.trim()) continue;
const cells = [];
let cell = '';
let quoted = false;
for (let i = 0; i < line.length; i += 1) {
const ch = line[i];
if (quoted) {
if (ch === '"' && line[i + 1] === '"') {
cell += '"';
i += 1;
} else if (ch === '"') quoted = false;
else cell += ch;
} else if (ch === '"') quoted = true;
else if (ch === ',') {
cells.push(cell);
cell = '';
} else cell += ch;
}
cells.push(cell);
rows.push(cells.map((c) => c.trim()));
}
const [header, ...data] = rows;
return data.map((cells) => Object.fromEntries(header.map((h, i) => [h, cells[i] ?? ''])));
}
/** Sends envelopes one at a time and keeps going when a row fails. */
export async function bulkSend(config, rows, { delayMs = 250 } = {}) {
const results = [];
for (const [index, row] of rows.entries()) {
const payload = buildServiceAgreement({
name: row.name,
email: row.email,
metadata: { customer_ref: row.customer_ref, batch_row: String(index + 1) },
});
try {
const envelope = await api(config, 'POST', '/v1/envelopes', { json: payload });
results.push({ row: index + 1, email: row.email, ok: true, envelopeId: envelope.id });
} catch (error) {
results.push({ row: index + 1, email: row.email, ok: false, error: error.message });
}
if (delayMs) await new Promise((resolve) => setTimeout(resolve, delayMs));
}
return results;
}
if (isMain(import.meta.url)) {
const file = process.argv[2] || 'signers.csv';
try {
const rows = parseCsv(await readFile(file, 'utf8'));
const results = await bulkSend(getConfig(), rows);
for (const r of results) {
console.log(r.ok ? `row ${r.row} ${r.email}: sent ${r.envelopeId}` : `row ${r.row} ${r.email}: FAILED ${r.error}`);
}
const failed = results.filter((r) => !r.ok).length;
console.log(`${results.length - failed} sent, ${failed} failed`);
if (failed) process.exit(1);
} catch (error) {
fail(error);
}
}