Skip to content

Commit 4eddd09

Browse files
author
sachin-maheshwari
authored
Merge pull request #201 from topcoder-platform/dev
Avoid duplicate broadcast notifications
2 parents 4796443 + b14e314 commit 4eddd09

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

migrations/v2.0.4.sql

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
-- query to identify the duplicate rows in bulk_message_user_refs table
3+
SELECT count(*), bulk_message_id, user_id FROM "bulk_message_user_refs"
4+
GROUP BY bulk_message_id, user_id HAVING count(*) > 1;
5+
6+
-- create temp table and store duplicate broadcast notification rows
7+
SELECT * INTO temptable FROM "Notifications" WHERE id IN
8+
(
9+
SELECT a.notification_id FROM "bulk_message_user_refs" AS "a",
10+
"bulk_message_user_refs" AS "b"
11+
WHERE a.id < b.id
12+
AND a.bulk_message_id = b.bulk_message_id
13+
AND a.user_id = b.user_id
14+
);
15+
16+
-- DELETE duplicate rows from bulk_message_user_refs table
17+
DELETE FROM "bulk_message_user_refs" AS "a"
18+
USING "bulk_message_user_refs" AS "b"
19+
WHERE a.id < b.id
20+
AND a.bulk_message_id = b.bulk_message_id
21+
AND a.user_id = b.user_id;
22+
23+
-- DELETE duplicate rows from Notifications
24+
DELETE FROM "Notifications"
25+
WHERE id IN (SELECT id FROM temptable)
26+
AND type = 'admin.notification.broadcast';
27+
28+
-- make unique column to avoid duplicate insert
29+
ALTER TABLE bulk_message_user_refs ADD UNIQUE (bulk_message_id, user_id);
30+
31+
-- get duplicate broadcast rows
32+
SELECT * FROM "Notifications" AS a
33+
LEFT JOIN "bulk_message_user_refs" AS b
34+
ON a.id=b.notification_id
35+
WHERE a.type='admin.notification.broadcast'
36+
AND b.id IS NULL;
37+

src/hooks/hookBulkMessage.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,13 @@ async function isBroadCastMessageForUser(userId, bulkMessage, memberInfo, userGr
116116
*
117117
* @param {Integer} userId
118118
* @param {Integer} bulkMessageId
119-
* @param {Integer} notificationId
119+
* @param {Object} notificationObj
120120
*/
121-
async function insertUserRefs(userId, bulkMessageId, notificationId) {
121+
async function insertUserRefs(userId, bulkMessageId, notificationObj) {
122+
let notificationId = null
123+
if (notificationObj) {
124+
notificationId = notificationObj.id
125+
}
122126
try {
123127
const r = await models.BulkMessageUserRefs.create({
124128
bulk_message_id: bulkMessageId,
@@ -129,6 +133,15 @@ async function insertUserRefs(userId, bulkMessageId, notificationId) {
129133
return r
130134
} catch (e) {
131135
logger.error(`${logPrefix} Failed to insert userRef record for user: ${userId}, error: ${e}`)
136+
if (notificationId && notificationObj) {
137+
try {
138+
await notificationObj.destroy()
139+
logger.info(`Deleted/reverted duplicate/ref-transaction failed, broadcast notification ${notificationId} for user: ${userId}`)
140+
} catch (error) {
141+
logger.error(`Error in deleting duplicate notification record, ${error}`)
142+
}
143+
144+
}
132145
throw new Error(`insertUserRefs() : ${e}`)
133146
}
134147
}
@@ -155,7 +168,7 @@ async function createNotificationForUser(userId, bulkMessage) {
155168
})
156169
logger.info(`${logPrefix} Inserted notification record ${n.id} for current user ${userId}`)
157170
// TODO need to be in transaction so that rollback will be possible
158-
const result = await insertUserRefs(userId, bulkMessage.id, n.id)
171+
const result = await insertUserRefs(userId, bulkMessage.id, n)
159172
return result
160173
} catch (e) {
161174
logger.error(`${logPrefix} insert broadcast notification error: ${e} `)

0 commit comments

Comments
 (0)