delete all

This commit is contained in:
Alex Holliday committed 2025-11-26 23:57:40 +00:00
1 parent 6c04f46e8a
commit ee04ac2b98
7 files changed
+115 -8

No files matched your search

@@ -15,6 +15,11 @@ export interface IMonitorController {
togglePause(req: Request, res: Response, next: NextFunction): Promise<void>;
update(req: Request, res: Response, next: NextFunction): Promise<void>;
delete(req: Request, res: Response, next: NextFunction): Promise<void>;
deleteAllInOrg(
req: Request,
res: Response,
next: NextFunction
): Promise<void>;
testNotifications(
req: Request,
res: Response,
@@ -287,6 +292,28 @@ class MonitorController {
}
};
deleteAllInOrg = async (req: Request, res: Response, next: NextFunction) => {
try {
const tokenizedUser = req.user;
if (!tokenizedUser) {
return res.status(401).json({ message: "Unauthorized" });
}
const orgId = tokenizedUser.orgId;
if (!orgId) {
throw new ApiError("No organization ID", 400);
}
await this.monitorService.deleteAllInOrg(orgId);
res.status(200).json({
message: "All monitors in organization deleted successfully",
});
} catch (error) {
next(error);
}
};
testNotifications = async (
req: Request,
res: Response,
+11 -1
View File
@@ -1,7 +1,10 @@
import { Router } from "express";
import { MonitorController } from "@/controllers/index.js";
import { verifyToken } from "@/middleware/VerifyToken.js";
import { verifyTeamPermission } from "@/middleware/VerifyPermission.js";
import {
verifyOrgPermission,
verifyTeamPermission,
} from "@/middleware/VerifyPermission.js";
import { addUserContext } from "@/middleware/AddUserContext.js";
import { PERMISSIONS } from "@/services/business/AuthService.js";
import { validateBody, validateQuery } from "@/middleware/validation.js";
@@ -109,6 +112,13 @@ class MonitorRoutes {
verifyTeamPermission([PERMISSIONS.monitors.delete]),
this.controller.delete
);
this.router.delete(
"/",
verifyToken,
addUserContext,
verifyOrgPermission([PERMISSIONS.monitors.delete]),
this.controller.deleteAllInOrg
);
};
getRouter() {
@@ -59,6 +59,7 @@ export interface IMonitorService {
updateData: Partial<IMonitor>
) => Promise<IMonitor>;
delete: (teamId: string, monitorId: string) => Promise<boolean>;
deleteAllInOrg: (orgId: string) => Promise<boolean>;
export: (teamId: string) => Promise<Array<Record<string, unknown>>>;
import: (
orgId: string,
@@ -665,6 +666,15 @@ class MonitorService implements IMonitorService {
return true;
}
async deleteAllInOrg(orgId: string) {
const montiors = await Monitor.find({ orgId });
for (const monitor of montiors) {
await this.jobQueue.deleteJob(monitor);
}
const deleted = await Monitor.deleteMany({ orgId });
return deleted.acknowledged;
}
export = async (teamId: string) => {
return Monitor.find({ teamId })
.select("name url port type interval n secret -_id")
+1 -1
View File
@@ -45,7 +45,7 @@ export const inviteSchema = z.object({
});
const urlRegex =
/^(https?:\/\/)?(localhost|[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}|(\d{1,3}\.){3}\d{1,3})(:\d{1,5})?(\/.*)?$/;
/^(https?:\/\/)?(localhost|([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}|[a-zA-Z0-9-]+|(\d{1,3}\.){3}\d{1,3})(:\d{1,5})?(\/.*)?$/;
export const monitorSchema = z
.object({