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;
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,

View File

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

View File

@ -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) {