diff --git a/server/src/controllers/notificationController.ts b/server/src/controllers/notificationController.ts index 0cee3726e..9c39daee7 100644 --- a/server/src/controllers/notificationController.ts +++ b/server/src/controllers/notificationController.ts @@ -1,16 +1,21 @@ import { Request, Response, NextFunction } from "express"; -import { Notification } from "@/types/index.js"; -import { createNotificationBodyValidation } from "@/validation/notificationValidation.js"; +import { + createNotificationBodyValidation, + deleteNotificationParamValidation, + getNotificationByIdParamValidation, + testNotificationBodyValidation, + editNotificationParamValidation, + testAllNotificationsBodyValidation, +} from "@/validation/notificationValidation.js"; import { AppError } from "@/utils/AppError.js"; import { IMonitorsRepository } from "@/repositories/index.js"; import { INotificationsService } from "@/service/index.js"; -import { requireTeamId } from "./controllerUtils.js"; +import { requireTeamId, requireUserId } from "./controllerUtils.js"; const SERVICE_NAME = "NotificationController"; class NotificationController { - static SERVICE_NAME = SERVICE_NAME; private notificationsService: INotificationsService; private monitorsRepository: IMonitorsRepository; constructor(notificationsService: INotificationsService, monitorsRepository: IMonitorsRepository) { @@ -18,13 +23,9 @@ class NotificationController { this.monitorsRepository = monitorsRepository; } - get serviceName() { - return NotificationController.SERVICE_NAME; - } - testNotification = async (req: Request, res: Response, next: NextFunction) => { try { - const notification: Notification = req.body; + const notification = testNotificationBodyValidation.parse(req.body); const success = await this.notificationsService.sendTestNotification(notification); if (!success) { @@ -34,6 +35,7 @@ class NotificationController { return res.status(200).json({ success: true, msg: "Notification sent successfully", + details: { service: SERVICE_NAME }, }); } catch (error) { next(error); @@ -42,23 +44,12 @@ class NotificationController { createNotification = async (req: Request, res: Response, next: NextFunction) => { try { - createNotificationBodyValidation.parse(req.body); + const validatedBody = createNotificationBodyValidation.parse(req.body); - const body = req.body; + const teamId = requireTeamId(req.user?.teamId); + const userId = requireUserId(req.user?.id); - 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.notificationsService.createNotification(body); + const notification = await this.notificationsService.createNotification(validatedBody, userId, teamId); return res.status(200).json({ success: true, msg: "Notification created successfully", @@ -71,11 +62,7 @@ class NotificationController { 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 teamId = requireTeamId(req.user?.teamId); const notifications = await this.notificationsService.findNotificationsByTeamId(teamId); return res.status(200).json({ @@ -90,17 +77,10 @@ class NotificationController { 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 teamId = requireTeamId(req.user?.teamId); + const validatedParams = deleteNotificationParamValidation.parse(req.params); - const notificationId = req.params.id as string; - if (!notificationId) { - throw new AppError({ message: "Notification ID is required", status: 400 }); - } - - await this.notificationsService.deleteById(notificationId, teamId); + await this.notificationsService.deleteById(validatedParams.id, teamId); return res.status(200).json({ success: true, msg: "Notification deleted successfully", @@ -113,12 +93,9 @@ class NotificationController { getNotificationById = async (req: Request, res: Response, next: NextFunction) => { try { const teamId = requireTeamId(req.user?.teamId); - const notificationId = req.params.id as string; - if (!notificationId) { - throw new AppError({ message: "Notification ID is required", status: 400 }); - } + const validatedParams = getNotificationByIdParamValidation.parse(req.params); - const notification = await this.notificationsService.findById(notificationId, teamId); + const notification = await this.notificationsService.findById(validatedParams.id, teamId); return res.status(200).json({ success: true, @@ -132,14 +109,13 @@ class NotificationController { editNotification = async (req: Request, res: Response, next: NextFunction) => { try { - createNotificationBodyValidation.parse(req.body); + const validatedBody = createNotificationBodyValidation.parse(req.body); + const validatedParams = editNotificationParamValidation.parse(req.params); const teamId = requireTeamId(req.user?.teamId); - const notificationId = req.params.id as string; - if (!notificationId) { - throw new AppError({ message: "Notification ID is required", status: 400 }); - } - const editedNotification = await this.notificationsService.updateById(notificationId, teamId, req.body); + const notificationId = validatedParams.id; + + const editedNotification = await this.notificationsService.updateById(notificationId, teamId, validatedBody); return res.status(200).json({ success: true, msg: "Notification updated successfully", @@ -152,13 +128,11 @@ class NotificationController { 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 validatedBody = testAllNotificationsBodyValidation.parse(req.body); - const monitor = await this.monitorsRepository.findById(monitorId, teamId); + const teamId = requireTeamId(req.user?.teamId); + + const monitor = await this.monitorsRepository.findById(validatedBody.monitorId, teamId); const notifications = monitor.notifications; if (notifications.length === 0) { diff --git a/server/src/service/infrastructure/notificationProviders/INotificationProvider.ts b/server/src/service/infrastructure/notificationProviders/INotificationProvider.ts index 87056ef7a..2743dfab9 100644 --- a/server/src/service/infrastructure/notificationProviders/INotificationProvider.ts +++ b/server/src/service/infrastructure/notificationProviders/INotificationProvider.ts @@ -3,5 +3,5 @@ import type { NotificationMessage } from "@/types/notificationMessage.js"; export interface INotificationProvider { sendMessage: (notification: Notification, message: NotificationMessage) => Promise; - sendTestAlert(notification: Notification): Promise; + sendTestAlert(notification: Partial): Promise; } diff --git a/server/src/service/infrastructure/notificationProviders/discord.ts b/server/src/service/infrastructure/notificationProviders/discord.ts index e630e4e86..3bd2556ae 100644 --- a/server/src/service/infrastructure/notificationProviders/discord.ts +++ b/server/src/service/infrastructure/notificationProviders/discord.ts @@ -13,7 +13,7 @@ export class DiscordProvider implements INotificationProvider { this.logger = logger; } - sendTestAlert = async (notification: Notification) => { + sendTestAlert = async (notification: Partial) => { if (!notification.address) { return false; } diff --git a/server/src/service/infrastructure/notificationProviders/email.ts b/server/src/service/infrastructure/notificationProviders/email.ts index 338ceb619..b3686651c 100644 --- a/server/src/service/infrastructure/notificationProviders/email.ts +++ b/server/src/service/infrastructure/notificationProviders/email.ts @@ -14,7 +14,7 @@ export class EmailProvider implements INotificationProvider { this.logger = logger; } - async sendTestAlert(notification: Notification): Promise { + async sendTestAlert(notification: Partial): Promise { const subject = "Test notification"; const html = await buildTestEmail(this.emailService); diff --git a/server/src/service/infrastructure/notificationProviders/matrix.ts b/server/src/service/infrastructure/notificationProviders/matrix.ts index ce53215bd..f14ea4d17 100644 --- a/server/src/service/infrastructure/notificationProviders/matrix.ts +++ b/server/src/service/infrastructure/notificationProviders/matrix.ts @@ -13,7 +13,7 @@ export class MatrixProvider implements INotificationProvider { this.logger = logger; } - sendTestAlert = async (notification: Notification) => { + sendTestAlert = async (notification: Partial) => { const { homeserverUrl, accessToken, roomId } = notification; if (!homeserverUrl || !accessToken || !roomId) { return false; diff --git a/server/src/service/infrastructure/notificationProviders/pagerduty.ts b/server/src/service/infrastructure/notificationProviders/pagerduty.ts index b15717878..304c38cf4 100644 --- a/server/src/service/infrastructure/notificationProviders/pagerduty.ts +++ b/server/src/service/infrastructure/notificationProviders/pagerduty.ts @@ -14,7 +14,7 @@ export class PagerDutyProvider implements INotificationProvider { this.logger = logger; } - async sendTestAlert(notification: Notification): Promise { + async sendTestAlert(notification: Partial): Promise { try { await got.post("https://events.pagerduty.com/v2/enqueue", { json: { diff --git a/server/src/service/infrastructure/notificationProviders/slack.ts b/server/src/service/infrastructure/notificationProviders/slack.ts index e13362669..cfb135901 100644 --- a/server/src/service/infrastructure/notificationProviders/slack.ts +++ b/server/src/service/infrastructure/notificationProviders/slack.ts @@ -13,7 +13,7 @@ export class SlackProvider implements INotificationProvider { this.logger = logger; } - async sendTestAlert(notification: Notification): Promise { + async sendTestAlert(notification: Partial): Promise { if (!notification.address) { return false; } diff --git a/server/src/service/infrastructure/notificationProviders/teams.ts b/server/src/service/infrastructure/notificationProviders/teams.ts index 2a4476807..dcf48c4cd 100644 --- a/server/src/service/infrastructure/notificationProviders/teams.ts +++ b/server/src/service/infrastructure/notificationProviders/teams.ts @@ -65,7 +65,7 @@ export class TeamsProvider implements INotificationProvider { this.logger = logger; } - async sendTestAlert(notification: Notification): Promise { + async sendTestAlert(notification: Partial): Promise { if (!notification.address) { return false; } diff --git a/server/src/service/infrastructure/notificationProviders/webhook.ts b/server/src/service/infrastructure/notificationProviders/webhook.ts index 6a00f3f1e..fcb5728d4 100644 --- a/server/src/service/infrastructure/notificationProviders/webhook.ts +++ b/server/src/service/infrastructure/notificationProviders/webhook.ts @@ -98,7 +98,7 @@ export class WebhookProvider implements INotificationProvider { }; } - sendTestAlert = async (notification: Notification) => { + sendTestAlert = async (notification: Partial) => { if (!notification.address) { return false; } diff --git a/server/src/service/infrastructure/notificationsService.ts b/server/src/service/infrastructure/notificationsService.ts index ea7b78a4f..c75477c88 100644 --- a/server/src/service/infrastructure/notificationsService.ts +++ b/server/src/service/infrastructure/notificationsService.ts @@ -8,14 +8,14 @@ import { ILogger } from "@/utils/logger.js"; import type { INotificationMessageBuilder } from "@/service/infrastructure/notificationMessageBuilder.js"; export interface INotificationsService { - createNotification: (notificationData: Partial) => Promise; + createNotification: (notificationData: Partial, userId: string, teamId: string) => Promise; findById: (id: string, teamId: string) => Promise; findNotificationsByTeamId: (teamId: string) => Promise; updateById(id: string, teamId: string, updateData: Partial): Promise; deleteById: (id: string, teamId: string) => Promise; handleNotifications: (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision) => Promise; - sendTestNotification: (notification: Notification) => Promise; + sendTestNotification: (notification: Partial) => Promise; testAllNotifications: (notificationIds: string[]) => Promise; } @@ -141,7 +141,7 @@ export class NotificationsService implements INotificationsService { return await this.sendNotifications(monitor, monitorStatusResponse, decision); }; - sendTestNotification = async (notification: Notification) => { + sendTestNotification = async (notification: Partial) => { switch (notification.type) { case "email": return await this.emailProvider.sendTestAlert(notification); @@ -174,7 +174,9 @@ export class NotificationsService implements INotificationsService { return true; }; - createNotification = async (notificationData: Partial): Promise => { + createNotification = async (notificationData: Partial, userId: string, teamId: string): Promise => { + notificationData.userId = userId; + notificationData.teamId = teamId; return await this.notificationsRepository.create(notificationData); }; diff --git a/server/src/validation/notificationValidation.ts b/server/src/validation/notificationValidation.ts index 5aa0b976e..9ae4e9fdc 100644 --- a/server/src/validation/notificationValidation.ts +++ b/server/src/validation/notificationValidation.ts @@ -67,6 +67,22 @@ export const createNotificationBodyValidation = z.discriminatedUnion("type", [ }), ]); +export const testNotificationBodyValidation = createNotificationBodyValidation; + +export const deleteNotificationParamValidation = z.object({ + id: z.string().min(1, "Notification ID is required"), +}); +export const getNotificationByIdParamValidation = z.object({ + id: z.string().min(1, "Notification ID is required"), +}); +export const editNotificationParamValidation = z.object({ + id: z.string().min(1, "Notification ID is required"), +}); + +export const testAllNotificationsBodyValidation = z.object({ + monitorId: z.string().min(1, "Monitor ID is required"), +}); + export const sendTestEmailBodyValidation = z.object({ to: z.string().min(1, "To field is required"), systemEmailHost: z.string().optional(),