-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
142 lines (130 loc) · 4.88 KB
/
index.js
File metadata and controls
142 lines (130 loc) · 4.88 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';
const path = require('path');
const csv = require('csv');
const parse = require('csv-parse');
const fs = require('fs');
const mailHelper = require('./helpers/mailer');
const handlebars = require('handlebars');
// app/email specific variables
const csvRecords = [];
let eventRegistrations = [];
let failedRecords = [];
const emailColIndexInCSV = 2; // based on the data in your csv file
const nameColIndexInCSV = 1; // based on the data in your csv file
const sourceCSVFile = path.join(__dirname, './files/test.csv');
const outCSVFile = path.join(__dirname, './files/error.csv');
const emailTemplate = 'registration-email.html'; // will be used when sending emails
const senderEmailAddress = 'admin@mydomain.com'; // this will show up in the target user's inbox as who sent the email
const emailVars = {
name: 'My Event Name',
date: '03-30-2020',
time: '3:00 PM',
venue: 'Grand America Hotel | Salt Lake City',
venueLink: 'https://www.google.com/maps/place/The+Grand+America+Hotel/@40.7574406,-111.8902221,15z/data=!4m2!3m1!1s0x0:0x6fae71fa7326abbd?sa=X&ved=0ahUKEwinuISQtdfaAhUHPo8KHXQODCQQ_BII4AEwCg'
}
const emailSubject = `Registration - ${emailVars.name}`;
/**
* @author Ahsan Ayaz
* @desc Entry method for the script
*/
const startExecution = async () => {
await readCSVFile()
await sendEmailsToTargetUsers();
console.log('** Emails sent **');
}
/**
* @author Ahsan Ayaz
* @desc Reads the CSV file for the records containing user emails and user names
* Creates a records array for the data from CSV
* @returns {Promise} which gets resolved when the csv stream ends
*/
const readCSVFile = () => {
return new Promise((resolve, reject) => {
fs.createReadStream(sourceCSVFile)
.pipe(parse({delimiter: ','}))
.on('data', (csvRow) => {
csvRecords.push(csvRow);
})
.on('end', () => {
resolve();
});
});
}
/**
* @author Siraj Haq
* @desc Writes into CSV file for failed records
* Creates a records error.csv file for the data passed
* @returns {Promise} which gets resolved when data is written in csv
*/
const writeCSVFile = (records) => {
return new Promise((resolve, reject) => {
try {
csv.stringify(records, { header: true }, (err, output) => {
if (err) throw err;
fs.writeFile(outCSVFile, output, (err) => {
if (err) throw err;
console.log('csv file saved.');
resolve();
});
});
} catch (ex) {
console.log(ex);
reject(ex);
}
});
}
/**
* @author Ahsan Ayaz
* @desc Fetches the email template, compiles while replacing the variable placeholders with real data.
* Then sends the email to the target users
* @returns {Promise} which gets resolved once all the emails have been sent.
*/
const sendEmailsToTargetUsers = async () => {
eventRegistrations = csvRecords;
failedRecords = [csvRecords[0]]; //add header for failed records csv
eventRegistrations.splice(0, 1);
// uncomment the below chunk and put your email & name for testing
// eventRegistrations = [{
// name: "Test User",
// email: 'test@test.com'
// }];
return new Promise(async (resolve, reject) => {
try{
const rawHtml = await mailHelper.readHTMLTemplate(__dirname + "/templates/" + emailTemplate);
const template = handlebars.compile(rawHtml);
for (let i = 0, len = eventRegistrations.length; i < len; ++i) {
const registration = eventRegistrations[i];
const replacements = {
userName: registration[nameColIndexInCSV],
eventName: emailVars.name,
eventVenue: emailVars.venue,
eventDate: emailVars.date,
eventTime: emailVars.time,
eventVenueLink: emailVars.venueLink
};
const htmlToSend = template(replacements);
const mailOptions = {
from: senderEmailAddress,
to: registration[emailColIndexInCSV],
subject: emailSubject,
html: htmlToSend
};
let emailResponse = await mailHelper.sendMail(mailOptions);
if (!emailResponse) {
//Failure Case: Push Failed records in an array
failedRecords.push(eventRegistrations[i]);
}
}
if(failedRecords.length > 1) {
//Write into csv file if records failed
await writeCSVFile(failedRecords);
}
resolve();
} catch (ex) {
console.log(ex);
reject(ex)
}
});
}
// initialize the awesome!
startExecution();