From 3908345344a2ad71fcf9f415b0166248168ce564 Mon Sep 17 00:00:00 2001 From: Bheru Singh Panwar <92213232+EAGLEARCHER@users.noreply.github.com> Date: Sat, 18 May 2024 22:22:07 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=8D=80=20[Backend]=20delete=20api=20for?= =?UTF-8?q?=20contactUs=20detail=20created=20(#926)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/routes/contactUs/delete.js | 55 ++++++++++++++++++++++++++ backend/app/routes/contactUs/index.js | 2 + backend/app/routes/index.js | 2 + 3 files changed, 59 insertions(+) create mode 100644 backend/app/routes/contactUs/delete.js 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);