From 64320c7acacb2f440ebc299cc3d36f30d124649d Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 9 Jan 2026 15:02:19 -0800 Subject: [PATCH] js -> ts --- server/src/config/controllers.js | 5 +- .../controllers/v1/notificationController.js | 180 ---------------- .../controllers/v1/notificationController.ts | 197 ++++++++++++++++++ 3 files changed, 198 insertions(+), 184 deletions(-) delete mode 100644 server/src/controllers/v1/notificationController.js create mode 100644 server/src/controllers/v1/notificationController.ts diff --git a/server/src/config/controllers.js b/server/src/config/controllers.js index 4589ddd85..fff25351c 100644 --- a/server/src/config/controllers.js +++ b/server/src/config/controllers.js @@ -51,10 +51,7 @@ export const initializeControllers = (services) => { }); controllers.logController = new LogController(commonDependencies); controllers.statusPageController = new StatusPageController(commonDependencies); - controllers.notificationController = new NotificationController(commonDependencies, { - notificationService: services.notificationService, - statusService: services.statusService, - }); + controllers.notificationController = new NotificationController(services.notificationService, services.db); controllers.diagnosticController = new DiagnosticController(commonDependencies, { diagnosticService: services.diagnosticService, }); diff --git a/server/src/controllers/v1/notificationController.js b/server/src/controllers/v1/notificationController.js deleted file mode 100644 index 37c2b4e60..000000000 --- a/server/src/controllers/v1/notificationController.js +++ /dev/null @@ -1,180 +0,0 @@ -import { createNotificationBodyValidation } from "../../validation/joi.js"; -import BaseController from "./baseController.js"; - -const SERVICE_NAME = "NotificationController"; - -class NotificationController extends BaseController { - static SERVICE_NAME = SERVICE_NAME; - constructor(commonDependencies, { notificationService, statusService }) { - super(commonDependencies); - this.notificationService = notificationService; - this.statusService = statusService; - } - - get serviceName() { - return NotificationController.SERVICE_NAME; - } - - testNotification = this.asyncHandler( - async (req, res) => { - const notification = req.body; - - const success = await this.notificationService.sendTestNotification(notification); - - if (!success) { - throw this.errorService.createServerError("Sending notification failed"); - } - - return res.success({ - msg: "Notification sent successfully", - }); - }, - SERVICE_NAME, - "testNotification" - ); - - createNotification = this.asyncHandler( - async (req, res) => { - await createNotificationBodyValidation.validateAsync(req.body, { - abortEarly: false, - }); - - const body = req.body; - - const teamId = req?.user?.teamId; - if (!teamId) { - throw this.errorService.createBadRequestError("Team ID is required"); - } - - const userId = req?.user?._id; - if (!userId) { - throw this.errorService.createBadRequestError("User ID is required"); - } - body.userId = userId; - body.teamId = teamId; - - const notification = await this.db.notificationModule.createNotification(body); - return res.success({ - msg: "Notification created successfully", - data: notification, - }); - }, - SERVICE_NAME, - "createNotification" - ); - - getNotificationsByTeamId = this.asyncHandler( - async (req, res) => { - const teamId = req?.user?.teamId; - if (!teamId) { - throw this.errorService.createBadRequestError("Team ID is required"); - } - - const notifications = await this.db.notificationModule.getNotificationsByTeamId(teamId); - - return res.success({ - msg: "Notifications fetched successfully", - data: notifications, - }); - }, - SERVICE_NAME, - "getNotificationsByTeamId" - ); - - deleteNotification = this.asyncHandler( - async (req, res) => { - const teamId = req?.user?.teamId; - if (!teamId) { - throw this.errorService.createBadRequestError("Team ID is required"); - } - - const notification = await this.db.notificationModule.getNotificationById(req.params.id); - if (!notification.teamId.equals(teamId)) { - throw this.errorService.createAuthorizationError(); - } - - await this.db.notificationModule.deleteNotificationById(req.params.id); - return res.success({ - msg: "Notification deleted successfully", - }); - }, - SERVICE_NAME, - "deleteNotification" - ); - - getNotificationById = this.asyncHandler( - async (req, res) => { - const notification = await this.db.notificationModule.getNotificationById(req.params.id); - - const teamId = req?.user?.teamId; - if (!teamId) { - throw this.errorService.createBadRequestError("Team ID is required"); - } - - if (!notification.teamId.equals(teamId)) { - throw this.errorService.createAuthorizationError(); - } - return res.success({ - msg: "Notification fetched successfully", - data: notification, - }); - }, - SERVICE_NAME, - "getNotificationById" - ); - - editNotification = this.asyncHandler( - async (req, res) => { - await createNotificationBodyValidation.validateAsync(req.body, { - abortEarly: false, - }); - - const teamId = req?.user?.teamId; - if (!teamId) { - throw this.errorService.createBadRequestError("Team ID is required"); - } - - const notification = await this.db.notificationModule.getNotificationById(req.params.id); - - if (!notification.teamId.equals(teamId)) { - throw this.errorService.createAuthorizationError(); - } - - const editedNotification = await this.db.notificationModule.editNotification(req.params.id, req.body); - return res.success({ - msg: "Notification updated successfully", - data: editedNotification, - }); - }, - SERVICE_NAME, - "editNotification" - ); - - testAllNotifications = this.asyncHandler( - async (req, res) => { - const monitorId = req.body.monitorId; - const teamId = req?.user?.teamId; - if (!teamId) { - throw this.errorService.createBadRequestError("Team ID is required"); - } - - const monitor = await this.db.monitorModule.getMonitorById(monitorId); - - if (!monitor.teamId.equals(teamId)) { - throw this.errorService.createAuthorizationError(); - } - - const notifications = monitor.notifications; - if (notifications.length === 0) throw this.errorService.createBadRequestError("No notifications"); - const result = await this.notificationService.testAllNotifications(notifications); - if (!result) throw this.errorService.createServerError("Failed to send all notifications"); - return res.success({ - msg: "All notifications sent successfully", - }); - }, - SERVICE_NAME, - "testAllNotifications" - ); -} - -export default NotificationController; diff --git a/server/src/controllers/v1/notificationController.ts b/server/src/controllers/v1/notificationController.ts new file mode 100644 index 000000000..71f154a2e --- /dev/null +++ b/server/src/controllers/v1/notificationController.ts @@ -0,0 +1,197 @@ +import { Request, Response, NextFunction } from "express"; + +import { createNotificationBodyValidation } from "@/validation/joi.js"; +import { AppError } from "@/utils/AppError.js"; + +const SERVICE_NAME = "NotificationController"; + +class NotificationController { + static SERVICE_NAME = SERVICE_NAME; + private db: any; + private notificationService: any; + constructor(notificationService: any, statusService: any, db: any) { + this.notificationService = notificationService; + this.db = db; + } + + get serviceName() { + return NotificationController.SERVICE_NAME; + } + + testNotification = async (req: Request, res: Response, next: NextFunction) => { + try { + const notification = req.body; + + const success = await this.notificationService.sendTestNotification(notification); + + if (!success) { + throw new AppError({ message: "Sending notification failed", status: 500 }); + } + + return res.status(200).json({ + success: true, + msg: "Notification sent successfully", + }); + } catch (error) { + next(error); + } + }; + + createNotification = async (req: Request, res: Response, next: NextFunction) => { + try { + await createNotificationBodyValidation.validateAsync(req.body, { + abortEarly: false, + }); + + const body = req.body; + + const teamId = req?.user?.teamId; + if (!teamId) { + throw new AppError({ message: "Team ID is required", status: 400 }); + } + + const userId = req?.user?._id; + if (!userId) { + throw new AppError({ message: "User ID is required", status: 400 }); + } + body.userId = userId; + body.teamId = teamId; + + const notification = await this.db.notificationModule.createNotification(body); + return res.status(200).json({ + success: true, + msg: "Notification created successfully", + data: notification, + }); + } catch (error) { + next(error); + } + }; + + getNotificationsByTeamId = async (req: Request, res: Response, next: NextFunction) => { + try { + const teamId = req?.user?.teamId; + if (!teamId) { + throw new AppError({ message: "Team ID is required", status: 400 }); + } + + const notifications = await this.db.notificationModule.getNotificationsByTeamId(teamId); + + return res.status(200).json({ + success: true, + msg: "Notifications fetched successfully", + data: notifications, + }); + } catch (error) { + next(error); + } + }; + + deleteNotification = async (req: Request, res: Response, next: NextFunction) => { + try { + const teamId = req?.user?.teamId; + if (!teamId) { + throw new AppError({ message: "Team ID is required", status: 400 }); + } + + const notification = await this.db.notificationModule.getNotificationById(req.params.id); + if (!notification.teamId.equals(teamId)) { + throw new AppError({ message: "Unauthorized", status: 403 }); + } + + await this.db.notificationModule.deleteNotificationById(req.params.id); + return res.status(200).json({ + success: true, + msg: "Notification deleted successfully", + }); + } catch (error) { + next(error); + } + }; + + getNotificationById = async (req: Request, res: Response, next: NextFunction) => { + try { + const notification = await this.db.notificationModule.getNotificationById(req.params.id); + + const teamId = req?.user?.teamId; + if (!teamId) { + throw new AppError({ message: "Team ID is required", status: 400 }); + } + + if (!notification.teamId.equals(teamId)) { + throw new AppError({ message: "Unauthorized", status: 403 }); + } + return res.status(200).json({ + success: true, + msg: "Notification fetched successfully", + data: notification, + }); + } catch (error) { + next(error); + } + }; + + editNotification = async (req: Request, res: Response, next: NextFunction) => { + try { + await createNotificationBodyValidation.validateAsync(req.body, { + abortEarly: false, + }); + + const teamId = req?.user?.teamId; + if (!teamId) { + throw new AppError({ message: "Team ID is required", status: 400 }); + } + + const notification = await this.db.notificationModule.getNotificationById(req.params.id); + + if (!notification.teamId.equals(teamId)) { + throw new AppError({ message: "Unauthorized", status: 403 }); + } + + const editedNotification = await this.db.notificationModule.editNotification(req.params.id, req.body); + return res.status(200).json({ + success: true, + msg: "Notification updated successfully", + data: editedNotification, + }); + } catch (error) { + next(error); + } + }; + + testAllNotifications = async (req: Request, res: Response, next: NextFunction) => { + try { + const monitorId = req.body.monitorId; + const teamId = req?.user?.teamId; + if (!teamId) { + throw new AppError({ message: "Team ID is required", status: 400 }); + } + + const monitor = await this.db.monitorModule.getMonitorById(monitorId); + + if (!monitor.teamId.equals(teamId)) { + throw new AppError({ message: "Unauthorized", status: 403 }); + } + + const notifications = monitor.notifications; + if (notifications.length === 0) { + throw new AppError({ message: "No notifications", status: 400 }); + } + + const result = await this.notificationService.testAllNotifications(notifications); + + if (!result) { + throw new AppError({ message: "Failed to send all notifications", status: 500 }); + } + + return res.status(200).json({ + success: true, + msg: "All notifications sent successfully", + }); + } catch (error) { + next(error); + } + }; +} + +export default NotificationController;