11import { Request , Response , NextFunction } from "express" ;
22
3- import { Notification } from "@/types/index.js" ;
4- import { createNotificationBodyValidation } from "@/validation/notificationValidation.js" ;
3+ import {
4+ createNotificationBodyValidation ,
5+ deleteNotificationParamValidation ,
6+ getNotificationByIdParamValidation ,
7+ testNotificationBodyValidation ,
8+ editNotificationParamValidation ,
9+ testAllNotificationsBodyValidation ,
10+ } from "@/validation/notificationValidation.js" ;
511import { AppError } from "@/utils/AppError.js" ;
612import { IMonitorsRepository } from "@/repositories/index.js" ;
713import { INotificationsService } from "@/service/index.js" ;
8- import { requireTeamId } from "./controllerUtils.js" ;
14+ import { requireTeamId , requireUserId } from "./controllerUtils.js" ;
915
1016const SERVICE_NAME = "NotificationController" ;
1117
1218class NotificationController {
13- static SERVICE_NAME = SERVICE_NAME ;
1419 private notificationsService : INotificationsService ;
1520 private monitorsRepository : IMonitorsRepository ;
1621 constructor ( notificationsService : INotificationsService , monitorsRepository : IMonitorsRepository ) {
1722 this . notificationsService = notificationsService ;
1823 this . monitorsRepository = monitorsRepository ;
1924 }
2025
21- get serviceName ( ) {
22- return NotificationController . SERVICE_NAME ;
23- }
24-
2526 testNotification = async ( req : Request , res : Response , next : NextFunction ) => {
2627 try {
27- const notification : Notification = req . body ;
28+ const notification = testNotificationBodyValidation . parse ( req . body ) ;
2829 const success = await this . notificationsService . sendTestNotification ( notification ) ;
2930
3031 if ( ! success ) {
@@ -34,6 +35,7 @@ class NotificationController {
3435 return res . status ( 200 ) . json ( {
3536 success : true ,
3637 msg : "Notification sent successfully" ,
38+ details : { service : SERVICE_NAME } ,
3739 } ) ;
3840 } catch ( error ) {
3941 next ( error ) ;
@@ -42,23 +44,12 @@ class NotificationController {
4244
4345 createNotification = async ( req : Request , res : Response , next : NextFunction ) => {
4446 try {
45- createNotificationBodyValidation . parse ( req . body ) ;
46-
47- const body = req . body ;
48-
49- const teamId = req ?. user ?. teamId ;
50- if ( ! teamId ) {
51- throw new AppError ( { message : "Team ID is required" , status : 400 } ) ;
52- }
47+ const validatedBody = createNotificationBodyValidation . parse ( req . body ) ;
5348
54- const userId = req ?. user ?. id ;
55- if ( ! userId ) {
56- throw new AppError ( { message : "User ID is required" , status : 400 } ) ;
57- }
58- body . userId = userId ;
59- body . teamId = teamId ;
49+ const teamId = requireTeamId ( req . user ?. teamId ) ;
50+ const userId = requireUserId ( req . user ?. id ) ;
6051
61- const notification = await this . notificationsService . createNotification ( body ) ;
52+ const notification = await this . notificationsService . createNotification ( validatedBody , userId , teamId ) ;
6253 return res . status ( 200 ) . json ( {
6354 success : true ,
6455 msg : "Notification created successfully" ,
@@ -71,11 +62,7 @@ class NotificationController {
7162
7263 getNotificationsByTeamId = async ( req : Request , res : Response , next : NextFunction ) => {
7364 try {
74- const teamId = req ?. user ?. teamId ;
75- if ( ! teamId ) {
76- throw new AppError ( { message : "Team ID is required" , status : 400 } ) ;
77- }
78-
65+ const teamId = requireTeamId ( req . user ?. teamId ) ;
7966 const notifications = await this . notificationsService . findNotificationsByTeamId ( teamId ) ;
8067
8168 return res . status ( 200 ) . json ( {
@@ -90,17 +77,10 @@ class NotificationController {
9077
9178 deleteNotification = async ( req : Request , res : Response , next : NextFunction ) => {
9279 try {
93- const teamId = req ?. user ?. teamId ;
94- if ( ! teamId ) {
95- throw new AppError ( { message : "Team ID is required" , status : 400 } ) ;
96- }
97-
98- const notificationId = req . params . id as string ;
99- if ( ! notificationId ) {
100- throw new AppError ( { message : "Notification ID is required" , status : 400 } ) ;
101- }
80+ const teamId = requireTeamId ( req . user ?. teamId ) ;
81+ const validatedParams = deleteNotificationParamValidation . parse ( req . params ) ;
10282
103- await this . notificationsService . deleteById ( notificationId , teamId ) ;
83+ await this . notificationsService . deleteById ( validatedParams . id , teamId ) ;
10484 return res . status ( 200 ) . json ( {
10585 success : true ,
10686 msg : "Notification deleted successfully" ,
@@ -113,12 +93,9 @@ class NotificationController {
11393 getNotificationById = async ( req : Request , res : Response , next : NextFunction ) => {
11494 try {
11595 const teamId = requireTeamId ( req . user ?. teamId ) ;
116- const notificationId = req . params . id as string ;
117- if ( ! notificationId ) {
118- throw new AppError ( { message : "Notification ID is required" , status : 400 } ) ;
119- }
96+ const validatedParams = getNotificationByIdParamValidation . parse ( req . params ) ;
12097
121- const notification = await this . notificationsService . findById ( notificationId , teamId ) ;
98+ const notification = await this . notificationsService . findById ( validatedParams . id , teamId ) ;
12299
123100 return res . status ( 200 ) . json ( {
124101 success : true ,
@@ -132,14 +109,13 @@ class NotificationController {
132109
133110 editNotification = async ( req : Request , res : Response , next : NextFunction ) => {
134111 try {
135- createNotificationBodyValidation . parse ( req . body ) ;
112+ const validatedBody = createNotificationBodyValidation . parse ( req . body ) ;
113+ const validatedParams = editNotificationParamValidation . parse ( req . params ) ;
136114
137115 const teamId = requireTeamId ( req . user ?. teamId ) ;
138- const notificationId = req . params . id as string ;
139- if ( ! notificationId ) {
140- throw new AppError ( { message : "Notification ID is required" , status : 400 } ) ;
141- }
142- const editedNotification = await this . notificationsService . updateById ( notificationId , teamId , req . body ) ;
116+ const notificationId = validatedParams . id ;
117+
118+ const editedNotification = await this . notificationsService . updateById ( notificationId , teamId , validatedBody ) ;
143119 return res . status ( 200 ) . json ( {
144120 success : true ,
145121 msg : "Notification updated successfully" ,
@@ -152,13 +128,11 @@ class NotificationController {
152128
153129 testAllNotifications = async ( req : Request , res : Response , next : NextFunction ) => {
154130 try {
155- const monitorId = req . body . monitorId ;
156- const teamId = req ?. user ?. teamId ;
157- if ( ! teamId ) {
158- throw new AppError ( { message : "Team ID is required" , status : 400 } ) ;
159- }
131+ const validatedBody = testAllNotificationsBodyValidation . parse ( req . body ) ;
132+
133+ const teamId = requireTeamId ( req . user ?. teamId ) ;
160134
161- const monitor = await this . monitorsRepository . findById ( monitorId , teamId ) ;
135+ const monitor = await this . monitorsRepository . findById ( validatedBody . monitorId , teamId ) ;
162136
163137 const notifications = monitor . notifications ;
164138 if ( notifications . length === 0 ) {
0 commit comments