From a25e6eb326d5da3a703b7821154cd443177704b0 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 16 Mar 2023 15:09:19 +0100 Subject: [PATCH] notify leave group --- .../notifications/notificationsMiddleware.js | 12 ++++ .../notificationsMiddleware.spec.js | 61 ++++++++++++++++++- backend/src/schema/resolvers/notifications.js | 3 +- backend/src/schema/types/type/NOTIFIED.gql | 1 + 4 files changed, 75 insertions(+), 2 deletions(-) diff --git a/backend/src/middleware/notifications/notificationsMiddleware.js b/backend/src/middleware/notifications/notificationsMiddleware.js index 80c78cfeb..efe363b24 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.js @@ -62,6 +62,17 @@ const handleJoinGroup = async (resolve, root, args, context, resolveInfo) => { return user } +const handleLeaveGroup = async (resolve, root, args, context, resolveInfo) => { + const { groupId, userId } = args + const user = await resolve(root, args, context, resolveInfo) + if (user) { + await publishNotifications(context, [ + notifyOwnersOfGroup(groupId, userId, 'user_left_group', context), + ]) + } + return user +} + const handleContentDataOfPost = async (resolve, root, args, context, resolveInfo) => { const idsOfUsers = extractMentionedUsers(args.content) const post = await resolve(root, args, context, resolveInfo) @@ -232,5 +243,6 @@ export default { CreateComment: handleContentDataOfComment, UpdateComment: handleContentDataOfComment, JoinGroup: handleJoinGroup, + LeaveGroup: handleLeaveGroup, }, } diff --git a/backend/src/middleware/notifications/notificationsMiddleware.spec.js b/backend/src/middleware/notifications/notificationsMiddleware.spec.js index cca84df0e..2a609dc55 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.spec.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.spec.js @@ -6,7 +6,7 @@ import createServer, { pubsub } from '../../server' import { createGroupMutation, joinGroupMutation, - // leaveGroupMutation, + leaveGroupMutation, // changeGroupMemberRoleMutation, // removeUserFromGroupMutation, } from '../../graphql/groups' @@ -704,5 +704,64 @@ describe('notifications', () => { }) }) }) + + describe('user leaves group', () => { + beforeEach(async () => { + authenticatedUser = await notifiedUser.toJson() + await mutate({ + mutation: joinGroupMutation(), + variables: { + groupId: 'closed-group', + userId: authenticatedUser.id, + }, + }) + await mutate({ + mutation: leaveGroupMutation(), + variables: { + groupId: 'closed-group', + userId: authenticatedUser.id, + }, + }) + authenticatedUser = await groupOwner.toJson() + }) + + it('has two the notification in database', async () => { + await expect( + query({ + query: notificationQuery, + }), + ).resolves.toMatchObject({ + data: { + notifications: [ + { + read: false, + reason: 'user_left_group', + createdAt: expect.any(String), + from: { + __typename: 'Group', + id: 'closed-group', + }, + relatedUser: { + id: 'you', + }, + }, + { + read: false, + reason: 'user_joined_group', + createdAt: expect.any(String), + from: { + __typename: 'Group', + id: 'closed-group', + }, + relatedUser: { + id: 'you', + }, + }, + ], + }, + errors: undefined, + }) + }) + }) }) }) diff --git a/backend/src/schema/resolvers/notifications.js b/backend/src/schema/resolvers/notifications.js index 042a55c13..37b40846e 100644 --- a/backend/src/schema/resolvers/notifications.js +++ b/backend/src/schema/resolvers/notifications.js @@ -47,7 +47,8 @@ export default { ` MATCH (resource {deleted: false, disabled: false})-[notification:NOTIFIED]->(user:User {id:$id}) ${whereClause} - OPTIONAL MATCH (resource)<-[membership:MEMBER_OF]-(relatedUser:User { id: notification.relatedUserId }) + OPTIONAL MATCH (relatedUser:User { id: notification.relatedUserId }) + OPTIONAL MATCH (resource)<-[membership:MEMBER_OF]-(relatedUser) WITH user, notification, resource, membership, relatedUser, [(resource)<-[:WROTE]-(author:User) | author {.*}] AS authors, [(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post {.*, author: properties(author)} ] AS posts diff --git a/backend/src/schema/types/type/NOTIFIED.gql b/backend/src/schema/types/type/NOTIFIED.gql index 213e4c512..61bdd5141 100644 --- a/backend/src/schema/types/type/NOTIFIED.gql +++ b/backend/src/schema/types/type/NOTIFIED.gql @@ -23,6 +23,7 @@ enum NotificationReason { mentioned_in_comment commented_on_post user_joined_group + user_left_group } type Query {