From 8c8979ea33613366f6fd072fdbabdf5f7134820f Mon Sep 17 00:00:00 2001 From: Michael Piazza Date: Thu, 23 Jun 2022 17:45:46 +0000 Subject: [PATCH] [PAY-370] Dedupe reactions --- .../reactionNotification.js | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/identity-service/src/notifications/processNotifications/reactionNotification.js b/identity-service/src/notifications/processNotifications/reactionNotification.js index 416bd6912df..8cf03c40104 100644 --- a/identity-service/src/notifications/processNotifications/reactionNotification.js +++ b/identity-service/src/notifications/processNotifications/reactionNotification.js @@ -2,14 +2,39 @@ const models = require('../../models') const { notificationTypes } = require('../constants') async function processReactionNotifications (notifications, tx) { + const notifsToReturn = [] + for (const notification of notifications) { const { slot, initiator: reactorId, metadata: { reaction_value: reactionValue, reacted_to_entity: reactedToEntity, reaction_type: reactionType } } = notification // TODO: unhardcode assumptions about the userId receiving the notification, when // we have additional reaction types. - await models.SolanaNotification.findOrCreate({ + const existingNotification = await models.SolanaNotification.findOne({ where: { + type: notificationTypes.Reaction, + userId: reactedToEntity.tip_sender_id, // The user receiving the reaction is the user who sent the tip + entityId: reactorId, // The user who sent the reaction + metadata: { + reactionType, + reactedToEntity + } + }, + transaction: tx + }) + + // In the case that the notification already exists, avoid returning it to prevent + // sending it a second time. Just update the original reaction value. + if (existingNotification) { + // Have to recreate the metadata object for save to work properly + existingNotification.metadata = { + ...existingNotification.metadata, + reactionValue + } + await existingNotification.save({ transaction: tx }) + } else { + notifsToReturn.push(notification) + await models.SolanaNotification.create({ slot, type: notificationTypes.Reaction, userId: reactedToEntity.tip_sender_id, // The user receiving the reaction is the user who sent the tip @@ -20,10 +45,14 @@ async function processReactionNotifications (notifications, tx) { reactionValue } }, - transaction: tx - }) + { + transaction: tx + } + ) + } } - return notifications + + return notifsToReturn } module.exports = processReactionNotifications