From 6ec0bafecbf8b6477d387dde04fc047f7e24f597 Mon Sep 17 00:00:00 2001 From: Maksim Eltyshev Date: Fri, 23 Jan 2026 17:20:12 +0100 Subject: [PATCH] fix: Prevent sending notifications to deactivated users --- .../api/helpers/notifications/create-many.js | 87 ++++++++++--------- .../api/helpers/notifications/create-one.js | 61 +++++++------ server/api/hooks/query-methods/models/User.js | 12 ++- 3 files changed, 89 insertions(+), 71 deletions(-) diff --git a/server/api/helpers/notifications/create-many.js b/server/api/helpers/notifications/create-many.js index 42fca744..0af7e622 100644 --- a/server/api/helpers/notifications/create-many.js +++ b/server/api/helpers/notifications/create-many.js @@ -296,57 +296,60 @@ module.exports = { }); const notificationsByUserId = _.groupBy(notifications, 'userId'); - const userIds = Object.keys(notificationsByUserId); - const notificationServices = await NotificationService.qm.getByUserIds(userIds); - const { transporter } = await sails.helpers.utils.makeSmtpTransporter(); + const notifiableUsers = await User.qm.getByIds(Object.keys(notificationsByUserId), { + withDeactivated: false, + }); - if (notificationServices.length > 0 || transporter) { - const users = await User.qm.getByIds(userIds); - const userById = _.keyBy(users, 'id'); + if (notifiableUsers.length > 0) { + const notifiableUserIds = sails.helpers.utils.mapRecords(notifiableUsers); - const notificationServicesByUserId = _.groupBy(notificationServices, 'userId'); + const notificationServices = await NotificationService.qm.getByUserIds(notifiableUserIds); + const { transporter } = await sails.helpers.utils.makeSmtpTransporter(); - Object.keys(notificationsByUserId).forEach(async (userId) => { - const notifiableUser = userById[userId]; - const t = sails.helpers.utils.makeTranslator(notifiableUser.language); + if (notificationServices.length > 0 || transporter) { + const notificationServicesByUserId = _.groupBy(notificationServices, 'userId'); - const emails = notificationsByUserId[userId].flatMap((notification) => { - const values = valuesById[notification.id]; + notifiableUsers.forEach(async (notifiableUser) => { + const t = sails.helpers.utils.makeTranslator(notifiableUser.language); - if (notificationServicesByUserId[userId]) { - const services = notificationServicesByUserId[userId].map((notificationService) => - _.pick(notificationService, ['url', 'format']), - ); + const emails = notificationsByUserId[notifiableUser.id].flatMap((notification) => { + const values = valuesById[notification.id]; - buildAndSendNotifications( - services, - inputs.board, - values.card, - notification, - values.creatorUser, - t, - ); + if (notificationServicesByUserId[notifiableUser.id]) { + const services = notificationServicesByUserId[notifiableUser.id].map( + (notificationService) => _.pick(notificationService, ['url', 'format']), + ); + + buildAndSendNotifications( + services, + inputs.board, + values.card, + notification, + values.creatorUser, + t, + ); + } + + if (transporter) { + return buildEmail( + inputs.board, + values.card, + notification, + values.creatorUser, + notifiableUser, + t, + ); + } + + return []; + }); + + if (emails.length > 0) { + sendEmails(transporter, emails); } - - if (transporter) { - return buildEmail( - inputs.board, - values.card, - notification, - values.creatorUser, - notifiableUser, - t, - ); - } - - return []; }); - - if (emails.length > 0) { - sendEmails(transporter, emails); - } - }); + } } return notifications; diff --git a/server/api/helpers/notifications/create-one.js b/server/api/helpers/notifications/create-one.js index f4b16c04..e92ac158 100644 --- a/server/api/helpers/notifications/create-one.js +++ b/server/api/helpers/notifications/create-one.js @@ -277,38 +277,43 @@ module.exports = { user: values.creatorUser, }); - const notificationServices = await NotificationService.qm.getByUserId(notification.userId); - const { transporter } = await sails.helpers.utils.makeSmtpTransporter(); + const notifiableUser = await User.qm.getOneById(notification.userId, { + withDeactivated: false, + }); - if (notificationServices.length > 0 || transporter) { - const notifiableUser = await User.qm.getOneById(notification.userId); - const t = sails.helpers.utils.makeTranslator(notifiableUser.language); + if (notifiableUser) { + const notificationServices = await NotificationService.qm.getByUserId(notification.userId); + const { transporter } = await sails.helpers.utils.makeSmtpTransporter(); - if (notificationServices.length > 0) { - const services = notificationServices.map((notificationService) => - _.pick(notificationService, ['url', 'format']), - ); + if (notificationServices.length > 0 || transporter) { + const t = sails.helpers.utils.makeTranslator(notifiableUser.language); - buildAndSendNotifications( - services, - inputs.board, - values.card, - notification, - values.creatorUser, - t, - ); - } + if (notificationServices.length > 0) { + const services = notificationServices.map((notificationService) => + _.pick(notificationService, ['url', 'format']), + ); - if (transporter) { - buildAndSendEmail( - transporter, - inputs.board, - values.card, - notification, - values.creatorUser, - notifiableUser, - t, - ); + buildAndSendNotifications( + services, + inputs.board, + values.card, + notification, + values.creatorUser, + t, + ); + } + + if (transporter) { + buildAndSendEmail( + transporter, + inputs.board, + values.card, + notification, + values.creatorUser, + notifiableUser, + t, + ); + } } } diff --git a/server/api/hooks/query-methods/models/User.js b/server/api/hooks/query-methods/models/User.js index ada2ea3e..839794f2 100644 --- a/server/api/hooks/query-methods/models/User.js +++ b/server/api/hooks/query-methods/models/User.js @@ -48,7 +48,17 @@ const createOne = async (values) => { return User.create({ ...values }).fetch(); }; -const getByIds = (ids) => defaultFind(ids); +const getByIds = (ids, { withDeactivated = true } = {}) => { + const criteria = { + id: ids, + }; + + if (!withDeactivated) { + criteria.isDeactivated = false; + } + + return defaultFind(criteria); +}; const getAll = ({ roleOrRoles } = {}) => defaultFind({