-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
103 lines (86 loc) · 2.98 KB
/
index.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict';
const fs = require('fs');
const csv = require('csv/lib/sync');
const lo = require('lodash');
const delay = require('delay');
const chalk = require('chalk');
const request = require('request-promise-native');
require('dotenv');
const HOOK_URL = process.env.HOOK_URL;
const HOOK_TOKEN = process.env.HOOK_TOKEN; // Optional
const SIMULATE_CSV_UPLOADER = process.env.SIMULATE_CSV_UPLOADER; // Optional
const START_ROW = process.env.START_ROW; // Optional
const CONCURRENCY = 8; // Kustomer limit is 16.6 (1000 rpm)
const DELAY = 1000;
(async function() {
const sheet = readCsv();
console.log(`Found ${chalk.yellow(sheet.length)} rows`);
const chunks = lo.chunk(sheet, CONCURRENCY);
let count = 0;
for (const [index, chunk] of chunks.entries()) {
const requests = chunk.map(row => sendRequest(row));
const responses = Promise.all(requests);
count = count + chunk.length;
for (const [i, res] of responses.entries()) {
if (!res) {
console.log(`${chalk.red('Error')}: Failed webhook for row ${chalk.yellow(index*CONCURRENCY + i + 1)}`);
count--;
}
}
console.log(`Uploaded: Chunk ${chalk.yellow(index+1)} (${chalk.yellow(count)} rows)`);
await delay(DELAY);
}
async function sendRequest(payload) {
const body = SIMULATE_CSV_UPLOADER === 'true' ? { upload: payload } : payload;
const params = {
url: HOOK_URL,
method: 'GET',
json: true,
auth: {
bearer: HOOK_TOKEN
},
body: body
};
try {
return await request(params);
} catch(e) {
return null;
}
}
function readCsv() {
const file = readFile();
const sheet = parseSheet(file);
const slicedSheet = sliceSheet(sheet);
return slicedSheet;
function readFile() {
try {
return fs.readFile('./data.csv');
} catch (e) {
console.log(`${chalk.red('Error')}: Could not find CSV. The file must be named "data.csv" and be located in this folder`);
process.exit(1);
}
}
function parseSheet(file) {
try {
return csv.parse(file, { columns: true });
} catch (e) {
console.log(`${chalk.red('Error')}: Could not parse CSV`);
process.exit(1);
}
}
function sliceSheet(sheet) {
const offset = calculateOffset();
return sheet.slice(offset);
function calculateOffset() {
const startRow = parseInt(START_ROW);
if (typeof startRow !== 'number' || isNaN(startRow)) {
return 0;
} else if (startRow < 1) {
return 0;
} else {
return startRow - 1;
}
}
}
}
})();