From 94e28aa2ff36ca1f29737921d025c0ac56db3b50 Mon Sep 17 00:00:00 2001 From: Varun Date: Tue, 13 May 2025 14:48:01 +0530 Subject: [PATCH] reverted changes --- src/controllers/storeController.js | 42 +++++++++++++++++------------- src/models/store.js | 2 +- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 9c528b9b..b3471a16 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2268,6 +2268,14 @@ exports.acceptQuotation = async (req, reply) => { } else if (action === "accept") { const { customerId, masters, slaves, sensors, master_connections } = quotation; + const newOrder = new Order({ + ...quotation.toObject(), + storeId, + status: "pending", + }); + + await newOrder.save(); + // Step 1: Block Masters let blockedMasters = []; if (parseInt(masters) > 0) { @@ -2313,8 +2321,9 @@ exports.acceptQuotation = async (req, reply) => { { $set: { status: "blocked", customerId, connected_to: masterHardwareId } } ); - // Assign tanks to slaves + // Assign each slave a corresponding tank const tanks = masterData.tanks || []; + for (let j = 0; j < availableSlaves.length; j++) { const slaveId = availableSlaves[j]._id; const tank = tanks[j] || {}; @@ -2347,11 +2356,18 @@ exports.acceptQuotation = async (req, reply) => { ); } - // ✅ Step 2.5: Update master_connections.hardwareId - const updatedMasterConnections = quotation.master_connections.map((conn, index) => ({ - ...conn, - hardwareId: blockedMasters[index]?.hardwareId || null, - })); + // ✅ Step 2.5: Update master_connections.hardwareId in Order + const updatedMasterConnections = newOrder.master_connections.map((conn, index) => { + return { + ...conn, + hardwareId: blockedMasters[index]?.hardwareId || null, + }; + }); + + await Order.updateOne( + { _id: newOrder._id }, + { $set: { master_connections: updatedMasterConnections } } + ); // Step 3: Assign Sensors to Slaves if (parseInt(sensors) > 0 && blockedSlaves.length > 0) { @@ -2417,17 +2433,7 @@ exports.acceptQuotation = async (req, reply) => { } } - // Step 5: Create Order (with updated master_connections) - const newOrder = new Order({ - ...quotation.toObject(), - storeId, - status: "pending", - master_connections: updatedMasterConnections, - }); - - await newOrder.save(); - - // Step 6: Delete Quotation + // Step 5: Delete Quotation await SensorQuotation.deleteOne({ quatationId: quotationId }); return reply.send({ @@ -2442,7 +2448,7 @@ exports.acceptQuotation = async (req, reply) => { console.error("Error processing quotation:", err); return reply.status(500).send({ error: "Internal server error" }); } -}; +} diff --git a/src/models/store.js b/src/models/store.js index 68348077..57551f8f 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -605,7 +605,7 @@ const orderSchema = new mongoose.Schema({ master_connections: [ { master_name: { type: String, default: null }, - hardwareId:{ type: String, default: null }, + slaves: { type: String, default: null }, location: { type: String, default: null }, googleLocation: { type: String, default: null },