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
| const axios = require('axios');
const { writeFile } = require('fs');
const API_BASE_URL = 'https://my-api.com';
const PROFILE_ENDPOINT = `${API_BASE_URL}/v1/profile`;
let failedUsers = 0;
const fileOptions = {
flag: 'a'
};
const writeUser = (response) => {
const { data: { profile: { email, password } } } = response;
const obj = {
email,
password
};
writeFile(outputFileName, JSON.stringify(obj, null, 2) + ',' + '\n', fileOptions, err => err ? console.error(err) : '');
};
const logError = (err) => {
if (err) {
console.error(err);
failedUsers++;
}
}
const seedUsers = async () => {
console.log(`Creating ${desiredProfileCount} profiles and writing login credentials to ${outputFileName}`);
const promises = [];
const requestBody = {
serviceData: [
"CREDIT_CARD"
]
};
for (let i = 0; i < desiredProfileCount; i++) {
const promise = axios.post(PROFILE_ENDPOINT, requestBody)
.then(writeUser)
.catch(logError);
promises.push(promise);
}
await Promise.all(promises);
console.log(`\nUsers have been added. ${failedUsers} request(s) failed.`);
}
const validateInput = (args) => {
let result = true;
if (args[0] === undefined || isNaN(args[0])) {
console.log("ERROR: 1st argument is not a number or is missing, please pass desired number of profiles");
result = false;
}
if (args[1] === undefined) {
console.log("ERROR: 2nd argument missing, please pass an output file name.");
result = false;
}
if (!result) {
console.log("Example Usage: \"node asyncCreateUsers.js 10 output.json\"");
console.log("The above example writes 10 profiles to output.json\n");
}
return result;
};
const args = process.argv.slice(2);
const validInput = validateInput(args);
const desiredProfileCount = args[0];
const outputFileName = args[1];
if (validInput) {
seedUsers();
}
|