diff --git a/server/src/repositories/maintenance-windows/IMaintenanceWindowsRepository.ts b/server/src/repositories/maintenance-windows/IMaintenanceWindowsRepository.ts index b71b8d257..1bcb02347 100644 --- a/server/src/repositories/maintenance-windows/IMaintenanceWindowsRepository.ts +++ b/server/src/repositories/maintenance-windows/IMaintenanceWindowsRepository.ts @@ -4,11 +4,13 @@ export interface IMaintenanceWindowsRepository { create(data: Partial): Promise; // 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; // update + updateById(id: string, teamId: string, data: Partial): Promise; // delete + deleteById(id: string, teamId: string): Promise; // other 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 7f41b4e0c..e85af5cb9 100644 --- a/server/src/repositories/maintenance-windows/MongoMaintenanceWindowsRepository.ts +++ b/server/src/repositories/maintenance-windows/MongoMaintenanceWindowsRepository.ts @@ -63,6 +63,14 @@ class MongoMaintenanceWindowsRepository implements IMaintenanceWindowsRepository return this.toEntity(maintenanceWindow); }; + findByMonitorId = async (monitorId: string, teamId: string): Promise => { + const maintenanceWindows = await MaintenanceWindowModel.find({ + monitorId: monitorId, + teamId: teamId, + }); + return this.mapDocuments(maintenanceWindows); + }; + findByTeamId = async ( teamId: string, active: boolean, @@ -91,6 +99,31 @@ class MongoMaintenanceWindowsRepository implements IMaintenanceWindowsRepository return this.mapDocuments(maintenanceWindows); }; + updateById = async (id: string, teamId: string, data: Partial): Promise => { + const updated = await MaintenanceWindowModel.findOneAndUpdate( + { + _id: new mongoose.Types.ObjectId(id), + teamId: new mongoose.Types.ObjectId(teamId), + }, + { $set: data }, + { new: true, runValidators: true } + ); + if (!updated) { + throw new AppError({ message: "Maintenance window not found or could not be updated", status: 404 }); + } + return this.toEntity(updated); + }; + + deleteById = async (id: string, teamId: string): Promise => { + const deleted = await MaintenanceWindowModel.findOneAndDelete({ + _id: new mongoose.Types.ObjectId(id), + teamId: new mongoose.Types.ObjectId(teamId), + }); + if (!deleted) { + throw new AppError({ message: "Maintenance window not found or could not be deleted", status: 404 }); + } + return this.toEntity(deleted); + }; countByTeamId = async (teamId: string, active: boolean) => { const maintenanceQuery: Record = { teamId }; diff --git a/server/src/service/business/maintenanceWindowService.ts b/server/src/service/business/maintenanceWindowService.ts index ade7edf0f..561d97480 100644 --- a/server/src/service/business/maintenanceWindowService.ts +++ b/server/src/service/business/maintenanceWindowService.ts @@ -68,17 +68,15 @@ class MaintenanceWindowService { }; getMaintenanceWindowsByMonitorId = async ({ monitorId, teamId }: { monitorId: string; teamId: string }) => { - const maintenanceWindows = await this.db.maintenanceWindowModule.getMaintenanceWindowsByMonitorId({ monitorId, teamId }); - return maintenanceWindows; + return await this.maintenanceWindowsRepository.findByMonitorId(monitorId, teamId); }; deleteMaintenanceWindow = async ({ id, teamId }: { id: string; teamId: string }) => { - await this.db.maintenanceWindowModule.deleteMaintenanceWindowById({ id, teamId }); + return await this.maintenanceWindowsRepository.deleteById(id, teamId); }; editMaintenanceWindow = async ({ id, teamId, body }: { id: string; teamId: string; body: any }) => { - const editedMaintenanceWindow = await this.db.maintenanceWindowModule.editMaintenanceWindowById({ id, body, teamId }); - return editedMaintenanceWindow; + return await this.maintenanceWindowsRepository.updateById(id, teamId, body); }; }