Skip to content

Commit baefb98

Browse files
committed
Change to store read state as nullable datetime
This gives more info and can still be used as boolean flag
1 parent 9cbceb5 commit baefb98

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/components/notifications/dto/notification.dto.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Field, InterfaceType } from '@nestjs/graphql';
2+
import { DateTime } from 'luxon';
23
import { keys as keysOf } from 'ts-transformer-keys';
3-
import { Resource, SecuredProps } from '~/common';
4+
import { DateTimeField, Resource, SecuredProps } from '~/common';
45
import { LinkTo, RegisterResource } from '~/core/resources';
56

67
@RegisterResource()
@@ -15,6 +16,9 @@ export class Notification extends Resource {
1516

1617
@Field(() => Boolean)
1718
readonly unread: boolean;
19+
20+
@DateTimeField({ nullable: true })
21+
readonly readAt: DateTime | null;
1822
}
1923

2024
declare module '~/core/resources/map' {

src/components/notifications/notification.repository.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { forwardRef, Inject, Injectable } from '@nestjs/common';
22
import { Nil } from '@seedcompany/common';
3-
import { inArray, node, Query, relation } from 'cypher-query-builder';
3+
import {
4+
inArray,
5+
isNull,
6+
node,
7+
not,
8+
Query,
9+
relation,
10+
} from 'cypher-query-builder';
411
import { omit } from 'lodash';
512
import { DateTime } from 'luxon';
613
import {
@@ -14,7 +21,6 @@ import {
1421
import { CommonRepository } from '~/core/database';
1522
import {
1623
apoc,
17-
coalesce,
1824
createRelationships,
1925
filter,
2026
merge,
@@ -77,7 +83,7 @@ export class NotificationRepository extends CommonRepository {
7783
)
7884
.create([
7985
node('node'),
80-
relation('out', '', 'recipient', { unread: variable('true') }),
86+
relation('out', '', 'recipient'),
8187
node('recipient'),
8288
])
8389
.return<{ totalRecipients: number }>(
@@ -98,7 +104,7 @@ export class NotificationRepository extends CommonRepository {
98104
relation('out', 'recipient', 'recipient'),
99105
requestingUser(session),
100106
])
101-
.setValues({ recipient: { unread } }, true)
107+
.setValues({ 'recipient.readAt': unread ? null : DateTime.now() })
102108
.with('node')
103109
.apply(this.hydrate(session))
104110
.first();
@@ -128,9 +134,10 @@ export class NotificationRepository extends CommonRepository {
128134
q
129135
.match([
130136
node('node', 'Notification'),
131-
relation('out', '', 'recipient', { unread: variable('true') }),
137+
relation('out', 'recipient', 'recipient'),
132138
node('requestingUser'),
133139
])
140+
.where({ 'recipient.readAt': isNull() })
134141
.return<{ totalUnread: number }>('count(node) as totalUnread'),
135142
)
136143
.return(['items', 'hasMore', 'total', 'totalUnread'])
@@ -168,7 +175,8 @@ export class NotificationRepository extends CommonRepository {
168175
.return<{ dto: UnsecuredDto<Notification> }>(
169176
merge('node', 'extra', {
170177
__typename: 'node.type + "Notification"',
171-
unread: coalesce('recipient.unread', false),
178+
unread: 'recipient.readAt is null',
179+
readAt: 'recipient.readAt',
172180
}).as('dto'),
173181
);
174182
}
@@ -179,5 +187,7 @@ export class NotificationRepository extends CommonRepository {
179187
}
180188

181189
const notificationFilters = filter.define(() => NotificationFilters, {
182-
unread: ({ value }) => ({ recipient: { unread: value } }),
190+
unread: ({ value }) => ({
191+
'recipient.readAt': value ? isNull() : not(isNull()),
192+
}),
183193
});

0 commit comments

Comments
 (0)