delete functionality working
This commit is contained in:
1 parent
423faf3b08
commit
af0ec810c9
3 files changed
+10
-10
No files matched your search
@@ -7,7 +7,7 @@ const {
|
|||||||
recoveryValidation,
|
recoveryValidation,
|
||||||
recoveryTokenValidation,
|
recoveryTokenValidation,
|
||||||
newPasswordValidation,
|
newPasswordValidation,
|
||||||
userDeleteValidation
|
deleteUserParamValidation
|
||||||
} = require("../validation/joi");
|
} = require("../validation/joi");
|
||||||
const logger = require("../utils/logger");
|
const logger = require("../utils/logger");
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
@@ -248,16 +248,16 @@ const resetPasswordController = async (req, res, next) => {
|
|||||||
const deleteUserController = async (req, res, next) => {
|
const deleteUserController = async (req, res, next) => {
|
||||||
try {
|
try {
|
||||||
// Validate user
|
// Validate user
|
||||||
await userDeleteValidation.validateAsync(req.params.userId);
|
await deleteUserParamValidation.validateAsync(req.body);
|
||||||
|
|
||||||
// Check if the user exists
|
// Check if the user exists
|
||||||
const user = await req.db.getUserById(req.params.userId);
|
const user = await req.db.getUserByEmail(req);
|
||||||
if (!user) {
|
if (!user) {
|
||||||
throw new Error(errorMessages.DB_USER_NOT_FOUND);
|
throw new Error(errorMessages.DB_USER_NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1. Find all the monitors associated with the user id
|
// 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);
|
||||||
|
|
||||||
// 2. Delete jobs associated with each monitor
|
// 2. Delete jobs associated with each monitor
|
||||||
for (const monitor of monitors) {
|
for (const monitor of monitors) {
|
||||||
@@ -275,10 +275,10 @@ const deleteUserController = async (req, res, next) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 5. Delete each monitor
|
// 5. Delete each monitor
|
||||||
await req.db.deleteMonitorsByUserId(req.params.userId);
|
await req.db.deleteMonitorsByUserId(user._id);
|
||||||
|
|
||||||
// 6. Delete the user by id
|
// 6. Delete the user by id
|
||||||
await req.db.deleteUserById(req.params.userId);
|
await req.db.deleteUser(req);
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ 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);
|
router.delete("/user/:userId", deleteUserController);
|
||||||
|
|
||||||
//Recovery routes
|
//Recovery routes
|
||||||
router.post("/recovery/request", recoveryRequestController);
|
router.post("/recovery/request", recoveryRequestController);
|
||||||
|
|||||||
@@ -43,8 +43,8 @@ const newPasswordValidation = joi.object({
|
|||||||
confirm: joi.string(),
|
confirm: joi.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
const userDeleteValidation = joi.object({
|
const deleteUserParamValidation = joi.object({
|
||||||
userId: joi.string().required(),
|
email: joi.string().email().required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
//****************************************
|
//****************************************
|
||||||
@@ -162,5 +162,5 @@ module.exports = {
|
|||||||
createCheckBodyValidation,
|
createCheckBodyValidation,
|
||||||
getChecksParamValidation,
|
getChecksParamValidation,
|
||||||
deleteChecksParamValidation,
|
deleteChecksParamValidation,
|
||||||
userDeleteValidation
|
deleteUserParamValidation
|
||||||
};
|
};
|
||||||
Reference in new issue
Block a user