From 16d7e6c91ae8f079187baa55bfbcea3de03532e6 Mon Sep 17 00:00:00 2001 From: mattwr18 Date: Fri, 13 Sep 2019 20:07:49 +0200 Subject: [PATCH] Update notifications - create only one relationship by using merge, but do not update createdAt attribute/update test - order by updatedAt_desc --- .../notifications/notificationsMiddleware.js | 24 ++++++++++++++----- .../notificationsMiddleware.spec.js | 2 +- backend/src/schema/resolvers/notifications.js | 8 +++---- backend/src/schema/types/type/NOTIFIED.gql | 3 +++ .../NotificationMenu/NotificationMenu.vue | 13 +++++----- webapp/graphql/User.js | 2 +- 6 files changed, 33 insertions(+), 19 deletions(-) diff --git a/backend/src/middleware/notifications/notificationsMiddleware.js b/backend/src/middleware/notifications/notificationsMiddleware.js index ad1012c7a..a494783cf 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.js @@ -24,9 +24,13 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { MATCH (user: User) WHERE user.id in $idsOfUsers AND NOT (user)<-[:BLOCKED]-(author) - CREATE (post)-[notification:NOTIFIED {reason: $reason}]->(user) + MERGE (post)-[notification:NOTIFIED {reason: $reason}]->(user) SET notification.read = FALSE - SET notification.createdAt = toString(datetime()) + SET ( + CASE + WHEN notification.createdAt IS NULL + THEN notification END ).createdAt = toString(datetime()) + SET notification.updatedAt = toString(datetime()) ` break } @@ -37,9 +41,13 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { WHERE user.id in $idsOfUsers AND NOT (user)<-[:BLOCKED]-(author) AND NOT (user)<-[:BLOCKED]-(postAuthor) - CREATE (comment)-[notification:NOTIFIED {reason: $reason}]->(user) + MERGE (comment)-[notification:NOTIFIED {reason: $reason}]->(user) SET notification.read = FALSE - SET notification.createdAt = toString(datetime()) + SET ( + CASE + WHEN notification.createdAt IS NULL + THEN notification END ).createdAt = toString(datetime()) + SET notification.updatedAt = toString(datetime()) ` break } @@ -50,9 +58,13 @@ const notifyUsers = async (label, id, idsOfUsers, reason, context) => { WHERE user.id in $idsOfUsers AND NOT (user)<-[:BLOCKED]-(author) AND NOT (author)<-[:BLOCKED]-(user) - CREATE (comment)-[notification:NOTIFIED {reason: $reason}]->(user) + MERGE (comment)-[notification:NOTIFIED {reason: $reason}]->(user) SET notification.read = FALSE - SET notification.createdAt = toString(datetime()) + SET ( + CASE + WHEN notification.createdAt IS NULL + THEN notification END ).createdAt = toString(datetime()) + SET notification.updatedAt = toString(datetime()) ` break } diff --git a/backend/src/middleware/notifications/notificationsMiddleware.spec.js b/backend/src/middleware/notifications/notificationsMiddleware.spec.js index 94fb18668..88f91d688 100644 --- a/backend/src/middleware/notifications/notificationsMiddleware.spec.js +++ b/backend/src/middleware/notifications/notificationsMiddleware.spec.js @@ -77,7 +77,7 @@ afterEach(async () => { describe('notifications', () => { const notificationQuery = gql` query($read: Boolean) { - notifications(read: $read, orderBy: createdAt_desc) { + notifications(read: $read, orderBy: updatedAt_desc) { read reason createdAt diff --git a/backend/src/schema/resolvers/notifications.js b/backend/src/schema/resolvers/notifications.js index 3c1adbc5c..4cab1ffc4 100644 --- a/backend/src/schema/resolvers/notifications.js +++ b/backend/src/schema/resolvers/notifications.js @@ -32,11 +32,11 @@ export default { whereClause = '' } switch (args.orderBy) { - case 'createdAt_asc': - orderByClause = 'ORDER BY notification.createdAt ASC' + case 'updatedAt_asc': + orderByClause = 'ORDER BY notification.updatedAt ASC' break - case 'createdAt_desc': - orderByClause = 'ORDER BY notification.createdAt DESC' + case 'updatedAt_desc': + orderByClause = 'ORDER BY notification.updatedAt DESC' break default: orderByClause = '' diff --git a/backend/src/schema/types/type/NOTIFIED.gql b/backend/src/schema/types/type/NOTIFIED.gql index b90e30598..5082b5f7f 100644 --- a/backend/src/schema/types/type/NOTIFIED.gql +++ b/backend/src/schema/types/type/NOTIFIED.gql @@ -2,6 +2,7 @@ type NOTIFIED { from: NotificationSource to: User createdAt: String + updatedAt: String read: Boolean reason: NotificationReason } @@ -11,6 +12,8 @@ union NotificationSource = Post | Comment enum NotificationOrdering { createdAt_asc createdAt_desc + updatedAt_asc + updatedAt_desc } enum NotificationReason { diff --git a/webapp/components/notifications/NotificationMenu/NotificationMenu.vue b/webapp/components/notifications/NotificationMenu/NotificationMenu.vue index 0b71f3911..51b90089f 100644 --- a/webapp/components/notifications/NotificationMenu/NotificationMenu.vue +++ b/webapp/components/notifications/NotificationMenu/NotificationMenu.vue @@ -1,13 +1,12 @@