-
Notifications
You must be signed in to change notification settings - Fork 19
Deep logging for send notification API #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,21 +7,21 @@ | |||||
| import { NotificationActionTemplates } from "src/modules/notification_events/entity/notificationActionTemplates.entity"; | ||||||
| import NotifmeSdk from 'notifme-sdk'; | ||||||
| import { NotificationLog } from "../entity/notificationLogs.entity"; | ||||||
| import { NotificationService } from "../notification.service"; | ||||||
| import { NotificationService, maskEmail } from "../notification.service"; | ||||||
| import { LoggerUtil } from "src/common/logger/LoggerUtil"; | ||||||
| import { ERROR_MESSAGES, SUCCESS_MESSAGES } from "src/common/utils/constant.util"; | ||||||
|
|
||||||
| /** | ||||||
| * Interface for raw email data | ||||||
| */ | ||||||
| export interface RawEmailData { | ||||||
| to: string | string[]; | ||||||
| subject: string; | ||||||
| body: string; | ||||||
| from?: string; | ||||||
| isHtml?: boolean; | ||||||
| cc?: string[]; | ||||||
| bcc?: string[]; | ||||||
| to: string | string[]; | ||||||
| subject: string; | ||||||
| body: string; | ||||||
| from?: string; | ||||||
| isHtml?: boolean; | ||||||
| cc?: string[]; | ||||||
| bcc?: string[]; | ||||||
| } | ||||||
|
|
||||||
| @Injectable() | ||||||
|
|
@@ -35,35 +35,61 @@ | |||||
| * @param notificationDataArray Array of notification data objects | ||||||
| * @returns Results of notification attempts | ||||||
| */ | ||||||
| async sendNotification(notificationDataArray) { | ||||||
| async sendNotification(notificationDataArray, traceId?: string) { | ||||||
|
Check failure on line 38 in src/modules/notification/adapters/emailService.adapter.ts
|
||||||
| const results = []; | ||||||
| for (const notificationData of notificationDataArray) { | ||||||
|
|
||||||
| try { | ||||||
| const recipient = notificationData.recipient; | ||||||
| if (!recipient || !this.isValidEmail(recipient)) { | ||||||
| throw new BadRequestException(ERROR_MESSAGES.INVALID_EMAIL); | ||||||
| } | ||||||
| const result = await this.send(notificationData); | ||||||
|
|
||||||
| const loggingData = { ...notificationData }; | ||||||
| if (loggingData.recipient && typeof loggingData.recipient === 'string') { | ||||||
| loggingData.recipient = maskEmail(loggingData.recipient); | ||||||
| } | ||||||
| if (loggingData.cc && Array.isArray(loggingData.cc)) { | ||||||
| loggingData.cc = loggingData.cc.map((email: string) => maskEmail(email)); | ||||||
| } | ||||||
| if (loggingData.bcc && Array.isArray(loggingData.bcc)) { | ||||||
| loggingData.bcc = loggingData.bcc.map((email: string) => maskEmail(email)); | ||||||
| } | ||||||
| delete loggingData.body; | ||||||
| if (loggingData.replacements && loggingData.replacements['{OTP}']) { | ||||||
|
Check warning on line 59 in src/modules/notification/adapters/emailService.adapter.ts
|
||||||
| loggingData.replacements = { ...loggingData.replacements }; | ||||||
| delete loggingData.replacements['{OTP}']; | ||||||
| } | ||||||
|
|
||||||
| LoggerUtil.log(`ADAPTER_PREP ${traceId}`, traceId, '', 'info', { ...loggingData, status: 'ADAPTER_PREP', traceId: traceId }); | ||||||
|
|
||||||
| const startTime = Date.now(); | ||||||
| const result = await this.send(notificationData, traceId); | ||||||
| const timeTakenInMs = Date.now() - startTime; | ||||||
|
|
||||||
| if (result.status === 'success') { | ||||||
| LoggerUtil.log(`SENT ${traceId}`, traceId, '', 'info', { status: 'SENT', traceId: traceId, timeTaken: timeTakenInMs }); | ||||||
| results.push({ | ||||||
| recipient: recipient, | ||||||
| status: 200, | ||||||
| result: SUCCESS_MESSAGES.EMAIL_NOTIFICATION_SEND_SUCCESSFULLY | ||||||
| }); | ||||||
| } else { | ||||||
| LoggerUtil.error(`FAILED ${traceId}`, result.message, traceId, '', { status: 'FAILED', traceId: traceId, timeTaken: timeTakenInMs }); | ||||||
| results.push({ | ||||||
| recipient: recipient, | ||||||
| status: 'error', | ||||||
| error: `Email not sent: ${JSON.stringify(result.errors)}` | ||||||
| error: `Email not sent: ${JSON.stringify(result.message)}` | ||||||
| }); | ||||||
| } | ||||||
| } | ||||||
| catch (error) { | ||||||
| LoggerUtil.error(ERROR_MESSAGES.EMAIL_NOTIFICATION_FAILED, error); | ||||||
| const timeTakenInMs = Date.now() - (error.startTime || Date.now()); | ||||||
| LoggerUtil.error(`FAILED ${traceId}`, error.message, traceId, '', { status: 'FAILED', traceId: traceId, timeTaken: timeTakenInMs }); | ||||||
|
Comment on lines
86
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Line 87 references 🛠️ Proposed fix — capture startTime before try block async sendNotification(notificationDataArray, traceId?: string) {
const results = [];
for (const notificationData of notificationDataArray) {
-
+ const startTime = Date.now();
try {
const recipient = notificationData.recipient;
if (!recipient || !this.isValidEmail(recipient)) {
throw new BadRequestException(ERROR_MESSAGES.INVALID_EMAIL);
}
// ... existing masking code ...
- const startTime = Date.now();
const result = await this.send(notificationData, traceId);
const timeTakenInMs = Date.now() - startTime;
// ... rest of try block ...
}
catch (error) {
- const timeTakenInMs = Date.now() - (error.startTime || Date.now());
+ const timeTakenInMs = Date.now() - startTime;
LoggerUtil.error(`FAILED ${traceId}`, error.message, traceId, '', { status: 'FAILED', traceId: traceId, timeTaken: timeTakenInMs });Apply the same pattern to 🤖 Prompt for AI Agents |
||||||
| results.push({ | ||||||
| recipient: notificationData.recipient, | ||||||
| status: 'error', | ||||||
| error: error.toString() | ||||||
| error: error.message | ||||||
| }); | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -75,49 +101,77 @@ | |||||
| * @param rawEmailDataArray Array of raw email data objects | ||||||
| * @returns Results of raw email sending attempts | ||||||
| */ | ||||||
| async sendRawEmails(emailData) { | ||||||
| async sendRawEmails(traceId, emailData) { | ||||||
|
Check failure on line 104 in src/modules/notification/adapters/emailService.adapter.ts
|
||||||
| const results = []; | ||||||
|
|
||||||
| // Convert to array if not already an array | ||||||
| const emailDataArray = Array.isArray(emailData) ? emailData : [emailData]; | ||||||
|
|
||||||
| for (const singleEmailData of emailDataArray) { | ||||||
| try { | ||||||
| if (!singleEmailData.to || !this.isValidEmail(singleEmailData.to)) { | ||||||
| throw new BadRequestException(ERROR_MESSAGES.INVALID_EMAIL); | ||||||
| } | ||||||
|
|
||||||
| if (!singleEmailData.subject || !singleEmailData.body) { | ||||||
| throw new BadRequestException("Subject and Email body are required"); | ||||||
| try { | ||||||
|
|
||||||
| if (!singleEmailData.to || !this.isValidEmail(singleEmailData.to)) { | ||||||
| throw new BadRequestException(ERROR_MESSAGES.INVALID_EMAIL); | ||||||
| } | ||||||
|
|
||||||
| if (!singleEmailData.subject || !singleEmailData.body) { | ||||||
| throw new BadRequestException("Subject and Email body are required"); | ||||||
| } | ||||||
|
|
||||||
| const loggingData = { ...singleEmailData }; | ||||||
| if (loggingData.to) { | ||||||
| if (Array.isArray(loggingData.to)) { | ||||||
| loggingData.to = loggingData.to.map((email: string) => maskEmail(email)); | ||||||
| } else if (typeof loggingData.to === 'string') { | ||||||
| loggingData.to = maskEmail(loggingData.to as string); | ||||||
| } | ||||||
| } | ||||||
| if (loggingData.cc && Array.isArray(loggingData.cc)) { | ||||||
| loggingData.cc = loggingData.cc.map((email: string) => maskEmail(email)); | ||||||
| } | ||||||
| if (loggingData.bcc && Array.isArray(loggingData.bcc)) { | ||||||
| loggingData.bcc = loggingData.bcc.map((email: string) => maskEmail(email)); | ||||||
| } | ||||||
| delete loggingData.body; | ||||||
| if (loggingData.replacements && loggingData.replacements['{OTP}']) { | ||||||
|
Check warning on line 136 in src/modules/notification/adapters/emailService.adapter.ts
|
||||||
| loggingData.replacements = { ...loggingData.replacements }; | ||||||
| delete loggingData.replacements['{OTP}']; | ||||||
| } | ||||||
| LoggerUtil.log(`ADAPTER_PREP ${traceId}`, traceId, '', 'info', { ...loggingData, status: 'ADAPTER_PREP', traceId: traceId }); | ||||||
|
|
||||||
| const startTime = Date.now(); | ||||||
| const result = await this.sendRawEmail(singleEmailData); | ||||||
| const timeTakenInMs = Date.now() - startTime; | ||||||
|
|
||||||
| if (result.status === 'success') { | ||||||
| LoggerUtil.log(`SENT ${traceId}`, traceId, '', 'info', { status: 'SENT', traceId: traceId, timeTaken: timeTakenInMs }); | ||||||
| results.push({ | ||||||
| to: singleEmailData.to, | ||||||
| status: 200, | ||||||
| result: SUCCESS_MESSAGES.EMAIL_NOTIFICATION_SEND_SUCCESSFULLY, | ||||||
| messageId: result.messageId || `email-${Date.now()}` | ||||||
| }); | ||||||
| } else { | ||||||
| LoggerUtil.error(`FAILED ${traceId}`, JSON.stringify(result), traceId, '', { status: 'FAILED', traceId: traceId, timeTaken: timeTakenInMs }); | ||||||
| results.push({ | ||||||
| to: singleEmailData.to, | ||||||
| status: 400, | ||||||
| error: `Email not sent: ${JSON.stringify(result.errors)}` | ||||||
| }); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| const result = await this.sendRawEmail(singleEmailData); | ||||||
| if (result.status === 'success') { | ||||||
| results.push({ | ||||||
| to: singleEmailData.to, | ||||||
| status: 200, | ||||||
| result: SUCCESS_MESSAGES.EMAIL_NOTIFICATION_SEND_SUCCESSFULLY, | ||||||
| messageId: result.messageId || `email-${Date.now()}` | ||||||
| }); | ||||||
| } else { | ||||||
| results.push({ | ||||||
| to: singleEmailData.to, | ||||||
| status: 400, | ||||||
| error: `Email not sent: ${JSON.stringify(result.errors)}` | ||||||
| }); | ||||||
| catch (error) { | ||||||
| const timeTakenInMs = Date.now() - (error.startTime || Date.now()); | ||||||
| LoggerUtil.error(`FAILED ${traceId}`, error.message || error.toString(), traceId, '', { status: 'FAILED', traceId: traceId, timeTaken: timeTakenInMs }); | ||||||
| results.push({ | ||||||
| recipient: singleEmailData.to, | ||||||
| status: 500, | ||||||
| error: error.message || error.toString() | ||||||
| }); | ||||||
| } | ||||||
| } | ||||||
| catch (error) { | ||||||
| LoggerUtil.error(ERROR_MESSAGES.EMAIL_NOTIFICATION_FAILED, error); | ||||||
| results.push({ | ||||||
| recipient: singleEmailData.to, | ||||||
| status: 500, | ||||||
| error: error.message || error.toString() | ||||||
| }); | ||||||
| } | ||||||
| } | ||||||
| return results; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Creates a notification log entry | ||||||
|
|
@@ -186,14 +240,14 @@ | |||||
| /** | ||||||
| * Sends template-based email | ||||||
| */ | ||||||
| async send(notificationData) { | ||||||
| async send(notificationData, traceId) { | ||||||
| // Note: CC and BCC are not logged in notificationLogs for privacy/security reasons | ||||||
| // The NotificationLog entity doesn't have CC/BCC fields, and BCC is meant to be hidden | ||||||
| const notificationLogs = this.createNotificationLog(notificationData, notificationData.subject, notificationData.key, notificationData.body, notificationData.recipient); | ||||||
| try { | ||||||
| const emailConfig = this.getEmailConfig(notificationData.context); | ||||||
| const notifmeSdk = new NotifmeSdk(emailConfig); | ||||||
|
|
||||||
| const result = await notifmeSdk.send({ | ||||||
| email: { | ||||||
| from: emailConfig.email.from, | ||||||
|
|
@@ -207,17 +261,17 @@ | |||||
| if (result.status === 'success') { | ||||||
| notificationLogs.status = true; | ||||||
| await this.notificationServices.saveNotificationLogs(notificationLogs); | ||||||
| LoggerUtil.log(SUCCESS_MESSAGES.EMAIL_NOTIFICATION_SEND_SUCCESSFULLY); | ||||||
| LoggerUtil.log(`traceId: ${traceId} SUCCESS_MESSAGES.EMAIL_NOTIFICATION_SEND_SUCCESSFULLY`, traceId, "", "info",); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo in log message—string interpolation includes literal text. Line 264 logs 🛠️ Proposed fix- LoggerUtil.log(`traceId: ${traceId} SUCCESS_MESSAGES.EMAIL_NOTIFICATION_SEND_SUCCESSFULLY`, traceId, "", "info",);
+ LoggerUtil.log(`traceId: ${traceId} ${SUCCESS_MESSAGES.EMAIL_NOTIFICATION_SEND_SUCCESSFULLY}`, traceId, "", "info");📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| return result; | ||||||
| } | ||||||
| else { | ||||||
| throw new Error(`Email not send ${JSON.stringify(result.errors)}`) | ||||||
| } | ||||||
| } | ||||||
| catch (e) { | ||||||
| LoggerUtil.error(ERROR_MESSAGES.EMAIL_NOTIFICATION_FAILED, e); | ||||||
| LoggerUtil.error(ERROR_MESSAGES.EMAIL_NOTIFICATION_FAILED, e.message); | ||||||
| notificationLogs.status = false; | ||||||
| notificationLogs.error = e.toString(); | ||||||
| notificationLogs.error = e.message; | ||||||
| await this.notificationServices.saveNotificationLogs(notificationLogs); | ||||||
| return e; | ||||||
| } | ||||||
|
|
@@ -236,11 +290,11 @@ | |||||
| emailData.body, | ||||||
| emailData.to as string | ||||||
| ); | ||||||
|
|
||||||
| try { | ||||||
| const emailConfig = this.getEmailConfig('raw-email'); | ||||||
| const notifmeSdk = new NotifmeSdk(emailConfig); | ||||||
|
|
||||||
| const result = await notifmeSdk.send({ | ||||||
| email: { | ||||||
| from: emailData.from || emailConfig.email.from, | ||||||
|
|
@@ -251,14 +305,14 @@ | |||||
| ...(emailData.bcc && Array.isArray(emailData.bcc) && emailData.bcc.length > 0 ? { bcc: emailData.bcc } : {}), | ||||||
| }, | ||||||
| }); | ||||||
|
|
||||||
| if (result.status === 'success') { | ||||||
| notificationLogs.status = true; | ||||||
| await this.notificationServices.saveNotificationLogs(notificationLogs); | ||||||
| LoggerUtil.log(SUCCESS_MESSAGES.EMAIL_NOTIFICATION_SEND_SUCCESSFULLY); | ||||||
| return { | ||||||
| ...result, | ||||||
| messageId: result.id || `email-${Date.now()}` | ||||||
| messageId: result.id || `email - ${Date.now()}` | ||||||
| }; | ||||||
| } else { | ||||||
| throw new Error(`Email not sent: ${JSON.stringify(result.errors)}`) | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RawEmailData.tonow allows arrays, but this validator still rejects them.Line 18 advertises
string | string[], yet Line 113 passessingleEmailData.tostraight intoisValidEmail(). Any caller that uses the new multi-recipient shape will fail validation beforesendRawEmail()even though the rest of this method already handles arrays.🛠️ Proposed fix
Also applies to: 113-115
🤖 Prompt for AI Agents