From 997dff4decfb333134326f81c3f6e7bcc56643e9 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Wed, 15 Mar 2023 17:34:16 +0100 Subject: [PATCH] add related user to notified model --- .../notifications/notificationsMiddleware.js | 16 +++++----- .../notificationsMiddleware.spec.js | 32 ++++++++++++------- backend/src/schema/resolvers/notifications.js | 19 ++++++++--- backend/src/schema/types/type/NOTIFIED.gql | 1 + 4 files changed, 44 insertions(+), 24 deletions(-) diff --git a/backend/src/middleware/notifications/notificationsMiddleware.js b/backend/src/middleware/notifications/notificationsMiddleware.js index e619fcee8..80c78cfeb 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.js @@ -51,15 +51,13 @@ const publishNotifications = async (context, promises) => { }) } - const handleJoinGroup = async (resolve, root, args, context, resolveInfo) => { - const { groupId } = args + const { groupId, userId } = args const user = await resolve(root, args, context, resolveInfo) if (user) { - await publishNotifications( - context, - [notifyOwnersOfGroup(groupId, 'user_joined_group', context)] - ) + await publishNotifications(context, [ + notifyOwnersOfGroup(groupId, userId, 'user_joined_group', context), + ]) } return user } @@ -107,7 +105,7 @@ const postAuthorOfComment = async (commentId, { context }) => { } } -const notifyOwnersOfGroup = async (groupId, reason, context) => { +const notifyOwnersOfGroup = async (groupId, userId, reason, context) => { const cypher = ` MATCH (group:Group { id: $groupId })<-[membership:MEMBER_OF]-(owner:User) WHERE membership.role = 'owner' @@ -117,6 +115,7 @@ const notifyOwnersOfGroup = async (groupId, reason, context) => { SET notification.read = FALSE SET notification.createdAt = COALESCE(notification.createdAt, toString(datetime())) SET notification.updatedAt = toString(datetime()) + SET notification.relatedUserId = $userId RETURN notification {.*, from: group, to: properties(owner)} ` const session = context.driver.session() @@ -124,6 +123,7 @@ const notifyOwnersOfGroup = async (groupId, reason, context) => { const notificationTransactionResponse = await transaction.run(cypher, { groupId, reason, + userId, }) return notificationTransactionResponse.records.map((record) => record.get('notification')) }) @@ -131,7 +131,7 @@ const notifyOwnersOfGroup = async (groupId, reason, context) => { const notifications = await writeTxResultPromise return notifications } catch (error) { - throw new Error(error) + throw new Error(error) } finally { session.close() } diff --git a/backend/src/middleware/notifications/notificationsMiddleware.spec.js b/backend/src/middleware/notifications/notificationsMiddleware.spec.js index 42ff5a0c6..cca84df0e 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.spec.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.spec.js @@ -6,12 +6,11 @@ import createServer, { pubsub } from '../../server' import { createGroupMutation, joinGroupMutation, - leaveGroupMutation, - changeGroupMemberRoleMutation, - removeUserFromGroupMutation, + // leaveGroupMutation, + // changeGroupMemberRoleMutation, + // removeUserFromGroupMutation, } from '../../graphql/groups' - let server, query, mutate, notifiedUser, authenticatedUser let publishSpy const driver = getDriver() @@ -100,6 +99,9 @@ describe('notifications', () => { read reason createdAt + relatedUser { + id + } from { __typename ... on Post { @@ -196,6 +198,7 @@ describe('notifications', () => { id: 'c47', content: commentContent, }, + relatedUser: null, }, ], }, @@ -368,6 +371,7 @@ describe('notifications', () => { id: 'p47', content: expectedUpdatedContent, }, + relatedUser: null, }, ], }, @@ -524,6 +528,7 @@ describe('notifications', () => { id: 'c47', content: commentContent, }, + relatedUser: null, }, ], }, @@ -558,6 +563,7 @@ describe('notifications', () => { id: 'c47', content: commentContent, }, + relatedUser: null, }, ], }, @@ -630,8 +636,7 @@ describe('notifications', () => { describe('group notifications', () => { let groupOwner - let group - + beforeEach(async () => { groupOwner = await neode.create( 'User', @@ -673,10 +678,12 @@ describe('notifications', () => { authenticatedUser = await groupOwner.toJson() }) - it('works', async () => { - await expect(query({ - query: notificationQuery, - })).resolves.toMatchObject({ + it('has the notification in database', async () => { + await expect( + query({ + query: notificationQuery, + }), + ).resolves.toMatchObject({ data: { notifications: [ { @@ -686,7 +693,10 @@ describe('notifications', () => { from: { __typename: 'Group', id: 'closed-group', - } + }, + relatedUser: { + id: 'you', + }, }, ], }, diff --git a/backend/src/schema/resolvers/notifications.js b/backend/src/schema/resolvers/notifications.js index a2b850336..042a55c13 100644 --- a/backend/src/schema/resolvers/notifications.js +++ b/backend/src/schema/resolvers/notifications.js @@ -47,12 +47,21 @@ export default { ` MATCH (resource {deleted: false, disabled: false})-[notification:NOTIFIED]->(user:User {id:$id}) ${whereClause} - WITH user, notification, resource, + OPTIONAL MATCH (resource)<-[membership:MEMBER_OF]-(relatedUser:User { id: notification.relatedUserId }) + 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 - WITH resource, user, notification, authors, posts, - resource {.*, __typename: labels(resource)[0], author: authors[0], post: posts[0]} AS finalResource - RETURN notification {.*, from: finalResource, to: properties(user)} + [(resource)-[:COMMENTS]->(post:Post)<-[:WROTE]-(author:User) | post {.*, author: properties(author)} ] AS posts + WITH resource, user, notification, authors, posts, relatedUser, membership, + resource {.*, + __typename: labels(resource)[0], + author: authors[0], + post: posts[0], + myRole: membership.role } AS finalResource + RETURN notification {.*, + from: finalResource, + to: properties(user), + relatedUser: properties(relatedUser) + } ${orderByClause} ${offset} ${limit} `, diff --git a/backend/src/schema/types/type/NOTIFIED.gql b/backend/src/schema/types/type/NOTIFIED.gql index ea02ee87e..213e4c512 100644 --- a/backend/src/schema/types/type/NOTIFIED.gql +++ b/backend/src/schema/types/type/NOTIFIED.gql @@ -6,6 +6,7 @@ type NOTIFIED { updatedAt: String! read: Boolean reason: NotificationReason + relatedUser: User } union NotificationSource = Post | Comment | Group