image for orders

This commit is contained in:
Sibunnayak 2025-02-07 12:32:36 +05:30
parent e0754cdd08
commit bd1a54be8f
3 changed files with 46 additions and 43 deletions

View File

@ -209,9 +209,9 @@ export const updateBrand = async (req, res) => {
let image = brand.image; let image = brand.image;
if (file) { if (file) {
if (image.length > 0) { // if (image.length > 0) {
await cloudinary.v2.uploader.destroy(image[0].public_id); // await cloudinary.v2.uploader.destroy(image[0].public_id);
} // }
const result = await cloudinary.v2.uploader.upload(file.tempFilePath, { const result = await cloudinary.v2.uploader.upload(file.tempFilePath, {
folder: "chemiNova/brand", folder: "chemiNova/brand",
}); });
@ -224,20 +224,16 @@ export const updateBrand = async (req, res) => {
{ new: true, runValidators: true } { new: true, runValidators: true }
); );
res res.status(200).json({
.status(200) success: true,
.json({ updatedBrand,
success: true, message: "Brand updated successfully",
updatedBrand, });
message: "Brand updated successfully",
});
} catch (error) { } catch (error) {
res res.status(500).json({
.status(500) success: false,
.json({ message: error.message || "Something went wrong",
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" }); return res.status(404).json({ message: "Brand not found" });
} }
if (brand.image.length > 0) { // if (brand.image.length > 0) {
await cloudinary.v2.uploader.destroy(brand.image[0].public_id); // await cloudinary.v2.uploader.destroy(brand.image[0].public_id);
} // }
await BrandModel.findByIdAndDelete(_id); await BrandModel.findByIdAndDelete(_id);
res res
.status(200) .status(200)
.json({ success: true, message: "Brand deleted successfully" }); .json({ success: true, message: "Brand deleted successfully" });
} catch (error) { } catch (error) {
res res.status(500).json({
.status(500) success: false,
.json({ message: error.message || "Something went wrong",
success: false, });
message: error.message || "Something went wrong",
});
} }
}; };
@ -291,28 +285,28 @@ export const deleteImageFromCloudinary = async (req, res) => {
try { try {
// Step 1: Delete image from Cloudinary // 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") { if (response.result === "ok") {
// Step 2: Find the brand containing the image and update the database // Step 2: Find the brand containing the image and update the database
const brand = await BrandModel.findOne({ // const brand = await BrandModel.findOne({
"image.public_id": decodedPublicId, // "image.public_id": decodedPublicId,
}); // });
if (!brand) { // if (!brand) {
return res.status(404).json({ // return res.status(404).json({
success: false, // success: false,
msg: "Brand not found with the given image!", // msg: "Brand not found with the given image!",
}); // });
} // }
// Remove the image from the brand's image array // // Remove the image from the brand's image array
brand.image = brand.image.filter( // brand.image = brand.image.filter(
(img) => img.public_id !== decodedPublicId // (img) => img.public_id !== decodedPublicId
); // );
// Step 3: Save the updated brand document // // Step 3: Save the updated brand document
await brand.save(); // await brand.save();
return res.status(200).json({ return res.status(200).json({
success: true, success: true,

View File

@ -34,6 +34,12 @@ const orderItemSchema = new Schema({
type: Number, type: Number,
required: true, required: true,
}, },
image: [
{
public_id: String,
url: String,
},
],
processquantity: { processquantity: {
//updated quantity //updated quantity
type: Number, type: Number,

View File

@ -69,7 +69,7 @@ export const createOrderRD = async (req, res) => {
GST: item.GST, GST: item.GST,
HSN_Code: item.HSN_Code, HSN_Code: item.HSN_Code,
description: item.description, description: item.description,
image: item.image, image: item.brand?.image?.length > 0 ? item.brand.image[0] : item.image,
quantity: item.count, quantity: item.count,
remainingQuantity: item.count, remainingQuantity: item.count,
})), })),
@ -171,6 +171,9 @@ export const getPlacedOrderById = async (req, res) => {
const doc = await RdOrder.findById(id) const doc = await RdOrder.findById(id)
.populate({ .populate({
path: "orderItem.productId", path: "orderItem.productId",
populate: {
path: "brand",
},
}) })
.populate({ path: "invoices" }); .populate({ path: "invoices" });
if (doc) { if (doc) {