Update delete all method

This commit is contained in:
Alex Holliday committed 2024-09-11 11:22:33 +08:00
1 parent a89777333a
commit 3f5dc337a0
3 files changed
+18 -7

No files matched your search

+12 -3
View File
@@ -368,10 +368,20 @@ const deleteMonitor = async (req, res, next) => {
const deleteAllMonitors = async (req, res) => {
try {
const deleteCount = await req.db.deleteAllMonitors();
const token = getTokenFromHeaders(req.headers);
const { teamId } = jwt.verify(token, process.env.JWT_SECRET);
const { monitors, deletedCount } = await req.db.deleteAllMonitors(teamId);
await monitors.forEach(async (monitor) => {
await req.jobQueue.deleteJob(monitor);
await req.db.deleteChecks(monitor._id);
await req.db.deleteAlertByMonitorId(monitor._id);
await req.db.deletePageSpeedChecksByMonitorId(monitor._id);
await req.db.deleteNotificationsByMonitorId(monitor._id);
});
return res
.status(200)
.json({ success: true, msg: `Deleted ${deleteCount} monitors` });
.json({ success: true, msg: `Deleted ${deletedCount} monitors` });
} catch (error) {
error.service = SERVICE_NAME;
next(error);
@@ -472,7 +482,6 @@ const pauseMonitor = async (req, res, next) => {
const addDemoMonitors = async (req, res, next) => {
try {
const token = getTokenFromHeaders(req.headers);
// Get email from token
const { _id, teamId } = jwt.verify(token, process.env.JWT_SECRET);
const demoMonitors = await req.db.addDemoMonitors(_id, teamId);
await demoMonitors.forEach(async (monitor) => {
+5 -3
View File
@@ -451,10 +451,12 @@ const deleteMonitor = async (req, res) => {
* DELETE ALL MONITORS (TEMP)
*/
const deleteAllMonitors = async (req, res) => {
const deleteAllMonitors = async (teamId) => {
try {
const deletedCount = await Monitor.deleteMany({});
return deletedCount.deletedCount;
const monitors = await Monitor.find({ teamId });
const { deletedCount } = await Monitor.deleteMany({ teamId });
return { monitors, deletedCount };
} catch (error) {
throw error;
}
+1 -1
View File
@@ -32,7 +32,7 @@ router.put(
);
router.delete(
"/all",
"/",
isAllowed(["superadmin"]),
monitorController.deleteAllMonitors
);