diff --git a/Common/Server/Services/MonitorGroupService.ts b/Common/Server/Services/MonitorGroupService.ts index 1960c5a765..2c9c8ef6c1 100644 --- a/Common/Server/Services/MonitorGroupService.ts +++ b/Common/Server/Services/MonitorGroupService.ts @@ -12,12 +12,36 @@ import MonitorGroup from "Common/Models/DatabaseModels/MonitorGroup"; import MonitorGroupResource from "Common/Models/DatabaseModels/MonitorGroupResource"; import MonitorStatus from "Common/Models/DatabaseModels/MonitorStatus"; import MonitorStatusTimeline from "Common/Models/DatabaseModels/MonitorStatusTimeline"; +import DeleteBy from "../Types/Database/DeleteBy"; +import { OnDelete } from "../Types/Database/Hooks"; +import StatusPageResourceService from "./StatusPageResourceService"; export class Service extends DatabaseService { public constructor() { super(MonitorGroup); } + protected override async onBeforeDelete( + deleteBy: DeleteBy, + ): Promise> { + if (deleteBy.query._id) { + // delete all the status page resource for this monitor. + + await StatusPageResourceService.deleteBy({ + query: { + monitorGroupId: new ObjectID(deleteBy.query._id as string), + }, + limit: LIMIT_MAX, + skip: 0, + props: { + isRoot: true, + }, + }); + } + + return { deleteBy, carryForward: null }; + } + public async getStatusTimeline( monitorGroupId: ObjectID, startDate: Date, diff --git a/Common/Server/Services/MonitorService.ts b/Common/Server/Services/MonitorService.ts index c8092ef9e1..9385a10064 100644 --- a/Common/Server/Services/MonitorService.ts +++ b/Common/Server/Services/MonitorService.ts @@ -21,7 +21,7 @@ import URL from "../../Types/API/URL"; import DatabaseCommonInteractionProps from "../../Types/BaseDatabase/DatabaseCommonInteractionProps"; import SortOrder from "../../Types/BaseDatabase/SortOrder"; import { PlanType } from "../../Types/Billing/SubscriptionPlan"; -import { LIMIT_PER_PROJECT } from "../../Types/Database/LimitMax"; +import LIMIT_MAX, { LIMIT_PER_PROJECT } from "../../Types/Database/LimitMax"; import BadDataException from "../../Types/Exception/BadDataException"; import { JSONObject } from "../../Types/JSON"; import MonitorType, { @@ -50,12 +50,35 @@ import { CallRequestMessage } from "../../Types/Call/CallRequest"; import UserNotificationSettingService from "./UserNotificationSettingService"; import NotificationSettingEventType from "../../Types/NotificationSetting/NotificationSettingEventType"; import Query from "../Types/Database/Query"; +import DeleteBy from "../Types/Database/DeleteBy"; +import StatusPageResourceService from "./StatusPageResourceService"; export class Service extends DatabaseService { public constructor() { super(Model); } + protected override async onBeforeDelete( + deleteBy: DeleteBy, + ): Promise> { + if (deleteBy.query._id) { + // delete all the status page resource for this monitor. + + await StatusPageResourceService.deleteBy({ + query: { + monitorId: new ObjectID(deleteBy.query._id as string), + }, + limit: LIMIT_MAX, + skip: 0, + props: { + isRoot: true, + }, + }); + } + + return { deleteBy, carryForward: null }; + } + protected override async onDeleteSuccess( onDelete: OnDelete, _itemIdsBeforeDelete: ObjectID[],