From 80822ed665e8320b8a0035ee2ca5d5bcdd1079df Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 9 Jul 2025 20:50:59 +0530 Subject: [PATCH] status maintain --- src/controllers/storeController.js | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index d677fdb0..2c756824 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2644,7 +2644,7 @@ exports.getOrdersByInstallationId = async (req, reply) => { return reply.status(400).send({ error: "installationId is required" }); } - // Fetch orders with the matching installationId + // ✅ Do NOT use .lean() here const orders = await Order.find({ installationId }); if (!orders.length) { @@ -2657,42 +2657,37 @@ exports.getOrdersByInstallationId = async (req, reply) => { const uniqueCustomersMap = new Map(); - // Build unique customerId-based map for (const order of orders) { if (!uniqueCustomersMap.has(order.customerId)) { uniqueCustomersMap.set(order.customerId, order); } } - // Only keep one order per customerId const uniqueOrders = Array.from(uniqueCustomersMap.values()); - // Enrich with customer and sensor info, AND update work_status if missing const ordersWithDetails = await Promise.all( uniqueOrders.map(async (order) => { - const customer = await User.findOne({ customerId: order.customerId }).lean(); + // ✅ This checks if work_status is missing or empty, then sets and saves + if (!order.work_status || order.work_status.trim() === "") { + order.work_status = "active"; + await order.save(); + } + const customer = await User.findOne({ customerId: order.customerId }).lean(); const allocatedSensors = await Insensors.find({ storeId: order.storeId, customerId: order.customerId, status: "blocked", }).lean(); - // ✅ If missing, update work_status to "active" and save in DB - if (!order.work_status) { - order.work_status = "active"; - await order.save(); // This persists the change to MongoDB - } - return { - ...order.toObject(), + ...order.toObject(), // after save customer: customer || null, allocated_sensors: allocatedSensors, - work_status: order.work_status // Now always persisted }; }) ); - + console.log("orders", orders) return reply.send({ status_code: 200, message: "Orders fetched successfully", @@ -2710,6 +2705,7 @@ exports.getOrdersByInstallationId = async (req, reply) => { + exports.getallocatedsensorstouser= async (req, reply) => { try { const { customerId } = req.params;