@platform-x/hep-notification-client 1.1.19 → 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,7 +226,9 @@ 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');
|
|
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');
|
|
221
232
|
const Mail = sgHelpers.classes.Mail;
|
|
222
233
|
let email_from = isCFF ? config_1.default === null || config_1.default === void 0 ? void 0 : config_1.default.CFF_MAIL_FROM : config_1.default === null || config_1.default === void 0 ? void 0 : config_1.default.EMAIL_FROM;
|
|
223
234
|
logger_1.Logger.info('sendEmailWithPersonalizations : Email From:', JSON.stringify(email_from));
|
|
@@ -231,19 +242,40 @@ class EmailHandler {
|
|
|
231
242
|
// Set personalizations as per the emailConfig
|
|
232
243
|
if ((emailConfig === null || emailConfig === void 0 ? void 0 : emailConfig.personalizations) &&
|
|
233
244
|
(emailConfig === null || emailConfig === void 0 ? void 0 : emailConfig.personalizations.to.length)) {
|
|
234
|
-
const { to } = emailConfig === null || emailConfig === void 0 ? void 0 : emailConfig.personalizations;
|
|
235
|
-
|
|
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++) {
|
|
236
267
|
const personalization = new Personalization();
|
|
237
|
-
personalization.addTo(
|
|
238
|
-
if (
|
|
239
|
-
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) {
|
|
240
271
|
if (key) {
|
|
241
|
-
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]);
|
|
242
273
|
}
|
|
243
274
|
}
|
|
244
275
|
}
|
|
245
276
|
mail.addPersonalization(personalization);
|
|
246
|
-
}
|
|
277
|
+
}
|
|
278
|
+
// Handle attachments
|
|
247
279
|
if ((emailConfig === null || emailConfig === void 0 ? void 0 : emailConfig.attachments) && Array.isArray(emailConfig.attachments)) {
|
|
248
280
|
emailConfig.attachments.forEach((attachment) => {
|
|
249
281
|
mail.addAttachment({
|
|
@@ -257,9 +289,17 @@ class EmailHandler {
|
|
|
257
289
|
}
|
|
258
290
|
}
|
|
259
291
|
else {
|
|
260
|
-
// set to email for non personalized email
|
|
292
|
+
// set to email for non personalized email with CC support
|
|
261
293
|
const personalization = new Personalization();
|
|
262
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
|
+
}
|
|
263
303
|
logger_1.Logger.info('sendEmailWithPersonalizations : Email personalizations:', JSON.stringify(personalization));
|
|
264
304
|
mail.addPersonalization(personalization);
|
|
265
305
|
}
|
|
@@ -277,6 +317,28 @@ class EmailHandler {
|
|
|
277
317
|
});
|
|
278
318
|
});
|
|
279
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
|
+
}
|
|
280
342
|
/**
|
|
281
343
|
* prepare personalize email request to send email through sendgrid
|
|
282
344
|
* @param reqBody
|
|
@@ -286,7 +348,7 @@ class EmailHandler {
|
|
|
286
348
|
return __awaiter(this, void 0, void 0, function* () {
|
|
287
349
|
var _a, _b;
|
|
288
350
|
try {
|
|
289
|
-
let { email_type, replacement_variables, recipients, isCFF = false } = reqBody;
|
|
351
|
+
let { email_type, replacement_variables, recipients, cc_recipients, isCFF = false } = reqBody;
|
|
290
352
|
logger_1.Logger.info(`EmailHandler: reached in sendPersonalizeEmailRequest with emailType: ${email_type}`, 'sendPersonalizeEmailRequest');
|
|
291
353
|
// disable SOLR because of connectivity issue
|
|
292
354
|
logger_1.Logger.debug('Reached sendPersonalizeEmailRequest in emailHandler', 'sendPersonalizeEmailRequest');
|
|
@@ -305,6 +367,16 @@ class EmailHandler {
|
|
|
305
367
|
recipientsData = {
|
|
306
368
|
to,
|
|
307
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
|
+
}
|
|
308
380
|
}
|
|
309
381
|
if (templateData && ((_a = Object.keys(templateData)) === null || _a === void 0 ? void 0 : _a.length) !== 0) {
|
|
310
382
|
const root = (0, node_html_parser_1.parse)(templateData === null || templateData === void 0 ? void 0 : templateData.body);
|