Skip to content

Commit

Permalink
fix: no duplicate notification inserts (#10380)
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Krick <[email protected]>
  • Loading branch information
mattkrick authored Oct 18, 2024
1 parent 6a3e770 commit 2450d5e
Showing 1 changed file with 2 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import getTypeFromEntityMap from 'parabol-client/utils/draftjs/getTypeFromEntityMap'
import generateUID from '../../../generateUID'
import getKysely from '../../../postgres/getKysely'
import {selectNotifications} from '../../../postgres/select'
import {Task} from '../../../postgres/types'
import {TaskInvolvesNotification} from '../../../postgres/types/Notification'
import {analytics} from '../../../utils/analytics/analytics'
Expand All @@ -14,8 +13,8 @@ const publishChangeNotifications = async (
) => {
const pg = getKysely()
const changeAuthorId = `${changeUser.id}::${task.teamId}`
const {entityMap: oldEntityMap, blocks: oldBlocks} = JSON.parse(oldTask.content)
const {entityMap, blocks} = JSON.parse(task.content)
const {entityMap: oldEntityMap} = JSON.parse(oldTask.content)
const {entityMap} = JSON.parse(task.content)
const wasPrivate = oldTask.tags.includes('private')
const isPrivate = task.tags.includes('private')
const oldMentions = wasPrivate ? [] : getTypeFromEntityMap('MENTION', oldEntityMap)
Expand Down Expand Up @@ -63,22 +62,6 @@ const publishChangeNotifications = async (
userIdsToRemove.push(oldTask.userId)
}

// if we updated the task content, push a new one with an updated task
const oldContentLen = oldBlocks[0] ? oldBlocks[0].text.length : 0
if (oldContentLen < 3) {
const contentLen = blocks[0] ? blocks[0].text.length : 0
if (contentLen > oldContentLen && task.userId) {
const maybeInvolvedUserIds = mentions.concat(task.userId)
const existingTaskNotifications = await selectNotifications()
.where('userId', 'in', maybeInvolvedUserIds)
.where('type', '=', 'TASK_INVOLVES')
.where('taskId', '=', task.id)
.$narrowType<TaskInvolvesNotification>()
.execute()
notificationsToAdd.push(...existingTaskNotifications)
}
}

// update changes in the db
if (notificationsToAdd.length) {
await pg.insertInto('Notification').values(notificationsToAdd).execute()
Expand Down

0 comments on commit 2450d5e

Please sign in to comment.