Merge pull request #3411 from bluewave-labs/fix/notification-controller-validation

fix: implement NotificationController missing validation
This commit is contained in:
Alexander Holliday authored and GitHub committed 2026-03-11 13:46:51 -07:00
commit e4a228e61e
11 files changed
+60 -68

No files matched your search

@@ -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) {
@@ -3,5 +3,5 @@ import type { NotificationMessage } from "@/types/notificationMessage.js";
export interface INotificationProvider {
sendMessage: (notification: Notification, message: NotificationMessage) => Promise<boolean>;
sendTestAlert(notification: Notification): Promise<boolean>;
sendTestAlert(notification: Partial<Notification>): Promise<boolean>;
}
@@ -13,7 +13,7 @@ export class DiscordProvider implements INotificationProvider {
this.logger = logger;
}
sendTestAlert = async (notification: Notification) => {
sendTestAlert = async (notification: Partial<Notification>) => {
if (!notification.address) {
return false;
}
@@ -14,7 +14,7 @@ export class EmailProvider implements INotificationProvider {
this.logger = logger;
}
async sendTestAlert(notification: Notification): Promise<boolean> {
async sendTestAlert(notification: Partial<Notification>): Promise<boolean> {
const subject = "Test notification";
const html = await buildTestEmail(this.emailService);
@@ -13,7 +13,7 @@ export class MatrixProvider implements INotificationProvider {
this.logger = logger;
}
sendTestAlert = async (notification: Notification) => {
sendTestAlert = async (notification: Partial<Notification>) => {
const { homeserverUrl, accessToken, roomId } = notification;
if (!homeserverUrl || !accessToken || !roomId) {
return false;
@@ -14,7 +14,7 @@ export class PagerDutyProvider implements INotificationProvider {
this.logger = logger;
}
async sendTestAlert(notification: Notification): Promise<boolean> {
async sendTestAlert(notification: Partial<Notification>): Promise<boolean> {
try {
await got.post("https://events.pagerduty.com/v2/enqueue", {
json: {
@@ -13,7 +13,7 @@ export class SlackProvider implements INotificationProvider {
this.logger = logger;
}
async sendTestAlert(notification: Notification): Promise<boolean> {
async sendTestAlert(notification: Partial<Notification>): Promise<boolean> {
if (!notification.address) {
return false;
}
@@ -65,7 +65,7 @@ export class TeamsProvider implements INotificationProvider {
this.logger = logger;
}
async sendTestAlert(notification: Notification): Promise<boolean> {
async sendTestAlert(notification: Partial<Notification>): Promise<boolean> {
if (!notification.address) {
return false;
}
@@ -98,7 +98,7 @@ export class WebhookProvider implements INotificationProvider {
};
}
sendTestAlert = async (notification: Notification) => {
sendTestAlert = async (notification: Partial<Notification>) => {
if (!notification.address) {
return false;
}
@@ -8,14 +8,14 @@ import { ILogger } from "@/utils/logger.js";
import type { INotificationMessageBuilder } from "@/service/infrastructure/notificationMessageBuilder.js";
export interface INotificationsService {
createNotification: (notificationData: Partial<Notification>) => Promise<Notification>;
createNotification: (notificationData: Partial<Notification>, userId: string, teamId: string) => Promise<Notification>;
findById: (id: string, teamId: string) => Promise<Notification>;
findNotificationsByTeamId: (teamId: string) => Promise<Notification[]>;
updateById(id: string, teamId: string, updateData: Partial<Notification>): Promise<Notification>;
deleteById: (id: string, teamId: string) => Promise<Notification>;
handleNotifications: (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse, decision: MonitorActionDecision) => Promise<boolean>;
sendTestNotification: (notification: Notification) => Promise<boolean>;
sendTestNotification: (notification: Partial<Notification>) => Promise<boolean>;
testAllNotifications: (notificationIds: string[]) => Promise<boolean>;
}
@@ -141,7 +141,7 @@ export class NotificationsService implements INotificationsService {
return await this.sendNotifications(monitor, monitorStatusResponse, decision);
};
sendTestNotification = async (notification: Notification) => {
sendTestNotification = async (notification: Partial<Notification>) => {
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<Notification>): Promise<Notification> => {
createNotification = async (notificationData: Partial<Notification>, userId: string, teamId: string): Promise<Notification> => {
notificationData.userId = userId;
notificationData.teamId = teamId;
return await this.notificationsRepository.create(notificationData);
};
@@ -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(),