Files
Checkmate/server/src/service/business/maintenanceWindowService.ts
T

86 lines
2.7 KiB
TypeScript
Raw Normal View History

2026-01-15 20:55:01 +00:00
import { IMonitorsRepository } from "@/repositories/index.js";
2025-07-23 15:14:47 -07:00
const SERVICE_NAME = "maintenanceWindowService";
class MaintenanceWindowService {
static SERVICE_NAME = SERVICE_NAME;
2026-01-15 20:55:01 +00:00
private db: any;
private settingsService: any;
private stringService: any;
private errorService: any;
private monitorsRepository: IMonitorsRepository;
2025-07-25 15:15:14 -07:00
2026-01-15 20:55:01 +00:00
constructor({
db,
settingsService,
stringService,
errorService,
monitorsRepository,
}: {
db: any;
settingsService: any;
stringService: any;
errorService: any;
monitorsRepository: IMonitorsRepository;
}) {
2025-07-23 15:14:47 -07:00
this.db = db;
this.settingsService = settingsService;
this.stringService = stringService;
2025-07-25 10:16:29 -07:00
this.errorService = errorService;
2026-01-15 20:55:01 +00:00
this.monitorsRepository = monitorsRepository;
2025-07-23 15:14:47 -07:00
}
2025-07-25 15:15:14 -07:00
get serviceName() {
return MaintenanceWindowService.SERVICE_NAME;
}
2026-01-15 20:55:01 +00:00
createMaintenanceWindow = async ({ teamId, body }: { teamId: string; body: any }) => {
2025-07-23 15:14:47 -07:00
const monitorIds = body.monitors;
2026-01-15 20:55:01 +00:00
const monitors = await this.monitorsRepository.findByIds(monitorIds);
2025-07-23 15:14:47 -07:00
2026-01-15 20:55:01 +00:00
const unauthorizedMonitors = monitors.filter((monitor) => monitor.teamId !== teamId);
2025-07-23 15:14:47 -07:00
if (unauthorizedMonitors.length > 0) {
2025-07-25 10:16:29 -07:00
throw this.errorService.createAuthorizationError();
2025-07-23 15:14:47 -07:00
}
2026-01-15 20:55:01 +00:00
const dbTransactions = monitorIds.map((monitorId: string) => {
2025-07-30 11:36:19 -07:00
return this.db.maintenanceWindowModule.createMaintenanceWindow({
2025-07-23 15:14:47 -07:00
teamId,
monitorId,
name: body.name,
active: body.active ? body.active : true,
repeat: body.repeat,
start: body.start,
end: body.end,
});
});
await Promise.all(dbTransactions);
};
2026-01-15 20:55:01 +00:00
getMaintenanceWindowById = async ({ id, teamId }: { id: string; teamId: string }) => {
2025-07-30 11:36:19 -07:00
const maintenanceWindow = await this.db.maintenanceWindowModule.getMaintenanceWindowById({ id, teamId });
2025-07-23 15:14:47 -07:00
return maintenanceWindow;
};
2026-01-15 20:55:01 +00:00
getMaintenanceWindowsByTeamId = async ({ teamId, query }: { teamId: string; query: any }) => {
2025-07-30 11:36:19 -07:00
const maintenanceWindows = await this.db.maintenanceWindowModule.getMaintenanceWindowsByTeamId(teamId, query);
2025-07-23 15:14:47 -07:00
return maintenanceWindows;
};
2026-01-15 20:55:01 +00:00
getMaintenanceWindowsByMonitorId = async ({ monitorId, teamId }: { monitorId: string; teamId: string }) => {
2025-07-30 11:36:19 -07:00
const maintenanceWindows = await this.db.maintenanceWindowModule.getMaintenanceWindowsByMonitorId({ monitorId, teamId });
2025-07-23 15:14:47 -07:00
return maintenanceWindows;
};
2026-01-15 20:55:01 +00:00
deleteMaintenanceWindow = async ({ id, teamId }: { id: string; teamId: string }) => {
2025-07-30 11:36:19 -07:00
await this.db.maintenanceWindowModule.deleteMaintenanceWindowById({ id, teamId });
2025-07-23 15:14:47 -07:00
};
2026-01-15 20:55:01 +00:00
editMaintenanceWindow = async ({ id, teamId, body }: { id: string; teamId: string; body: any }) => {
2025-07-30 11:36:19 -07:00
const editedMaintenanceWindow = await this.db.maintenanceWindowModule.editMaintenanceWindowById({ id, body, teamId });
2025-07-23 15:14:47 -07:00
return editedMaintenanceWindow;
};
}
export default MaintenanceWindowService;