From e66c69418e60a4d2257b9b111753961f52bc32b6 Mon Sep 17 00:00:00 2001 From: Varun Date: Tue, 4 Mar 2025 16:47:18 +0530 Subject: [PATCH] changes in accept quotation --- src/controllers/storeController.js | 49 ++++++++++++++++++------------ 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 875f392d..0dc5fedf 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2021,11 +2021,9 @@ exports.getOrdersByCustomer = async (req, reply) => { exports.acceptQuotation = async (req, reply) => { try { const { quotationId } = req.params; - const { action } = req.body; + let { action } = req.body; - if (action !== "accept") { - return reply.status(400).send({ error: "Invalid action" }); - } + action = action.toLowerCase(); // Convert action to lowercase // Find the quotation by ID const quotation = await SensorQuotation.findOne({ quatationId: quotationId }); @@ -2034,26 +2032,37 @@ exports.acceptQuotation = async (req, reply) => { return reply.status(404).send({ error: "Quotation not found" }); } - // Convert quotation to an order object - const newOrder = new Order({ - ...quotation.toObject(), // Copy all fields - status: "pending", // Set status to "pending" - }); + if (action === "reject") { + // Update status to "rejected" in SensorQuotation + await SensorQuotation.updateOne({ quatationId: quotationId }, { $set: { status: "rejected" } }); - // Save to the Orders collection - await newOrder.save(); + return reply.send({ + status_code: 200, + message: "Quotation rejected successfully", + }); + } else if (action === "accept") { + // Convert quotation to an order object + const newOrder = new Order({ + ...quotation.toObject(), // Copy all fields + status: "pending", // Set status to "pending" + }); - // Delete the record from SensorQuotation - await SensorQuotation.deleteOne({ quatationId: quotationId }); + // Save to the Orders collection + await newOrder.save(); - return reply.send({ - status_code: 200, - message: "Quotation accepted and moved to Orders", - data: newOrder, - }); + // Delete the record from SensorQuotation + await SensorQuotation.deleteOne({ quatationId: quotationId }); + return reply.send({ + status_code: 200, + message: "Quotation accepted and moved to Orders", + data: newOrder, + }); + } else { + return reply.status(400).send({ error: "Invalid action" }); + } } catch (err) { - console.error("Error accepting quotation:", err); + console.error("Error processing quotation:", err); return reply.status(500).send({ error: "Internal server error" }); } -}; \ No newline at end of file +};