From a2931a52b0fc0581edb8b3a1c16955fb6c21b748 Mon Sep 17 00:00:00 2001 From: Clara Youdale Date: Mon, 13 Nov 2023 16:24:07 -0300 Subject: [PATCH] Fix/submission emails (#2405) * Only send email to the submitter of that particular issue * Fix editor filter * Fix test for pending submissions --- .../e2e/unit/functions/processNotifications.cy.js | 8 ++++++-- site/gatsby-site/src/templates/citeTemplate.js | 1 + site/realm/functions/processNotifications.js | 10 ++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/site/gatsby-site/cypress/e2e/unit/functions/processNotifications.cy.js b/site/gatsby-site/cypress/e2e/unit/functions/processNotifications.cy.js index fd4f88bdc4..a6f326f061 100644 --- a/site/gatsby-site/cypress/e2e/unit/functions/processNotifications.cy.js +++ b/site/gatsby-site/cypress/e2e/unit/functions/processNotifications.cy.js @@ -269,9 +269,14 @@ const stubEverything = () => { .returns({ toArray: () => subscriptionsToIncidentUpdates }); } + const incidentIds = pendingNotificationsToPromotedIncidents.map( + (pendingNotification) => pendingNotification.incident_id + ); + stub .withArgs({ type: SUBSCRIPTION_TYPE.submissionPromoted, + incident_id: { $in: incidentIds }, }) .as(`subscriptions.find("${SUBSCRIPTION_TYPE.submissionPromoted}")`) .returns({ toArray: () => subscriptionsToPromotedIncidents }); @@ -450,6 +455,7 @@ describe('Functions', () => { expect(subscriptionsCollection.find.getCall(5).args[0]).to.deep.equal({ type: SUBSCRIPTION_TYPE.submissionPromoted, + incident_id: { $in: [217] }, }); for (const subscription of subscriptionsToPromotedIncidents) { @@ -465,8 +471,6 @@ describe('Functions', () => { incident_id: pendingNotification.incident_id, }); - console.log('subscriptionsToPromotedIncidents', global.context.functions); - const userIds = subscriptionsToPromotedIncidents.map((subscription) => subscription.userId); const incident = incidents.find((i) => i.incident_id == pendingNotification.incident_id); diff --git a/site/gatsby-site/src/templates/citeTemplate.js b/site/gatsby-site/src/templates/citeTemplate.js index 97a9c6fa5f..76641c73a8 100644 --- a/site/gatsby-site/src/templates/citeTemplate.js +++ b/site/gatsby-site/src/templates/citeTemplate.js @@ -248,6 +248,7 @@ function CiteTemplate({ reportCount: sortedReports.length, incidentDate: incident.date, editors: incident.editors + .filter((editor) => editor && editor.first_name && editor.last_name) .map(({ first_name, last_name }) => `${first_name} ${last_name}`) .join(', '), }} diff --git a/site/realm/functions/processNotifications.js b/site/realm/functions/processNotifications.js index 74c795ad97..3af86ef9ec 100644 --- a/site/realm/functions/processNotifications.js +++ b/site/realm/functions/processNotifications.js @@ -4,7 +4,7 @@ const getRecipients = async (userIds) => { for (const userId of userIds) { const userResponse = await context.functions.execute('getUser', { userId }); - if (userResponse.email) { + if (userResponse?.email) { recipients.push({ email: userResponse.email, userId, @@ -262,13 +262,19 @@ exports = async function () { // Notifications to New Promotions try { + + // Finds all pending notifications to New Promotions const pendingNotificationsToNewPromotions = await notificationsCollection.find({ processed: false, type: 'submission-promoted' }).toArray(); + // Gets all incident ids from pending notifications to New Promotions + const pendingNotificationsIncidentIds = pendingNotificationsToNewPromotions.map((notification) => notification.incident_id); + if (pendingNotificationsToNewPromotions.length > 0) { result += pendingNotificationsToNewPromotions.length; - const subscriptionsToNewPromotions = await subscriptionsCollection.find({ type: 'submission-promoted' }).toArray(); + // Finds all subscriptions to New Promotions for those new incidents + const subscriptionsToNewPromotions = await subscriptionsCollection.find({ type: 'submission-promoted', incident_id: { $in: pendingNotificationsIncidentIds } }).toArray(); // Process subscriptions to New Incidents if (subscriptionsToNewPromotions.length > 0) {