Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
204 lines (173 loc) · 6.14 KB
/
Copy pathindex.js
File metadata and controls
204 lines (173 loc) · 6.14 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* @module SendinBlue adapter for parse-server
* @description Used to send reset password and verification emails through SendinBlue
*/
var SendinBlueSdk = require("sib-api-v3-sdk");
var SendinBlueClient = SendinBlueSdk.ApiClient.instance;
var sendinBlueAdapter = options => {
// check the required options
if (!options || !options.apiKey ||
!options.fromName || !options.fromEmail ||
!options.translation || !options.translation.default
)
{
throw "SendinBlueAdapter requires: an API key, a default email for the sender, a default name for the sender and the translation default options.";
}
// check the password reset options
if (!options.passwordResetTemplateId &&
(!options.passwordResetSubject || !options.passwordResetTextPart || !options.passwordResetHtmlPart)
)
{
throw "If passwordResetTemplateId is not set, you have to define passwordResetSubject, passwordResetTextPart and passwordResetHtmlPart.";
}
// check the validation options
if (!options.verificationEmailTemplateId &&
(!options.verificationEmailSubject || !options.verificationEmailTextPart || !options.verificationEmailHtmlPart)
)
{
throw "If verificationEmailTemplateId is not set, you have to define verificationEmailSubject, verificationEmailTextPart and verificationEmailHtmlPart.";
}
var SendinBlueApiKey = SendinBlueClient.authentications["api-key"];
SendinBlueApiKey.apiKey = options.apiKey;
/**
* @function getOptions
* @description Get the [individual] options for the specific mailing task
*/
var getOptions = mail => {
if (options.getIndividualOptions) {
return Promise.resolve(options.getIndividualOptions(mail)).then(opts => {
return Object.assign({}, options, opts);
});
}
else {
return Promise.resolve(options);
}
};
/**
* @function replaceVariables
* @description Replace the email, application's name, and link variables with
* with the correct value into the given text
*/
var replaceVariables = (text, mail) => {
var result = text;
if (result) {
result = result.replace("{EMAIL}", mail.to);
result = result.replace("%APP_NAME%", mail.appName);
result = result.replace("%LINK%", mail.link);
result = result.replace("%LINK_SHORT%", mail.link.replace(/^https?\:\/\//i, ""));
}
return result;
};
/**
* @function sendLink
* @description Sends the reset password or verify email links
*/
var sendLink = (mail, opts, templates, subjects, textParts, htmlParts) => {
// lookup for email in username field if email is undefined
var email = mail.user.get("email") || mail.user.get("username");
// look if a locale field is defined for the User class
var locale = options.translation.default;
if (options.translation.locale) {
var user_locale = mail.user.get(options.translation.locale);
if (user_locale) {
locale = user_locale;
}
}
// get the template id according to the locale of the user
if (templates) {
var templateId = templates[locale];
if (!templateId) {
templateId = templates[options.translation.default];
}
// construct and send the request to SendinBlue
var smtpApi = new SendinBlueSdk.SMTPApi();
var sendEmail = new SendinBlueSdk.SendEmail();
sendEmail.emailTo = [email];
sendEmail.attributes = {
"APP_NAME": mail.appName,
"LINK": mail.link,
"LINK_SHORT": mail.link.replace(/^https?\:\/\//i, "")
};
return new Promise((resolve, reject) => {
smtpApi.sendTemplate(templateId, sendEmail).then(resolve, reject);
});
}
// if not template was defined, use the plain/html options
else {
return sendMail({
appName: mail.appName,
link: mail.link,
to: email,
subject: subjects[locale] ? subjects[locale] : subjects[options.translation.default],
text: textParts[locale] ? textParts[locale] : textParts[options.translation.default],
html: htmlParts[locale] ? htmlParts[locale] : htmlParts[options.translation.default]
});
}
};
/**
* @function sendPasswordResetEmail
* @description Sends the link to reset the password
*/
var sendPasswordResetEmail = mail => {
return getOptions(mail).then(opts => {
return sendLink(
mail,
opts,
opts.passwordResetTemplateId,
opts.passwordResetSubject,
opts.passwordResetTextPart,
opts.passwordResetHtmlPart
);
});
};
/**
* @function sendVerificationEmail
* @description Sends the link to verify an email
*/
var sendVerificationEmail = mail => {
return getOptions(mail).then(opts => {
return sendLink(
mail,
opts,
opts.verificationEmailTemplateId,
opts.verificationEmailSubject,
opts.verificationEmailTextPart,
opts.verificationEmailHtmlPart
);
});
};
/**
* @function sendMail
* @description Sends a pre-defined email
*/
var sendMail = mail => {
return getOptions(mail).then(opts => {
// replace the variables into the subject, plain text and html text
var subject = replaceVariables(mail.subject, mail);
var text = replaceVariables(mail.text, mail);
var html = replaceVariables(mail.html, mail);
// construct and send the request to SendinBlue
var smtpApi = new SendinBlueSdk.SMTPApi();
var sendSmtpEmail = new SendinBlueSdk.SendSmtpEmail();
var sender = new SendinBlueSdk.SendSmtpEmailSender();
sender.name = opts.fromName;
sender.email = opts.fromEmail;
var to = new SendinBlueSdk.SendSmtpEmailTo();
to.email = mail.to;
sendSmtpEmail.sender = sender;
sendSmtpEmail.to = [to];
sendSmtpEmail.subject = subject;
sendSmtpEmail.textContent = text;
sendSmtpEmail.htmlContent = html;
return new Promise((resolve, reject) => {
smtpApi.sendTransacEmail(sendSmtpEmail).then(resolve, reject);
});
});
};
return Object.freeze({
sendVerificationEmail: sendVerificationEmail,
sendPasswordResetEmail: sendPasswordResetEmail,
sendMail: sendMail
});
};
module.exports = sendinBlueAdapter;