diff --git a/backend/app/routes/contactUs/delete.js b/backend/app/routes/contactUs/delete.js new file mode 100644 index 00000000..b00df2ba --- /dev/null +++ b/backend/app/routes/contactUs/delete.js @@ -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(); +}; \ No newline at end of file diff --git a/backend/app/routes/contactUs/index.js b/backend/app/routes/contactUs/index.js index 60701905..25c8d7c0 100644 --- a/backend/app/routes/contactUs/index.js +++ b/backend/app/routes/contactUs/index.js @@ -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; diff --git a/backend/app/routes/index.js b/backend/app/routes/index.js index 1a209920..aa25dd28 100644 --- a/backend/app/routes/index.js +++ b/backend/app/routes/index.js @@ -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'); @@ -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);