js -> ts
This commit is contained in:
1 parent
a9ce980d03
commit
64320c7aca
3 files changed
+198
-184
No files matched your search
@@ -51,10 +51,7 @@ export const initializeControllers = (services) => {
|
|||||||
});
|
});
|
||||||
controllers.logController = new LogController(commonDependencies);
|
controllers.logController = new LogController(commonDependencies);
|
||||||
controllers.statusPageController = new StatusPageController(commonDependencies);
|
controllers.statusPageController = new StatusPageController(commonDependencies);
|
||||||
controllers.notificationController = new NotificationController(commonDependencies, {
|
controllers.notificationController = new NotificationController(services.notificationService, services.db);
|
||||||
notificationService: services.notificationService,
|
|
||||||
statusService: services.statusService,
|
|
||||||
});
|
|
||||||
controllers.diagnosticController = new DiagnosticController(commonDependencies, {
|
controllers.diagnosticController = new DiagnosticController(commonDependencies, {
|
||||||
diagnosticService: services.diagnosticService,
|
diagnosticService: services.diagnosticService,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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;
|
|
||||||
@@ -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;
|
||||||
Reference in new issue
Block a user