Skip to content

Commit

Permalink
πŸ€ [Backend] delete api for contactUs detail created (#926)
Browse files Browse the repository at this point in the history
  • Loading branch information
BheruSinghPanwar authored May 18, 2024
1 parent eaa5048 commit 3908345
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
55 changes: 55 additions & 0 deletions backend/app/routes/contactUs/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const to = require('await-to-js').default;
const { ErrorHandler } = require('../../../helpers/error');
const constants = require('../../../constants');
const Contact = require("../../models/contactUs");
const Admin = require("../../models/Admin");

// Controller to delete a contact by adminid and contactdocumentid
module.exports = async (req, res, next) => {
const { contactId, adminId } = req.body;

// Check if contactId is provided
if (!contactId || !adminId) {
const error = new ErrorHandler(constants.ERRORS.VALIDATION, {
statusCode: 400,
message: 'Validation Error',
errStack: 'Both IDs are required to delete a contact',
});
return next(error);
}
//Find if the user is admin or not
const admin = await to(Admin.findOne({ _id: adminId }));
if (!admin) {
const error = new ErrorHandler(constants.ERRORS.USER, {
statusCode: 400,
message: "Admin Validation Error",
errStack: "Admin user provided not found in database"
})
return next(error);
}
// Delete the contact
const [err, result] = await to(Contact.findByIdAndDelete(contactId));

if (err) {
const error = new ErrorHandler(constants.ERRORS.DATABASE, {
statusCode: 500,
message: 'Database Error',
errStack: err,
});
return next(error);
}

if (!result) {
const error = new ErrorHandler(constants.ERRORS.NOT_FOUND, {
statusCode: 404,
message: 'Contact Not Found',
});
return next(error);
}

res.status(200).send({
message: 'Contact deleted successfully',
});

return next();
};
2 changes: 2 additions & 0 deletions backend/app/routes/contactUs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ const contactValidationSchema = require('./@validationSchema');
const validation = require('../../../helpers/middlewares/validation');
const postContact = require('./post');
const getContact = require('./get');
const deleteContactUs = require("./delete");
const { authMiddleware } = require('../../../helpers/middlewares/auth');

router.get('/getcontactus', authMiddleware, getContact);
router.post('/contactus', validation(contactValidationSchema), postContact);
router.delete("/deleteContactUs", deleteContactUs);

module.exports = router;
2 changes: 2 additions & 0 deletions backend/app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const deleteFaq = require('./faq/deleteFaq');
const updateFaq = require('./faq/updateFaq')
const joinUs = require('./joinUs');
const contactUs = require('./contactUs/post');
const deleteContactUs = require("./contactUs/delete");

const getContactUs = require('./contactUs/get');
const contactValidationSchema = require('./contactUs/@validationSchema');
Expand All @@ -29,6 +30,7 @@ router.use('/updateFaq',updateFaq)
router.use('/getFaq', getFaq);
router.use('/contactus', validation(contactValidationSchema), contactUs);
router.use('/getcontactus', getContactUs);
router.use("/deleteContactUs", deleteContactUs);
router.use('/broadcast', broadcast);
router.use('/question', question);
router.use('/answers', answer);
Expand Down

0 comments on commit 3908345

Please sign in to comment.