modifications

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

No files matched your search

+26 -10
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) => { const deleteUserController = async (req, res, next) => {
try { try {
// Validate user // Validate user
@@ -247,22 +256,29 @@ const deleteUserController = async (req, res, next) => {
throw new Error(errorMessages.DB_USER_NOT_FOUND); 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); const monitors = await req.db.getMonitorsByUserId(req.params.userId);
// Step 2: For each monitor, delete all associated checks and alerts // 2. Delete jobs associated with each monitor
if (monitors) { for (const monitor of monitors) {
for (const monitor of monitors) { await req.jobQueue.deleteJob(monitor);
await req.db.deleteChecks(monitor.id);
await req.db.deleteAlertByMonitorId(monitor.id);
}
} }
// 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); await req.db.deleteMonitorsByUserId(req.params.userId);
// Step 4: Finally, delete the user // 6. Delete the user by id
await req.db.deleteUser(req.params.userId); await req.db.deleteUserById(req.params.userId);
return res.status(200).json({ return res.status(200).json({
success: true, success: true,
+8 -1
View File
@@ -396,7 +396,7 @@ const getChecks = async (monitorId) => {
const deleteChecks = async (monitorId) => { const deleteChecks = async (monitorId) => {
try { try {
const result = await Check.deleteMany({ monitorId }); const result = await Check.deleteMany({ monitorId });
return result; return result.deletedCount;
} catch (error) { } catch (error) {
throw 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) => { const deleteAlertByMonitorId = async (monitorId) => {
try { try {
const result = await Alert.deleteMany({ monitorId }); const result = await Alert.deleteMany({ monitorId });
+1 -3
View File
@@ -15,13 +15,11 @@ const {
router.post("/register", registerController); router.post("/register", registerController);
router.post("/login", loginController); router.post("/login", loginController);
router.post("/user/:userId", verifyJWT, userEditController); router.post("/user/:userId", verifyJWT, userEditController);
router.delete("/delete/:userId", deleteUserController);
//Recovery routes //Recovery routes
router.post("/recovery/request", recoveryRequestController); router.post("/recovery/request", recoveryRequestController);
router.post("/recovery/validate", validateRecoveryTokenController); router.post("/recovery/validate", validateRecoveryTokenController);
router.post("/recovery/reset/", resetPasswordController); router.post("/recovery/reset/", resetPasswordController);
//Delete routes
router.delete("/delete/:userId", deleteUserController);
module.exports = router; module.exports = router;