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,
|
||||
} 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({
|
||||
|
||||
@@ -5,12 +5,12 @@ export interface IMaintenanceWindowsRepository {
|
||||
// fetch
|
||||
findById(id: 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
|
||||
updateById(id: string, teamId: string, data: Partial<MaintenanceWindow>): Promise<MaintenanceWindow>;
|
||||
// delete
|
||||
deleteById(id: string, teamId: string): Promise<MaintenanceWindow>;
|
||||
// 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 (
|
||||
teamId: string,
|
||||
active: boolean,
|
||||
page: number,
|
||||
rowsPerPage: number,
|
||||
field: string,
|
||||
order: string
|
||||
field?: string,
|
||||
order?: string,
|
||||
active?: boolean
|
||||
): Promise<MaintenanceWindow[]> => {
|
||||
const maintenanceQuery: Record<string, any> = { 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<string, any> = { teamId };
|
||||
|
||||
if (active !== undefined) maintenanceQuery.active = active;
|
||||
|
||||
@@ -10,7 +10,11 @@ export interface IMaintenanceWindowService {
|
||||
getMaintenanceWindowById(params: { id: string; teamId: string }): Promise<MaintenanceWindow>;
|
||||
getMaintenanceWindowsByTeamId(params: {
|
||||
teamId: string;
|
||||
query: Record<string, string>;
|
||||
active?: boolean;
|
||||
page?: number;
|
||||
rowsPerPage?: number;
|
||||
field?: string;
|
||||
order?: string;
|
||||
}): Promise<{ maintenanceWindows: MaintenanceWindow[]; maintenanceWindowCount: number }>;
|
||||
getMaintenanceWindowsByMonitorId(params: { monitorId: 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);
|
||||
};
|
||||
|
||||
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 };
|
||||
};
|
||||
|
||||
@@ -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(),
|
||||
|
||||
Reference in new issue
Block a user