Files
Checkmate/server/src/controllers/notificationController.ts
T

163 lines
5.5 KiB
TypeScript
Raw Normal View History

2026-01-09 15:02:19 -08:00
import { Request, Response, NextFunction } from "express";
2026-03-11 13:28:48 -07:00
import {
createNotificationBodyValidation,
deleteNotificationParamValidation,
getNotificationByIdParamValidation,
testNotificationBodyValidation,
editNotificationParamValidation,
testAllNotificationsBodyValidation,
} from "@/validation/notificationValidation.js";
2026-01-09 15:02:19 -08:00
import { AppError } from "@/utils/AppError.js";
2026-01-19 21:39:02 +00:00
import { INotificationsService } from "@/service/index.js";
2026-03-11 13:28:48 -07:00
import { requireTeamId, requireUserId } from "./controllerUtils.js";
import { IMonitorsRepository } from "@/repositories/index.js";
2026-01-09 15:02:19 -08:00
const SERVICE_NAME = "NotificationController";
2026-03-20 11:45:04 -07:00
export interface INotificationController {
testNotification: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
createNotification: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
getNotificationsByTeamId: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
deleteNotification: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
getNotificationById: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
editNotification: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
testAllNotifications: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
}
class NotificationController implements INotificationController {
2026-01-19 21:39:02 +00:00
private notificationsService: INotificationsService;
2026-01-15 20:55:01 +00:00
private monitorsRepository: IMonitorsRepository;
2026-01-21 13:51:04 -08:00
constructor(notificationsService: INotificationsService, monitorsRepository: IMonitorsRepository) {
2026-01-19 21:39:02 +00:00
this.notificationsService = notificationsService;
2026-01-15 20:55:01 +00:00
this.monitorsRepository = monitorsRepository;
2026-01-09 15:02:19 -08:00
}
testNotification = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-03-11 13:28:48 -07:00
const notification = testNotificationBodyValidation.parse(req.body);
2026-01-19 21:39:02 +00:00
const success = await this.notificationsService.sendTestNotification(notification);
2026-01-09 15:02:19 -08:00
return res.status(200).json({
success,
msg: success ? "Notification sent successfully" : "Notification could not be sent — check the destination details.",
2026-03-11 13:30:39 -07:00
details: { service: SERVICE_NAME },
2026-01-09 15:02:19 -08:00
});
} catch (error) {
next(error);
}
};
createNotification = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-03-11 13:28:48 -07:00
const validatedBody = createNotificationBodyValidation.parse(req.body);
2026-01-09 15:02:19 -08:00
2026-03-11 13:28:48 -07:00
const teamId = requireTeamId(req.user?.teamId);
const userId = requireUserId(req.user?.id);
2026-01-09 15:02:19 -08:00
2026-03-11 13:28:48 -07:00
const notification = await this.notificationsService.createNotification(validatedBody, userId, teamId);
2026-01-09 15:02:19 -08:00
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 {
2026-03-11 13:28:48 -07:00
const teamId = requireTeamId(req.user?.teamId);
2026-01-21 13:51:04 -08:00
const notifications = await this.notificationsService.findNotificationsByTeamId(teamId);
2026-01-09 15:02:19 -08:00
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 {
2026-03-11 13:28:48 -07:00
const teamId = requireTeamId(req.user?.teamId);
const validatedParams = deleteNotificationParamValidation.parse(req.params);
2026-01-09 15:02:19 -08:00
2026-03-11 13:28:48 -07:00
await this.notificationsService.deleteById(validatedParams.id, teamId);
2026-01-09 15:02:19 -08:00
return res.status(200).json({
success: true,
msg: "Notification deleted successfully",
});
} catch (error) {
next(error);
}
};
getNotificationById = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-01-21 13:51:04 -08:00
const teamId = requireTeamId(req.user?.teamId);
2026-03-11 13:28:48 -07:00
const validatedParams = getNotificationByIdParamValidation.parse(req.params);
2026-01-09 15:02:19 -08:00
2026-03-11 13:28:48 -07:00
const notification = await this.notificationsService.findById(validatedParams.id, teamId);
2026-01-21 13:51:04 -08:00
2026-01-09 15:02:19 -08:00
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 {
2026-03-11 13:28:48 -07:00
const validatedBody = createNotificationBodyValidation.parse(req.body);
const validatedParams = editNotificationParamValidation.parse(req.params);
2026-01-09 15:02:19 -08:00
2026-01-21 13:51:04 -08:00
const teamId = requireTeamId(req.user?.teamId);
2026-03-11 13:28:48 -07:00
const notificationId = validatedParams.id;
2026-03-11 13:45:17 -07:00
2026-03-11 13:28:48 -07:00
const editedNotification = await this.notificationsService.updateById(notificationId, teamId, validatedBody);
2026-01-09 15:02:19 -08:00
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 {
2026-03-11 13:28:48 -07:00
const validatedBody = testAllNotificationsBodyValidation.parse(req.body);
const teamId = requireTeamId(req.user?.teamId);
2026-01-09 15:02:19 -08:00
2026-03-11 13:28:48 -07:00
const monitor = await this.monitorsRepository.findById(validatedBody.monitorId, teamId);
const notifications = monitor.notifications || [];
2026-01-09 15:02:19 -08:00
if (notifications.length === 0) {
throw new AppError({ message: "No notifications", status: 400 });
}
2026-01-19 21:39:02 +00:00
const result = await this.notificationsService.testAllNotifications(notifications);
2026-01-09 15:02:19 -08:00
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;