maintenacne window
This commit is contained in:
1 parent
533a8911cd
commit
a4a3249b5b
2 files changed
+54
-15
No files matched your search
@@ -21,7 +21,17 @@ class MaintenanceWindowController {
|
|||||||
try {
|
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);
|
||||||
await this.maintenanceWindowService.createMaintenanceWindow({ teamId, body: validatedBody });
|
|
||||||
|
const monitorIDs = validatedBody.monitors;
|
||||||
|
const name = validatedBody.name;
|
||||||
|
const active = validatedBody.active ? validatedBody.active : true;
|
||||||
|
const duration = validatedBody.duration;
|
||||||
|
const durationUnit = validatedBody.durationUnit;
|
||||||
|
const repeat = validatedBody.repeat;
|
||||||
|
const start = validatedBody.start;
|
||||||
|
const end = validatedBody.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,
|
||||||
|
|||||||
@@ -1,11 +1,21 @@
|
|||||||
import { IMaintenanceWindowsRepository, IMonitorsRepository } from "@/repositories/index.js";
|
import { IMaintenanceWindowsRepository, IMonitorsRepository } from "@/repositories/index.js";
|
||||||
import type { MaintenanceWindow } from "@/types/index.js";
|
import type { DurationUnit, MaintenanceWindow } from "@/types/index.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "maintenanceWindowService";
|
const SERVICE_NAME = "maintenanceWindowService";
|
||||||
|
|
||||||
export interface IMaintenanceWindowService {
|
export interface IMaintenanceWindowService {
|
||||||
createMaintenanceWindow(params: { teamId: string; body: Partial<MaintenanceWindow> & { monitors: string[] } }): Promise<void>;
|
createMaintenanceWindow(params: {
|
||||||
|
teamId: string;
|
||||||
|
monitorIDs: string[];
|
||||||
|
name: string;
|
||||||
|
active: boolean;
|
||||||
|
duration: number;
|
||||||
|
durationUnit: DurationUnit;
|
||||||
|
repeat: number;
|
||||||
|
start: string;
|
||||||
|
end: string;
|
||||||
|
}): Promise<void>;
|
||||||
getMaintenanceWindowById(params: { id: string; teamId: string }): Promise<MaintenanceWindow>;
|
getMaintenanceWindowById(params: { id: string; teamId: string }): Promise<MaintenanceWindow>;
|
||||||
getMaintenanceWindowsByTeamId(params: {
|
getMaintenanceWindowsByTeamId(params: {
|
||||||
teamId: string;
|
teamId: string;
|
||||||
@@ -40,9 +50,28 @@ export class MaintenanceWindowService implements IMaintenanceWindowService {
|
|||||||
return MaintenanceWindowService.SERVICE_NAME;
|
return MaintenanceWindowService.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
createMaintenanceWindow = async ({ teamId, body }: { teamId: string; body: any }) => {
|
createMaintenanceWindow = async ({
|
||||||
const monitorIds = body.monitors;
|
teamId,
|
||||||
const monitors = await this.monitorsRepository.findByIds(monitorIds);
|
monitorIDs,
|
||||||
|
name,
|
||||||
|
active,
|
||||||
|
duration,
|
||||||
|
durationUnit,
|
||||||
|
repeat,
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
}: {
|
||||||
|
teamId: string;
|
||||||
|
monitorIDs: string[];
|
||||||
|
name: string;
|
||||||
|
active: boolean;
|
||||||
|
duration: number;
|
||||||
|
durationUnit: DurationUnit;
|
||||||
|
repeat: number;
|
||||||
|
start: string;
|
||||||
|
end: string;
|
||||||
|
}) => {
|
||||||
|
const monitors = await this.monitorsRepository.findByIds(monitorIDs);
|
||||||
|
|
||||||
const unauthorizedMonitors = monitors.filter((monitor) => monitor.teamId !== teamId);
|
const unauthorizedMonitors = monitors.filter((monitor) => monitor.teamId !== teamId);
|
||||||
|
|
||||||
@@ -55,17 +84,17 @@ export class MaintenanceWindowService implements IMaintenanceWindowService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const dbTransactions = monitorIds.map((monitorId: string) => {
|
const dbTransactions = monitorIDs.map((monitorId: string) => {
|
||||||
return this.maintenanceWindowsRepository.create({
|
return this.maintenanceWindowsRepository.create({
|
||||||
teamId,
|
teamId,
|
||||||
monitorId,
|
monitorId,
|
||||||
name: body.name,
|
name: name,
|
||||||
active: body.active ? body.active : true,
|
active: active,
|
||||||
duration: body.duration,
|
duration: duration,
|
||||||
durationUnit: body.durationUnit,
|
durationUnit: durationUnit,
|
||||||
repeat: body.repeat,
|
repeat: repeat,
|
||||||
start: body.start,
|
start: start,
|
||||||
end: body.end,
|
end: end,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
await Promise.all(dbTransactions);
|
await Promise.all(dbTransactions);
|
||||||
@@ -106,7 +135,7 @@ export class MaintenanceWindowService implements IMaintenanceWindowService {
|
|||||||
return await this.maintenanceWindowsRepository.deleteById(id, teamId);
|
return await this.maintenanceWindowsRepository.deleteById(id, teamId);
|
||||||
};
|
};
|
||||||
|
|
||||||
editMaintenanceWindow = async ({ id, teamId, body }: { id: string; teamId: string; body: any }) => {
|
editMaintenanceWindow = async ({ id, teamId, body }: { id: string; teamId: string; body: Partial<MaintenanceWindow> }) => {
|
||||||
return await this.maintenanceWindowsRepository.updateById(id, teamId, body);
|
return await this.maintenanceWindowsRepository.updateById(id, teamId, body);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user