@@ -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