@platform-x/hep-notification-client 1.1.20 → 1.1.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -3,6 +3,7 @@ interface EmailOptions {
|
|
|
3
3
|
emailTemplate: string;
|
|
4
4
|
subject: string;
|
|
5
5
|
bcc?: string;
|
|
6
|
+
cc?: string | string[];
|
|
6
7
|
personalizations?: Personalize;
|
|
7
8
|
}
|
|
8
9
|
interface Personalize {
|
|
@@ -42,6 +43,12 @@ export declare class EmailHandler {
|
|
|
42
43
|
* @returns
|
|
43
44
|
*/
|
|
44
45
|
sendEmailWithPersonalizations(emailConfig: any, isCFF?: any): Promise<unknown>;
|
|
46
|
+
/**
|
|
47
|
+
* Utility method to prepare CC emails in standardized format
|
|
48
|
+
* @param ccEmails - Can be string, array of strings, or Recipients array
|
|
49
|
+
* @returns Array of email strings
|
|
50
|
+
*/
|
|
51
|
+
private prepareCCEmails;
|
|
45
52
|
/**
|
|
46
53
|
* prepare personalize email request to send email through sendgrid
|
|
47
54
|
* @param reqBody
|
|
@@ -77,13 +77,22 @@ class EmailHandler {
|
|
|
77
77
|
return __awaiter(this, void 0, void 0, function* () {
|
|
78
78
|
logger_1.Logger.info('EmailHandler: Reached sendEmail method', 'sendEmail');
|
|
79
79
|
sgMail.setApiKey(config_1.default.SENDGRID_API_KEY);
|
|
80
|
+
// Build personalization object
|
|
81
|
+
const personalization = {
|
|
82
|
+
to: [{ email: emailConfig.email }],
|
|
83
|
+
};
|
|
84
|
+
// Add CC emails if provided
|
|
85
|
+
if (emailConfig.cc) {
|
|
86
|
+
const ccEmails = Array.isArray(emailConfig.cc) ? emailConfig.cc : [emailConfig.cc];
|
|
87
|
+
personalization.cc = ccEmails.map((email) => ({ email }));
|
|
88
|
+
logger_1.Logger.info(`Adding CC emails to non-personalized email: ${ccEmails.join(', ')}`, 'sendEmail');
|
|
89
|
+
}
|
|
90
|
+
// Add BCC if provided
|
|
91
|
+
if (emailConfig.bcc) {
|
|
92
|
+
personalization.bcc = [{ email: emailConfig.bcc }];
|
|
93
|
+
}
|
|
80
94
|
const msg = {
|
|
81
|
-
personalizations: [
|
|
82
|
-
{
|
|
83
|
-
to: [{ email: emailConfig.email }],
|
|
84
|
-
// cc: emailCC,
|
|
85
|
-
},
|
|
86
|
-
],
|
|
95
|
+
personalizations: [personalization],
|
|
87
96
|
from: { email: config_1.default.EMAIL_FROM },
|
|
88
97
|
html: emailConfig.emailTemplate,
|
|
89
98
|
subject: emailConfig.subject,
|
|
@@ -217,6 +226,7 @@ class EmailHandler {
|
|
|
217
226
|
*/
|
|
218
227
|
sendEmailWithPersonalizations(emailConfig, isCFF) {
|
|
219
228
|
return __awaiter(this, void 0, void 0, function* () {
|
|
229
|
+
var _a, _b, _c, _d, _e, _f;
|
|
220
230
|
logger_1.Logger.info(`EmailHandler: Reached sendEmailWithPersonalizations method with emailConfig: ${JSON.stringify(emailConfig)}`, 'sendEmailWithPersonalizations');
|
|
221
231
|
logger_1.Logger.info(`SENDGRID CREDENTIALS-----------------> ${config_1.default === null || config_1.default === void 0 ? void 0 : config_1.default.SENDGRID_API_KEY} EMAIL_FROM-------> ${config_1.default === null || config_1.default === void 0 ? void 0 : config_1.default.EMAIL_FROM}`, 'triggerEmails');
|
|
222
232
|
const Mail = sgHelpers.classes.Mail;
|
|
@@ -232,19 +242,40 @@ class EmailHandler {
|
|
|
232
242
|
// Set personalizations as per the emailConfig
|
|
233
243
|
if ((emailConfig === null || emailConfig === void 0 ? void 0 : emailConfig.personalizations) &&
|
|
234
244
|
(emailConfig === null || emailConfig === void 0 ? void 0 : emailConfig.personalizations.to.length)) {
|
|
235
|
-
const { to } = emailConfig === null || emailConfig === void 0 ? void 0 : emailConfig.personalizations;
|
|
236
|
-
|
|
245
|
+
const { to, cc } = emailConfig === null || emailConfig === void 0 ? void 0 : emailConfig.personalizations;
|
|
246
|
+
// First personalization with TO and CC
|
|
247
|
+
const firstPersonalization = new Personalization();
|
|
248
|
+
firstPersonalization.addTo(to[0].to);
|
|
249
|
+
// Add CC emails to first personalization only (to avoid duplicate CC emails)
|
|
250
|
+
if (cc && cc.length > 0) {
|
|
251
|
+
cc.forEach((ccData) => {
|
|
252
|
+
firstPersonalization.addCc(ccData.to);
|
|
253
|
+
logger_1.Logger.info(`Adding CC email to personalized email: ${ccData.to}`, 'sendEmailWithPersonalizations');
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
// Add TO substitutions for first recipient
|
|
257
|
+
if ((_a = to[0]) === null || _a === void 0 ? void 0 : _a.substitutions) {
|
|
258
|
+
for (const key in (_b = to[0]) === null || _b === void 0 ? void 0 : _b.substitutions) {
|
|
259
|
+
if (key) {
|
|
260
|
+
firstPersonalization === null || firstPersonalization === void 0 ? void 0 : firstPersonalization.addSubstitution(key, (_c = to[0]) === null || _c === void 0 ? void 0 : _c.substitutions[key]);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
mail.addPersonalization(firstPersonalization);
|
|
265
|
+
// Remaining personalizations with only TO (no CC to avoid duplicates)
|
|
266
|
+
for (let i = 1; i < to.length; i++) {
|
|
237
267
|
const personalization = new Personalization();
|
|
238
|
-
personalization.addTo(
|
|
239
|
-
if (
|
|
240
|
-
for (const key in
|
|
268
|
+
personalization.addTo(to[i].to);
|
|
269
|
+
if ((_d = to[i]) === null || _d === void 0 ? void 0 : _d.substitutions) {
|
|
270
|
+
for (const key in (_e = to[i]) === null || _e === void 0 ? void 0 : _e.substitutions) {
|
|
241
271
|
if (key) {
|
|
242
|
-
personalization === null || personalization === void 0 ? void 0 : personalization.addSubstitution(key,
|
|
272
|
+
personalization === null || personalization === void 0 ? void 0 : personalization.addSubstitution(key, (_f = to[i]) === null || _f === void 0 ? void 0 : _f.substitutions[key]);
|
|
243
273
|
}
|
|
244
274
|
}
|
|
245
275
|
}
|
|
246
276
|
mail.addPersonalization(personalization);
|
|
247
|
-
}
|
|
277
|
+
}
|
|
278
|
+
// Handle attachments
|
|
248
279
|
if ((emailConfig === null || emailConfig === void 0 ? void 0 : emailConfig.attachments) && Array.isArray(emailConfig.attachments)) {
|
|
249
280
|
emailConfig.attachments.forEach((attachment) => {
|
|
250
281
|
mail.addAttachment({
|
|
@@ -258,9 +289,17 @@ class EmailHandler {
|
|
|
258
289
|
}
|
|
259
290
|
}
|
|
260
291
|
else {
|
|
261
|
-
// set to email for non personalized email
|
|
292
|
+
// set to email for non personalized email with CC support
|
|
262
293
|
const personalization = new Personalization();
|
|
263
294
|
personalization.addTo(emailConfig.email);
|
|
295
|
+
// Add CC emails if provided in non-personalized mode
|
|
296
|
+
if (emailConfig.cc) {
|
|
297
|
+
const ccEmails = Array.isArray(emailConfig.cc) ? emailConfig.cc : [emailConfig.cc];
|
|
298
|
+
ccEmails.forEach((ccEmail) => {
|
|
299
|
+
personalization.addCc(ccEmail);
|
|
300
|
+
logger_1.Logger.info(`Adding CC email to non-personalized email: ${ccEmail}`, 'sendEmailWithPersonalizations');
|
|
301
|
+
});
|
|
302
|
+
}
|
|
264
303
|
logger_1.Logger.info('sendEmailWithPersonalizations : Email personalizations:', JSON.stringify(personalization));
|
|
265
304
|
mail.addPersonalization(personalization);
|
|
266
305
|
}
|
|
@@ -278,6 +317,28 @@ class EmailHandler {
|
|
|
278
317
|
});
|
|
279
318
|
});
|
|
280
319
|
}
|
|
320
|
+
/**
|
|
321
|
+
* Utility method to prepare CC emails in standardized format
|
|
322
|
+
* @param ccEmails - Can be string, array of strings, or Recipients array
|
|
323
|
+
* @returns Array of email strings
|
|
324
|
+
*/
|
|
325
|
+
prepareCCEmails(ccEmails) {
|
|
326
|
+
if (!ccEmails)
|
|
327
|
+
return [];
|
|
328
|
+
if (typeof ccEmails === 'string') {
|
|
329
|
+
return [ccEmails];
|
|
330
|
+
}
|
|
331
|
+
if (Array.isArray(ccEmails)) {
|
|
332
|
+
return ccEmails.map((cc) => {
|
|
333
|
+
if (typeof cc === 'string')
|
|
334
|
+
return cc;
|
|
335
|
+
if (cc.email)
|
|
336
|
+
return cc.email; // Recipients object
|
|
337
|
+
return cc.to || cc; // ToWithSubstitutions object
|
|
338
|
+
}).filter(email => email && typeof email === 'string');
|
|
339
|
+
}
|
|
340
|
+
return [];
|
|
341
|
+
}
|
|
281
342
|
/**
|
|
282
343
|
* prepare personalize email request to send email through sendgrid
|
|
283
344
|
* @param reqBody
|
|
@@ -287,7 +348,7 @@ class EmailHandler {
|
|
|
287
348
|
return __awaiter(this, void 0, void 0, function* () {
|
|
288
349
|
var _a, _b;
|
|
289
350
|
try {
|
|
290
|
-
let { email_type, replacement_variables, recipients, isCFF = false } = reqBody;
|
|
351
|
+
let { email_type, replacement_variables, recipients, cc_recipients, isCFF = false } = reqBody;
|
|
291
352
|
logger_1.Logger.info(`EmailHandler: reached in sendPersonalizeEmailRequest with emailType: ${email_type}`, 'sendPersonalizeEmailRequest');
|
|
292
353
|
// disable SOLR because of connectivity issue
|
|
293
354
|
logger_1.Logger.debug('Reached sendPersonalizeEmailRequest in emailHandler', 'sendPersonalizeEmailRequest');
|
|
@@ -306,6 +367,16 @@ class EmailHandler {
|
|
|
306
367
|
recipientsData = {
|
|
307
368
|
to,
|
|
308
369
|
};
|
|
370
|
+
// Add CC recipients if they exist
|
|
371
|
+
if (cc_recipients && (0, lodash_1.isArray)(cc_recipients)) {
|
|
372
|
+
const cc = [];
|
|
373
|
+
cc_recipients.forEach((ccRecipient) => {
|
|
374
|
+
cc.push({
|
|
375
|
+
to: ccRecipient === null || ccRecipient === void 0 ? void 0 : ccRecipient.email,
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
recipientsData.cc = cc;
|
|
379
|
+
}
|
|
309
380
|
}
|
|
310
381
|
if (templateData && ((_a = Object.keys(templateData)) === null || _a === void 0 ? void 0 : _a.length) !== 0) {
|
|
311
382
|
const root = (0, node_html_parser_1.parse)(templateData === null || templateData === void 0 ? void 0 : templateData.body);
|