diff --git a/resources/Content/ContentController.js b/resources/Content/ContentController.js index f7500ff..b366441 100644 --- a/resources/Content/ContentController.js +++ b/resources/Content/ContentController.js @@ -51,16 +51,24 @@ export const getTermsAndCondition = async (req, res) => { export const updateTermsAndConditions = async (req, res) => { try { if (!req?.user) return res.status(400).json({ message: "please login !" }); - // console.log(req?.user) - const { content } = req.body; - const termsAndCondition = await TermsAndCondition.findOneAndUpdate( - { - addedBy: req.user._id, - }, - { - termsAndContionContent: content, - } - ); + // new content + const { content } = req.body; + + // id of the terms and conndition document + const id = req.query.id; + + // object for updated terms and conndition data + const updatedTermsData = { + termsAndContionContent: content, + addedBy: req.user._id + } + + // update the terms and conndition in database + const termsAndCondition = await TermsAndCondition.findByIdAndUpdate( + { _id: id }, + { $set: updatedTermsData }, + { new: true } + ); res.status(200).json({ success: true, @@ -80,23 +88,10 @@ export const RefundPolicy = async (req, res) => { if (!req?.user) return res.status(400).json({ message: "please login !" }); // console.log(req?.user) const { content } = req.body; - const findv = await Refundpolicy.findOne(); - let refundPolicy; - if (findv) { - refundPolicy = await Refundpolicy.findOneAndUpdate( - { - addedBy: req.user._id, - }, - { - Refundpolicy: content, - } - ); - } else { - refundPolicy = await Refundpolicy.create({ + const refundPolicy = await Refundpolicy.create({ addedBy: req.user._id, Refundpolicy: content, }); - } res.status(200).json({ success: true, @@ -130,6 +125,42 @@ export const getRefundPolicy = async (req, res) => { } }; +// update refund policy +export const updateRefundPolicy = async (req, res) => { + try { + if (!req?.user) return res.status(400).json({ message: "please login !" }); + + const {content} = req.body; + // id of the refund policy document + const id = req.query.id; + + // object for updated refund policy data + const updatedRefundPolicyData = { + Refundpolicy: content, + addedBy: req.user._id + } + + // update the refund policy in database + const refundPolicy = await Refundpolicy.findByIdAndUpdate( + { _id: id }, + { $set: updatedRefundPolicyData }, + { new: true } + ); + + res.status(200).json({ + success: true, + refundPolicy, + message: "updated successfully ", + }); + } catch (error) { + res.status(500).json({ + success: false, + message: error.message ? error.message : "Something went Wrong", + }); + } +}; + + // Privacy policy controller functions export const AddPrivacyAndPolicy = async (req, res) => { @@ -180,15 +211,24 @@ export const getPrivacyPolicy = async (req, res) => { export const updatePrivacyPolicy = async (req, res) => { try { if (!req?.user) return res.status(400).json({ message: "please login !" }); - // console.log(req?.user) + + // new content const { content } = req.body; - const privacyAndPolicy = await PrivacyAndPolicy.findOneAndUpdate( - { - addedBy: req.user._id, - }, - { - privacyAndPolicyContent: content, - } + + // id of the privacy policy document + const id = req.query.id; + + // object for updated privacy policy data + const updatedPrivacyPolicyData = { + privacyAndPolicyContent: content, + addedBy: req.user._id + } + + // update the privacy policy in database + const privacyAndPolicy = await PrivacyAndPolicy.findByIdAndUpdate( + { _id: id }, + { $set: updatedPrivacyPolicyData }, + { new: true } ); res.status(200).json({ @@ -254,15 +294,23 @@ export const getShipping = async (req, res) => { export const updateShipping = async (req, res) => { try { if (!req?.user) return res.status(400).json({ message: "please login !" }); - // console.log(req?.user) + // new content const { content } = req.body; - const shipping = await Shipping.findOneAndUpdate( - { - addedBy: req.user._id, - }, - { - shippingContent: content, - } + + // id of the shipping policy document + const id = req.query.id; + + // object for updated shipping policy data + const updatedShippingData = { + shippingContent: content, + addedBy: req.user._id + } + + // update the shipping policy in database + const shipping = await Shipping.findByIdAndUpdate( + { _id: id }, + { $set: updatedShippingData }, + { new: true } ); res.status(200).json({ diff --git a/resources/Content/ContentRoutes.js b/resources/Content/ContentRoutes.js index 907dafe..eddc29a 100644 --- a/resources/Content/ContentRoutes.js +++ b/resources/Content/ContentRoutes.js @@ -11,6 +11,7 @@ import { updatePrivacyPolicy, updateShipping, updateTermsAndConditions, + updateRefundPolicy } from "./ContentController.js"; import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js"; @@ -46,7 +47,10 @@ router router.route("/refund-policy").get(getRefundPolicy); router .route("/refund-policy") - .patch(isAuthenticatedUser, authorizeRoles("admin"), RefundPolicy); + .post(isAuthenticatedUser, authorizeRoles("admin"), RefundPolicy); +router + .route("/refund-policy-update") + .patch(isAuthenticatedUser, authorizeRoles("admin"), updateRefundPolicy); // export default router;