1- import { DBHandle } from "@dotkomonline/db"
2- import { Notification , NotificationId , NotificationWrite , UserId , NotificationRecipientId , NotificationRecipient } from "@dotkomonline/types"
1+ import type { DBHandle } from "@dotkomonline/db"
2+ import type {
3+ Notification ,
4+ NotificationId ,
5+ NotificationWrite ,
6+ NotificationRecipientId ,
7+ NotificationRecipient ,
8+ UserNotification ,
9+ } from "./notification"
310
4- export interface NotificationRepository {
11+ import type { UserId } from "@dotkomonline/types"
512
13+ export interface NotificationRepository {
614 findById ( handle : DBHandle , notificationId : NotificationId ) : Promise < Notification | null >
715 create ( handle : DBHandle , notificationData : NotificationWrite ) : Promise < Notification >
8- update ( handle : DBHandle , notificationId : NotificationId , notificationData : Partial < NotificationWrite > ) : Promise < Notification >
16+ update (
17+ handle : DBHandle ,
18+ notificationId : NotificationId ,
19+ notificationData : Partial < NotificationWrite >
20+ ) : Promise < Notification >
921 delete ( handle : DBHandle , notificationId : NotificationId ) : Promise < Notification | null >
1022
1123 addRecipients ( handle : DBHandle , notificationId : NotificationId , recipientIds : UserId [ ] ) : Promise < void >
1224 removeRecipients ( handle : DBHandle , notificationId : NotificationId , recipientIds : UserId [ ] ) : Promise < void >
1325
14- findRecipient ( handle : DBHandle , recipientId : NotificationRecipientId , userId : UserId ) : Promise < NotificationRecipient | null >
15- findAllforUser ( handle : DBHandle , userId : UserId ) : Promise < Notification [ ] >
16- getUnreadCountforUser ( handle : DBHandle , userId : UserId ) : Promise < number >
26+ findRecipient (
27+ handle : DBHandle ,
28+ recipientId : NotificationRecipientId ,
29+ userId : UserId
30+ ) : Promise < NotificationRecipient | null >
31+ findAllForUser ( handle : DBHandle , userId : UserId ) : Promise < UserNotification [ ] >
32+ getUnreadCountForUser ( handle : DBHandle , userId : UserId ) : Promise < number >
1733 markAsRead ( handle : DBHandle , notificationId : NotificationId , userId : UserId ) : Promise < void >
1834 markAllAsRead ( handle : DBHandle , userId : UserId ) : Promise < void >
1935}
@@ -36,10 +52,9 @@ export function getNotificationRepository(): NotificationRepository {
3652 const { recipientIds, ...data } = notificationData
3753 const notification = await handle . notification . update ( {
3854 where : { id : notificationId } ,
39- data,
55+ data,
4056 } )
4157 return notification
42-
4358 } ,
4459 async delete ( handle , notificationId ) {
4560 const notification = await handle . notification . findUnique ( {
@@ -68,12 +83,12 @@ export function getNotificationRepository(): NotificationRepository {
6883 } )
6984 } ,
7085 async removeRecipients ( handle , notificationId , recipientIds ) {
71- await handle . notificationRecipient . deleteMany ( {
72- where : {
73- notificationId,
74- userId : { in : recipientIds } ,
75- } ,
76- } )
86+ await handle . notificationRecipient . deleteMany ( {
87+ where : {
88+ notificationId,
89+ userId : { in : recipientIds } ,
90+ } ,
91+ } )
7792 } ,
7893
7994 async findAllForUser ( handle , userId ) {
@@ -83,17 +98,17 @@ export function getNotificationRepository(): NotificationRepository {
8398 } )
8499 } ,
85100
86- async getUnreadCountforUser ( handle , userId ) {
87- await handle . notificationRecipient . count ( {
88- where : { userId, readAt : null } ,
89- } )
90- } ,
91-
92101 async getUnreadCountForUser ( handle , userId ) {
93102 return handle . notificationRecipient . count ( {
94103 where : { userId, readAt : null } ,
95104 } )
96105 } ,
106+ async markAsRead ( handle , notificationId , userId ) {
107+ await handle . notificationRecipient . updateMany ( {
108+ where : { notificationId, userId, readAt : null } ,
109+ data : { readAt : new Date ( ) } ,
110+ } )
111+ } ,
97112
98113 async markAllAsRead ( handle , userId ) {
99114 await handle . notificationRecipient . updateMany ( {
@@ -102,4 +117,4 @@ export function getNotificationRepository(): NotificationRepository {
102117 } )
103118 } ,
104119 }
105- }
120+ }
0 commit comments