udpate service to use monitorIds instead of monitorId
This commit is contained in:
1 parent
7a2dab6cc4
commit
e78524d1c9
1 file changed
+42
-16
@@ -27,7 +27,11 @@ export interface IMaintenanceWindowService {
|
|||||||
}): Promise<{ maintenanceWindows: MaintenanceWindow[]; maintenanceWindowCount: number }>;
|
}): Promise<{ maintenanceWindows: MaintenanceWindow[]; maintenanceWindowCount: number }>;
|
||||||
getMaintenanceWindowsByMonitorId(params: { monitorId: string; teamId: string }): Promise<MaintenanceWindow[]>;
|
getMaintenanceWindowsByMonitorId(params: { monitorId: string; teamId: string }): Promise<MaintenanceWindow[]>;
|
||||||
deleteMaintenanceWindow(params: { id: string; teamId: string }): Promise<MaintenanceWindow>;
|
deleteMaintenanceWindow(params: { id: string; teamId: string }): Promise<MaintenanceWindow>;
|
||||||
editMaintenanceWindow(params: { id: string; teamId: string; body: Partial<MaintenanceWindow> }): Promise<MaintenanceWindow>;
|
editMaintenanceWindow(params: {
|
||||||
|
id: string;
|
||||||
|
teamId: string;
|
||||||
|
body: Partial<Omit<MaintenanceWindow, "monitorIds">> & { monitors?: string[] };
|
||||||
|
}): Promise<MaintenanceWindow>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class MaintenanceWindowService implements IMaintenanceWindowService {
|
export class MaintenanceWindowService implements IMaintenanceWindowService {
|
||||||
@@ -84,20 +88,17 @@ export class MaintenanceWindowService implements IMaintenanceWindowService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const dbTransactions = monitorIDs.map((monitorId: string) => {
|
await this.maintenanceWindowsRepository.create({
|
||||||
return this.maintenanceWindowsRepository.create({
|
teamId,
|
||||||
teamId,
|
monitorIds: monitorIDs,
|
||||||
monitorId,
|
name: name,
|
||||||
name: name,
|
active: active,
|
||||||
active: active,
|
duration: duration,
|
||||||
duration: duration,
|
durationUnit: durationUnit,
|
||||||
durationUnit: durationUnit,
|
repeat: repeat,
|
||||||
repeat: repeat,
|
start: start,
|
||||||
start: start,
|
end: end,
|
||||||
end: end,
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
await Promise.all(dbTransactions);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
getMaintenanceWindowById = async ({ id, teamId }: { id: string; teamId: string }) => {
|
getMaintenanceWindowById = async ({ id, teamId }: { id: string; teamId: string }) => {
|
||||||
@@ -135,7 +136,32 @@ 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: Partial<MaintenanceWindow> }) => {
|
editMaintenanceWindow = async ({
|
||||||
return await this.maintenanceWindowsRepository.updateById(id, teamId, body);
|
id,
|
||||||
|
teamId,
|
||||||
|
body,
|
||||||
|
}: {
|
||||||
|
id: string;
|
||||||
|
teamId: string;
|
||||||
|
body: Partial<Omit<MaintenanceWindow, "monitorIds">> & { monitors?: string[] };
|
||||||
|
}) => {
|
||||||
|
const { monitors, ...rest } = body;
|
||||||
|
const update: Partial<MaintenanceWindow> = rest;
|
||||||
|
|
||||||
|
if (monitors !== undefined) {
|
||||||
|
const monitorDocs = await this.monitorsRepository.findByIds(monitors);
|
||||||
|
const unauthorizedMonitors = monitorDocs.filter((monitor) => monitor.teamId !== teamId);
|
||||||
|
if (unauthorizedMonitors.length > 0) {
|
||||||
|
throw new AppError({
|
||||||
|
message: "Unauthorized to edit maintenance window for one or more monitors",
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "editMaintenanceWindow",
|
||||||
|
status: 403,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
update.monitorIds = monitors;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.maintenanceWindowsRepository.updateById(id, teamId, update);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user