From bd1a54be8f950f2afa71bd508e976774de20e7e9 Mon Sep 17 00:00:00 2001 From: Sibunnayak Date: Fri, 7 Feb 2025 12:32:36 +0530 Subject: [PATCH] image for orders --- resources/Brands/BrandsController.js | 78 ++++++++++++------------- resources/RD_Ordes/invoiceModalRD.js | 6 ++ resources/RD_Ordes/rdOrderController.js | 5 +- 3 files changed, 46 insertions(+), 43 deletions(-) diff --git a/resources/Brands/BrandsController.js b/resources/Brands/BrandsController.js index 6c4b5ae..52e8651 100644 --- a/resources/Brands/BrandsController.js +++ b/resources/Brands/BrandsController.js @@ -209,9 +209,9 @@ export const updateBrand = async (req, res) => { let image = brand.image; if (file) { - if (image.length > 0) { - await cloudinary.v2.uploader.destroy(image[0].public_id); - } + // if (image.length > 0) { + // await cloudinary.v2.uploader.destroy(image[0].public_id); + // } const result = await cloudinary.v2.uploader.upload(file.tempFilePath, { folder: "chemiNova/brand", }); @@ -224,20 +224,16 @@ export const updateBrand = async (req, res) => { { new: true, runValidators: true } ); - res - .status(200) - .json({ - success: true, - updatedBrand, - message: "Brand updated successfully", - }); + res.status(200).json({ + success: true, + updatedBrand, + message: "Brand updated successfully", + }); } catch (error) { - res - .status(500) - .json({ - success: false, - message: error.message || "Something went wrong", - }); + res.status(500).json({ + success: false, + message: error.message || "Something went wrong", + }); } }; @@ -258,21 +254,19 @@ export const deleteBrand = async (req, res) => { return res.status(404).json({ message: "Brand not found" }); } - if (brand.image.length > 0) { - await cloudinary.v2.uploader.destroy(brand.image[0].public_id); - } + // if (brand.image.length > 0) { + // await cloudinary.v2.uploader.destroy(brand.image[0].public_id); + // } await BrandModel.findByIdAndDelete(_id); res .status(200) .json({ success: true, message: "Brand deleted successfully" }); } catch (error) { - res - .status(500) - .json({ - success: false, - message: error.message || "Something went wrong", - }); + res.status(500).json({ + success: false, + message: error.message || "Something went wrong", + }); } }; @@ -291,28 +285,28 @@ export const deleteImageFromCloudinary = async (req, res) => { try { // Step 1: Delete image from Cloudinary - const response = await cloudinary.v2.uploader.destroy(decodedPublicId); - + // const response = await cloudinary.v2.uploader.destroy(decodedPublicId); + const response = { result: "ok" }; // Mock response for testing if (response.result === "ok") { // Step 2: Find the brand containing the image and update the database - const brand = await BrandModel.findOne({ - "image.public_id": decodedPublicId, - }); + // const brand = await BrandModel.findOne({ + // "image.public_id": decodedPublicId, + // }); - if (!brand) { - return res.status(404).json({ - success: false, - msg: "Brand not found with the given image!", - }); - } + // if (!brand) { + // return res.status(404).json({ + // success: false, + // msg: "Brand not found with the given image!", + // }); + // } - // Remove the image from the brand's image array - brand.image = brand.image.filter( - (img) => img.public_id !== decodedPublicId - ); + // // Remove the image from the brand's image array + // brand.image = brand.image.filter( + // (img) => img.public_id !== decodedPublicId + // ); - // Step 3: Save the updated brand document - await brand.save(); + // // Step 3: Save the updated brand document + // await brand.save(); return res.status(200).json({ success: true, diff --git a/resources/RD_Ordes/invoiceModalRD.js b/resources/RD_Ordes/invoiceModalRD.js index 001a5c7..854c01a 100644 --- a/resources/RD_Ordes/invoiceModalRD.js +++ b/resources/RD_Ordes/invoiceModalRD.js @@ -34,6 +34,12 @@ const orderItemSchema = new Schema({ type: Number, required: true, }, + image: [ + { + public_id: String, + url: String, + }, + ], processquantity: { //updated quantity type: Number, diff --git a/resources/RD_Ordes/rdOrderController.js b/resources/RD_Ordes/rdOrderController.js index 3460ab8..de447da 100644 --- a/resources/RD_Ordes/rdOrderController.js +++ b/resources/RD_Ordes/rdOrderController.js @@ -69,7 +69,7 @@ export const createOrderRD = async (req, res) => { GST: item.GST, HSN_Code: item.HSN_Code, description: item.description, - image: item.image, + image: item.brand?.image?.length > 0 ? item.brand.image[0] : item.image, quantity: item.count, remainingQuantity: item.count, })), @@ -171,6 +171,9 @@ export const getPlacedOrderById = async (req, res) => { const doc = await RdOrder.findById(id) .populate({ path: "orderItem.productId", + populate: { + path: "brand", + }, }) .populate({ path: "invoices" }); if (doc) {