diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 4fa50511..7fa151d8 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2251,12 +2251,9 @@ exports.acceptQuotation = async (req, reply) => { const { quotationId } = req.params; let { action, storeId } = req.body; - action = action.toLowerCase(); // Convert action to lowercase + action = action.toLowerCase(); - // Find the quotation by ID const quotation = await SensorQuotation.findOne({ quatationId: quotationId }); - console.log(quotation,"quotation") - if (!quotation) { return reply.status(404).send({ error: "Quotation not found" }); } @@ -2270,19 +2267,16 @@ exports.acceptQuotation = async (req, reply) => { }); } else if (action === "accept") { const { customerId, masters, slaves, sensors, master_connections } = quotation; - console.log(customerId,masters,slaves,sensors,master_connections) - // Convert quotation to an order object and include storeId const newOrder = new Order({ ...quotation.toObject(), storeId, status: "pending", }); - // Save the order await newOrder.save(); - // **Step 1: Block Masters in Insensors** + // Step 1: Block Masters let blockedMasters = []; if (parseInt(masters) > 0) { const availableMasters = await Insensors.find({ storeId, type: "master", status: "available" }) @@ -2300,19 +2294,19 @@ exports.acceptQuotation = async (req, reply) => { } } - // **Step 2: Process Master Connections & Assign Slaves** + // Step 2: Process Master Connections & Assign Slaves let blockedSlaveIds = []; let blockedSlaves = []; for (let i = 0; i < master_connections.length; i++) { const masterData = master_connections[i]; - if (i >= blockedMasters.length) break; // Ensure we don't exceed blocked masters + if (i >= blockedMasters.length) break; - const masterHardwareId = blockedMasters[i].hardwareId; // Get the hardwareId of the blocked master + const masterHardwareId = blockedMasters[i].hardwareId; const slaveCount = parseInt(masterData.slaves) || 0; + // Assign Slaves if (slaveCount > 0) { - // **Find and Assign Slaves** const availableSlaves = await Insensors.find({ storeId, type: "slave", status: "available" }) .limit(slaveCount) .lean(); @@ -2326,46 +2320,61 @@ exports.acceptQuotation = async (req, reply) => { { _id: { $in: slaveIds } }, { $set: { status: "blocked", customerId, connected_to: masterHardwareId } } ); - } - } - // **Step 3: Update Tanks in Masters** - if (masterData.tanks && masterData.tanks.length > 0) { - await Insensors.updateOne( - { _id: blockedMasters[i]._id }, - { - $set: { - tanks: masterData.tanks.map(tank => ({ - tankName: tank.tankName || "", - tankLocation: tank.tankLocation || "", - })), - }, + // 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] || {}; + + await Insensors.updateOne( + { _id: slaveId }, + { + $set: { + tankName: tank.tankName || "", + tankLocation: tank.tankLocation || "", + }, + } + ); } - ); + } } + + // Update tanks and motor switches for masters + await Insensors.updateOne( + { _id: blockedMasters[i]._id }, + { + $set: { + tanks: masterData.tanks.map(tank => ({ + tankName: tank.tankName || "", + tankLocation: tank.tankLocation || "", + })), + motor_switches: masterData.motor_switches || [], + }, + } + ); } - // **Step 4: Assign Sensors to Slaves** - if (parseInt(sensors) > 0) { + // Step 3: Assign Sensors to Slaves + if (parseInt(sensors) > 0 && blockedSlaves.length > 0) { const availableSensors = await Insensors.find({ storeId, type: "sensor", status: "available" }) .limit(parseInt(sensors)) .lean(); const sensorIds = availableSensors.map(sensor => sensor._id); - if (sensorIds.length > 0) { - for (let i = 0; i < sensorIds.length; i++) { - const assignedSlave = blockedSlaves[i % blockedSlaves.length]; // Distribute sensors to slaves + for (let i = 0; i < sensorIds.length; i++) { + const assignedSlave = blockedSlaves[i % blockedSlaves.length]; - await Insensors.updateOne( - { _id: sensorIds[i] }, - { $set: { status: "blocked", customerId, connected_to: assignedSlave.hardwareId } } - ); - } + await Insensors.updateOne( + { _id: sensorIds[i] }, + { $set: { status: "blocked", customerId, connected_to: assignedSlave.hardwareId } } + ); } } - // **Step 5: Update Sensor Stock** + // Step 4: Update Sensor Stock const sensorTypes = [ { type: "master", count: parseInt(masters || 0) }, { type: "slave", count: parseInt(slaves || 0) }, @@ -2379,7 +2388,6 @@ exports.acceptQuotation = async (req, reply) => { if (stock) { let available = stock.total_available || 0; let needed = sensor.count; - let toBlock = Math.min(available, needed); let excessNeeded = needed - toBlock; @@ -2412,7 +2420,7 @@ exports.acceptQuotation = async (req, reply) => { } } - // **Step 6: Delete Quotation** + // Step 5: Delete Quotation await SensorQuotation.deleteOne({ quatationId: quotationId }); return reply.send({ @@ -2431,7 +2439,6 @@ exports.acceptQuotation = async (req, reply) => { - exports.getOrdersByStoreId = async (req, reply) => { try { const { storeId } = req.params; diff --git a/src/models/store.js b/src/models/store.js index 47afe5c4..6fb72d5a 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -444,6 +444,14 @@ const insensorsSchema = new mongoose.Schema({ connected_status: { type: String, enum: ["connected", "Not connected", "unknown"], default: "unknown" }, masterName: { type: String, default: null }, location: { type: String, default: null }, + motor_switches: [ + { + from_tank: { type: String, default: null }, + from_location: { type: String, default: null }, + to_tank: { type: String, default: null }, + to_location: { type: String, default: null }, + }, + ], connected_gsm_time: { type: String, default: null }, connected_gsm_date: { type: String, default: null }, connected_lora_date: { type: String, default: null }, @@ -594,6 +602,33 @@ const orderSchema = new mongoose.Schema({ datetime: { type: String, default: null }, updated_at: { type: String, default: null }, assignedTeamMembers: [{ type: String }], + master_connections: [ + { + master_name: { type: String, default: null }, + slaves: { type: String, default: null }, + location: { type: String, default: null }, + googleLocation: { type: String, default: null }, + longitude: { type : Number,default: 0.0}, + latitude: {type: Number,default: 0.0}, + tanks: [ + { + tankName: { type: String, default: null }, + tankLocation: { type: String, default: null }, + + }, + ], + motor_switches: [ + { + from_tank: { type: String, default: null }, + from_location: { type: String, default: null }, + to_tank: { type: String, default: null }, + to_location: { type: String, default: null }, + }, + ], + + + }, + ], electricals: [ {