@@ -116,9 +116,13 @@ async function isBroadCastMessageForUser(userId, bulkMessage, memberInfo, userGr
116
116
*
117
117
* @param {Integer } userId
118
118
* @param {Integer } bulkMessageId
119
- * @param {Integer } notificationId
119
+ * @param {Object } notificationObj
120
120
*/
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
+ }
122
126
try {
123
127
const r = await models . BulkMessageUserRefs . create ( {
124
128
bulk_message_id : bulkMessageId ,
@@ -129,6 +133,15 @@ async function insertUserRefs(userId, bulkMessageId, notificationId) {
129
133
return r
130
134
} catch ( e ) {
131
135
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
+ }
132
145
throw new Error ( `insertUserRefs() : ${ e } ` )
133
146
}
134
147
}
@@ -155,7 +168,7 @@ async function createNotificationForUser(userId, bulkMessage) {
155
168
} )
156
169
logger . info ( `${ logPrefix } Inserted notification record ${ n . id } for current user ${ userId } ` )
157
170
// 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 )
159
172
return result
160
173
} catch ( e ) {
161
174
logger . error ( `${ logPrefix } insert broadcast notification error: ${ e } ` )
0 commit comments