-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger.js
112 lines (103 loc) · 2.82 KB
/
trigger.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
104
105
106
107
108
109
110
111
112
const { Knock } = require("@knocklabs/node");
const prompt = require("prompt");
const fs = require("fs");
const { parse } = require("csv-parse");
require("dotenv").config();
const knock = new Knock(process.env.KNOCK_API_KEY);
prompt.start();
const foo = async () => {
//get users from CSV
let files = fs.readdirSync(`./`);
let file = files.find((x) => x.toLowerCase().includes(".csv"));
if (!file) {
console.error("Missing CSV file in root directory.");
return;
}
let line = 1;
let fullnameIndex = -1;
let emailIndex = -1;
let users = [];
let emails = [];
fs.createReadStream(file)
.pipe(parse({ delimiter: ",", from_line: 1 }))
.on("data", function (row) {
if (line === 1) {
//header row with column names
row.forEach((col) => {
let colName = col.trim();
let index = row.indexOf(col);
switch (colName) {
case "fullname":
fullnameIndex = index;
break;
case "email":
emailIndex = index;
}
});
} else if (emailIndex >= 0 && fullnameIndex >= 0) {
//add email and name to user list
let user = {
id: row[emailIndex],
name: row[fullnameIndex],
email: row[emailIndex],
};
emails.push(row[emailIndex]);
users.push(user);
}
line++;
})
.on("end", async function () {
if (!emails.length) {
console.error("No emails found in csv file");
return;
}
console.log(
"Emails found",
emails.slice(0, 3),
`and ${emails.length - 3} more`
);
console.log(
"What Knock workflow would you like to trigger? (enter the key to the workflow)"
);
const { workflow_key } = await prompt
.get({
name: "workflow_key",
required: true,
})
.catch(console.err);
console.log(
`are you sure you would like to trigger the workflow '${workflow_key}' for ${emails.length} people?`
);
let goodYes = ["yes", "y"];
let goodNo = ["no", "n"];
const { confirm } = await prompt
.get({
name: "confirm",
required: true,
conform: (val) => {
if (
goodYes.includes(val.toLowerCase()) ||
goodNo.includes(val.toLowerCase())
) {
return true;
}
return false;
},
})
.catch(console.err);
if (goodNo.includes(confirm.toLowerCase())) {
console.log("Ok, goodbye.");
return;
}
try {
await knock.notify(workflow_key, {
// actor: "DSGT Knock Script",
recipients: emails,
});
console.log("DONE!");
} catch (err) {
console.error(err);
}
});
};
foo();