-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathemail.service.js
155 lines (151 loc) Β· 4.83 KB
/
email.service.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
"use strict";
const logger = require("./logger.service");
const client = require("@sendgrid/mail");
const fs = require("fs");
const path = require("path");
const TAG = `[ EMAIL.SERVICE ]`;
const env = require("../services/env.service");
const Constants = require("../constants/general.constant");
const Handlebars = require("handlebars");
class EmailService {
constructor(apiKey) {
client.setApiKey(apiKey);
}
/**
* Send one email
* @param {*} mailData
* @param {(err?)=>void} callback
*/
send(mailData, callback = () => {}) {
if (env.isTest()) {
//Silence all actual emails if we're testing
mailData.mailSettings = {
sandboxMode: {
enable: true
}
};
}
return client.send(mailData, false, (error) => {
if (error) {
logger.error(`${TAG} ` + JSON.stringify(error));
callback(error);
} else {
callback();
}
});
}
/**
* Send separate emails to the list of users in mailData
* @param {*} mailData
* @param {(err?)=>void} callback
*/
sendMultiple(mailData, callback = () => {}) {
return client.sendMultiple(mailData, (error) => {
if (error) {
logger.error(`${TAG} ` + JSON.stringify(error));
callback(error);
} else {
callback();
}
});
}
/**
* Send email with ticket.
* @param {string} firstName the recipient's first name
* @param {string} recipient the recipient's email address
* @param {string} ticket the ticket image (must be base-64 string)
* @param {(err?)=>void} callback
*/
sendWeekOfEmail(firstName, recipient, ticket, callback) {
const handlebarsPath = path.join(
__dirname,
`../assets/email/Ticket.hbs`
);
const html = this.renderEmail(handlebarsPath, {
firstName: firstName,
ticket: ticket
});
const mailData = {
to: recipient,
from: process.env.NO_REPLY_EMAIL,
subject: Constants.EMAIL_SUBJECTS[Constants.WEEK_OF],
html: html
};
this.send(mailData).then((response) => {
if (
!response ||
(response[0].statusCode >= 200 && response[0].statusCode < 300)
) {
callback();
} else {
callback(response[0]);
}
}, callback);
}
/**
* Send email with ticket.
* @param {string} firstName the recipient's first name
* @param {string} recipient the recipient's email address
* @param {(err?)=>void} callback
*/
sendDayOfEmail(firstName, recipient, callback) {
const handlebarsPath = path.join(
__dirname,
`../assets/email/Welcome.hbs`
);
const html = this.renderEmail(handlebarsPath, {
firstName: firstName
});
const mailData = {
to: recipient,
from: process.env.NO_REPLY_EMAIL,
subject: Constants.EMAIL_SUBJECTS[Constants.WEEK_OF],
html: html
};
this.send(mailData).then((response) => {
if (
!response ||
(response[0].statusCode >= 200 && response[0].statusCode < 300)
) {
callback();
} else {
callback(response[0]);
}
}, callback);
}
sendStatusUpdate(firstName, recipient, status, callback) {
const handlebarsPath = path.join(
__dirname,
`../assets/email/statusEmail/${status}.hbs`
);
const mailData = {
to: recipient,
from: process.env.NO_REPLY_EMAIL,
subject: Constants.EMAIL_SUBJECTS[status],
html: this.renderEmail(handlebarsPath, {
firstName: firstName
})
};
this.send(mailData).then((response) => {
if (
!response ||
(response[0].statusCode >= 200 && response[0].statusCode < 300)
) {
callback();
} else {
callback(response[0]);
}
}, callback);
}
/**
* Generates the HTML from the handlebars template file found at the given path.
* @param {string} path the absolute path to the handlebars template file
* @param {*} context any variables that need to be replaced in the template file
*/
renderEmail(path, context) {
const templateStr = fs.readFileSync(path).toString();
const template = Handlebars.compile(templateStr);
return template(context);
}
}
module.exports = new EmailService(process.env.SENDGRID_API_KEY);