change all images fuctionality

This commit is contained in:
pawan-dot 2024-06-12 18:05:38 +05:30
parent e9b473a197
commit 56438a92aa
8 changed files with 336 additions and 159 deletions

7
.env
View File

@ -1,8 +1,8 @@
# DB_URL="mongodb+srv://smellica:Anjefef23dnsfjne@cluster0.c5gfqzm.mongodb.net/" DB_URL="mongodb+srv://pulseayur:rEF9gYORzKavLeiy@cluster0.vi4tl1l.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
# pulseayur Data Connection # pulseayur Data Connection
DB_URL="mongodb+srv://pulseayur:rEF9gYORzKavLeiy@cluster0.vi4tl1l.mongodb.net/" # DB_URL="mongodb+srv://pulseayur:rEF9gYORzKavLeiy@cluster0.vi4tl1l.mongodb.net/"
# DB_URL="mongodb://localhost:27017/smellica" # DB_URL="mongodb://localhost:27017/smellica"
PORT = 5000 PORT = 5000
@ -37,4 +37,5 @@ SMPT_PASSWORD="ND5sgVnWtrpUFfTb"
PAYPAL_CLIENT_ID="AemCjVuWswklp1sWUo4peCFg9eS4bofMsMR0RCrVRB2DifYR1IUSrWqtHpVmQlrVMKTI2cWZXLJAdYwn" PAYPAL_CLIENT_ID="AemCjVuWswklp1sWUo4peCFg9eS4bofMsMR0RCrVRB2DifYR1IUSrWqtHpVmQlrVMKTI2cWZXLJAdYwn"
PAYPAL_CLIENT_SECRET="EAo0Y9ff3jpHHg1QAbftdebfh7cb_-vnebhQrP9KALbCVer908yx2tO2eHO39r7EJSfqc4D69Qgx8R31" PAYPAL_CLIENT_SECRET="EAo0Y9ff3jpHHg1QAbftdebfh7cb_-vnebhQrP9KALbCVer908yx2tO2eHO39r7EJSfqc4D69Qgx8R31"
STRIPE_SECRET="sk_test_51OhPRdSG6gbAOwcEid1GavJ4FTD0ZuHVTferdvJwKal77RlMtFJGBzL5GjtL0ie8ZJztsGjUWi8DWrnw1pDdDRGS005Hk0ahql" STRIPE_SECRET="sk_test_51OhPRdSG6gbAOwcEid1GavJ4FTD0ZuHVTferdvJwKal77RlMtFJGBzL5GjtL0ie8ZJztsGjUWi8DWrnw1pDdDRGS005Hk0ahql"

View File

@ -2,7 +2,7 @@ import { PrivacyAndPolicy } from "./PrivacyPolicyModel.js";
import { Refundpolicy } from "./RefundModel.js"; import { Refundpolicy } from "./RefundModel.js";
import { Shipping } from "./ShippingModel.js"; import { Shipping } from "./ShippingModel.js";
import { TermsAndCondition } from "./TermsandConditonModel.js"; import { TermsAndCondition } from "./TermsandConditonModel.js";
import { AboutUs } from './AboutUsModel.js' import { AboutUs } from "./AboutUsModel.js";
export const AddTermsAndConditions = async (req, res) => { export const AddTermsAndConditions = async (req, res) => {
try { try {
@ -54,15 +54,14 @@ export const updateTermsAndConditions = async (req, res) => {
if (!req?.user) return res.status(400).json({ message: "please login !" }); if (!req?.user) return res.status(400).json({ message: "please login !" });
// new content // new content
const { content } = req.body; const { content } = req.body;
// id of the terms and conndition document // id of the terms and conndition document
const id = req.query.id; const id = req.query.id;
// object for updated terms and conndition data // object for updated terms and conndition data
const updatedTermsData = { const updatedTermsData = {
termsAndContionContent: content, termsAndContionContent: content,
addedBy: req.user._id addedBy: req.user._id,
} };
// update the terms and conndition in database // update the terms and conndition in database
const termsAndCondition = await TermsAndCondition.findByIdAndUpdate( const termsAndCondition = await TermsAndCondition.findByIdAndUpdate(
@ -138,8 +137,8 @@ export const updateRefundPolicy = async (req, res) => {
// object for updated refund policy data // object for updated refund policy data
const updatedRefundPolicyData = { const updatedRefundPolicyData = {
Refundpolicy: content, Refundpolicy: content,
addedBy: req.user._id addedBy: req.user._id,
} };
// update the refund policy in database // update the refund policy in database
const refundPolicy = await Refundpolicy.findByIdAndUpdate( const refundPolicy = await Refundpolicy.findByIdAndUpdate(
@ -161,7 +160,6 @@ export const updateRefundPolicy = async (req, res) => {
} }
}; };
// Privacy policy controller functions // Privacy policy controller functions
export const AddPrivacyAndPolicy = async (req, res) => { export const AddPrivacyAndPolicy = async (req, res) => {
@ -222,8 +220,8 @@ export const updatePrivacyPolicy = async (req, res) => {
// object for updated privacy policy data // object for updated privacy policy data
const updatedPrivacyPolicyData = { const updatedPrivacyPolicyData = {
privacyAndPolicyContent: content, privacyAndPolicyContent: content,
addedBy: req.user._id addedBy: req.user._id,
} };
// update the privacy policy in database // update the privacy policy in database
const privacyAndPolicy = await PrivacyAndPolicy.findByIdAndUpdate( const privacyAndPolicy = await PrivacyAndPolicy.findByIdAndUpdate(
@ -304,8 +302,8 @@ export const updateShipping = async (req, res) => {
// object for updated shipping policy data // object for updated shipping policy data
const updatedShippingData = { const updatedShippingData = {
shippingContent: content, shippingContent: content,
addedBy: req.user._id addedBy: req.user._id,
} };
// update the shipping policy in database // update the shipping policy in database
const shipping = await Shipping.findByIdAndUpdate( const shipping = await Shipping.findByIdAndUpdate(
@ -387,8 +385,8 @@ export const updateAboutUs = async (req, res) => {
// object for updated about us data // object for updated about us data
const updatedAboutUsData = { const updatedAboutUsData = {
aboutUsContent: content, aboutUsContent: content,
addedBy: req.user._id addedBy: req.user._id,
} };
// update the about us in database // update the about us in database
const aboutUs = await AboutUs.findByIdAndUpdate( const aboutUs = await AboutUs.findByIdAndUpdate(
@ -409,5 +407,3 @@ export const updateAboutUs = async (req, res) => {
}); });
} }
}; };

View File

@ -5,60 +5,60 @@ import { LoginImageModel } from "./LoginImageModel.js";
// Add new Category // Add new Category
export const addImage = async (req, res) => { export const addImage = async (req, res) => {
// const { bannerName } = req.body; // const { bannerName } = req.body;
const { bannerImage } = req.files; const { bannerImage } = req.files;
// console.log("image", bannerImage); // console.log("image", bannerImage);
// console.log(categoryName, categoryImage); // console.log(categoryName, categoryImage);
if (!req?.user) return res.status(400).json({ message: "please login !" }); if (!req?.user) return res.status(400).json({ message: "please login !" });
try { try {
if (!mongoose.Types.ObjectId.isValid(req.user._id)) { if (!mongoose.Types.ObjectId.isValid(req.user._id)) {
return res.status(400).json({ message: "please login again " }); return res.status(400).json({ message: "please login again " });
}
const result = await cloudinary.v2.uploader.upload(
bannerImage.tempFilePath,
{
folder: "GetSygnal/loginImage",
}
);
if (result) {
const regsiterImage = await LoginImageModel.create({
image: result,
addedBy: req.user._id,
});
if (regsiterImage) {
return res
.status(201)
.json({ success: true, regsiterImage, message: "Image Added" });
}
}
} catch (error) {
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went Wrong",
});
} }
const result = await cloudinary.v2.uploader.upload(
bannerImage.tempFilePath,
{
folder: "Ayurpulse/loginImage",
}
);
if (result) {
const regsiterImage = await LoginImageModel.create({
image: result,
addedBy: req.user._id,
});
if (regsiterImage) {
return res
.status(201)
.json({ success: true, regsiterImage, message: "Image Added" });
}
}
} catch (error) {
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went Wrong",
});
}
}; };
export const getImage = async (req, res) => { export const getImage = async (req, res) => {
try { try {
// if (!req?.user) return res.status(400).json({ message: "please login !" }); // if (!req?.user) return res.status(400).json({ message: "please login !" });
const image = await LoginImageModel.find().sort({ const image = await LoginImageModel.find().sort({
createdAt: -1, createdAt: -1,
}); });
if (!image) { if (!image) {
return res.status(404).json({ message: "No categories found" }); return res.status(404).json({ message: "No categories found" });
}
// console.log("image", image);
res.status(200).json({ success: true, image });
} catch (error) {
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went wrong",
});
} }
// console.log("image", image);
res.status(200).json({ success: true, image });
} catch (error) {
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went wrong",
});
}
}; };
// export const updateImage = async (req, res) => { // export const updateImage = async (req, res) => {
// try { // try {
@ -84,7 +84,7 @@ export const getImage = async (req, res) => {
// const result = await cloudinary.v2.uploader.upload( // const result = await cloudinary.v2.uploader.upload(
// bannerImag.tempFilePath, // bannerImag.tempFilePath,
// { // {
// folder: "GetSygnal/loginImage", // folder: "Ayurpulse/loginImage",
// } // }
// ); // );
@ -124,98 +124,144 @@ export const getImage = async (req, res) => {
// }; // };
export const updateImage = async (req, res) => { export const updateImage = async (req, res) => {
try { try {
if (!req?.user) return res.status(400).json({ message: "please login !" }); if (!req?.user) return res.status(400).json({ message: "please login !" });
const { _id } = req.params; const { _id } = req.params;
const { bannerName } = req.body; const olderImage = req.body?.olderImage;
const olderImage = req.body?.olderImage; const bannerImag = req.files?.bannerImage;
const bannerImag = req.files?.bannerImage;
// console.log("bannerImag", bannerImag);
if (!mongoose.Types.ObjectId.isValid(_id)) { if (!mongoose.Types.ObjectId.isValid(_id)) {
return res.status(404).json({ error: "Can not find the document " }); return res.status(404).json({ error: "Can not find the document " });
}
// console.log(JSON.parse(olderImage).length);
// find the document with the id to delete the image from cloudinary
if (olderImage) {
const deletefromCloudinary = await LoginImageModel.findOne({ _id: _id });
// console.log("deletefromCloudinary", deletefromCloudinary)
const deleteresponse = await cloudinary.v2.uploader.destroy(
deletefromCloudinary.image.public_id
);
if (deleteresponse) {
const result = await cloudinary.v2.uploader.upload(
bannerImag.tempFilePath,
{
folder: "GetSygnal/loginImage",
}
);
console.log("result", result);
const update = await LoginImageModel.findOneAndUpdate(
{ _id: _id },
{ image: result }, // Provide the updated categoryName
{ new: true } // To return the updated document
);
if (!update) {
return res
.status(404)
.json({ message: "Can not update document, something went wrong" });
} else {
return res.status(200).json({ success: true, update });
}
}
} else {
const update = await LoginImageModel.findOneAndUpdate(
{ _id: _id },
{ image: JSON.parse(olderImage) }, // Provide the updated categoryName
{ new: true } // To return the updated document
);
if (update) {
return res.status(200).json({ success: true, update });
}
}
} catch (error) {
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went wrong",
});
} }
if (olderImage) {
const deletefromCloudinary = await LoginImageModel.findOne({ _id: _id });
const deleteresponse = await cloudinary.v2.uploader.destroy(
deletefromCloudinary.image.public_id
);
if (deleteresponse) {
const result = await cloudinary.v2.uploader.upload(
bannerImag.tempFilePath,
{
folder: "Ayurpulse/loginImage",
}
);
const update = await LoginImageModel.findOneAndUpdate(
{ _id: _id },
{ image: result }, // Provide the updated categoryName
{ new: true } // To return the updated document
);
if (!update) {
return res
.status(404)
.json({ message: "Can not update document, something went wrong" });
} else {
return res.status(200).json({ success: true, update });
}
}
} else {
const update = await LoginImageModel.findOneAndUpdate(
{ _id: _id },
{ image: JSON.parse(olderImage) }, // Provide the updated categoryName
{ new: true } // To return the updated document
);
if (update) {
return res.status(200).json({ success: true, update });
}
}
} catch (error) {
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went wrong",
});
}
}; };
export const deleteImage = async (req, res) => { export const deleteImage = async (req, res) => {
try { try {
if (!req?.user) return res.status(400).json({ message: "please login !" }); if (!req?.user) return res.status(400).json({ message: "please login !" });
const { _id } = req.params; const { _id } = req.params;
if (!mongoose.Types.ObjectId.isValid(_id)) { if (!mongoose.Types.ObjectId.isValid(_id)) {
return res.status(404).json({ error: "Can not find the document " }); return res.status(404).json({ error: "Can not find the document " });
}
const deletefromCloudinary = await LoginImageModel.findOne({ _id: _id });
// console.log(deletefromCloudinary);
const deleteresponse = await cloudinary.v2.uploader.destroy(
deletefromCloudinary.image.public_id
);
if (deleteresponse) {
const deleteBanner = await LoginImageModel.findOneAndDelete({ _id: _id });
if (!deleteBanner) {
return res.status(404).json({
error: "Can not find the document with the provided id to delete ",
});
}
res.status(200).json({ success: true, deleteBanner });
} else {
return res.status(404).json({ error: "can not delete the banner " });
}
} catch (error) {
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went wrong",
});
} }
const deletefromCloudinary = await LoginImageModel.findOne({ _id: _id });
// console.log(deletefromCloudinary);
const deleteresponse = await cloudinary.v2.uploader.destroy(
deletefromCloudinary.image.public_id
);
if (deleteresponse) {
const deleteBanner = await LoginImageModel.findOneAndDelete({ _id: _id });
if (!deleteBanner) {
return res.status(404).json({
error: "Can not find the document with the provided id to delete ",
});
}
res.status(200).json({ success: true, deleteBanner });
} else {
return res.status(404).json({ error: "can not delete the banner " });
}
} catch (error) {
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went wrong",
});
}
};
export const CreateAndUpdateImage = async (req, res) => {
try {
const LoginImag = req.files?.LoginImg;
const findImage = await LoginImageModel.find();
if (findImage?.length > 0) {
const deleteImg = await cloudinary.v2.uploader.destroy(
findImage[0]?.image.public_id
);
if (deleteImg) {
const result = await cloudinary.v2.uploader.upload(
LoginImag.tempFilePath,
{
folder: "Ayurpulse/loginImage",
}
);
// console.log("result", result);
const update = await LoginImageModel.findOneAndUpdate(
{ _id: findImage[0]?._id },
{ image: result }, // Provide the updated categoryName
{ new: true } // To return the updated document
);
if (update)
return res
.status(200)
.json({ message: "Login Image Update successfully" });
}
} else {
const result = await cloudinary.v2.uploader.upload(
LoginImag.tempFilePath,
{
folder: "Ayurpulse/loginImage",
}
);
if (result) {
const regsiterImage = await LoginImageModel.create({
image: result,
addedBy: req.user._id,
});
if (regsiterImage) {
return res.status(201).json({
success: true,
regsiterImage,
message: " Login Image Added successfully",
});
}
}
}
} catch (error) {
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went wrong",
});
}
}; };

View File

@ -3,6 +3,7 @@ import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
// import { addImage, deleteImage, getImage, updateImage } from "./RegistrationImageController.js"; // import { addImage, deleteImage, getImage, updateImage } from "./RegistrationImageController.js";
import { import {
CreateAndUpdateImage,
addImage, addImage,
deleteImage, deleteImage,
getImage, getImage,
@ -17,6 +18,13 @@ router.route("/getImage").get(getImage);
router router
.route("/update/:_id") .route("/update/:_id")
.patch(isAuthenticatedUser, authorizeRoles("admin", "Employee"), updateImage); .patch(isAuthenticatedUser, authorizeRoles("admin", "Employee"), updateImage);
router
.route("/addmodify")
.post(
isAuthenticatedUser,
authorizeRoles("admin", "Employee"),
CreateAndUpdateImage
);
router router
.route("/delete/:_id") .route("/delete/:_id")
.delete( .delete(

View File

@ -18,7 +18,7 @@ export const addImage = async (req, res) => {
const result = await cloudinary.v2.uploader.upload( const result = await cloudinary.v2.uploader.upload(
bannerImage.tempFilePath, bannerImage.tempFilePath,
{ {
folder: "GetSygnal/registrationImage", folder: "Register/registrationImage",
} }
); );
@ -84,7 +84,7 @@ export const getImage = async (req, res) => {
// const result = await cloudinary.v2.uploader.upload( // const result = await cloudinary.v2.uploader.upload(
// bannerImag.tempFilePath, // bannerImag.tempFilePath,
// { // {
// folder: "GetSygnal/registrationImage", // folder: "Register/registrationImage",
// } // }
// ); // );
@ -152,10 +152,10 @@ export const updateImage = async (req, res) => {
const result = await cloudinary.v2.uploader.upload( const result = await cloudinary.v2.uploader.upload(
bannerImag.tempFilePath, bannerImag.tempFilePath,
{ {
folder: "GetSygnal/registrationImage", folder: "Register/registrationImage",
} }
); );
console.log("result", result); // console.log("result", result);
const update = await RegistrationImageModel.findOneAndUpdate( const update = await RegistrationImageModel.findOneAndUpdate(
{ _id: _id }, { _id: _id },
{ image: result }, // Provide the updated categoryName { image: result }, // Provide the updated categoryName
@ -222,3 +222,58 @@ export const deleteImage = async (req, res) => {
}); });
} }
}; };
export const CreateAndUpdateImage = async (req, res) => {
try {
const RegisterImag = req.files?.RegisterImglogo;
const findImage = await RegistrationImageModel.find();
if (findImage?.length > 0) {
const deleteImg = await cloudinary.v2.uploader.destroy(
findImage[0]?.image.public_id
);
if (deleteImg) {
const result = await cloudinary.v2.uploader.upload(
RegisterImag.tempFilePath,
{
folder: "Ayurpulse/RegisterImage",
}
);
// console.log("result", result);
const update = await RegistrationImageModel.findOneAndUpdate(
{ _id: findImage[0]?._id },
{ image: result }, // Provide the updated categoryName
{ new: true } // To return the updated document
);
if (update)
return res
.status(200)
.json({ message: "Register Image Update successfully" });
}
} else {
const result = await cloudinary.v2.uploader.upload(
RegisterImag.tempFilePath,
{
folder: "Ayurpulse/RegisterImage",
}
);
if (result) {
const regsiterImage = await RegistrationImageModel.create({
image: result,
addedBy: req.user._id,
});
if (regsiterImage) {
return res.status(201).json({
success: true,
regsiterImage,
message: " Register Image Added successfully",
});
}
}
}
} catch (error) {
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went wrong",
});
}
};

View File

@ -6,6 +6,7 @@ import {
deleteImage, deleteImage,
getImage, getImage,
updateImage, updateImage,
CreateAndUpdateImage,
} from "./RegistrationImageController.js"; } from "./RegistrationImageController.js";
const router = express.Router(); const router = express.Router();
@ -16,6 +17,13 @@ router.route("/getImage").get(getImage);
router router
.route("/update/:_id") .route("/update/:_id")
.patch(isAuthenticatedUser, authorizeRoles("admin", "Employee"), updateImage); .patch(isAuthenticatedUser, authorizeRoles("admin", "Employee"), updateImage);
router
.route("/addmodify")
.post(
isAuthenticatedUser,
authorizeRoles("admin", "Employee"),
CreateAndUpdateImage
);
router router
.route("/delete/:_id") .route("/delete/:_id")
.delete( .delete(

View File

@ -155,7 +155,7 @@ export const updateImage = async (req, res) => {
folder: "GetSygnal/shopImage", folder: "GetSygnal/shopImage",
} }
); );
console.log("result", result); // console.log("result", result);
const update = await ShopPageImageModel.findOneAndUpdate( const update = await ShopPageImageModel.findOneAndUpdate(
{ _id: _id }, { _id: _id },
{ image: result }, // Provide the updated categoryName { image: result }, // Provide the updated categoryName
@ -220,3 +220,58 @@ export const deleteImage = async (req, res) => {
}); });
} }
}; };
export const CreateAndUpdateImage = async (req, res) => {
try {
const ShopImag = req.files?.ShopImg;
const findImage = await ShopPageImageModel.find();
if (findImage?.length > 0) {
const deleteImg = await cloudinary.v2.uploader.destroy(
findImage[0]?.image.public_id
);
if (deleteImg) {
const result = await cloudinary.v2.uploader.upload(
ShopImag.tempFilePath,
{
folder: "Ayurpulse/ShopImage",
}
);
// console.log("result", result);
const update = await ShopPageImageModel.findOneAndUpdate(
{ _id: findImage[0]?._id },
{ image: result }, // Provide the updated categoryName
{ new: true } // To return the updated document
);
if (update)
return res
.status(200)
.json({ message: "Shop Image Update successfully" });
}
} else {
const result = await cloudinary.v2.uploader.upload(
ShopImag.tempFilePath,
{
folder: "Ayurpulse/shopImage",
}
);
if (result) {
const shopImage = await ShopPageImageModel.create({
image: result,
addedBy: req.user._id,
});
if (shopImage) {
return res.status(201).json({
success: true,
shopImage,
message: " Shop Image Added successfully",
});
}
}
}
} catch (error) {
res.status(500).json({
success: false,
message: error.message ? error.message : "Something went wrong",
});
}
};

View File

@ -5,6 +5,7 @@ import {
deleteImage, deleteImage,
getImage, getImage,
updateImage, updateImage,
CreateAndUpdateImage,
} from "./ShopPageImageController.js"; } from "./ShopPageImageController.js";
// import { addImage, deleteImage, getImage, updateImage } from "./RegistrationImageController.js"; // import { addImage, deleteImage, getImage, updateImage } from "./RegistrationImageController.js";
@ -18,6 +19,13 @@ router.route("/getImage").get(getImage);
router router
.route("/update/:_id") .route("/update/:_id")
.patch(isAuthenticatedUser, authorizeRoles("admin", "Employee"), updateImage); .patch(isAuthenticatedUser, authorizeRoles("admin", "Employee"), updateImage);
router
.route("/addmodify")
.post(
isAuthenticatedUser,
authorizeRoles("admin", "Employee"),
CreateAndUpdateImage
);
router router
.route("/delete/:_id") .route("/delete/:_id")
.delete( .delete(