Merge pull request #3783 from bluewave-labs/chore/controller-refactor
chore: add catchAsync to handle errors, remove all try/catch blocks from con…
This commit is contained in:
15 files changed
+1243
-1535
No files matched your search
@@ -1,4 +1,5 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, RequestHandler } from "express";
|
||||||
|
import { catchAsync } from "@/utils/catchAsync.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import { requireTeamId, requireUserEmail, requireUserId, requireUserRoles } from "@/api/controllers/controllerUtils.js";
|
import { requireTeamId, requireUserEmail, requireUserId, requireUserRoles } from "@/api/controllers/controllerUtils.js";
|
||||||
|
|
||||||
@@ -24,20 +25,20 @@ import { IUserService } from "@/domain/users/user.service.js";
|
|||||||
const SERVICE_NAME = "authController";
|
const SERVICE_NAME = "authController";
|
||||||
|
|
||||||
export interface IAuthController {
|
export interface IAuthController {
|
||||||
registerUser(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
registerUser: RequestHandler;
|
||||||
createUser(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
createUser: RequestHandler;
|
||||||
loginUser(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
loginUser: RequestHandler;
|
||||||
editUser(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
editUser: RequestHandler;
|
||||||
checkSuperadminExists(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
checkSuperadminExists: RequestHandler;
|
||||||
requestRecovery(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
requestRecovery: RequestHandler;
|
||||||
validateRecovery(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
validateRecovery: RequestHandler;
|
||||||
resetPassword(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
resetPassword: RequestHandler;
|
||||||
deleteUser(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
deleteUser: RequestHandler;
|
||||||
deleteUserById(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
deleteUserById: RequestHandler;
|
||||||
getAllUsers(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
getAllUsers: RequestHandler;
|
||||||
getUserById(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
getUserById: RequestHandler;
|
||||||
editUserById(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
editUserById: RequestHandler;
|
||||||
editUserPasswordById(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
editUserPasswordById: RequestHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
class AuthController implements IAuthController {
|
class AuthController implements IAuthController {
|
||||||
@@ -53,239 +54,183 @@ class AuthController implements IAuthController {
|
|||||||
return AuthController.SERVICE_NAME;
|
return AuthController.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
registerUser = async (req: Request, res: Response, next: NextFunction) => {
|
registerUser = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const newUser = req.body.user;
|
||||||
const newUser = req.body.user;
|
const newUserToken = req.body.token;
|
||||||
const newUserToken = req.body.token;
|
if (newUser?.email) {
|
||||||
if (newUser?.email) {
|
const newUserEmail = requireUserEmail(newUser.email);
|
||||||
const newUserEmail = requireUserEmail(newUser.email);
|
newUser.email = newUserEmail.toLowerCase();
|
||||||
newUser.email = newUserEmail.toLowerCase();
|
|
||||||
}
|
|
||||||
const validatedBody = registrationBodyValidation.parse(newUser);
|
|
||||||
|
|
||||||
const { user, token } = await this.userService.registerUser(validatedBody, newUserToken, req?.file ?? null);
|
|
||||||
res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "User registered successfully",
|
|
||||||
data: { user, token },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
}
|
||||||
};
|
const validatedBody = registrationBodyValidation.parse(newUser);
|
||||||
|
|
||||||
createUser = async (req: Request, res: Response, next: NextFunction) => {
|
const { user, token } = await this.userService.registerUser(validatedBody, newUserToken, req?.file ?? null);
|
||||||
try {
|
res.status(200).json({
|
||||||
const userData = req.body;
|
success: true,
|
||||||
if (userData?.email) {
|
msg: "User registered successfully",
|
||||||
userData.email = userData.email.toLowerCase();
|
data: { user, token },
|
||||||
}
|
});
|
||||||
const validatedBody = createUserBodyValidation.parse(userData);
|
});
|
||||||
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
createUser = catchAsync(async (req: Request, res: Response) => {
|
||||||
const actorRoles = requireUserRoles(req.user?.role);
|
const userData = req.body;
|
||||||
const newUser = await this.userService.createUser(validatedBody, teamId, actorRoles, req?.file ?? null);
|
if (userData?.email) {
|
||||||
res.status(201).json({
|
userData.email = userData.email.toLowerCase();
|
||||||
success: true,
|
|
||||||
msg: "User created successfully",
|
|
||||||
data: newUser,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
}
|
||||||
};
|
const validatedBody = createUserBodyValidation.parse(userData);
|
||||||
|
|
||||||
loginUser = async (req: Request, res: Response, next: NextFunction) => {
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
try {
|
const actorRoles = requireUserRoles(req.user?.role);
|
||||||
if (req.body?.email) {
|
const newUser = await this.userService.createUser(validatedBody, teamId, actorRoles, req?.file ?? null);
|
||||||
req.body.email = req.body.email?.toLowerCase();
|
res.status(201).json({
|
||||||
}
|
success: true,
|
||||||
loginValidation.parse(req.body);
|
msg: "User created successfully",
|
||||||
const { user, token } = await this.userService.loginUser(req.body.email, req.body.password);
|
data: newUser,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
return res.status(200).json({
|
loginUser = catchAsync(async (req: Request, res: Response) => {
|
||||||
success: true,
|
if (req.body?.email) {
|
||||||
msg: "User logged in successfully",
|
req.body.email = req.body.email?.toLowerCase();
|
||||||
data: {
|
|
||||||
user,
|
|
||||||
token,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
}
|
||||||
};
|
loginValidation.parse(req.body);
|
||||||
|
const { user, token } = await this.userService.loginUser(req.body.email, req.body.password);
|
||||||
|
|
||||||
editUser = async (req: Request, res: Response, next: NextFunction) => {
|
return res.status(200).json({
|
||||||
try {
|
success: true,
|
||||||
const validatedBody = editUserBodyValidation.parse(req.body);
|
msg: "User logged in successfully",
|
||||||
const userId = requireUserId(req.user?.id);
|
data: {
|
||||||
const userEmail = requireUserEmail(req.user?.email);
|
user,
|
||||||
const updatedUser = await this.userService.editUser(validatedBody, req?.file ?? null, userId, userEmail);
|
token,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
res.status(200).json({
|
editUser = catchAsync(async (req: Request, res: Response) => {
|
||||||
success: true,
|
const validatedBody = editUserBodyValidation.parse(req.body);
|
||||||
msg: "User updated successfully",
|
const userId = requireUserId(req.user?.id);
|
||||||
data: updatedUser,
|
const userEmail = requireUserEmail(req.user?.email);
|
||||||
});
|
const updatedUser = await this.userService.editUser(validatedBody, req?.file ?? null, userId, userEmail);
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "User updated successfully",
|
||||||
|
data: updatedUser,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
checkSuperadminExists = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const superAdminExists = await this.userService.checkSuperadminExists();
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Superadmin existence checked successfully",
|
||||||
|
data: superAdminExists,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
requestRecovery = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
recoveryValidation.parse(req.body);
|
||||||
|
const email = req?.body?.email;
|
||||||
|
const msgId = await this.userService.requestRecovery(email);
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Password recovery email sent successfully",
|
||||||
|
data: msgId,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
validateRecovery = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
recoveryTokenBodyValidation.parse(req.body);
|
||||||
|
await this.userService.validateRecovery(req.body.recoveryToken);
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Recovery token is valid",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
resetPassword = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
newPasswordValidation.parse(req.body);
|
||||||
|
const { user, token } = await this.userService.resetPassword(req.body.password, req.body.recoveryToken);
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Password has been reset successfully",
|
||||||
|
data: { user, token },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
deleteUser = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const userId = requireUserId(req.user?.id);
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const roles = requireUserRoles(req.user?.role);
|
||||||
|
|
||||||
|
await this.userService.deleteUser({
|
||||||
|
userId,
|
||||||
|
teamId,
|
||||||
|
roles,
|
||||||
|
});
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "User deleted successfully",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
deleteUserById = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedParams = getUserByIdParamValidation.parse(req.params);
|
||||||
|
const targetUserId = validatedParams.userId;
|
||||||
|
const actorId = requireUserId(req.user?.id);
|
||||||
|
const actorTeamId = requireTeamId(req.user?.teamId);
|
||||||
|
const actorRoles = requireUserRoles(req.user?.role);
|
||||||
|
await this.userService.deleteUserById({ actorId, actorTeamId, actorRoles, targetUserId });
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "User removed successfully",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
getAllUsers = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const allUsers = await this.userService.getAllUsers();
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Users retrieved successfully",
|
||||||
|
data: allUsers,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
getUserById = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedParams = getUserByIdParamValidation.parse(req.params);
|
||||||
|
const actorRoles = requireUserRoles(req.user?.role);
|
||||||
|
const user = await this.userService.getUserById(actorRoles, validatedParams.userId);
|
||||||
|
|
||||||
|
return res.status(200).json({ success: true, msg: "ok", data: user });
|
||||||
|
});
|
||||||
|
|
||||||
|
editUserById = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const actorRoles = requireUserRoles(req.user?.role);
|
||||||
|
const actorId = requireUserId(req.user?.id);
|
||||||
|
|
||||||
|
if (!actorRoles.includes("superadmin")) {
|
||||||
|
throw new AppError({ message: "Unauthorized", status: 403 });
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
checkSuperadminExists = async (req: Request, res: Response, next: NextFunction) => {
|
const validatedParams = editUserByIdParamValidation.parse(req.params);
|
||||||
try {
|
// If this is superadmin self edit, allow "superadmin" role
|
||||||
const superAdminExists = await this.userService.checkSuperadminExists();
|
const validatedBody =
|
||||||
return res.status(200).json({
|
validatedParams.userId === actorId ? editSuperadminUserByIdBodyValidation.parse(req.body) : editUserByIdBodyValidation.parse(req.body);
|
||||||
success: true,
|
|
||||||
msg: "Superadmin existence checked successfully",
|
await this.userService.editUserById(validatedParams.userId, validatedBody);
|
||||||
data: superAdminExists,
|
return res.status(200).json({ success: true, msg: "ok" });
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
editUserPasswordById = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const actorRoles = requireUserRoles(req.user?.role);
|
||||||
|
if (!actorRoles.includes("superadmin")) {
|
||||||
|
throw new AppError({ message: "Unauthorized", status: 403 });
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
requestRecovery = async (req: Request, res: Response, next: NextFunction) => {
|
const validatedParams = editUserByIdParamValidation.parse(req.params);
|
||||||
try {
|
const validatedBody = editUserPasswordByIdBodyValidation.parse(req.body);
|
||||||
recoveryValidation.parse(req.body);
|
await this.userService.setPasswordByUserId(validatedParams.userId, validatedBody.password);
|
||||||
const email = req?.body?.email;
|
return res.status(200).json({ success: true, msg: "Password reset successfully" });
|
||||||
const msgId = await this.userService.requestRecovery(email);
|
});
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Password recovery email sent successfully",
|
|
||||||
data: msgId,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
validateRecovery = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
recoveryTokenBodyValidation.parse(req.body);
|
|
||||||
await this.userService.validateRecovery(req.body.recoveryToken);
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Recovery token is valid",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
resetPassword = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
newPasswordValidation.parse(req.body);
|
|
||||||
const { user, token } = await this.userService.resetPassword(req.body.password, req.body.recoveryToken);
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Password has been reset successfully",
|
|
||||||
data: { user, token },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
deleteUser = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const userId = requireUserId(req.user?.id);
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const roles = requireUserRoles(req.user?.role);
|
|
||||||
|
|
||||||
await this.userService.deleteUser({
|
|
||||||
userId,
|
|
||||||
teamId,
|
|
||||||
roles,
|
|
||||||
});
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "User deleted successfully",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
deleteUserById = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const validatedParams = getUserByIdParamValidation.parse(req.params);
|
|
||||||
const targetUserId = validatedParams.userId;
|
|
||||||
const actorId = requireUserId(req.user?.id);
|
|
||||||
const actorTeamId = requireTeamId(req.user?.teamId);
|
|
||||||
const actorRoles = requireUserRoles(req.user?.role);
|
|
||||||
await this.userService.deleteUserById({ actorId, actorTeamId, actorRoles, targetUserId });
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "User removed successfully",
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getAllUsers = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const allUsers = await this.userService.getAllUsers();
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Users retrieved successfully",
|
|
||||||
data: allUsers,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getUserById = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const validatedParams = getUserByIdParamValidation.parse(req.params);
|
|
||||||
const actorRoles = requireUserRoles(req.user?.role);
|
|
||||||
const user = await this.userService.getUserById(actorRoles, validatedParams.userId);
|
|
||||||
|
|
||||||
return res.status(200).json({ success: true, msg: "ok", data: user });
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
editUserById = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const actorRoles = requireUserRoles(req.user?.role);
|
|
||||||
const actorId = requireUserId(req.user?.id);
|
|
||||||
|
|
||||||
if (!actorRoles.includes("superadmin")) {
|
|
||||||
throw new AppError({ message: "Unauthorized", status: 403 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const validatedParams = editUserByIdParamValidation.parse(req.params);
|
|
||||||
// If this is superadmin self edit, allow "superadmin" role
|
|
||||||
const validatedBody =
|
|
||||||
validatedParams.userId === actorId ? editSuperadminUserByIdBodyValidation.parse(req.body) : editUserByIdBodyValidation.parse(req.body);
|
|
||||||
|
|
||||||
await this.userService.editUserById(validatedParams.userId, validatedBody);
|
|
||||||
return res.status(200).json({ success: true, msg: "ok" });
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
editUserPasswordById = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const actorRoles = requireUserRoles(req.user?.role);
|
|
||||||
if (!actorRoles.includes("superadmin")) {
|
|
||||||
throw new AppError({ message: "Unauthorized", status: 403 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const validatedParams = editUserByIdParamValidation.parse(req.params);
|
|
||||||
const validatedBody = editUserPasswordByIdBodyValidation.parse(req.body);
|
|
||||||
await this.userService.setPasswordByUserId(validatedParams.userId, validatedBody.password);
|
|
||||||
return res.status(200).json({ success: true, msg: "Password reset successfully" });
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default AuthController;
|
export default AuthController;
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, RequestHandler } from "express";
|
||||||
|
import { catchAsync } from "@/utils/catchAsync.js";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
getChecksParamValidation,
|
getChecksParamValidation,
|
||||||
@@ -14,11 +15,11 @@ const SERVICE_NAME = "checkController";
|
|||||||
|
|
||||||
export interface ICheckController {
|
export interface ICheckController {
|
||||||
serviceName: string;
|
serviceName: string;
|
||||||
getChecksByMonitor: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getChecksByMonitor: RequestHandler;
|
||||||
getChecksByTeam: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getChecksByTeam: RequestHandler;
|
||||||
getChecksSummaryByTeamId: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getChecksSummaryByTeamId: RequestHandler;
|
||||||
deleteChecks: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
deleteChecks: RequestHandler;
|
||||||
deleteChecksByTeamId: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
deleteChecksByTeamId: RequestHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
class CheckController implements ICheckController {
|
class CheckController implements ICheckController {
|
||||||
@@ -33,109 +34,89 @@ class CheckController implements ICheckController {
|
|||||||
return CheckController.SERVICE_NAME;
|
return CheckController.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
getChecksByMonitor = async (req: Request, res: Response, next: NextFunction) => {
|
getChecksByMonitor = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const validatedParams = getChecksParamValidation.parse(req.params);
|
||||||
const validatedParams = getChecksParamValidation.parse(req.params);
|
const validatedQuery = getChecksQueryValidation.parse(req.query);
|
||||||
const validatedQuery = getChecksQueryValidation.parse(req.query);
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
const result = await this.checkService.getChecksByMonitor({
|
const result = await this.checkService.getChecksByMonitor({
|
||||||
monitorId: validatedParams.monitorId,
|
monitorId: validatedParams.monitorId,
|
||||||
teamId,
|
teamId,
|
||||||
sortOrder: validatedQuery.sortOrder,
|
sortOrder: validatedQuery.sortOrder,
|
||||||
dateRange: validatedQuery.dateRange,
|
dateRange: validatedQuery.dateRange,
|
||||||
filter: validatedQuery.filter,
|
filter: validatedQuery.filter,
|
||||||
page: validatedQuery.page,
|
page: validatedQuery.page,
|
||||||
rowsPerPage: validatedQuery.rowsPerPage,
|
rowsPerPage: validatedQuery.rowsPerPage,
|
||||||
status: validatedQuery.status,
|
status: validatedQuery.status,
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Checks retrieved successfully",
|
msg: "Checks retrieved successfully",
|
||||||
data: result,
|
data: result,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getChecksByTeam = async (req: Request, res: Response, next: NextFunction) => {
|
getChecksByTeam = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const validatedQuery = getTeamChecksQueryValidation.parse(req.query);
|
||||||
const validatedQuery = getTeamChecksQueryValidation.parse(req.query);
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
|
|
||||||
const checkData = await this.checkService.getChecksByTeam({
|
const checkData = await this.checkService.getChecksByTeam({
|
||||||
teamId,
|
teamId,
|
||||||
sortOrder: validatedQuery.sortOrder,
|
sortOrder: validatedQuery.sortOrder,
|
||||||
dateRange: validatedQuery.dateRange,
|
dateRange: validatedQuery.dateRange,
|
||||||
page: validatedQuery.page,
|
page: validatedQuery.page,
|
||||||
rowsPerPage: validatedQuery.rowsPerPage,
|
rowsPerPage: validatedQuery.rowsPerPage,
|
||||||
filter: validatedQuery.filter,
|
filter: validatedQuery.filter,
|
||||||
});
|
});
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Team checks retrieved successfully",
|
msg: "Team checks retrieved successfully",
|
||||||
data: checkData,
|
data: checkData,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getChecksSummaryByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
getChecksSummaryByTeamId = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const validatedQuery = getChecksSummaryByTeamIdQueryValidation.parse(req.query);
|
||||||
const validatedQuery = getChecksSummaryByTeamIdQueryValidation.parse(req.query);
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
const dateRange = validatedQuery.dateRange ?? "hour";
|
||||||
const dateRange = validatedQuery.dateRange ?? "hour";
|
|
||||||
|
|
||||||
const summary = await this.checkService.getChecksSummaryByTeamId({ teamId, dateRange });
|
const summary = await this.checkService.getChecksSummaryByTeamId({ teamId, dateRange });
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Checks summary retrieved successfully",
|
msg: "Checks summary retrieved successfully",
|
||||||
data: summary,
|
data: summary,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
deleteChecks = async (req: Request, res: Response, next: NextFunction) => {
|
deleteChecks = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const validatedParams = deleteChecksParamValidation.parse(req.params);
|
||||||
const validatedParams = deleteChecksParamValidation.parse(req.params);
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
|
|
||||||
const deletedCount = await this.checkService.deleteChecks({
|
const deletedCount = await this.checkService.deleteChecks({
|
||||||
monitorId: validatedParams.monitorId,
|
monitorId: validatedParams.monitorId,
|
||||||
teamId,
|
teamId,
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Checks deleted successfully",
|
msg: "Checks deleted successfully",
|
||||||
data: { deletedCount },
|
data: { deletedCount },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
deleteChecksByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
deleteChecksByTeamId = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
|
|
||||||
const deletedCount = await this.checkService.deleteChecksByTeamId({ teamId });
|
const deletedCount = await this.checkService.deleteChecksByTeamId({ teamId });
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Checks deleted successfully",
|
msg: "Checks deleted successfully",
|
||||||
data: { deletedCount },
|
data: { deletedCount },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default CheckController;
|
export default CheckController;
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
import { IDiagnosticService } from "@/domain/diagnostics/diagnostic.service.js";
|
import { IDiagnosticService } from "@/domain/diagnostics/diagnostic.service.js";
|
||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, RequestHandler } from "express";
|
||||||
|
import { catchAsync } from "@/utils/catchAsync.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "diagnosticController";
|
const SERVICE_NAME = "diagnosticController";
|
||||||
|
|
||||||
export interface IDiagnosticController {
|
export interface IDiagnosticController {
|
||||||
getSystemStats: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getSystemStats: RequestHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
class DiagnosticController implements IDiagnosticController {
|
class DiagnosticController implements IDiagnosticController {
|
||||||
@@ -20,18 +21,14 @@ class DiagnosticController implements IDiagnosticController {
|
|||||||
return DiagnosticController.SERVICE_NAME;
|
return DiagnosticController.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
getSystemStats = async (req: Request, res: Response, next: NextFunction) => {
|
getSystemStats = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const diagnostics = await this.diagnosticService.getSystemStats();
|
||||||
const diagnostics = await this.diagnosticService.getSystemStats();
|
return res.status(200).json({
|
||||||
return res.status(200).json({
|
success: true,
|
||||||
success: true,
|
msg: "OK",
|
||||||
msg: "OK",
|
data: diagnostics,
|
||||||
data: diagnostics,
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default DiagnosticController;
|
export default DiagnosticController;
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, RequestHandler } from "express";
|
||||||
|
import { catchAsync } from "@/utils/catchAsync.js";
|
||||||
import { getChecksParamValidation, getChecksQueryValidation } from "@/api/validation/checkValidation.js";
|
import { getChecksParamValidation, getChecksQueryValidation } from "@/api/validation/checkValidation.js";
|
||||||
import type { IGeoChecksService } from "@/domain/geo-checks/geo-check.service.js";
|
import type { IGeoChecksService } from "@/domain/geo-checks/geo-check.service.js";
|
||||||
import { requireTeamId } from "./controllerUtils.js";
|
import { requireTeamId } from "./controllerUtils.js";
|
||||||
@@ -6,7 +7,7 @@ import { requireTeamId } from "./controllerUtils.js";
|
|||||||
const SERVICE_NAME = "geoCheckController";
|
const SERVICE_NAME = "geoCheckController";
|
||||||
|
|
||||||
export interface IGeoCheckController {
|
export interface IGeoCheckController {
|
||||||
getGeoChecksByMonitor: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getGeoChecksByMonitor: RequestHandler;
|
||||||
}
|
}
|
||||||
class GeoCheckController implements IGeoCheckController {
|
class GeoCheckController implements IGeoCheckController {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
@@ -20,32 +21,28 @@ class GeoCheckController implements IGeoCheckController {
|
|||||||
return GeoCheckController.SERVICE_NAME;
|
return GeoCheckController.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
getGeoChecksByMonitor = async (req: Request, res: Response, next: NextFunction) => {
|
getGeoChecksByMonitor = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const validatedParams = getChecksParamValidation.parse(req.params);
|
||||||
const validatedParams = getChecksParamValidation.parse(req.params);
|
const validatedQuery = getChecksQueryValidation.parse(req.query);
|
||||||
const validatedQuery = getChecksQueryValidation.parse(req.query);
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
const result = await this.geoChecksService.getGeoChecksByMonitor({
|
const result = await this.geoChecksService.getGeoChecksByMonitor({
|
||||||
monitorId: validatedParams.monitorId,
|
monitorId: validatedParams.monitorId,
|
||||||
teamId: teamId,
|
teamId: teamId,
|
||||||
sortOrder: validatedQuery.sortOrder,
|
sortOrder: validatedQuery.sortOrder,
|
||||||
dateRange: validatedQuery.dateRange,
|
dateRange: validatedQuery.dateRange,
|
||||||
page: validatedQuery.page,
|
page: validatedQuery.page,
|
||||||
rowsPerPage: validatedQuery.rowsPerPage,
|
rowsPerPage: validatedQuery.rowsPerPage,
|
||||||
continent: validatedQuery.continent,
|
continent: validatedQuery.continent,
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Geo checks retrieved successfully",
|
msg: "Geo checks retrieved successfully",
|
||||||
data: result,
|
data: result,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default GeoCheckController;
|
export default GeoCheckController;
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, RequestHandler } from "express";
|
||||||
|
import { catchAsync } from "@/utils/catchAsync.js";
|
||||||
import { requireTeamId, requireUserId, requireUserEmail, extractString } from "./controllerUtils.js";
|
import { requireTeamId, requireUserId, requireUserEmail, extractString } from "./controllerUtils.js";
|
||||||
import { IIncidentService } from "@/domain/incidents/incident.service.js";
|
import { IIncidentService } from "@/domain/incidents/incident.service.js";
|
||||||
import { getIncidentsByTeamQueryValidation, getIncidentSummaryQueryValidation } from "@/api/validation/incidentValidation.js";
|
import { getIncidentsByTeamQueryValidation, getIncidentSummaryQueryValidation } from "@/api/validation/incidentValidation.js";
|
||||||
@@ -7,10 +8,10 @@ import { getIncidentsByTeamQueryValidation, getIncidentSummaryQueryValidation }
|
|||||||
const SERVICE_NAME = "IncidentController";
|
const SERVICE_NAME = "IncidentController";
|
||||||
|
|
||||||
export interface IIncidentController {
|
export interface IIncidentController {
|
||||||
getIncidentsByTeam: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getIncidentsByTeam: RequestHandler;
|
||||||
getIncidentSummary: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getIncidentSummary: RequestHandler;
|
||||||
getIncidentById: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getIncidentById: RequestHandler;
|
||||||
resolveIncidentManually: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
resolveIncidentManually: RequestHandler;
|
||||||
}
|
}
|
||||||
class IncidentController implements IIncidentController {
|
class IncidentController implements IIncidentController {
|
||||||
private incidentService: IIncidentService;
|
private incidentService: IIncidentService;
|
||||||
@@ -18,90 +19,74 @@ class IncidentController implements IIncidentController {
|
|||||||
this.incidentService = incidentService;
|
this.incidentService = incidentService;
|
||||||
}
|
}
|
||||||
|
|
||||||
getIncidentsByTeam = async (req: Request, res: Response, next: NextFunction) => {
|
getIncidentsByTeam = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const validatedQuery = getIncidentsByTeamQueryValidation.parse(req.query);
|
||||||
const validatedQuery = getIncidentsByTeamQueryValidation.parse(req.query);
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
const result = await this.incidentService.getIncidentsByTeam(
|
const result = await this.incidentService.getIncidentsByTeam(
|
||||||
teamId,
|
teamId,
|
||||||
validatedQuery.sortOrder,
|
validatedQuery.sortOrder,
|
||||||
validatedQuery.dateRange,
|
validatedQuery.dateRange,
|
||||||
validatedQuery.page,
|
validatedQuery.page,
|
||||||
validatedQuery.rowsPerPage,
|
validatedQuery.rowsPerPage,
|
||||||
validatedQuery.status,
|
validatedQuery.status,
|
||||||
validatedQuery.monitorId,
|
validatedQuery.monitorId,
|
||||||
validatedQuery.resolutionType
|
validatedQuery.resolutionType
|
||||||
);
|
);
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Incidents retrieved successfully",
|
msg: "Incidents retrieved successfully",
|
||||||
data: result,
|
data: result,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
|
getIncidentSummary = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const validatedQuery = getIncidentSummaryQueryValidation.parse(req.query);
|
||||||
|
|
||||||
|
const summary = await this.incidentService.getIncidentSummary(teamId, validatedQuery.limit);
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Incident summary retrieved successfully",
|
||||||
|
data: summary,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
getIncidentById = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const incidentId = req.params.incidentId as string;
|
||||||
|
if (!incidentId) {
|
||||||
|
throw new AppError({ message: "Incident ID is required", service: SERVICE_NAME, status: 400 });
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
getIncidentSummary = async (req: Request, res: Response, next: NextFunction) => {
|
const incident = await this.incidentService.getIncidentById(incidentId, teamId);
|
||||||
try {
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const validatedQuery = getIncidentSummaryQueryValidation.parse(req.query);
|
|
||||||
|
|
||||||
const summary = await this.incidentService.getIncidentSummary(teamId, validatedQuery.limit);
|
return res.status(200).json({
|
||||||
return res.status(200).json({
|
success: true,
|
||||||
success: true,
|
msg: "Incident retrieved successfully",
|
||||||
msg: "Incident summary retrieved successfully",
|
data: incident,
|
||||||
data: summary,
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
resolveIncidentManually = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const userId = requireUserId(req.user?.id);
|
||||||
|
const userEmail = requireUserEmail(req.user?.email);
|
||||||
|
const incidentId = extractString(req.params?.incidentId);
|
||||||
|
if (!incidentId) {
|
||||||
|
throw new AppError({ message: "Incident ID is required", service: SERVICE_NAME, status: 400 });
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
getIncidentById = async (req: Request, res: Response, next: NextFunction) => {
|
const comment = extractString(req.body?.comment);
|
||||||
try {
|
const resolvedIncident = await this.incidentService.resolveIncident(incidentId, userId, teamId, comment, userEmail);
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const incidentId = req.params.incidentId as string;
|
|
||||||
if (!incidentId) {
|
|
||||||
throw new AppError({ message: "Incident ID is required", service: SERVICE_NAME, status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const incident = await this.incidentService.getIncidentById(incidentId, teamId);
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
return res.status(200).json({
|
msg: "Incident resolved successfully",
|
||||||
success: true,
|
data: resolvedIncident,
|
||||||
msg: "Incident retrieved successfully",
|
});
|
||||||
data: incident,
|
});
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
resolveIncidentManually = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const userId = requireUserId(req.user?.id);
|
|
||||||
const userEmail = requireUserEmail(req.user?.email);
|
|
||||||
const incidentId = extractString(req.params?.incidentId);
|
|
||||||
if (!incidentId) {
|
|
||||||
throw new AppError({ message: "Incident ID is required", service: SERVICE_NAME, status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const comment = extractString(req.body?.comment);
|
|
||||||
const resolvedIncident = await this.incidentService.resolveIncident(incidentId, userId, teamId, comment, userEmail);
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Incident resolved successfully",
|
|
||||||
data: resolvedIncident,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default IncidentController;
|
export default IncidentController;
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, RequestHandler } from "express";
|
||||||
|
import { catchAsync } from "@/utils/catchAsync.js";
|
||||||
import { inviteBodyValidation, inviteVerificationBodyValidation } from "@/api/validation/authValidation.js";
|
import { inviteBodyValidation, inviteVerificationBodyValidation } from "@/api/validation/authValidation.js";
|
||||||
import { requireFirstName, requireTeamId, requireUserRoles } from "@/api/controllers/controllerUtils.js";
|
import { requireFirstName, requireTeamId, requireUserRoles } from "@/api/controllers/controllerUtils.js";
|
||||||
import { IInviteService } from "@/domain/invites/invite.service.js";
|
import { IInviteService } from "@/domain/invites/invite.service.js";
|
||||||
const SERVICE_NAME = "inviteController";
|
const SERVICE_NAME = "inviteController";
|
||||||
|
|
||||||
export interface IInviteController {
|
export interface IInviteController {
|
||||||
getInviteToken: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getInviteToken: RequestHandler;
|
||||||
sendInviteEmail: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
sendInviteEmail: RequestHandler;
|
||||||
verifyInviteToken: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
verifyInviteToken: RequestHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
class InviteController implements IInviteController {
|
class InviteController implements IInviteController {
|
||||||
@@ -21,62 +22,50 @@ class InviteController implements IInviteController {
|
|||||||
return InviteController.SERVICE_NAME;
|
return InviteController.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
getInviteToken = async (req: Request, res: Response, next: NextFunction) => {
|
getInviteToken = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
const userRoles = requireUserRoles(req.user?.role);
|
||||||
const userRoles = requireUserRoles(req.user?.role);
|
const invite = req.body;
|
||||||
const invite = req.body;
|
invite.teamId = teamId;
|
||||||
invite.teamId = teamId;
|
inviteBodyValidation.parse(invite);
|
||||||
inviteBodyValidation.parse(invite);
|
const inviteToken = await this.inviteService.getInviteToken({ invite, teamId, userRoles });
|
||||||
const inviteToken = await this.inviteService.getInviteToken({ invite, teamId, userRoles });
|
return res.status(200).json({
|
||||||
return res.status(200).json({
|
success: true,
|
||||||
success: true,
|
msg: "Invite token generated successfully",
|
||||||
msg: "Invite token generated successfully",
|
data: inviteToken,
|
||||||
data: inviteToken,
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
sendInviteEmail = async (req: Request, res: Response, next: NextFunction) => {
|
sendInviteEmail = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
const userRoles = requireUserRoles(req.user?.role);
|
||||||
const userRoles = requireUserRoles(req.user?.role);
|
const firstName = requireFirstName(req.user?.firstName);
|
||||||
const firstName = requireFirstName(req.user?.firstName);
|
|
||||||
|
|
||||||
const inviteRequest = req.body;
|
const inviteRequest = req.body;
|
||||||
inviteRequest.teamId = teamId;
|
inviteRequest.teamId = teamId;
|
||||||
inviteBodyValidation.parse(inviteRequest);
|
inviteBodyValidation.parse(inviteRequest);
|
||||||
|
|
||||||
const inviteToken = await this.inviteService.sendInviteEmail({
|
const inviteToken = await this.inviteService.sendInviteEmail({
|
||||||
invite: inviteRequest,
|
invite: inviteRequest,
|
||||||
firstName,
|
firstName,
|
||||||
userRoles,
|
userRoles,
|
||||||
});
|
});
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Invite issued successfully",
|
msg: "Invite issued successfully",
|
||||||
data: inviteToken,
|
data: inviteToken,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
verifyInviteToken = async (req: Request, res: Response, next: NextFunction) => {
|
verifyInviteToken = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
inviteVerificationBodyValidation.parse(req.body);
|
||||||
inviteVerificationBodyValidation.parse(req.body);
|
const invite = await this.inviteService.verifyInviteToken({ inviteToken: req?.body?.token });
|
||||||
const invite = await this.inviteService.verifyInviteToken({ inviteToken: req?.body?.token });
|
return res.status(200).json({
|
||||||
return res.status(200).json({
|
success: true,
|
||||||
success: true,
|
msg: "Invite verified successfully",
|
||||||
msg: "Invite verified successfully",
|
data: invite,
|
||||||
data: invite,
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default InviteController;
|
export default InviteController;
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
import { ILogger } from "@/utils/logger.js";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, RequestHandler } from "express";
|
||||||
|
import { catchAsync } from "@/utils/catchAsync.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "LogController";
|
const SERVICE_NAME = "LogController";
|
||||||
|
|
||||||
export interface ILogController {
|
export interface ILogController {
|
||||||
getLogs: (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
getLogs: RequestHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
class LogController {
|
class LogController {
|
||||||
@@ -17,17 +18,13 @@ class LogController {
|
|||||||
return LogController.SERVICE_NAME;
|
return LogController.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
getLogs = async (req: Request, res: Response, next: NextFunction) => {
|
getLogs = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const logs = this.logger.getLogs();
|
||||||
const logs = this.logger.getLogs();
|
res.status(200).json({
|
||||||
res.status(200).json({
|
success: true,
|
||||||
success: true,
|
msg: "Logs fetched successfully",
|
||||||
msg: "Logs fetched successfully",
|
data: logs,
|
||||||
data: logs,
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
export default LogController;
|
export default LogController;
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, RequestHandler } from "express";
|
||||||
|
import { catchAsync } from "@/utils/catchAsync.js";
|
||||||
import {
|
import {
|
||||||
createMaintenanceWindowBodyValidation,
|
createMaintenanceWindowBodyValidation,
|
||||||
editMaintenanceWindowByIdParamValidation,
|
editMaintenanceWindowByIdParamValidation,
|
||||||
@@ -12,12 +13,12 @@ import { requireTeamId } from "@/api/controllers/controllerUtils.js";
|
|||||||
import { IMaintenanceWindowService } from "@/domain/maintenance-windows/maintenance-window.service.js";
|
import { IMaintenanceWindowService } from "@/domain/maintenance-windows/maintenance-window.service.js";
|
||||||
|
|
||||||
export interface IMaintenanceWindowController {
|
export interface IMaintenanceWindowController {
|
||||||
createMaintenanceWindows: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
createMaintenanceWindows: RequestHandler;
|
||||||
getMaintenanceWindowById: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getMaintenanceWindowById: RequestHandler;
|
||||||
getMaintenanceWindowsByTeamId: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getMaintenanceWindowsByTeamId: RequestHandler;
|
||||||
getMaintenanceWindowsByMonitorId: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getMaintenanceWindowsByMonitorId: RequestHandler;
|
||||||
deleteMaintenanceWindow: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
deleteMaintenanceWindow: RequestHandler;
|
||||||
editMaintenanceWindow: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
editMaintenanceWindow: RequestHandler;
|
||||||
}
|
}
|
||||||
class MaintenanceWindowController implements IMaintenanceWindowController {
|
class MaintenanceWindowController implements IMaintenanceWindowController {
|
||||||
private maintenanceWindowService: IMaintenanceWindowService;
|
private maintenanceWindowService: IMaintenanceWindowService;
|
||||||
@@ -25,131 +26,107 @@ class MaintenanceWindowController implements IMaintenanceWindowController {
|
|||||||
this.maintenanceWindowService = maintenanceWindowService;
|
this.maintenanceWindowService = maintenanceWindowService;
|
||||||
}
|
}
|
||||||
|
|
||||||
createMaintenanceWindows = async (req: Request, res: Response, next: NextFunction) => {
|
createMaintenanceWindows = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const validatedBody = createMaintenanceWindowBodyValidation.parse(req.body);
|
||||||
const validatedBody = createMaintenanceWindowBodyValidation.parse(req.body);
|
const teamId = requireTeamId(req?.user?.teamId);
|
||||||
const teamId = requireTeamId(req?.user?.teamId);
|
|
||||||
|
|
||||||
const monitorIDs = validatedBody.monitors;
|
const monitorIDs = validatedBody.monitors;
|
||||||
const name = validatedBody.name;
|
const name = validatedBody.name;
|
||||||
const active = validatedBody.active ?? true;
|
const active = validatedBody.active ?? true;
|
||||||
const duration = validatedBody.duration;
|
const duration = validatedBody.duration;
|
||||||
const durationUnit = validatedBody.durationUnit;
|
const durationUnit = validatedBody.durationUnit;
|
||||||
const repeat = validatedBody.repeat;
|
const repeat = validatedBody.repeat;
|
||||||
const start = validatedBody.start;
|
const start = validatedBody.start;
|
||||||
const end = validatedBody.end;
|
const end = validatedBody.end;
|
||||||
|
|
||||||
await this.maintenanceWindowService.createMaintenanceWindow({ teamId, monitorIDs, name, active, duration, durationUnit, repeat, start, end });
|
await this.maintenanceWindowService.createMaintenanceWindow({ teamId, monitorIDs, name, active, duration, durationUnit, repeat, start, end });
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Maintenance window created successfully",
|
msg: "Maintenance window created successfully",
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
getMaintenanceWindowById = catchAsync(async (req: Request, res: Response) => {
|
||||||
}
|
const validatedParams = getMaintenanceWindowByIdParamValidation.parse(req.params);
|
||||||
};
|
|
||||||
getMaintenanceWindowById = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const validatedParams = getMaintenanceWindowByIdParamValidation.parse(req.params);
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
const maintenanceWindow = await this.maintenanceWindowService.getMaintenanceWindowById({ id: validatedParams.id, teamId });
|
const maintenanceWindow = await this.maintenanceWindowService.getMaintenanceWindowById({ id: validatedParams.id, teamId });
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Maintenance window fetched successfully",
|
msg: "Maintenance window fetched successfully",
|
||||||
data: maintenanceWindow,
|
data: maintenanceWindow,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getMaintenanceWindowsByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
getMaintenanceWindowsByTeamId = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const validatedQuery = getMaintenanceWindowsByTeamIdQueryValidation.parse(req.query);
|
||||||
const validatedQuery = getMaintenanceWindowsByTeamIdQueryValidation.parse(req.query);
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req?.user?.teamId);
|
const teamId = requireTeamId(req?.user?.teamId);
|
||||||
|
|
||||||
const maintenanceWindows = await this.maintenanceWindowService.getMaintenanceWindowsByTeamId({
|
const maintenanceWindows = await this.maintenanceWindowService.getMaintenanceWindowsByTeamId({
|
||||||
teamId,
|
teamId,
|
||||||
active: validatedQuery.active,
|
active: validatedQuery.active,
|
||||||
page: validatedQuery.page,
|
page: validatedQuery.page,
|
||||||
rowsPerPage: validatedQuery.rowsPerPage,
|
rowsPerPage: validatedQuery.rowsPerPage,
|
||||||
field: validatedQuery.field,
|
field: validatedQuery.field,
|
||||||
order: validatedQuery.order,
|
order: validatedQuery.order,
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Maintenance windows fetched successfully",
|
msg: "Maintenance windows fetched successfully",
|
||||||
data: maintenanceWindows,
|
data: maintenanceWindows,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getMaintenanceWindowsByMonitorId = async (req: Request, res: Response, next: NextFunction) => {
|
getMaintenanceWindowsByMonitorId = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const validatedParams = getMaintenanceWindowsByMonitorIdParamValidation.parse(req.params);
|
||||||
const validatedParams = getMaintenanceWindowsByMonitorIdParamValidation.parse(req.params);
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req?.user?.teamId);
|
const teamId = requireTeamId(req?.user?.teamId);
|
||||||
|
|
||||||
const maintenanceWindows = await this.maintenanceWindowService.getMaintenanceWindowsByMonitorId({
|
const maintenanceWindows = await this.maintenanceWindowService.getMaintenanceWindowsByMonitorId({
|
||||||
monitorId: validatedParams.monitorId,
|
monitorId: validatedParams.monitorId,
|
||||||
teamId,
|
teamId,
|
||||||
});
|
});
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Maintenance windows fetched successfully",
|
msg: "Maintenance windows fetched successfully",
|
||||||
data: maintenanceWindows,
|
data: maintenanceWindows,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
deleteMaintenanceWindow = catchAsync(async (req: Request, res: Response) => {
|
||||||
}
|
const validatedParams = deleteMaintenanceWindowByIdParamValidation.parse(req.params);
|
||||||
};
|
|
||||||
deleteMaintenanceWindow = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const validatedParams = deleteMaintenanceWindowByIdParamValidation.parse(req.params);
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req?.user?.teamId);
|
const teamId = requireTeamId(req?.user?.teamId);
|
||||||
|
|
||||||
await this.maintenanceWindowService.deleteMaintenanceWindow({ id: validatedParams.id, teamId });
|
await this.maintenanceWindowService.deleteMaintenanceWindow({ id: validatedParams.id, teamId });
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Maintenance window deleted successfully",
|
msg: "Maintenance window deleted successfully",
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
editMaintenanceWindow = async (req: Request, res: Response, next: NextFunction) => {
|
editMaintenanceWindow = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const validatedParams = editMaintenanceWindowByIdParamValidation.parse(req.params);
|
||||||
const validatedParams = editMaintenanceWindowByIdParamValidation.parse(req.params);
|
const validatedBody = editMaintenanceByIdWindowBodyValidation.parse(req.body);
|
||||||
const validatedBody = editMaintenanceByIdWindowBodyValidation.parse(req.body);
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
const editedMaintenanceWindow = await this.maintenanceWindowService.editMaintenanceWindow({
|
const editedMaintenanceWindow = await this.maintenanceWindowService.editMaintenanceWindow({
|
||||||
id: validatedParams.id,
|
id: validatedParams.id,
|
||||||
body: validatedBody,
|
body: validatedBody,
|
||||||
teamId,
|
teamId,
|
||||||
});
|
});
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Maintenance window edited successfully",
|
msg: "Maintenance window edited successfully",
|
||||||
data: editedMaintenanceWindow,
|
data: editedMaintenanceWindow,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default MaintenanceWindowController;
|
export default MaintenanceWindowController;
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, RequestHandler } from "express";
|
||||||
|
import { catchAsync } from "@/utils/catchAsync.js";
|
||||||
import { updateNotificationsValidation } from "@/api/validation/notificationValidation.js";
|
import { updateNotificationsValidation } from "@/api/validation/notificationValidation.js";
|
||||||
import {
|
import {
|
||||||
getMonitorByIdParamValidation,
|
getMonitorByIdParamValidation,
|
||||||
@@ -25,26 +26,26 @@ import { INotificationsService } from "@/domain/notifications/notification.servi
|
|||||||
const SERVICE_NAME = "monitorController";
|
const SERVICE_NAME = "monitorController";
|
||||||
|
|
||||||
export interface IMonitorController {
|
export interface IMonitorController {
|
||||||
getMonitorCertificate: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getMonitorCertificate: RequestHandler;
|
||||||
getUptimeDetailsById: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getUptimeDetailsById: RequestHandler;
|
||||||
getHardwareDetailsById: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getHardwareDetailsById: RequestHandler;
|
||||||
getPageSpeedDetailsById: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getPageSpeedDetailsById: RequestHandler;
|
||||||
getGeoChecksByMonitorId: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getGeoChecksByMonitorId: RequestHandler;
|
||||||
getMonitorById: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getMonitorById: RequestHandler;
|
||||||
createMonitor: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
createMonitor: RequestHandler;
|
||||||
importMonitorsFromJSON: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
importMonitorsFromJSON: RequestHandler;
|
||||||
deleteMonitor: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
deleteMonitor: RequestHandler;
|
||||||
deleteAllMonitors: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
deleteAllMonitors: RequestHandler;
|
||||||
editMonitor: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
editMonitor: RequestHandler;
|
||||||
pauseMonitor: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
pauseMonitor: RequestHandler;
|
||||||
bulkPauseMonitors: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
bulkPauseMonitors: RequestHandler;
|
||||||
addDemoMonitors: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
addDemoMonitors: RequestHandler;
|
||||||
getMonitorsByTeamId: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getMonitorsByTeamId: RequestHandler;
|
||||||
getMonitorsWithChecksByTeamId: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getMonitorsWithChecksByTeamId: RequestHandler;
|
||||||
exportMonitorsToJSON: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
exportMonitorsToJSON: RequestHandler;
|
||||||
getAllGames: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getAllGames: RequestHandler;
|
||||||
getGroupsByTeamId: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getGroupsByTeamId: RequestHandler;
|
||||||
updateNotifications: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
updateNotifications: RequestHandler;
|
||||||
}
|
}
|
||||||
class MonitorController implements IMonitorController {
|
class MonitorController implements IMonitorController {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
@@ -61,430 +62,350 @@ class MonitorController implements IMonitorController {
|
|||||||
return MonitorController.SERVICE_NAME;
|
return MonitorController.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
getMonitorCertificate = async (req: Request, res: Response, next: NextFunction) => {
|
getMonitorCertificate = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const validatedParams = getCertificateParamValidation.parse(req.params);
|
||||||
const validatedParams = getCertificateParamValidation.parse(req.params);
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
const monitor = await this.monitorService.getMonitorById({ teamId, monitorId: validatedParams.monitorId });
|
||||||
const monitor = await this.monitorService.getMonitorById({ teamId, monitorId: validatedParams.monitorId });
|
const certificate = await fetchMonitorCertificate(sslChecker, monitor);
|
||||||
const certificate = await fetchMonitorCertificate(sslChecker, monitor);
|
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "SSL certificate retrieved successfully",
|
msg: "SSL certificate retrieved successfully",
|
||||||
data: {
|
data: {
|
||||||
certificateDate: new Date(certificate.validTo),
|
certificateDate: new Date(certificate.validTo),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
|
getUptimeDetailsById = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedParams = getUptimeDetailsByIdParamValidation.parse(req.params);
|
||||||
|
const validatedQuery = getUptimeDetailsByIdQueryValidation.parse(req.query);
|
||||||
|
|
||||||
|
const monitorId = validatedParams.monitorId;
|
||||||
|
const dateRange = validatedQuery.dateRange;
|
||||||
|
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
|
const data = await this.monitorService.getUptimeDetailsById({
|
||||||
|
teamId,
|
||||||
|
monitorId,
|
||||||
|
dateRange,
|
||||||
|
});
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Uptime details retrieved successfully",
|
||||||
|
data: data,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
getHardwareDetailsById = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedParams = getHardwareDetailsByIdParamValidation.parse(req.params);
|
||||||
|
const validatedQuery = getHardwareDetailsByIdQueryValidation.parse(req.query);
|
||||||
|
|
||||||
|
const monitorId = validatedParams.monitorId;
|
||||||
|
const dateRange = validatedQuery.dateRange || "recent";
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
|
const data = await this.monitorService.getHardwareDetailsById({
|
||||||
|
teamId,
|
||||||
|
monitorId,
|
||||||
|
dateRange,
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Hardware details retrieved successfully",
|
||||||
|
data: data,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
getPageSpeedDetailsById = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedParams = getHardwareDetailsByIdParamValidation.parse(req.params);
|
||||||
|
const validatedQuery = getHardwareDetailsByIdQueryValidation.parse(req.query);
|
||||||
|
|
||||||
|
const monitorId = validatedParams.monitorId;
|
||||||
|
const dateRange = validatedQuery.dateRange || "recent";
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
|
const data = await this.monitorService.getPageSpeedDetailsById({
|
||||||
|
teamId,
|
||||||
|
monitorId,
|
||||||
|
dateRange,
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Page speed details retrieved successfully",
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
getGeoChecksByMonitorId = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedParams = getMonitorByIdParamValidation.parse(req.params);
|
||||||
|
const validatedQuery = getMonitorByIdQueryValidation.parse(req.query);
|
||||||
|
|
||||||
|
const monitorId = validatedParams.monitorId;
|
||||||
|
const dateRange = validatedQuery.dateRange || "recent";
|
||||||
|
const continentParam = validatedQuery.continent;
|
||||||
|
const continents = continentParam ? (Array.isArray(continentParam) ? continentParam : [continentParam]) : undefined;
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
|
const data = await this.monitorService.getGeoChecksByMonitorId({
|
||||||
|
teamId,
|
||||||
|
monitorId,
|
||||||
|
dateRange,
|
||||||
|
continents,
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Geo checks retrieved successfully",
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
getMonitorById = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedParams = getMonitorByIdParamValidation.parse(req.params);
|
||||||
|
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const monitorId = validatedParams.monitorId;
|
||||||
|
|
||||||
|
const monitor = await this.monitorService.getMonitorById({ teamId, monitorId });
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Monitor retrieved successfully",
|
||||||
|
data: monitor,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
createMonitor = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedBody = createMonitorBodyValidation.parse(req.body);
|
||||||
|
|
||||||
|
const userId = requireUserId(req.user?.id);
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
|
const monitor = await this.monitorService.createMonitor(teamId, userId, validatedBody);
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Monitor created successfully",
|
||||||
|
data: monitor,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
importMonitorsFromJSON = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const userId = requireUserId(req.user?.id);
|
||||||
|
const validatedBody = importMonitorsBodyValidation.parse(req.body);
|
||||||
|
const monitors = validatedBody.monitors;
|
||||||
|
|
||||||
|
const result = await this.monitorService.importMonitorsFromJSON({ teamId, userId, monitors });
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: `Successfully imported ${result.imported} monitor(s)`,
|
||||||
|
data: result,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
deleteMonitor = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedParams = getMonitorByIdParamValidation.parse(req.params);
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const monitorId = validatedParams.monitorId;
|
||||||
|
|
||||||
|
const deletedMonitor = await this.monitorService.deleteMonitor({ teamId, monitorId });
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Monitor deleted successfully",
|
||||||
|
data: deletedMonitor,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
deleteAllMonitors = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const deletedCount = await this.monitorService.deleteAllMonitors({ teamId });
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: `Deleted ${deletedCount} monitors`,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
editMonitor = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedParams = getMonitorByIdParamValidation.parse(req.params);
|
||||||
|
const validatedBody = editMonitorBodyValidation.parse(req.body);
|
||||||
|
const monitorId = validatedParams.monitorId;
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
|
const editedMonitor = await this.monitorService.editMonitor({ teamId, monitorId, body: validatedBody });
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Monitor edited successfully",
|
||||||
|
data: editedMonitor,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
pauseMonitor = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedParams = pauseMonitorParamValidation.parse(req.params);
|
||||||
|
|
||||||
|
const monitorId = validatedParams.monitorId;
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
|
const monitor = await this.monitorService.pauseMonitor({ teamId, monitorId });
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: monitor.isActive ? "Monitor resumed successfully" : "Monitor paused successfully",
|
||||||
|
data: monitor,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
bulkPauseMonitors = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedBody = bulkPauseMonitorBodyValidation.parse(req.body);
|
||||||
|
|
||||||
|
const { monitorIds, pause } = validatedBody;
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
|
const { monitors, failedCount } = await this.monitorService.bulkPauseMonitors({ teamId, monitorIds, pause });
|
||||||
|
|
||||||
|
const action = pause ? "paused" : "resumed";
|
||||||
|
const monitorStr = monitors.length === 1 ? "monitor" : "monitors";
|
||||||
|
|
||||||
|
let msg = `${monitors.length} ${monitorStr} ${action} successfully`;
|
||||||
|
if (failedCount > 0) {
|
||||||
|
msg = `${monitors.length} ${monitorStr} ${action} in database, but ${failedCount} failed to sync with the job queue. Please check logs.`;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
getUptimeDetailsById = async (req: Request, res: Response, next: NextFunction) => {
|
return res.status(200).json({
|
||||||
try {
|
success: true,
|
||||||
const validatedParams = getUptimeDetailsByIdParamValidation.parse(req.params);
|
msg,
|
||||||
const validatedQuery = getUptimeDetailsByIdQueryValidation.parse(req.query);
|
data: { monitors, failedCount },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
const monitorId = validatedParams.monitorId;
|
addDemoMonitors = catchAsync(async (req: Request, res: Response) => {
|
||||||
const dateRange = validatedQuery.dateRange;
|
const id = requireUserId(req.user?.id);
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const demoMonitors = await this.monitorService.addDemoMonitors({ userId: id, teamId });
|
||||||
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Demo monitors added successfully",
|
||||||
|
data: demoMonitors?.length ?? 0,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
const data = await this.monitorService.getUptimeDetailsById({
|
getMonitorsByTeamId = catchAsync(async (req: Request, res: Response) => {
|
||||||
teamId,
|
const validatedQuery = getMonitorsByTeamIdQueryValidation.parse(req.query);
|
||||||
monitorId,
|
|
||||||
dateRange,
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const type = validatedQuery.type;
|
||||||
|
const tags = validatedQuery.tags;
|
||||||
|
const filter = validatedQuery.filter;
|
||||||
|
|
||||||
|
const monitors = await this.monitorService.getMonitorsByTeamId({ teamId, type, tags, filter });
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Monitors retrieved successfully",
|
||||||
|
data: monitors,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
getMonitorsWithChecksByTeamId = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedQuery = getMonitorsWithChecksQueryValidation.parse(req.query);
|
||||||
|
|
||||||
|
const limit = validatedQuery.limit;
|
||||||
|
const page = validatedQuery.page;
|
||||||
|
const rowsPerPage = validatedQuery.rowsPerPage;
|
||||||
|
const filter = validatedQuery.filter;
|
||||||
|
const field = validatedQuery.field;
|
||||||
|
const order = validatedQuery.order;
|
||||||
|
const type = validatedQuery.type;
|
||||||
|
const tags = validatedQuery.tags;
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
|
const monitors = await this.monitorService.getMonitorsWithChecksByTeamId({
|
||||||
|
teamId,
|
||||||
|
limit,
|
||||||
|
type,
|
||||||
|
tags,
|
||||||
|
page,
|
||||||
|
rowsPerPage,
|
||||||
|
filter,
|
||||||
|
field,
|
||||||
|
order,
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
msg: "Monitors retrieved successfully",
|
||||||
|
data: monitors,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
exportMonitorsToJSON = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const json = await this.monitorService.exportMonitorsToJSON({ teamId });
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Monitors exported successfully",
|
||||||
|
data: json,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
getAllGames = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const games = this.monitorService.getAllGames();
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Supported games retrieved successfully",
|
||||||
|
data: games,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
getGroupsByTeamId = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const groups = await this.monitorService.getGroupsByTeamId({ teamId });
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
msg: "Groups retrieved successfully",
|
||||||
|
data: groups,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
updateNotifications = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
updateNotificationsValidation.parse(req.body);
|
||||||
|
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const { monitorIds, notificationIds, action } = req.body;
|
||||||
|
|
||||||
|
// Verify all requested notification IDs actually belong to this team
|
||||||
|
const teamNotifications = await this.notificationsService.findNotificationsByTeamId(teamId);
|
||||||
|
const validNotificationIds = teamNotifications.map((n) => n.id);
|
||||||
|
|
||||||
|
const invalidIds = notificationIds.filter((id: string) => !validNotificationIds.includes(id));
|
||||||
|
if (invalidIds.length > 0) {
|
||||||
|
throw new AppError({
|
||||||
|
message: `The following notification IDs are invalid or do not belong to your team: ${invalidIds.join(", ")}`,
|
||||||
|
status: 403,
|
||||||
});
|
});
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Uptime details retrieved successfully",
|
|
||||||
data: data,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
getHardwareDetailsById = async (req: Request, res: Response, next: NextFunction) => {
|
const modifiedCount = await this.monitorService.updateNotifications({
|
||||||
try {
|
teamId,
|
||||||
const validatedParams = getHardwareDetailsByIdParamValidation.parse(req.params);
|
monitorIds,
|
||||||
const validatedQuery = getHardwareDetailsByIdQueryValidation.parse(req.query);
|
notificationIds,
|
||||||
|
action,
|
||||||
|
});
|
||||||
|
|
||||||
const monitorId = validatedParams.monitorId;
|
return res.status(200).json({
|
||||||
const dateRange = validatedQuery.dateRange || "recent";
|
success: true,
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
msg: `Notifications updated successfully on ${modifiedCount} monitor(s)`,
|
||||||
|
data: { modifiedCount },
|
||||||
const data = await this.monitorService.getHardwareDetailsById({
|
});
|
||||||
teamId,
|
});
|
||||||
monitorId,
|
|
||||||
dateRange,
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Hardware details retrieved successfully",
|
|
||||||
data: data,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
getPageSpeedDetailsById = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const validatedParams = getHardwareDetailsByIdParamValidation.parse(req.params);
|
|
||||||
const validatedQuery = getHardwareDetailsByIdQueryValidation.parse(req.query);
|
|
||||||
|
|
||||||
const monitorId = validatedParams.monitorId;
|
|
||||||
const dateRange = validatedQuery.dateRange || "recent";
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
|
|
||||||
const data = await this.monitorService.getPageSpeedDetailsById({
|
|
||||||
teamId,
|
|
||||||
monitorId,
|
|
||||||
dateRange,
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Page speed details retrieved successfully",
|
|
||||||
data,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getGeoChecksByMonitorId = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const validatedParams = getMonitorByIdParamValidation.parse(req.params);
|
|
||||||
const validatedQuery = getMonitorByIdQueryValidation.parse(req.query);
|
|
||||||
|
|
||||||
const monitorId = validatedParams.monitorId;
|
|
||||||
const dateRange = validatedQuery.dateRange || "recent";
|
|
||||||
const continentParam = validatedQuery.continent;
|
|
||||||
const continents = continentParam ? (Array.isArray(continentParam) ? continentParam : [continentParam]) : undefined;
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
|
|
||||||
const data = await this.monitorService.getGeoChecksByMonitorId({
|
|
||||||
teamId,
|
|
||||||
monitorId,
|
|
||||||
dateRange,
|
|
||||||
continents,
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Geo checks retrieved successfully",
|
|
||||||
data,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getMonitorById = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const validatedParams = getMonitorByIdParamValidation.parse(req.params);
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const monitorId = validatedParams.monitorId;
|
|
||||||
|
|
||||||
const monitor = await this.monitorService.getMonitorById({ teamId, monitorId });
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Monitor retrieved successfully",
|
|
||||||
data: monitor,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
createMonitor = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const validatedBody = createMonitorBodyValidation.parse(req.body);
|
|
||||||
|
|
||||||
const userId = requireUserId(req.user?.id);
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
|
|
||||||
const monitor = await this.monitorService.createMonitor(teamId, userId, validatedBody);
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Monitor created successfully",
|
|
||||||
data: monitor,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
importMonitorsFromJSON = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const userId = requireUserId(req.user?.id);
|
|
||||||
const validatedBody = importMonitorsBodyValidation.parse(req.body);
|
|
||||||
const monitors = validatedBody.monitors;
|
|
||||||
|
|
||||||
const result = await this.monitorService.importMonitorsFromJSON({ teamId, userId, monitors });
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: `Successfully imported ${result.imported} monitor(s)`,
|
|
||||||
data: result,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
deleteMonitor = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const validatedParams = getMonitorByIdParamValidation.parse(req.params);
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const monitorId = validatedParams.monitorId;
|
|
||||||
|
|
||||||
const deletedMonitor = await this.monitorService.deleteMonitor({ teamId, monitorId });
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Monitor deleted successfully",
|
|
||||||
data: deletedMonitor,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
deleteAllMonitors = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const deletedCount = await this.monitorService.deleteAllMonitors({ teamId });
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: `Deleted ${deletedCount} monitors`,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
editMonitor = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const validatedParams = getMonitorByIdParamValidation.parse(req.params);
|
|
||||||
const validatedBody = editMonitorBodyValidation.parse(req.body);
|
|
||||||
const monitorId = validatedParams.monitorId;
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
|
|
||||||
const editedMonitor = await this.monitorService.editMonitor({ teamId, monitorId, body: validatedBody });
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Monitor edited successfully",
|
|
||||||
data: editedMonitor,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
pauseMonitor = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const validatedParams = pauseMonitorParamValidation.parse(req.params);
|
|
||||||
|
|
||||||
const monitorId = validatedParams.monitorId;
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
|
|
||||||
const monitor = await this.monitorService.pauseMonitor({ teamId, monitorId });
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: monitor.isActive ? "Monitor resumed successfully" : "Monitor paused successfully",
|
|
||||||
data: monitor,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
bulkPauseMonitors = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const validatedBody = bulkPauseMonitorBodyValidation.parse(req.body);
|
|
||||||
|
|
||||||
const { monitorIds, pause } = validatedBody;
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
|
|
||||||
const { monitors, failedCount } = await this.monitorService.bulkPauseMonitors({ teamId, monitorIds, pause });
|
|
||||||
|
|
||||||
const action = pause ? "paused" : "resumed";
|
|
||||||
const monitorStr = monitors.length === 1 ? "monitor" : "monitors";
|
|
||||||
|
|
||||||
let msg = `${monitors.length} ${monitorStr} ${action} successfully`;
|
|
||||||
if (failedCount > 0) {
|
|
||||||
msg = `${monitors.length} ${monitorStr} ${action} in database, but ${failedCount} failed to sync with the job queue. Please check logs.`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg,
|
|
||||||
data: { monitors, failedCount },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
addDemoMonitors = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const id = requireUserId(req.user?.id);
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const demoMonitors = await this.monitorService.addDemoMonitors({ userId: id, teamId });
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Demo monitors added successfully",
|
|
||||||
data: demoMonitors?.length ?? 0,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getMonitorsByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const validatedQuery = getMonitorsByTeamIdQueryValidation.parse(req.query);
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const type = validatedQuery.type;
|
|
||||||
const tags = validatedQuery.tags;
|
|
||||||
const filter = validatedQuery.filter;
|
|
||||||
|
|
||||||
const monitors = await this.monitorService.getMonitorsByTeamId({ teamId, type, tags, filter });
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Monitors retrieved successfully",
|
|
||||||
data: monitors,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getMonitorsWithChecksByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const validatedQuery = getMonitorsWithChecksQueryValidation.parse(req.query);
|
|
||||||
|
|
||||||
const limit = validatedQuery.limit;
|
|
||||||
const page = validatedQuery.page;
|
|
||||||
const rowsPerPage = validatedQuery.rowsPerPage;
|
|
||||||
const filter = validatedQuery.filter;
|
|
||||||
const field = validatedQuery.field;
|
|
||||||
const order = validatedQuery.order;
|
|
||||||
const type = validatedQuery.type;
|
|
||||||
const tags = validatedQuery.tags;
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
|
|
||||||
const monitors = await this.monitorService.getMonitorsWithChecksByTeamId({
|
|
||||||
teamId,
|
|
||||||
limit,
|
|
||||||
type,
|
|
||||||
tags,
|
|
||||||
page,
|
|
||||||
rowsPerPage,
|
|
||||||
filter,
|
|
||||||
field,
|
|
||||||
order,
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
msg: "Monitors retrieved successfully",
|
|
||||||
data: monitors,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
exportMonitorsToJSON = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const json = await this.monitorService.exportMonitorsToJSON({ teamId });
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Monitors exported successfully",
|
|
||||||
data: json,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getAllGames = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const games = this.monitorService.getAllGames();
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Supported games retrieved successfully",
|
|
||||||
data: games,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getGroupsByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const groups = await this.monitorService.getGroupsByTeamId({ teamId });
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
msg: "Groups retrieved successfully",
|
|
||||||
data: groups,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
updateNotifications = async (req: Request, res: Response, next: NextFunction) => {
|
|
||||||
try {
|
|
||||||
updateNotificationsValidation.parse(req.body);
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const { monitorIds, notificationIds, action } = req.body;
|
|
||||||
|
|
||||||
// Verify all requested notification IDs actually belong to this team
|
|
||||||
const teamNotifications = await this.notificationsService.findNotificationsByTeamId(teamId);
|
|
||||||
const validNotificationIds = teamNotifications.map((n) => n.id);
|
|
||||||
|
|
||||||
const invalidIds = notificationIds.filter((id: string) => !validNotificationIds.includes(id));
|
|
||||||
if (invalidIds.length > 0) {
|
|
||||||
throw new AppError({
|
|
||||||
message: `The following notification IDs are invalid or do not belong to your team: ${invalidIds.join(", ")}`,
|
|
||||||
status: 403,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const modifiedCount = await this.monitorService.updateNotifications({
|
|
||||||
teamId,
|
|
||||||
monitorIds,
|
|
||||||
notificationIds,
|
|
||||||
action,
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: `Notifications updated successfully on ${modifiedCount} monitor(s)`,
|
|
||||||
data: { modifiedCount },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default MonitorController;
|
export default MonitorController;
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, RequestHandler } from "express";
|
||||||
|
import { catchAsync } from "@/utils/catchAsync.js";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
createNotificationBodyValidation,
|
createNotificationBodyValidation,
|
||||||
@@ -16,13 +17,13 @@ import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interf
|
|||||||
const SERVICE_NAME = "NotificationController";
|
const SERVICE_NAME = "NotificationController";
|
||||||
|
|
||||||
export interface INotificationController {
|
export interface INotificationController {
|
||||||
testNotification: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
testNotification: RequestHandler;
|
||||||
createNotification: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
createNotification: RequestHandler;
|
||||||
getNotificationsByTeamId: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getNotificationsByTeamId: RequestHandler;
|
||||||
deleteNotification: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
deleteNotification: RequestHandler;
|
||||||
getNotificationById: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getNotificationById: RequestHandler;
|
||||||
editNotification: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
editNotification: RequestHandler;
|
||||||
testAllNotifications: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
testAllNotifications: RequestHandler;
|
||||||
}
|
}
|
||||||
class NotificationController implements INotificationController {
|
class NotificationController implements INotificationController {
|
||||||
private notificationsService: INotificationsService;
|
private notificationsService: INotificationsService;
|
||||||
@@ -32,132 +33,104 @@ class NotificationController implements INotificationController {
|
|||||||
this.monitorsRepository = monitorsRepository;
|
this.monitorsRepository = monitorsRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
testNotification = async (req: Request, res: Response, next: NextFunction) => {
|
testNotification = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const notification = testNotificationBodyValidation.parse(req.body);
|
||||||
const notification = testNotificationBodyValidation.parse(req.body);
|
const success = await this.notificationsService.sendTestNotification(notification);
|
||||||
const success = await this.notificationsService.sendTestNotification(notification);
|
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success,
|
success,
|
||||||
msg: success ? "Notification sent successfully" : "Notification could not be sent — check the destination details.",
|
msg: success ? "Notification sent successfully" : "Notification could not be sent — check the destination details.",
|
||||||
details: { service: SERVICE_NAME },
|
details: { service: SERVICE_NAME },
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
|
createNotification = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedBody = createNotificationBodyValidation.parse(req.body);
|
||||||
|
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const userId = requireUserId(req.user?.id);
|
||||||
|
|
||||||
|
const notification = await this.notificationsService.createNotification(validatedBody, userId, teamId);
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Notification created successfully",
|
||||||
|
data: notification,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
getNotificationsByTeamId = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const notifications = await this.notificationsService.findNotificationsByTeamId(teamId);
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Notifications fetched successfully",
|
||||||
|
data: notifications,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
deleteNotification = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const validatedParams = deleteNotificationParamValidation.parse(req.params);
|
||||||
|
|
||||||
|
await this.notificationsService.deleteById(validatedParams.id, teamId);
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Notification deleted successfully",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
getNotificationById = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const validatedParams = getNotificationByIdParamValidation.parse(req.params);
|
||||||
|
|
||||||
|
const notification = await this.notificationsService.findById(validatedParams.id, teamId);
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Notification fetched successfully",
|
||||||
|
data: notification,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
editNotification = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedBody = createNotificationBodyValidation.parse(req.body);
|
||||||
|
const validatedParams = editNotificationParamValidation.parse(req.params);
|
||||||
|
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const notificationId = validatedParams.id;
|
||||||
|
|
||||||
|
const editedNotification = await this.notificationsService.updateById(notificationId, teamId, validatedBody);
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Notification updated successfully",
|
||||||
|
data: editedNotification,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
testAllNotifications = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedBody = testAllNotificationsBodyValidation.parse(req.body);
|
||||||
|
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
|
||||||
|
const monitor = await this.monitorsRepository.findById(validatedBody.monitorId, teamId);
|
||||||
|
const notifications = monitor.notifications || [];
|
||||||
|
|
||||||
|
if (notifications.length === 0) {
|
||||||
|
throw new AppError({ message: "No notifications", status: 400 });
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
createNotification = async (req: Request, res: Response, next: NextFunction) => {
|
const result = await this.notificationsService.testAllNotifications(notifications);
|
||||||
try {
|
|
||||||
const validatedBody = createNotificationBodyValidation.parse(req.body);
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
if (!result) {
|
||||||
const userId = requireUserId(req.user?.id);
|
throw new AppError({ message: "Failed to send all notifications", status: 500 });
|
||||||
|
|
||||||
const notification = await this.notificationsService.createNotification(validatedBody, userId, teamId);
|
|
||||||
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) => {
|
return res.status(200).json({
|
||||||
try {
|
success: true,
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
msg: "All notifications sent successfully",
|
||||||
const notifications = await this.notificationsService.findNotificationsByTeamId(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 = requireTeamId(req.user?.teamId);
|
|
||||||
const validatedParams = deleteNotificationParamValidation.parse(req.params);
|
|
||||||
|
|
||||||
await this.notificationsService.deleteById(validatedParams.id, teamId);
|
|
||||||
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 teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const validatedParams = getNotificationByIdParamValidation.parse(req.params);
|
|
||||||
|
|
||||||
const notification = await this.notificationsService.findById(validatedParams.id, teamId);
|
|
||||||
|
|
||||||
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 {
|
|
||||||
const validatedBody = createNotificationBodyValidation.parse(req.body);
|
|
||||||
const validatedParams = editNotificationParamValidation.parse(req.params);
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
const notificationId = validatedParams.id;
|
|
||||||
|
|
||||||
const editedNotification = await this.notificationsService.updateById(notificationId, teamId, validatedBody);
|
|
||||||
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 validatedBody = testAllNotificationsBodyValidation.parse(req.body);
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
|
|
||||||
const monitor = await this.monitorsRepository.findById(validatedBody.monitorId, teamId);
|
|
||||||
const notifications = monitor.notifications || [];
|
|
||||||
|
|
||||||
if (notifications.length === 0) {
|
|
||||||
throw new AppError({ message: "No notifications", status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await this.notificationsService.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;
|
export default NotificationController;
|
||||||
@@ -1,14 +1,15 @@
|
|||||||
import { IJobScheduler } from "@/worker/worker.interface.js";
|
import { IJobScheduler } from "@/worker/worker.interface.js";
|
||||||
import { getQueueJobsQueryValidation } from "@/api/validation/queueValidation.js";
|
import { getQueueJobsQueryValidation } from "@/api/validation/queueValidation.js";
|
||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, RequestHandler } from "express";
|
||||||
|
import { catchAsync } from "@/utils/catchAsync.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "JobQueueController";
|
const SERVICE_NAME = "JobQueueController";
|
||||||
|
|
||||||
export interface IJobQueueController {
|
export interface IJobQueueController {
|
||||||
getMetrics(req: Request, res: Response, next: NextFunction): Promise<void>;
|
getMetrics: RequestHandler;
|
||||||
getJobs(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
getJobs: RequestHandler;
|
||||||
getAllMetrics(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
getAllMetrics: RequestHandler;
|
||||||
flushQueue(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
flushQueue: RequestHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
class JobQueueController implements IJobQueueController {
|
class JobQueueController implements IJobQueueController {
|
||||||
@@ -19,59 +20,43 @@ class JobQueueController implements IJobQueueController {
|
|||||||
return JobQueueController.SERVICE_NAME;
|
return JobQueueController.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
getMetrics = async (req: Request, res: Response, next: NextFunction) => {
|
getMetrics = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const metrics = await this.scheduler.getMetrics();
|
||||||
const metrics = await this.scheduler.getMetrics();
|
res.status(200).json({
|
||||||
res.status(200).json({
|
success: true,
|
||||||
success: true,
|
msg: "Queue metrics fetched successfully",
|
||||||
msg: "Queue metrics fetched successfully",
|
data: metrics,
|
||||||
data: metrics,
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getJobs = async (req: Request, res: Response, next: NextFunction) => {
|
getJobs = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const pagination = getQueueJobsQueryValidation.parse(req.query);
|
||||||
const pagination = getQueueJobsQueryValidation.parse(req.query);
|
const { jobs, count } = await this.scheduler.getJobs(pagination);
|
||||||
const { jobs, count } = await this.scheduler.getJobs(pagination);
|
return res.status(200).json({
|
||||||
return res.status(200).json({
|
success: true,
|
||||||
success: true,
|
msg: "Queue jobs fetched successfully",
|
||||||
msg: "Queue jobs fetched successfully",
|
data: { jobs, count },
|
||||||
data: { jobs, count },
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getAllMetrics = async (req: Request, res: Response, next: NextFunction) => {
|
getAllMetrics = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const pagination = getQueueJobsQueryValidation.parse(req.query);
|
||||||
const pagination = getQueueJobsQueryValidation.parse(req.query);
|
const { jobs, count } = await this.scheduler.getJobs(pagination);
|
||||||
const { jobs, count } = await this.scheduler.getJobs(pagination);
|
const metrics = await this.scheduler.getMetrics();
|
||||||
const metrics = await this.scheduler.getMetrics();
|
return res.status(200).json({
|
||||||
return res.status(200).json({
|
success: true,
|
||||||
success: true,
|
msg: "Queue metrics fetched successfully",
|
||||||
msg: "Queue metrics fetched successfully",
|
data: { jobs, count, metrics },
|
||||||
data: { jobs, count, metrics },
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
flushQueue = async (req: Request, res: Response, next: NextFunction) => {
|
flushQueue = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const result = await this.scheduler.flushQueues();
|
||||||
const result = await this.scheduler.flushQueues();
|
return res.status(200).json({
|
||||||
return res.status(200).json({
|
success: true,
|
||||||
success: true,
|
msg: "Queue flushed successfully",
|
||||||
msg: "Queue flushed successfully",
|
data: result,
|
||||||
data: result,
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
export default JobQueueController;
|
export default JobQueueController;
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, RequestHandler } from "express";
|
||||||
|
import { catchAsync } from "@/utils/catchAsync.js";
|
||||||
import { updateAppSettingsBodyValidation } from "@/api/validation/settingsValidation.js";
|
import { updateAppSettingsBodyValidation } from "@/api/validation/settingsValidation.js";
|
||||||
import { sendTestEmailBodyValidation } from "@/api/validation/notificationValidation.js";
|
import { sendTestEmailBodyValidation } from "@/api/validation/notificationValidation.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
@@ -10,9 +11,9 @@ const SERVICE_NAME = "SettingsController";
|
|||||||
|
|
||||||
export interface ISettingsController {
|
export interface ISettingsController {
|
||||||
serviceName: string;
|
serviceName: string;
|
||||||
getAppSettings(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
getAppSettings: RequestHandler;
|
||||||
updateAppSettings(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
updateAppSettings: RequestHandler;
|
||||||
sendTestEmail(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
sendTestEmail: RequestHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
class SettingsController implements ISettingsController {
|
class SettingsController implements ISettingsController {
|
||||||
@@ -50,94 +51,82 @@ class SettingsController implements ISettingsController {
|
|||||||
return returnSettings;
|
return returnSettings;
|
||||||
};
|
};
|
||||||
|
|
||||||
getAppSettings = async (req: Request, res: Response, next: NextFunction) => {
|
getAppSettings = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const dbSettings = await this.settingsService.getDBSettings();
|
||||||
const dbSettings = await this.settingsService.getDBSettings();
|
|
||||||
|
|
||||||
const returnSettings = this.buildAppSettings(dbSettings);
|
const returnSettings = this.buildAppSettings(dbSettings);
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "App settings fetched successfully",
|
msg: "App settings fetched successfully",
|
||||||
data: returnSettings,
|
data: returnSettings,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
|
||||||
|
updateAppSettings = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const validatedBody = updateAppSettingsBodyValidation.parse(req.body);
|
||||||
|
|
||||||
|
const updatedSettings = await this.settingsService.updateDbSettings(validatedBody);
|
||||||
|
const returnSettings = this.buildAppSettings(updatedSettings);
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "App settings updated successfully",
|
||||||
|
data: returnSettings,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
sendTestEmail = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
sendTestEmailBodyValidation.parse(req.body);
|
||||||
|
|
||||||
|
const {
|
||||||
|
to,
|
||||||
|
systemEmailHost,
|
||||||
|
systemEmailPort,
|
||||||
|
systemEmailAddress,
|
||||||
|
systemEmailDisplayName,
|
||||||
|
systemEmailPassword,
|
||||||
|
systemEmailUser,
|
||||||
|
systemEmailConnectionHost,
|
||||||
|
systemEmailSecure,
|
||||||
|
systemEmailPool,
|
||||||
|
systemEmailIgnoreTLS,
|
||||||
|
systemEmailRequireTLS,
|
||||||
|
systemEmailRejectUnauthorized,
|
||||||
|
systemEmailTLSServername,
|
||||||
|
} = req.body;
|
||||||
|
|
||||||
|
const subject = "This is a test email from Checkmate";
|
||||||
|
const context = { testName: "Monitoring System" };
|
||||||
|
|
||||||
|
const html = await this.emailService.buildEmail("testEmailTemplate", context);
|
||||||
|
if (!html) {
|
||||||
|
throw new AppError({ message: "Failed to build email template.", status: 500 });
|
||||||
}
|
}
|
||||||
};
|
const messageId = await this.emailService.sendEmail(to, subject, html, {
|
||||||
|
systemEmailHost,
|
||||||
|
systemEmailPort,
|
||||||
|
systemEmailUser,
|
||||||
|
systemEmailAddress,
|
||||||
|
systemEmailDisplayName,
|
||||||
|
systemEmailPassword,
|
||||||
|
systemEmailConnectionHost,
|
||||||
|
systemEmailSecure,
|
||||||
|
systemEmailPool,
|
||||||
|
systemEmailIgnoreTLS,
|
||||||
|
systemEmailRequireTLS,
|
||||||
|
systemEmailRejectUnauthorized,
|
||||||
|
systemEmailTLSServername,
|
||||||
|
});
|
||||||
|
|
||||||
updateAppSettings = async (req: Request, res: Response, next: NextFunction) => {
|
if (!messageId) {
|
||||||
try {
|
throw new AppError({ message: "Failed to send test email.", status: 500 });
|
||||||
const validatedBody = updateAppSettingsBodyValidation.parse(req.body);
|
|
||||||
|
|
||||||
const updatedSettings = await this.settingsService.updateDbSettings(validatedBody);
|
|
||||||
const returnSettings = this.buildAppSettings(updatedSettings);
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "App settings updated successfully",
|
|
||||||
data: returnSettings,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
sendTestEmail = async (req: Request, res: Response, next: NextFunction) => {
|
return res.status(200).json({
|
||||||
try {
|
success: true,
|
||||||
sendTestEmailBodyValidation.parse(req.body);
|
msg: "Test email sent successfully",
|
||||||
|
data: { messageId },
|
||||||
const {
|
});
|
||||||
to,
|
});
|
||||||
systemEmailHost,
|
|
||||||
systemEmailPort,
|
|
||||||
systemEmailAddress,
|
|
||||||
systemEmailDisplayName,
|
|
||||||
systemEmailPassword,
|
|
||||||
systemEmailUser,
|
|
||||||
systemEmailConnectionHost,
|
|
||||||
systemEmailSecure,
|
|
||||||
systemEmailPool,
|
|
||||||
systemEmailIgnoreTLS,
|
|
||||||
systemEmailRequireTLS,
|
|
||||||
systemEmailRejectUnauthorized,
|
|
||||||
systemEmailTLSServername,
|
|
||||||
} = req.body;
|
|
||||||
|
|
||||||
const subject = "This is a test email from Checkmate";
|
|
||||||
const context = { testName: "Monitoring System" };
|
|
||||||
|
|
||||||
const html = await this.emailService.buildEmail("testEmailTemplate", context);
|
|
||||||
if (!html) {
|
|
||||||
throw new AppError({ message: "Failed to build email template.", status: 500 });
|
|
||||||
}
|
|
||||||
const messageId = await this.emailService.sendEmail(to, subject, html, {
|
|
||||||
systemEmailHost,
|
|
||||||
systemEmailPort,
|
|
||||||
systemEmailUser,
|
|
||||||
systemEmailAddress,
|
|
||||||
systemEmailDisplayName,
|
|
||||||
systemEmailPassword,
|
|
||||||
systemEmailConnectionHost,
|
|
||||||
systemEmailSecure,
|
|
||||||
systemEmailPool,
|
|
||||||
systemEmailIgnoreTLS,
|
|
||||||
systemEmailRequireTLS,
|
|
||||||
systemEmailRejectUnauthorized,
|
|
||||||
systemEmailTLSServername,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!messageId) {
|
|
||||||
throw new AppError({ message: "Failed to send test email.", status: 500 });
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Test email sent successfully",
|
|
||||||
data: { messageId },
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default SettingsController;
|
export default SettingsController;
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, RequestHandler } from "express";
|
||||||
|
import { catchAsync } from "@/utils/catchAsync.js";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
createStatusPageBodyValidation,
|
createStatusPageBodyValidation,
|
||||||
@@ -16,12 +17,12 @@ const SERVICE_NAME = "statusPageController";
|
|||||||
|
|
||||||
export interface IStatusPageController {
|
export interface IStatusPageController {
|
||||||
readonly serviceName: string;
|
readonly serviceName: string;
|
||||||
createStatusPage(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
createStatusPage: RequestHandler;
|
||||||
updateStatusPage(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
updateStatusPage: RequestHandler;
|
||||||
getStatusPageByUrl(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
getStatusPageByUrl: RequestHandler;
|
||||||
resolveStatusPageByDomain(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
resolveStatusPageByDomain: RequestHandler;
|
||||||
getStatusPagesByTeamId(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
getStatusPagesByTeamId: RequestHandler;
|
||||||
deleteStatusPage(req: Request, res: Response, next: NextFunction): Promise<Response | void>;
|
deleteStatusPage: RequestHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
class StatusPageController implements IStatusPageController {
|
class StatusPageController implements IStatusPageController {
|
||||||
@@ -35,132 +36,108 @@ class StatusPageController implements IStatusPageController {
|
|||||||
return StatusPageController.SERVICE_NAME;
|
return StatusPageController.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
createStatusPage = async (req: Request, res: Response, next: NextFunction) => {
|
createStatusPage = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
createStatusPageBodyValidation.parse(req.body);
|
||||||
createStatusPageBodyValidation.parse(req.body);
|
if (req.file) {
|
||||||
if (req.file) {
|
imageValidation.parse(req.file);
|
||||||
imageValidation.parse(req.file);
|
|
||||||
}
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req?.user?.teamId);
|
|
||||||
const userId = requireUserId(req?.user?.id);
|
|
||||||
const statusPage = await this.statusPageService.createStatusPage(userId, teamId, req.file, req.body);
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Status page created successfully",
|
|
||||||
data: statusPage,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
updateStatusPage = async (req: Request, res: Response, next: NextFunction) => {
|
const teamId = requireTeamId(req?.user?.teamId);
|
||||||
try {
|
const userId = requireUserId(req?.user?.id);
|
||||||
createStatusPageBodyValidation.parse(req.body);
|
const statusPage = await this.statusPageService.createStatusPage(userId, teamId, req.file, req.body);
|
||||||
if (req.file) {
|
|
||||||
imageValidation.parse(req.file);
|
|
||||||
}
|
|
||||||
|
|
||||||
const teamId = requireTeamId(req?.user?.teamId);
|
return res.status(200).json({
|
||||||
const statusPageId = req.params.id as string;
|
success: true,
|
||||||
if (!statusPageId) {
|
msg: "Status page created successfully",
|
||||||
throw new AppError({ message: "Status page ID is required", status: 400 });
|
data: statusPage,
|
||||||
}
|
});
|
||||||
const statusPage = await this.statusPageService.updateStatusPage(statusPageId, teamId, req.file, req.body);
|
});
|
||||||
if (statusPage === null) {
|
|
||||||
throw new AppError({ message: "Status page not found", status: 404 });
|
updateStatusPage = catchAsync(async (req: Request, res: Response) => {
|
||||||
}
|
createStatusPageBodyValidation.parse(req.body);
|
||||||
res.status(200).json({
|
if (req.file) {
|
||||||
success: true,
|
imageValidation.parse(req.file);
|
||||||
msg: "Status page updated successfully",
|
|
||||||
data: statusPage,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
getStatusPageByUrl = async (req: Request, res: Response, next: NextFunction) => {
|
const teamId = requireTeamId(req?.user?.teamId);
|
||||||
try {
|
const statusPageId = req.params.id as string;
|
||||||
getStatusPageParamValidation.parse(req.params);
|
if (!statusPageId) {
|
||||||
getStatusPageQueryValidation.parse(req.query);
|
throw new AppError({ message: "Status page ID is required", status: 400 });
|
||||||
|
|
||||||
if (!req.params.url) {
|
|
||||||
throw new AppError({ message: "Status page URL is required", status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const statusPage = await this.statusPageService.getStatusPageByUrl(req.params.url as string);
|
|
||||||
const data = await this.statusPageService.getPublicStatusPagePayload(statusPage, req.user?.teamId);
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Status page retrieved successfully",
|
|
||||||
data,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
}
|
||||||
};
|
const statusPage = await this.statusPageService.updateStatusPage(statusPageId, teamId, req.file, req.body);
|
||||||
|
if (statusPage === null) {
|
||||||
resolveStatusPageByDomain = async (req: Request, res: Response, next: NextFunction) => {
|
throw new AppError({ message: "Status page not found", status: 404 });
|
||||||
try {
|
|
||||||
resolveStatusPageQueryValidation.parse(req.query);
|
|
||||||
|
|
||||||
const domain = resolveStatusPageDomainFromRequest(req.hostname, req.query.domain as string | undefined);
|
|
||||||
if (!domain) {
|
|
||||||
throw new AppError({ message: "Domain is required", status: 400 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const statusPage = await this.statusPageService.getStatusPageByCustomDomain(domain);
|
|
||||||
if (!statusPage.isPublished) {
|
|
||||||
throw new AppError({ message: "Status page not found", status: 404 });
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await this.statusPageService.getPublicStatusPagePayload(statusPage, req.user?.teamId);
|
|
||||||
|
|
||||||
return res.status(200).json({
|
|
||||||
success: true,
|
|
||||||
msg: "Status page retrieved successfully",
|
|
||||||
data,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
}
|
||||||
};
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Status page updated successfully",
|
||||||
|
data: statusPage,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
getStatusPagesByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
getStatusPageByUrl = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
getStatusPageParamValidation.parse(req.params);
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
getStatusPageQueryValidation.parse(req.query);
|
||||||
const statusPages = await this.statusPageService.getStatusPagesByTeamId(teamId);
|
|
||||||
|
|
||||||
return res.status(200).json({
|
if (!req.params.url) {
|
||||||
success: true,
|
throw new AppError({ message: "Status page URL is required", status: 400 });
|
||||||
msg: "Status pages retrieved successfully",
|
|
||||||
data: statusPages,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
deleteStatusPage = async (req: Request, res: Response, next: NextFunction) => {
|
const statusPage = await this.statusPageService.getStatusPageByUrl(req.params.url as string);
|
||||||
try {
|
const data = await this.statusPageService.getPublicStatusPagePayload(statusPage, req.user?.teamId);
|
||||||
const statusPageId = req.params.id as string;
|
|
||||||
if (!statusPageId) {
|
return res.status(200).json({
|
||||||
throw new AppError({ message: "Status page ID is required", status: 400 });
|
success: true,
|
||||||
}
|
msg: "Status page retrieved successfully",
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
data,
|
||||||
await this.statusPageService.deleteStatusPage(statusPageId, teamId);
|
});
|
||||||
return res.status(200).json({
|
});
|
||||||
success: true,
|
|
||||||
msg: "Status page deleted successfully",
|
resolveStatusPageByDomain = catchAsync(async (req: Request, res: Response) => {
|
||||||
});
|
resolveStatusPageQueryValidation.parse(req.query);
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
const domain = resolveStatusPageDomainFromRequest(req.hostname, req.query.domain as string | undefined);
|
||||||
|
if (!domain) {
|
||||||
|
throw new AppError({ message: "Domain is required", status: 400 });
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
const statusPage = await this.statusPageService.getStatusPageByCustomDomain(domain);
|
||||||
|
if (!statusPage.isPublished) {
|
||||||
|
throw new AppError({ message: "Status page not found", status: 404 });
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await this.statusPageService.getPublicStatusPagePayload(statusPage, req.user?.teamId);
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Status page retrieved successfully",
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
getStatusPagesByTeamId = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const statusPages = await this.statusPageService.getStatusPagesByTeamId(teamId);
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Status pages retrieved successfully",
|
||||||
|
data: statusPages,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
deleteStatusPage = catchAsync(async (req: Request, res: Response) => {
|
||||||
|
const statusPageId = req.params.id as string;
|
||||||
|
if (!statusPageId) {
|
||||||
|
throw new AppError({ message: "Status page ID is required", status: 400 });
|
||||||
|
}
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
await this.statusPageService.deleteStatusPage(statusPageId, teamId);
|
||||||
|
return res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
msg: "Status page deleted successfully",
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export default StatusPageController;
|
export default StatusPageController;
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, RequestHandler } from "express";
|
||||||
|
import { catchAsync } from "@/utils/catchAsync.js";
|
||||||
import { ITagsService } from "@/domain/tags/tag.service.js";
|
import { ITagsService } from "@/domain/tags/tag.service.js";
|
||||||
import { requireTeamId } from "./controllerUtils.js";
|
import { requireTeamId } from "./controllerUtils.js";
|
||||||
import {
|
import {
|
||||||
@@ -10,89 +11,69 @@ import {
|
|||||||
} from "@/api/validation/index.js";
|
} from "@/api/validation/index.js";
|
||||||
|
|
||||||
export interface ITagsController {
|
export interface ITagsController {
|
||||||
createTag: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
createTag: RequestHandler;
|
||||||
getTagById: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getTagById: RequestHandler;
|
||||||
getTagsByTeamId: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
getTagsByTeamId: RequestHandler;
|
||||||
editTag: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
editTag: RequestHandler;
|
||||||
deleteTag: (req: Request, res: Response, next: NextFunction) => Promise<Response | void>;
|
deleteTag: RequestHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
class TagsController implements ITagsController {
|
class TagsController implements ITagsController {
|
||||||
constructor(private tagsService: ITagsService) {}
|
constructor(private tagsService: ITagsService) {}
|
||||||
|
|
||||||
createTag = async (req: Request, res: Response, next: NextFunction) => {
|
createTag = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const validatedBody = createTagBodyValidation.parse(req.body);
|
||||||
const validatedBody = createTagBodyValidation.parse(req.body);
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
|
||||||
|
|
||||||
const tag = await this.tagsService.createTag(validatedBody, teamId);
|
const tag = await this.tagsService.createTag(validatedBody, teamId);
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Tag created successfully",
|
msg: "Tag created successfully",
|
||||||
data: tag,
|
data: tag,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
next(error);
|
getTagById = catchAsync(async (req: Request, res: Response) => {
|
||||||
}
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
};
|
const { id: tagId } = getTagByIdParamValidation.parse(req.params);
|
||||||
getTagById = async (req: Request, res: Response, next: NextFunction) => {
|
const tag = await this.tagsService.getTag(tagId, teamId);
|
||||||
try {
|
return res.status(200).json({
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
success: true,
|
||||||
const { id: tagId } = getTagByIdParamValidation.parse(req.params);
|
msg: "Tag retrieved successfully",
|
||||||
const tag = await this.tagsService.getTag(tagId, teamId);
|
data: tag,
|
||||||
return res.status(200).json({
|
});
|
||||||
success: true,
|
});
|
||||||
msg: "Tag retrieved successfully",
|
|
||||||
data: tag,
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
getTagsByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
getTagsByTeamId = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
const tags = await this.tagsService.getTagsByTeamId(teamId);
|
||||||
const tags = await this.tagsService.getTagsByTeamId(teamId);
|
return res.status(200).json({
|
||||||
return res.status(200).json({
|
success: true,
|
||||||
success: true,
|
msg: "Tags retrieved successfully",
|
||||||
msg: "Tags retrieved successfully",
|
data: tags,
|
||||||
data: tags,
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
editTag = async (req: Request, res: Response, next: NextFunction) => {
|
editTag = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
const { id: tagId } = editTagParamValidation.parse(req.params);
|
||||||
const { id: tagId } = editTagParamValidation.parse(req.params);
|
const validatedBody = editTagBodyValidation.parse(req.body);
|
||||||
const validatedBody = editTagBodyValidation.parse(req.body);
|
const updatedTag = await this.tagsService.updateTag(tagId, teamId, validatedBody);
|
||||||
const updatedTag = await this.tagsService.updateTag(tagId, teamId, validatedBody);
|
return res.status(200).json({
|
||||||
return res.status(200).json({
|
success: true,
|
||||||
success: true,
|
msg: "Tag updated successfully",
|
||||||
msg: "Tag updated successfully",
|
data: updatedTag,
|
||||||
data: updatedTag,
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
deleteTag = async (req: Request, res: Response, next: NextFunction) => {
|
deleteTag = catchAsync(async (req: Request, res: Response) => {
|
||||||
try {
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
const teamId = requireTeamId(req.user?.teamId);
|
const { id: tagId } = deleteTagParamValidation.parse(req.params);
|
||||||
const { id: tagId } = deleteTagParamValidation.parse(req.params);
|
await this.tagsService.deleteTag(tagId, teamId);
|
||||||
await this.tagsService.deleteTag(tagId, teamId);
|
return res.status(200).json({
|
||||||
return res.status(200).json({
|
success: true,
|
||||||
success: true,
|
msg: "Tag deleted successfully",
|
||||||
msg: "Tag deleted successfully",
|
});
|
||||||
});
|
});
|
||||||
} catch (error) {
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default TagsController;
|
export default TagsController;
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import type { NextFunction, Request, RequestHandler, Response } from "express";
|
||||||
|
type AsyncRequestHandler = (req: Request, res: Response) => Promise<unknown>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps an async route handler so that any error it throws or any promise it
|
||||||
|
* rejects is passed to Express's error middleware (`handleErrors`). Every async controller method must be wrapped.
|
||||||
|
*/
|
||||||
|
export const catchAsync = (fn: AsyncRequestHandler): RequestHandler => {
|
||||||
|
const wrappedHandler = (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
fn(req, res).catch(next);
|
||||||
|
};
|
||||||
|
|
||||||
|
return wrappedHandler;
|
||||||
|
};
|
||||||
Reference in new issue
Block a user