From d55b8e317dddcf26870726662c95bdcda3fb5d6f Mon Sep 17 00:00:00 2001 From: Marek Libra Date: Wed, 14 Aug 2024 14:13:18 +0200 Subject: [PATCH] Avoid false check for broadcast emails Signed-off-by: Marek Libra --- .changeset/flat-guests-behave.md | 6 ++++++ .../src/processor/NotificationsEmailProcessor.ts | 13 ++++++++++--- .../src/service/getUsersForEntityRef.ts | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 .changeset/flat-guests-behave.md diff --git a/.changeset/flat-guests-behave.md b/.changeset/flat-guests-behave.md new file mode 100644 index 0000000000..ad4b238e03 --- /dev/null +++ b/.changeset/flat-guests-behave.md @@ -0,0 +1,6 @@ +--- +'@backstage/plugin-notifications-backend-module-email': patch +'@backstage/plugin-notifications-backend': patch +--- + +Avoid sending broadcast emails as a fallback in case the entity-typed notification user can not be resolved. diff --git a/plugins/notifications-backend-module-email/src/processor/NotificationsEmailProcessor.ts b/plugins/notifications-backend-module-email/src/processor/NotificationsEmailProcessor.ts index 03388a77a2..a876a548d2 100644 --- a/plugins/notifications-backend-module-email/src/processor/NotificationsEmailProcessor.ts +++ b/plugins/notifications-backend-module-email/src/processor/NotificationsEmailProcessor.ts @@ -207,12 +207,17 @@ export class NotificationsEmailProcessor implements NotificationProcessor { private async getRecipientEmails( notification: Notification, options: NotificationSendOptions, - ) { + ): Promise { let emails: string[]; - if (options.recipients.type === 'broadcast' || notification.user === null) { + if (options.recipients.type === 'broadcast') { emails = await this.getBroadcastEmails(); - } else { + } else if (options.recipients.type === 'entity' && !!notification.user) { emails = await this.getUserEmail(notification.user); + } else { + this.logger.info( + `Unknown notification type ${options.recipients.type} or missing user.`, + ); + return []; } if (this.allowlistEmailAddresses) { @@ -338,6 +343,8 @@ export class NotificationsEmailProcessor implements NotificationProcessor { return; } + this.logger.debug(`Sending notification emails to: ${emails.join(',')}`); + if (!this.templateRenderer) { await this.sendPlainEmail(notification, emails); return; diff --git a/plugins/notifications-backend/src/service/getUsersForEntityRef.ts b/plugins/notifications-backend/src/service/getUsersForEntityRef.ts index fd564d6528..9ced24709b 100644 --- a/plugins/notifications-backend/src/service/getUsersForEntityRef.ts +++ b/plugins/notifications-backend/src/service/getUsersForEntityRef.ts @@ -157,5 +157,5 @@ export const getUsersForEntityRef = async ( users.push(...u); } - return [...new Set(users)]; + return [...new Set(users)].filter(Boolean); };