diff --git a/backend/src/middleware/notifications/notificationsMiddleware.js b/backend/src/middleware/notifications/notificationsMiddleware.js index 56c7366d8..054519289 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.js @@ -25,7 +25,9 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { MATCH (user: User) WHERE user.id in $idsOfUsers AND NOT (user)<-[:BLOCKED]-(author) - MERGE (post)-[:NOTIFIED {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt }]->(user) + MERGE (post)-[notification:NOTIFIED {reason: $reason}]->(user) + SET notification.read = FALSE + SET notification.createdAt = $createdAt ` break } @@ -36,7 +38,9 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { WHERE user.id in $idsOfUsers AND NOT (user)<-[:BLOCKED]-(author) AND NOT (user)<-[:BLOCKED]-(postAuthor) - MERGE (comment)-[:NOTIFIED {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt }]->(user) + MERGE (comment)-[notification:NOTIFIED {reason: $reason}]->(user) + SET notification.read = FALSE + SET notification.createdAt = $createdAt ` break } @@ -47,7 +51,9 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { WHERE user.id in $idsOfUsers AND NOT (user)<-[:BLOCKED]-(author) AND NOT (author)<-[:BLOCKED]-(user) - MERGE (comment)-[:NOTIFIED {id: apoc.create.uuid(), read: false, reason: $reason, createdAt: $createdAt }]->(user) + MERGE (comment)-[notification:NOTIFIED {reason: $reason}]->(user) + SET notification.read = FALSE + SET notification.createdAt = $createdAt ` break } diff --git a/backend/src/middleware/notifications/notificationsMiddleware.spec.js b/backend/src/middleware/notifications/notificationsMiddleware.spec.js index 9c0fb61bf..66d86c645 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.spec.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.spec.js @@ -80,6 +80,7 @@ describe('notifications', () => { notifications(read: $read, orderBy: createdAt_desc) { read reason + createdAt from { __typename ... on Post { @@ -160,6 +161,7 @@ describe('notifications', () => { notifications: [ { read: false, + createdAt: expect.any(String), reason: 'comment_on_post', from: { __typename: 'Comment', @@ -250,6 +252,7 @@ describe('notifications', () => { notifications: [ { read: false, + createdAt: expect.any(String), reason: 'mentioned_in_post', from: { __typename: 'Post', @@ -272,6 +275,9 @@ describe('notifications', () => { }) describe('updates the post and mentions me again', () => { + const expectedUpdatedContent = + '
One more mention to

@al-capone

and again:

@al-capone

and again

@al-capone

' + const updatePostAction = async () => { const updatedContent = ` One more mention to @@ -303,18 +309,17 @@ describe('notifications', () => { it('creates no duplicate notification for the same resource', async () => { await createPostAction() await updatePostAction() - const expectedContent = - '
One more mention to

@al-capone

and again:

@al-capone

and again

@al-capone

' const expected = expect.objectContaining({ data: { notifications: [ { read: false, + createdAt: expect.any(String), reason: 'mentioned_in_post', from: { __typename: 'Post', id: 'p47', - content: expectedContent, + content: expectedUpdatedContent, }, }, ], @@ -329,12 +334,67 @@ describe('notifications', () => { }), ).resolves.toEqual(expected) }) - }) - describe('if the notification was marked as read earlier', () => { - describe('but the next mentioning happens after the notification was marked as read', () => { - it.todo('sets the `read` attribute to false again') - it.todo('updates the `createdAt` attribute') + describe('if the notification was marked as read earlier', () => { + const markAsReadAction = async () => { + const mutation = gql` + mutation($id: ID!) { + markAsRead(id: $id) { + read + } + } + ` + await mutate({ mutation, variables: { id: 'p47' } }) + } + + describe('but the next mentioning happens after the notification was marked as read', () => { + it('sets the `read` attribute to false again', async () => { + await createPostAction() + await markAsReadAction() + const { + data: { + notifications: [{ read: readBefore }], + }, + } = await query({ + query: notificationQuery, + }) + await updatePostAction() + const { + data: { + notifications: [{ read: readAfter }], + }, + } = await query({ + query: notificationQuery, + }) + expect(readBefore).toEqual(true) + expect(readAfter).toEqual(false) + }) + + it('updates the `createdAt` attribute', async () => { + await createPostAction() + await markAsReadAction() + const { + data: { + notifications: [{ createdAt: createdAtBefore }], + }, + } = await query({ + query: notificationQuery, + }) + await updatePostAction() + const { + data: { + notifications: [{ createdAt: createdAtAfter }], + }, + } = await query({ + query: notificationQuery, + }) + expect(createdAtBefore).toBeTruthy() + expect(Date.parse(createdAtBefore)).toEqual(expect.any(Number)) + expect(createdAtAfter).toBeTruthy() + expect(Date.parse(createdAtAfter)).toEqual(expect.any(Number)) + expect(createdAtBefore).not.toEqual(createdAtAfter) + }) + }) }) }) @@ -387,6 +447,7 @@ describe('notifications', () => { notifications: [ { read: false, + createdAt: expect.any(String), reason: 'mentioned_in_comment', from: { __typename: 'Comment',