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

188 lines
5.3 KiB
TypeScript
Raw Normal View History

2026-01-09 15:02:19 -08:00
import { Request, Response, NextFunction } from "express";
2026-01-19 21:39:02 +00:00
import { Notification } from "@/types/index.js";
2026-01-09 15:02:19 -08:00
import { createNotificationBodyValidation } from "@/validation/joi.js";
import { AppError } from "@/utils/AppError.js";
2026-01-15 20:55:01 +00:00
import { IMonitorsRepository } from "@/repositories/index.js";
2026-01-19 21:39:02 +00:00
import { INotificationsService } from "@/service/index.js";
2026-01-21 13:51:04 -08:00
import { requireTeamId } from "./controllerUtils.js";
2026-01-09 15:02:19 -08:00
const SERVICE_NAME = "NotificationController";
class NotificationController {
static SERVICE_NAME = SERVICE_NAME;
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
}
get serviceName() {
return NotificationController.SERVICE_NAME;
}
testNotification = async (req: Request, res: Response, next: NextFunction) => {
try {
2026-01-19 21:39:02 +00:00
const notification: Notification = req.body;
const success = await this.notificationsService.sendTestNotification(notification);
2026-01-09 15:02:19 -08:00
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 });
}
2026-01-16 22:41:12 +00:00
const userId = req?.user?.id;
2026-01-09 15:02:19 -08:00
if (!userId) {
throw new AppError({ message: "User ID is required", status: 400 });
}
body.userId = userId;
body.teamId = teamId;
2026-01-21 13:51:04 -08:00
const notification = await this.notificationsService.createNotification(body);
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 {
const teamId = req?.user?.teamId;
if (!teamId) {
throw new AppError({ message: "Team ID is required", status: 400 });
}
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 {
const teamId = req?.user?.teamId;
if (!teamId) {
throw new AppError({ message: "Team ID is required", status: 400 });
}
2026-01-27 21:54:20 +00:00
const notificationId = req.params.id as string;
2026-01-21 13:51:04 -08:00
if (!notificationId) {
throw new AppError({ message: "Notification ID is required", status: 400 });
2026-01-09 15:02:19 -08:00
}
2026-01-21 13:51:04 -08:00
await this.notificationsService.deleteById(notificationId, 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-01-27 21:54:20 +00:00
const notificationId = req.params.id as string;
2026-01-21 13:51:04 -08:00
if (!notificationId) {
throw new AppError({ message: "Notification ID is required", status: 400 });
2026-01-09 15:02:19 -08:00
}
2026-01-21 13:51:04 -08:00
const notification = await this.notificationsService.findById(notificationId, teamId);
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 {
await createNotificationBodyValidation.validateAsync(req.body, {
abortEarly: false,
});
2026-01-21 13:51:04 -08:00
const teamId = requireTeamId(req.user?.teamId);
2026-01-27 21:54:20 +00:00
const notificationId = req.params.id as string;
2026-01-21 13:51:04 -08:00
if (!notificationId) {
throw new AppError({ message: "Notification ID is required", status: 400 });
2026-01-09 15:02:19 -08:00
}
2026-01-21 13:51:04 -08:00
const editedNotification = await this.notificationsService.updateById(notificationId, teamId, req.body);
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 {
const monitorId = req.body.monitorId;
const teamId = req?.user?.teamId;
if (!teamId) {
throw new AppError({ message: "Team ID is required", status: 400 });
}
2026-01-15 20:55:01 +00:00
const monitor = await this.monitorsRepository.findById(monitorId, teamId);
2026-01-09 15:02:19 -08:00
const notifications = monitor.notifications;
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;