modifications

This commit is contained in:
MuhammadKhalilzadeh committed 2024-06-29 11:58:27 +03:30
1 parent 704980045d
commit 81cd302acc
3 files changed
+38 -17

No files matched your search

+29 -13
View File
@@ -236,6 +236,15 @@ const resetPasswordController = async (req, res, next) => {
}
};
/**
* Deletes a user and all associated monitors, checks, and alerts.
*
* @param {Object} req - The request object.
* @param {Object} res - The response object.
* @param {Function} next - The next middleware function.
* @returns {Object} The response object with success status and message.
* @throws {Error} If user validation fails or user is not found in the database.
*/
const deleteUserController = async (req, res, next) => {
try {
// Validate user
@@ -247,22 +256,29 @@ const deleteUserController = async (req, res, next) => {
throw new Error(errorMessages.DB_USER_NOT_FOUND);
}
// Step 1: Find all monitors associated with the user
// 1. Find all the monitors associated with the user id
const monitors = await req.db.getMonitorsByUserId(req.params.userId);
// Step 2: For each monitor, delete all associated checks and alerts
if (monitors) {
for (const monitor of monitors) {
await req.db.deleteChecks(monitor.id);
await req.db.deleteAlertByMonitorId(monitor.id);
}
// 2. Delete jobs associated with each monitor
for (const monitor of monitors) {
await req.jobQueue.deleteJob(monitor);
}
// Step 3: Delete all monitors associated with the user
// 3. Delete all checks associated with each monitor
for (const monitor of monitors) {
await req.db.deleteChecks(monitor._id);
}
// 4. Delete all alerts associated with each monitor
for (const monitor of monitors) {
await req.db.deleteAlertByMonitorId(monitor._id);
}
// 5. Delete each monitor
await req.db.deleteMonitorsByUserId(req.params.userId);
// Step 4: Finally, delete the user
await req.db.deleteUser(req.params.userId);
// 6. Delete the user by id
await req.db.deleteUserById(req.params.userId);
return res.status(200).json({
success: true,
+8 -1
View File
@@ -396,7 +396,7 @@ const getChecks = async (monitorId) => {
const deleteChecks = async (monitorId) => {
try {
const result = await Check.deleteMany({ monitorId });
return result;
return result.deletedCount;
} catch (error) {
throw error;
}
@@ -519,6 +519,13 @@ const deleteAlert = async (alertId) => {
}
};
/**
* Deletes alerts by monitor ID.
*
* @param {string} monitorId - The ID of the monitor.
* @returns {Promise} A promise that resolves to the result of the delete operation.
* @throws {Error} If an error occurs while deleting the alerts.
*/
const deleteAlertByMonitorId = async (monitorId) => {
try {
const result = await Alert.deleteMany({ monitorId });
+1 -3
View File
@@ -15,13 +15,11 @@ const {
router.post("/register", registerController);
router.post("/login", loginController);
router.post("/user/:userId", verifyJWT, userEditController);
router.delete("/delete/:userId", deleteUserController);
//Recovery routes
router.post("/recovery/request", recoveryRequestController);
router.post("/recovery/validate", validateRecoveryTokenController);
router.post("/recovery/reset/", resetPasswordController);
//Delete routes
router.delete("/delete/:userId", deleteUserController);
module.exports = router;