type MWController
This commit is contained in:
1 parent
d07f259829
commit
e887c2308a
5 files changed
+52
-36
No files matched your search
@@ -9,25 +9,18 @@ import {
|
|||||||
deleteMaintenanceWindowByIdParamValidation,
|
deleteMaintenanceWindowByIdParamValidation,
|
||||||
} from "@/validation/maintenanceWindowValidation.js";
|
} from "@/validation/maintenanceWindowValidation.js";
|
||||||
import { requireTeamId } from "@/controllers/controllerUtils.js";
|
import { requireTeamId } from "@/controllers/controllerUtils.js";
|
||||||
|
import { IMaintenanceWindowService } from "@/service/index.js";
|
||||||
const SERVICE_NAME = "maintenanceWindowController";
|
|
||||||
|
|
||||||
class MaintenanceWindowController {
|
class MaintenanceWindowController {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
private maintenanceWindowService: IMaintenanceWindowService;
|
||||||
private maintenanceWindowService: any;
|
constructor(maintenanceWindowService: IMaintenanceWindowService) {
|
||||||
constructor(maintenanceWindowService: any) {
|
|
||||||
this.maintenanceWindowService = maintenanceWindowService;
|
this.maintenanceWindowService = maintenanceWindowService;
|
||||||
}
|
}
|
||||||
|
|
||||||
get serviceName() {
|
|
||||||
return MaintenanceWindowController.SERVICE_NAME;
|
|
||||||
}
|
|
||||||
|
|
||||||
createMaintenanceWindows = async (req: Request, res: Response, next: NextFunction) => {
|
createMaintenanceWindows = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
createMaintenanceWindowBodyValidation.parse(req.body);
|
createMaintenanceWindowBodyValidation.parse(req.body);
|
||||||
const teamId = requireTeamId(req?.user?.teamId);
|
const teamId = requireTeamId(req?.user?.teamId);
|
||||||
|
|
||||||
await this.maintenanceWindowService.createMaintenanceWindow({ teamId, body: req.body });
|
await this.maintenanceWindowService.createMaintenanceWindow({ teamId, body: req.body });
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
@@ -58,11 +51,18 @@ class MaintenanceWindowController {
|
|||||||
|
|
||||||
getMaintenanceWindowsByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
getMaintenanceWindowsByTeamId = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
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({ teamId, query: req.query });
|
const maintenanceWindows = await this.maintenanceWindowService.getMaintenanceWindowsByTeamId({
|
||||||
|
teamId,
|
||||||
|
active: validatedQuery.active,
|
||||||
|
page: validatedQuery.page,
|
||||||
|
rowsPerPage: validatedQuery.rowsPerPage,
|
||||||
|
field: validatedQuery.field,
|
||||||
|
order: validatedQuery.order,
|
||||||
|
});
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -76,12 +76,12 @@ class MaintenanceWindowController {
|
|||||||
|
|
||||||
getMaintenanceWindowsByMonitorId = async (req: Request, res: Response, next: NextFunction) => {
|
getMaintenanceWindowsByMonitorId = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
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: req.params.monitorId as string,
|
monitorId: validatedParams.monitorId,
|
||||||
teamId,
|
teamId,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -96,11 +96,11 @@ class MaintenanceWindowController {
|
|||||||
};
|
};
|
||||||
deleteMaintenanceWindow = async (req: Request, res: Response, next: NextFunction) => {
|
deleteMaintenanceWindow = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
deleteMaintenanceWindowByIdParamValidation.parse(req.params);
|
const validatedParams = deleteMaintenanceWindowByIdParamValidation.parse(req.params);
|
||||||
|
|
||||||
const teamId = requireTeamId(req?.user?.teamId);
|
const teamId = requireTeamId(req?.user?.teamId);
|
||||||
|
|
||||||
await this.maintenanceWindowService.deleteMaintenanceWindow({ id: req.params.id as string, teamId });
|
await this.maintenanceWindowService.deleteMaintenanceWindow({ id: validatedParams.id, teamId });
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
@@ -113,14 +113,14 @@ class MaintenanceWindowController {
|
|||||||
|
|
||||||
editMaintenanceWindow = async (req: Request, res: Response, next: NextFunction) => {
|
editMaintenanceWindow = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
editMaintenanceWindowByIdParamValidation.parse(req.params);
|
const validatedParams = editMaintenanceWindowByIdParamValidation.parse(req.params);
|
||||||
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: req.params.id as string,
|
id: validatedParams.id,
|
||||||
body: req.body,
|
body: validatedBody,
|
||||||
teamId,
|
teamId,
|
||||||
});
|
});
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ export interface IMaintenanceWindowsRepository {
|
|||||||
// fetch
|
// fetch
|
||||||
findById(id: string, teamId: string): Promise<MaintenanceWindow>;
|
findById(id: string, teamId: string): Promise<MaintenanceWindow>;
|
||||||
findByMonitorId(monitorId: string, teamId: string): Promise<MaintenanceWindow[]>;
|
findByMonitorId(monitorId: string, teamId: string): Promise<MaintenanceWindow[]>;
|
||||||
findByTeamId(teamId: string, active: boolean, page: number, rowsPerPage: number, field: string, order: string): Promise<MaintenanceWindow[]>;
|
findByTeamId(teamId: string, page: number, rowsPerPage: number, field?: string, order?: string, active?: boolean): Promise<MaintenanceWindow[]>;
|
||||||
|
|
||||||
// update
|
// update
|
||||||
updateById(id: string, teamId: string, data: Partial<MaintenanceWindow>): Promise<MaintenanceWindow>;
|
updateById(id: string, teamId: string, data: Partial<MaintenanceWindow>): Promise<MaintenanceWindow>;
|
||||||
// delete
|
// delete
|
||||||
deleteById(id: string, teamId: string): Promise<MaintenanceWindow>;
|
deleteById(id: string, teamId: string): Promise<MaintenanceWindow>;
|
||||||
// other
|
// other
|
||||||
countByTeamId(teamId: string, active: boolean): Promise<number>;
|
countByTeamId(teamId: string, active?: boolean): Promise<number>;
|
||||||
}
|
}
|
||||||
@@ -75,11 +75,11 @@ class MongoMaintenanceWindowsRepository implements IMaintenanceWindowsRepository
|
|||||||
|
|
||||||
findByTeamId = async (
|
findByTeamId = async (
|
||||||
teamId: string,
|
teamId: string,
|
||||||
active: boolean,
|
|
||||||
page: number,
|
page: number,
|
||||||
rowsPerPage: number,
|
rowsPerPage: number,
|
||||||
field: string,
|
field?: string,
|
||||||
order: string
|
order?: string,
|
||||||
|
active?: boolean
|
||||||
): Promise<MaintenanceWindow[]> => {
|
): Promise<MaintenanceWindow[]> => {
|
||||||
const maintenanceQuery: Record<string, any> = { teamId };
|
const maintenanceQuery: Record<string, any> = { teamId };
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ class MongoMaintenanceWindowsRepository implements IMaintenanceWindowsRepository
|
|||||||
}
|
}
|
||||||
return this.toEntity(deleted);
|
return this.toEntity(deleted);
|
||||||
};
|
};
|
||||||
countByTeamId = async (teamId: string, active: boolean) => {
|
countByTeamId = async (teamId: string, active?: boolean) => {
|
||||||
const maintenanceQuery: Record<string, any> = { teamId };
|
const maintenanceQuery: Record<string, any> = { teamId };
|
||||||
|
|
||||||
if (active !== undefined) maintenanceQuery.active = active;
|
if (active !== undefined) maintenanceQuery.active = active;
|
||||||
|
|||||||
@@ -10,7 +10,11 @@ export interface IMaintenanceWindowService {
|
|||||||
getMaintenanceWindowById(params: { id: string; teamId: string }): Promise<MaintenanceWindow>;
|
getMaintenanceWindowById(params: { id: string; teamId: string }): Promise<MaintenanceWindow>;
|
||||||
getMaintenanceWindowsByTeamId(params: {
|
getMaintenanceWindowsByTeamId(params: {
|
||||||
teamId: string;
|
teamId: string;
|
||||||
query: Record<string, string>;
|
active?: boolean;
|
||||||
|
page?: number;
|
||||||
|
rowsPerPage?: number;
|
||||||
|
field?: string;
|
||||||
|
order?: string;
|
||||||
}): Promise<{ maintenanceWindows: MaintenanceWindow[]; maintenanceWindowCount: number }>;
|
}): Promise<{ maintenanceWindows: MaintenanceWindow[]; maintenanceWindowCount: number }>;
|
||||||
getMaintenanceWindowsByMonitorId(params: { monitorId: string; teamId: string }): Promise<MaintenanceWindow[]>;
|
getMaintenanceWindowsByMonitorId(params: { monitorId: string; teamId: string }): Promise<MaintenanceWindow[]>;
|
||||||
deleteMaintenanceWindow(params: { id: string; teamId: string }): Promise<MaintenanceWindow>;
|
deleteMaintenanceWindow(params: { id: string; teamId: string }): Promise<MaintenanceWindow>;
|
||||||
@@ -72,14 +76,26 @@ export class MaintenanceWindowService implements IMaintenanceWindowService {
|
|||||||
return await this.maintenanceWindowsRepository.findById(id, teamId);
|
return await this.maintenanceWindowsRepository.findById(id, teamId);
|
||||||
};
|
};
|
||||||
|
|
||||||
getMaintenanceWindowsByTeamId = async ({ teamId, query }: { teamId: string; query: any }) => {
|
getMaintenanceWindowsByTeamId = async ({
|
||||||
let { active, page, rowsPerPage, field, order } = query || {};
|
teamId,
|
||||||
|
active,
|
||||||
|
page,
|
||||||
|
rowsPerPage,
|
||||||
|
field,
|
||||||
|
order,
|
||||||
|
}: {
|
||||||
|
teamId: string;
|
||||||
|
active?: boolean;
|
||||||
|
page?: number;
|
||||||
|
rowsPerPage?: number;
|
||||||
|
field?: string;
|
||||||
|
order?: string;
|
||||||
|
}) => {
|
||||||
active = typeof active === "undefined" ? undefined : ParseBoolean(active);
|
active = typeof active === "undefined" ? undefined : ParseBoolean(active);
|
||||||
page = parseInt(page) || 0;
|
page = page ?? 0;
|
||||||
rowsPerPage = parseInt(rowsPerPage) || 10;
|
rowsPerPage = rowsPerPage ?? 10;
|
||||||
|
|
||||||
const maintenanceWindows = await this.maintenanceWindowsRepository.findByTeamId(teamId, active, page, rowsPerPage, field, order);
|
const maintenanceWindows = await this.maintenanceWindowsRepository.findByTeamId(teamId, page, rowsPerPage, field, order, active);
|
||||||
const maintenanceWindowCount = await this.maintenanceWindowsRepository.countByTeamId(teamId, active);
|
const maintenanceWindowCount = await this.maintenanceWindowsRepository.countByTeamId(teamId, active);
|
||||||
return { maintenanceWindows, maintenanceWindowCount };
|
return { maintenanceWindows, maintenanceWindowCount };
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -45,9 +45,9 @@ export const editMaintenanceByIdWindowBodyValidation = z.object({
|
|||||||
active: z.boolean().optional(),
|
active: z.boolean().optional(),
|
||||||
name: z.string().optional(),
|
name: z.string().optional(),
|
||||||
repeat: z.number().optional(),
|
repeat: z.number().optional(),
|
||||||
start: z.coerce.date().optional(),
|
start: z.string().optional(),
|
||||||
end: z.coerce.date().optional(),
|
end: z.string().optional(),
|
||||||
expiry: z.coerce.date().optional(),
|
expiry: z.string().optional(),
|
||||||
monitors: z.array(z.unknown()).optional(),
|
monitors: z.array(z.unknown()).optional(),
|
||||||
duration: z.number().optional(),
|
duration: z.number().optional(),
|
||||||
durationUnit: z.enum(["seconds", "minutes", "hours", "days"]).optional(),
|
durationUnit: z.enum(["seconds", "minutes", "hours", "days"]).optional(),
|
||||||
|
|||||||
Reference in new issue
Block a user