diff --git a/backend/src/middleware/notifications/notificationsMiddleware.js b/backend/src/middleware/notifications/notificationsMiddleware.js index 9f594a1f7..1c97e9591 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.js @@ -84,6 +84,17 @@ const handleChangeGroupMemberRole = async (resolve, root, args, context, resolve return user } +const handleRemoveUserFromGroup = async (resolve, root, args, context, resolveInfo) => { + const { groupId, userId } = args + const user = await resolve(root, args, context, resolveInfo) + if (user) { + await publishNotifications(context, [ + notifyMemberOfGroup(groupId, userId, 'removed_user_from_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) @@ -160,15 +171,17 @@ const notifyOwnersOfGroup = async (groupId, userId, reason, context) => { } const notifyMemberOfGroup = async (groupId, userId, reason, context) => { + const { user: owner } = context const cypher = ` - MATCH (group:Group { id: $groupId })<-[membership:MEMBER_OF]-(user:User { id: $userId }) + MATCH (user:User { id: $userId }) + MATCH (group:Group { id: $groupId }) WITH user, group MERGE (group)-[notification:NOTIFIED {reason: $reason}]->(user) WITH group, user, notification SET notification.read = FALSE SET notification.createdAt = COALESCE(notification.createdAt, toString(datetime())) SET notification.updatedAt = toString(datetime()) - SET notification.relatedUserId = $userId + SET notification.relatedUserId = $ownerId RETURN notification {.*, from: group, to: properties(user)} ` const session = context.driver.session() @@ -177,6 +190,7 @@ const notifyMemberOfGroup = async (groupId, userId, reason, context) => { groupId, reason, userId, + ownerId: owner.id, }) return notificationTransactionResponse.records.map((record) => record.get('notification')) }) @@ -287,5 +301,6 @@ export default { JoinGroup: handleJoinGroup, LeaveGroup: handleLeaveGroup, ChangeGroupMemberRole: handleChangeGroupMemberRole, + RemoveUserFromGroup: handleRemoveUserFromGroup, }, } diff --git a/backend/src/middleware/notifications/notificationsMiddleware.spec.js b/backend/src/middleware/notifications/notificationsMiddleware.spec.js index 851c76f9c..50c423275 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.spec.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.spec.js @@ -8,7 +8,7 @@ import { joinGroupMutation, leaveGroupMutation, changeGroupMemberRoleMutation, - // removeUserFromGroupMutation, + removeUserFromGroupMutation, } from '../../graphql/groups' let server, query, mutate, notifiedUser, authenticatedUser @@ -803,7 +803,7 @@ describe('notifications', () => { id: 'closed-group', }, relatedUser: { - id: 'you', + id: 'group-owner', }, }, ], @@ -812,5 +812,53 @@ describe('notifications', () => { }) }) }) + + describe('user is removed from group', () => { + beforeEach(async () => { + authenticatedUser = await notifiedUser.toJson() + await mutate({ + mutation: joinGroupMutation(), + variables: { + groupId: 'closed-group', + userId: authenticatedUser.id, + }, + }) + authenticatedUser = await groupOwner.toJson() + await mutate({ + mutation: removeUserFromGroupMutation(), + variables: { + groupId: 'closed-group', + userId: 'you', + }, + }) + authenticatedUser = await notifiedUser.toJson() + }) + + it('has notification in database', async () => { + await expect( + query({ + query: notificationQuery, + }), + ).resolves.toMatchObject({ + data: { + notifications: [ + { + read: false, + reason: 'removed_user_from_group', + createdAt: expect.any(String), + from: { + __typename: 'Group', + id: 'closed-group', + }, + relatedUser: { + id: 'group-owner', + }, + }, + ], + }, + errors: undefined, + }) + }) + }) }) }) diff --git a/backend/src/schema/types/type/NOTIFIED.gql b/backend/src/schema/types/type/NOTIFIED.gql index 8173fc8f7..62a1f3696 100644 --- a/backend/src/schema/types/type/NOTIFIED.gql +++ b/backend/src/schema/types/type/NOTIFIED.gql @@ -25,6 +25,7 @@ enum NotificationReason { user_joined_group user_left_group changed_group_member_role + removed_user_from_group } type Query {