diff --git a/server/src/controllers/maintenanceWindowController.ts b/server/src/controllers/maintenanceWindowController.ts index ed16911bf..cd2174c1b 100644 --- a/server/src/controllers/maintenanceWindowController.ts +++ b/server/src/controllers/maintenanceWindowController.ts @@ -9,25 +9,18 @@ import { deleteMaintenanceWindowByIdParamValidation, } from "@/validation/maintenanceWindowValidation.js"; import { requireTeamId } from "@/controllers/controllerUtils.js"; - -const SERVICE_NAME = "maintenanceWindowController"; +import { IMaintenanceWindowService } from "@/service/index.js"; class MaintenanceWindowController { - static SERVICE_NAME = SERVICE_NAME; - private maintenanceWindowService: any; - constructor(maintenanceWindowService: any) { + private maintenanceWindowService: IMaintenanceWindowService; + constructor(maintenanceWindowService: IMaintenanceWindowService) { this.maintenanceWindowService = maintenanceWindowService; } - get serviceName() { - return MaintenanceWindowController.SERVICE_NAME; - } - createMaintenanceWindows = async (req: Request, res: Response, next: NextFunction) => { try { createMaintenanceWindowBodyValidation.parse(req.body); const teamId = requireTeamId(req?.user?.teamId); - await this.maintenanceWindowService.createMaintenanceWindow({ teamId, body: req.body }); return res.status(200).json({ @@ -58,11 +51,18 @@ class MaintenanceWindowController { getMaintenanceWindowsByTeamId = async (req: Request, res: Response, next: NextFunction) => { try { - getMaintenanceWindowsByTeamIdQueryValidation.parse(req.query); + const validatedQuery = getMaintenanceWindowsByTeamIdQueryValidation.parse(req.query); 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({ success: true, @@ -76,12 +76,12 @@ class MaintenanceWindowController { getMaintenanceWindowsByMonitorId = async (req: Request, res: Response, next: NextFunction) => { try { - getMaintenanceWindowsByMonitorIdParamValidation.parse(req.params); + const validatedParams = getMaintenanceWindowsByMonitorIdParamValidation.parse(req.params); const teamId = requireTeamId(req?.user?.teamId); const maintenanceWindows = await this.maintenanceWindowService.getMaintenanceWindowsByMonitorId({ - monitorId: req.params.monitorId as string, + monitorId: validatedParams.monitorId, teamId, }); @@ -96,11 +96,11 @@ class MaintenanceWindowController { }; deleteMaintenanceWindow = async (req: Request, res: Response, next: NextFunction) => { try { - deleteMaintenanceWindowByIdParamValidation.parse(req.params); + const validatedParams = deleteMaintenanceWindowByIdParamValidation.parse(req.params); 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({ success: true, @@ -113,14 +113,14 @@ class MaintenanceWindowController { editMaintenanceWindow = async (req: Request, res: Response, next: NextFunction) => { try { - editMaintenanceWindowByIdParamValidation.parse(req.params); - editMaintenanceByIdWindowBodyValidation.parse(req.body); + const validatedParams = editMaintenanceWindowByIdParamValidation.parse(req.params); + const validatedBody = editMaintenanceByIdWindowBodyValidation.parse(req.body); const teamId = requireTeamId(req.user?.teamId); const editedMaintenanceWindow = await this.maintenanceWindowService.editMaintenanceWindow({ - id: req.params.id as string, - body: req.body, + id: validatedParams.id, + body: validatedBody, teamId, }); return res.status(200).json({ diff --git a/server/src/repositories/maintenance-windows/IMaintenanceWindowsRepository.ts b/server/src/repositories/maintenance-windows/IMaintenanceWindowsRepository.ts index 1bcb02347..fca63ac35 100644 --- a/server/src/repositories/maintenance-windows/IMaintenanceWindowsRepository.ts +++ b/server/src/repositories/maintenance-windows/IMaintenanceWindowsRepository.ts @@ -5,12 +5,12 @@ export interface IMaintenanceWindowsRepository { // fetch findById(id: string, teamId: string): Promise; findByMonitorId(monitorId: string, teamId: string): Promise; - findByTeamId(teamId: string, active: boolean, page: number, rowsPerPage: number, field: string, order: string): Promise; + findByTeamId(teamId: string, page: number, rowsPerPage: number, field?: string, order?: string, active?: boolean): Promise; // update updateById(id: string, teamId: string, data: Partial): Promise; // delete deleteById(id: string, teamId: string): Promise; // other - countByTeamId(teamId: string, active: boolean): Promise; + countByTeamId(teamId: string, active?: boolean): Promise; } diff --git a/server/src/repositories/maintenance-windows/MongoMaintenanceWindowsRepository.ts b/server/src/repositories/maintenance-windows/MongoMaintenanceWindowsRepository.ts index 4aadf9cc2..d924613ee 100644 --- a/server/src/repositories/maintenance-windows/MongoMaintenanceWindowsRepository.ts +++ b/server/src/repositories/maintenance-windows/MongoMaintenanceWindowsRepository.ts @@ -75,11 +75,11 @@ class MongoMaintenanceWindowsRepository implements IMaintenanceWindowsRepository findByTeamId = async ( teamId: string, - active: boolean, page: number, rowsPerPage: number, - field: string, - order: string + field?: string, + order?: string, + active?: boolean ): Promise => { const maintenanceQuery: Record = { teamId }; @@ -126,7 +126,7 @@ class MongoMaintenanceWindowsRepository implements IMaintenanceWindowsRepository } return this.toEntity(deleted); }; - countByTeamId = async (teamId: string, active: boolean) => { + countByTeamId = async (teamId: string, active?: boolean) => { const maintenanceQuery: Record = { teamId }; if (active !== undefined) maintenanceQuery.active = active; diff --git a/server/src/service/business/maintenanceWindowService.ts b/server/src/service/business/maintenanceWindowService.ts index abf513eb5..1db2df9ca 100644 --- a/server/src/service/business/maintenanceWindowService.ts +++ b/server/src/service/business/maintenanceWindowService.ts @@ -10,7 +10,11 @@ export interface IMaintenanceWindowService { getMaintenanceWindowById(params: { id: string; teamId: string }): Promise; getMaintenanceWindowsByTeamId(params: { teamId: string; - query: Record; + active?: boolean; + page?: number; + rowsPerPage?: number; + field?: string; + order?: string; }): Promise<{ maintenanceWindows: MaintenanceWindow[]; maintenanceWindowCount: number }>; getMaintenanceWindowsByMonitorId(params: { monitorId: string; teamId: string }): Promise; deleteMaintenanceWindow(params: { id: string; teamId: string }): Promise; @@ -72,14 +76,26 @@ export class MaintenanceWindowService implements IMaintenanceWindowService { return await this.maintenanceWindowsRepository.findById(id, teamId); }; - getMaintenanceWindowsByTeamId = async ({ teamId, query }: { teamId: string; query: any }) => { - let { active, page, rowsPerPage, field, order } = query || {}; - + getMaintenanceWindowsByTeamId = async ({ + 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); - page = parseInt(page) || 0; - rowsPerPage = parseInt(rowsPerPage) || 10; + page = page ?? 0; + 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); return { maintenanceWindows, maintenanceWindowCount }; }; diff --git a/server/src/validation/maintenanceWindowValidation.ts b/server/src/validation/maintenanceWindowValidation.ts index e09ac2989..3470f4c38 100644 --- a/server/src/validation/maintenanceWindowValidation.ts +++ b/server/src/validation/maintenanceWindowValidation.ts @@ -45,9 +45,9 @@ export const editMaintenanceByIdWindowBodyValidation = z.object({ active: z.boolean().optional(), name: z.string().optional(), repeat: z.number().optional(), - start: z.coerce.date().optional(), - end: z.coerce.date().optional(), - expiry: z.coerce.date().optional(), + start: z.string().optional(), + end: z.string().optional(), + expiry: z.string().optional(), monitors: z.array(z.unknown()).optional(), duration: z.number().optional(), durationUnit: z.enum(["seconds", "minutes", "hours", "days"]).optional(),