@@ -6,7 +6,11 @@ import {
66} from '@nestjs/common' ;
77import { Model , Types } from 'mongoose' ;
88import { NotificationDto } from 'src/shared/dtos/notificatoin.dto' ;
9- import { NotificationInfo , User } from 'src/shared/schema' ;
9+ import {
10+ NotificationAuditInfo ,
11+ NotificationInfo ,
12+ User ,
13+ } from 'src/shared/schema' ;
1014
1115@Injectable ( )
1216export class NotificationsService {
@@ -15,6 +19,8 @@ export class NotificationsService {
1519 private readonly notificationModel : Model < NotificationInfo > ,
1620 @Inject ( User . name )
1721 private readonly userModel : Model < User > ,
22+ @Inject ( NotificationAuditInfo . name )
23+ private readonly notificationAuditModel : Model < NotificationAuditInfo > ,
1824 ) { }
1925
2026 async getNotifications ( userId : string ) : Promise < string [ ] > {
@@ -53,6 +59,21 @@ export class NotificationsService {
5359 } ;
5460 }
5561
62+ async getNotificationByUserId ( userId : string , entityId : string ) {
63+ const notification = await this . notificationModel . findOne ( {
64+ receiverId : userId ,
65+ entityId,
66+ } ) ;
67+
68+ if ( ! notification ) {
69+ throw new NotFoundException (
70+ 'No unread notifications found for this entity' ,
71+ ) ;
72+ }
73+
74+ return notification ;
75+ }
76+
5677 async getUnreadNotificationsByEntity ( entityId : string ) : Promise < any [ ] > {
5778 const notifications = await this . notificationModel
5879 . find ( { entityId } )
@@ -146,9 +167,10 @@ export class NotificationsService {
146167 }
147168
148169 async resolveNotification (
170+ notification_id : string ,
149171 admin_id : string ,
150172 message : string ,
151- notification_id : string ,
173+ status : string ,
152174 ) : Promise < string > {
153175 // Fetch the notification to copy its data
154176 const notification = await this . notificationModel
@@ -158,14 +180,23 @@ export class NotificationsService {
158180 if ( ! notification ) {
159181 throw new NotFoundException ( 'Notification not found' ) ;
160182 }
183+ const notificationToAudit = {
184+ actorId : admin_id ,
185+ notificationSummary : notification . message ,
186+ resolutionMessage : message ,
187+ resolutionStatus : status ,
188+ entityType : notification . notification_type ,
189+ entityId : notification . entityId ,
190+ } ;
161191
162192 // Delete the notification
163193 const deleted = await this . deleteNotification ( notification_id ) ;
164194 if ( ! deleted ) {
165195 throw new InternalServerErrorException ( 'Error deleting notification' ) ;
166196 }
167- // TODO: use notification to create audit file with admin id
168197
169- return '' ;
198+ await this . notificationAuditModel . create ( notificationToAudit ) ;
199+
200+ return 'Notification resolved successfully' ;
170201 }
171202}
0 commit comments