diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 78e0aae8..7fa151d8 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -1621,6 +1621,7 @@ exports.createquotationforSensor = async (req, reply) => { const quatationId = `AWQU${i_id}`; const { surveyId } = req.params; const { customerId, masters, slaves, sensors, motor_switches, electricals,master_connections } = req.body; + // Format electricals field const formattedElectricals = electricals.map((item) => ({ @@ -1633,6 +1634,9 @@ exports.createquotationforSensor = async (req, reply) => { master_name: item.master_name || "", slaves: item.slaves || "", location: item.location || "", + googleLocation: item.googleLocation || "", + latitude: item.latitude || "", + longitude: item.longitude || "", tanks: Array.isArray(item.tanks) ? item.tanks.map(tank => ({ tankName: tank.tankName || "", @@ -2247,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" }); } @@ -2266,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" }) @@ -2296,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(); @@ -2322,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) }, @@ -2375,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; @@ -2408,7 +2420,7 @@ exports.acceptQuotation = async (req, reply) => { } } - // **Step 6: Delete Quotation** + // Step 5: Delete Quotation await SensorQuotation.deleteOne({ quatationId: quotationId }); return reply.send({ @@ -2427,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 9483a365..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 }, @@ -525,6 +533,9 @@ const sensorquotationSchema = new mongoose.Schema({ 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 }, @@ -591,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: [ { diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index 18cb49fe..5038c582 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -1428,6 +1428,9 @@ fastify.post("/api/createquotationforSensor/:surveyId", { master_name: { type: "string", default: null }, slaves: { type: "string", default: null }, location: { type: "string", default: null }, + googleLocation: { type: "string", default: null }, + latitude: { type: 'number', default: 0.0 }, + longitude: { type: 'number', default: 0.0}, tanks: { type: "array", items: {