Skip to content

Commit

Permalink
fix: email bcc bug
Browse files Browse the repository at this point in the history
  • Loading branch information
KishenKumarrrrr committed Sep 13, 2023
1 parent b8bab32 commit 117d398
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
4 changes: 3 additions & 1 deletion backend/src/email/services/email-transactional.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ async function handleStatusCallbacks(
}
break
case SesEventType.Open:
// Cannot check that open applies to the main recipient
await EmailMessageTransactional.update(
{
status: TransactionalEmailMessageStatus.Opened,
Expand All @@ -202,13 +203,14 @@ async function handleStatusCallbacks(
)
break
case SesEventType.Send:
// Cannot check that send applies to the main recipient
await EmailMessageTransactional.update(
{
status: TransactionalEmailMessageStatus.Sent,
sentAt: metadata.timestamp,
},
{
where: { id },
where: { id, errorCode: null },
}
)
break
Expand Down
47 changes: 47 additions & 0 deletions backend/src/email/utils/callback/parsers/ses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ const parseNotificationAndEvent = async (
message: any,
metadata: Metadata
): Promise<void> => {
if (!isNotificationAndEventForMainRecipient(message, type)) {
logger.info({
message: 'SES notification or event is not for the main recipient',
action: 'filterNotification',
body: message,
})
return
}
switch (type) {
case SesEventType.Delivery:
await updateDeliveredStatus(metadata)
Expand Down Expand Up @@ -250,4 +258,43 @@ const parseRecord = async (record: SesRecord): Promise<void> => {
}
}

// Checks whether the notification/event is meant for the main recipient of the email.
function isNotificationAndEventForMainRecipient(
message: any,
type: SesEventType
): boolean {
// We cannot filter "OPEN" and "SEND" events due to the response given by AWS SES
if (type === SesEventType.Open || type === SesEventType.Send) {
return true
}
// There must be atleast one recipient of an email
const mainRecipient: string = message?.mail?.commonHeaders?.to[0]
if (!mainRecipient) {
throw new Error('Failed to find main recipient in message')
}
const mainRecipientDelivered = message?.delivery?.recipients?.some(
(e: string) => e === mainRecipient
)
const mainRecipientBounced = message.bounce?.bouncedRecipients?.some(
(e: any) => e.emailAddress === mainRecipient
)
const mainRecipientComplained = message.complaint?.complainedRecipients?.some(
(e: any) => e.emailAddress === mainRecipient
)

logger.info({
message: 'SES notification filter result',
deliveryRecipients: message?.delivery?.recipients,
bouncedRecipients: message.bounce?.bouncedRecipients,
complainedRecipients: message.complaint?.complainedRecipients,
mainRecipient,
result:
mainRecipientBounced || mainRecipientDelivered || mainRecipientComplained,
})

return (
mainRecipientBounced || mainRecipientDelivered || mainRecipientComplained
)
}

export { HttpEvent, SesRecord, isEvent, parseRecord, validateSignature }
2 changes: 1 addition & 1 deletion backend/src/email/utils/callback/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const updateMessageWithRead = async (
status: 'READ',
} as EmailMessage,
{
where: { id: messageId },
where: { id: messageId, errorCode: null },
returning: true,
}
)
Expand Down

0 comments on commit 117d398

Please sign in to comment.