diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index fc93cb74..09c5455e 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2268,14 +2268,6 @@ 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) { @@ -2294,7 +2286,7 @@ exports.acceptQuotation = async (req, reply) => { } } - // Step 2: Process Master Connections & Assign Slaves + // Step 2: Assign Slaves to Masters let blockedSlaveIds = []; let blockedSlaves = []; @@ -2303,6 +2295,7 @@ exports.acceptQuotation = async (req, reply) => { if (i >= blockedMasters.length) break; const masterHardwareId = blockedMasters[i].hardwareId; + const masterId = blockedMasters[i]._id; const slaveCount = parseInt(masterData.slaves) || 0; // Assign Slaves @@ -2315,38 +2308,33 @@ exports.acceptQuotation = async (req, reply) => { blockedSlaveIds.push(...slaveIds); blockedSlaves.push(...availableSlaves.map(slave => ({ _id: slave._id, hardwareId: slave.hardwareId }))); - if (slaveIds.length > 0) { - await Insensors.updateMany( - { _id: { $in: slaveIds } }, - { $set: { status: "blocked", customerId, connected_to: masterHardwareId } } - ); - - // Assign each slave a corresponding tank (1-to-1 mapping) - const tanks = masterData.tanks || []; - - for (let j = 0; j < availableSlaves.length; j++) { - const slaveId = availableSlaves[j]._id; - const tank = tanks[j] || {}; + for (let j = 0; j < availableSlaves.length; j++) { + const slave = availableSlaves[j]; + const tank = masterData.tanks?.[j] || {}; - await Insensors.updateOne( - { _id: slaveId }, - { - $set: { - tankName: tank.tankName || "", - tankLocation: tank.tankLocation || "", - }, - } - ); - } + await Insensors.updateOne( + { _id: slave._id }, + { + $set: { + status: "blocked", + customerId, + connected_to: masterHardwareId, + hardwareId: slave.hardwareId, + tankName: tank.tankName || "", + tankLocation: tank.tankLocation || "", + }, + } + ); } } - // Update tanks and motor switches for masters + // Update tanks and motor switches for master await Insensors.updateOne( - { _id: blockedMasters[i]._id }, + { _id: masterId }, { $set: { - tanks: masterData.tanks.map(tank => ({ + hardwareId: masterHardwareId, + tanks: (masterData.tanks || []).map(tank => ({ tankName: tank.tankName || "", tankLocation: tank.tankLocation || "", })), @@ -2356,6 +2344,15 @@ exports.acceptQuotation = async (req, reply) => { ); } + // Step 2.5: Update master_connections.hardwareId + const updatedMasterConnections = quotation.master_connections.map((conn, index) => { + const plain = conn.toObject ? conn.toObject() : conn; + return { + ...plain, + hardwareId: blockedMasters[index]?.hardwareId || null, + }; + }); + // Step 3: Assign Sensors to Slaves if (parseInt(sensors) > 0 && blockedSlaves.length > 0) { const availableSensors = await Insensors.find({ storeId, type: "sensor", status: "available" }) @@ -2369,7 +2366,13 @@ exports.acceptQuotation = async (req, reply) => { await Insensors.updateOne( { _id: sensorIds[i] }, - { $set: { status: "blocked", customerId, connected_to: assignedSlave.hardwareId } } + { + $set: { + status: "blocked", + customerId, + connected_to: assignedSlave.hardwareId, + }, + } ); } } @@ -2420,7 +2423,19 @@ exports.acceptQuotation = async (req, reply) => { } } - // Step 5: Delete Quotation + // Step 5: Create Order + const plainQuotation = quotation.toObject(); + plainQuotation.master_connections = updatedMasterConnections; + + const newOrder = new Order({ + ...plainQuotation, + storeId, + status: "pending", + }); + + await newOrder.save(); + + // Step 6: Delete Quotation await SensorQuotation.deleteOne({ quatationId: quotationId }); return reply.send({