Update notifications

- create only one relationship by using merge, but do not update
createdAt attribute/update test
- order by updatedAt_desc
This commit is contained in:
mattwr18 2019-09-13 20:07:49 +02:00
parent 67d68db231
commit 16d7e6c91a
6 changed files with 33 additions and 19 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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 = ''

View File

@ -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 {

View File

@ -1,13 +1,12 @@
<template>
<ds-button
v-if="totalNotifications <= 0"
class="notifications-menu"
disabled
icon="bell"
>{{ totalNotifications }}</ds-button>
<ds-button v-if="totalNotifications <= 0" class="notifications-menu" disabled icon="bell">
{{ totalNotifications }}
</ds-button>
<dropdown v-else class="notifications-menu" :placement="placement">
<template slot="default" slot-scope="{ toggleMenu }">
<ds-button primary icon="bell" @click.prevent="toggleMenu">{{ totalNotifications }}</ds-button>
<ds-button primary icon="bell" @click.prevent="toggleMenu">
{{ totalNotifications }}
</ds-button>
</template>
<template slot="popover">
<div class="notifications-menu-popover">

View File

@ -97,7 +97,7 @@ export const notificationQuery = i18n => {
${postFragment(lang)}
query {
notifications(read: false, orderBy: createdAt_desc) {
notifications(read: false, orderBy: updatedAt_desc) {
read
reason
createdAt