-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulk-customer-insert.js
executable file
·66 lines (61 loc) · 1.8 KB
/
bulk-customer-insert.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
#!/usr/bin/env node
let argv = require('yargs').argv;
let axios = require('axios');
let csv = require('csv-parser');
let fs = require('fs');
let HEADERS = ['email', 'firstname', 'lastname', 'password'];
let CUSTOMER_ENDPOINT = '/api/V1/customers';
let ROWS = [];
const axiosConfig = {
headers: {
'Content-Type': 'application/json',
},
};
const insertCustomer = async (url, data) => {
return new Promise((resolve, reject) => {
axios.post(url, data, axiosConfig).then(response => {
resolve(response.data);
}).catch(e => {
reject(e.response.data);
});
});
}
if (argv.file === undefined) {
console.log('--file flag cannot be empty.');
console.log('Sample usage:');
console.log(' ./bulkCustomerInsert.js --file=sample.csv');
} else if (argv.url === undefined) {
console.log('--url flag cannot be empty.');
console.log('Sample usage:');
console.log(' ./bulkCustomerInsert.js --url=localhost:3000');
} else {
let filePath = argv.file;
let urlPath= argv.url;
fs.createReadStream(filePath)
.pipe(csv())
.on('headers', (headers) => {
let difference = HEADERS.filter(x => !headers.includes(x));
if (difference.length !== 0) {
throw new Error('csv headers are corrupt. Headers should contain "email", "firstname", "lastname", "password"');
}
})
.on('data', (data) => {
ROWS.push(data);
})
.on('end', async () => {
for (let i = 0; i < ROWS.length; i++) {
let row = ROWS[i];
console.log('Index: ', i);
console.log('Row data:', row);
try {
let result = await insertCustomer(urlPath + CUSTOMER_ENDPOINT, row);
console.log(result);
} catch (e) {
console.log(e);
}
}
})
.on('error', (error) => {
console.log(error);
});
}