From 3ab01f802f60f6389bed2a729309c713fe1b401c Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 10 Jul 2025 14:05:55 +0530 Subject: [PATCH] update work status in pending --- src/controllers/storeController.js | 96 +++++++++++++++++++++++++++--- src/routes/storeRoute.js | 30 ++++++---- 2 files changed, 104 insertions(+), 22 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 133f277d..381f5609 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2849,43 +2849,120 @@ exports.getPendingOrdersByInstallationId = async (req, reply) => { +// exports.updateWorkStatusByInstallationId = async (req, reply) => { +// try { +// const { installationId, customerId } = req.params; +// const { work_status } = req.body; // 🟢 pass in body + +// if (!installationId) { +// return reply.status(400).send({ error: "installationId is required" }); +// } + +// if (!customerId) { +// return reply.status(400).send({ error: "customerId is required" }); +// } + +// if (!work_status || !['active', 'pending', 'complete'].includes(work_status)) { +// return reply.status(400).send({ error: "Valid work_status is required: active, pending or complete" }); +// } + +// // Find all orders for this installation + customer +// const orders = await Order.find({ installationId, customerId }); + +// if (!orders.length) { +// return reply.send({ +// status_code: 200, +// message: "No orders found for this installation and customer", +// data: [], +// }); +// } + +// // Update all found orders to new work_status +// for (const order of orders) { +// order.work_status = work_status; +// await order.save(); +// } + +// // After update, fetch all with updated work_status only +// const updatedOrders = await Order.find({ installationId, customerId, work_status }); + +// const ordersWithDetails = await Promise.all( +// updatedOrders.map(async (order) => { +// const customer = await User.findOne({ customerId: order.customerId }).lean(); +// const allocatedSensors = await Insensors.find({ +// storeId: order.storeId, +// customerId: order.customerId, +// status: "blocked", +// }).lean(); + +// return { +// ...order.toObject(), +// customer: customer || null, +// allocated_sensors: allocatedSensors, +// }; +// }) +// ); + +// return reply.send({ +// status_code: 200, +// message: `Orders updated to work_status '${work_status}' successfully`, +// data: ordersWithDetails, +// }); + +// } catch (err) { +// console.error("Error updating work_status:", err); +// return reply.status(500).send({ error: "Internal server error" }); +// } +// }; + exports.updateWorkStatusByInstallationId = async (req, reply) => { try { const { installationId, customerId } = req.params; - const { work_status } = req.body; // 🟢 pass in body + const { work_status, hardwareId } = req.body; if (!installationId) { return reply.status(400).send({ error: "installationId is required" }); } - if (!customerId) { return reply.status(400).send({ error: "customerId is required" }); } - + if (!hardwareId) { + return reply.status(400).send({ error: "hardwareId is required" }); + } if (!work_status || !['active', 'pending', 'complete'].includes(work_status)) { return reply.status(400).send({ error: "Valid work_status is required: active, pending or complete" }); } - // Find all orders for this installation + customer - const orders = await Order.find({ installationId, customerId }); + // ✅ Correct query + const orders = await Order.find({ + installationId, + customerId, + 'master_connections.hardwareId': hardwareId + }); if (!orders.length) { return reply.send({ status_code: 200, - message: "No orders found for this installation and customer", + message: "No orders found for this installation, customer and hardwareId", data: [], }); } - // Update all found orders to new work_status + // Update work_status for (const order of orders) { order.work_status = work_status; await order.save(); } - // After update, fetch all with updated work_status only - const updatedOrders = await Order.find({ installationId, customerId, work_status }); + // Fetch updated orders + const updatedOrders = await Order.find({ + installationId, + customerId, + 'master_connections.hardwareId': hardwareId, + work_status + }); + // Enrich data const ordersWithDetails = await Promise.all( updatedOrders.map(async (order) => { const customer = await User.findOne({ customerId: order.customerId }).lean(); @@ -2919,6 +2996,7 @@ exports.updateWorkStatusByInstallationId = async (req, reply) => { + exports.getallocatedsensorstouser= async (req, reply) => { try { const { customerId } = req.params; diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index fe5ebf8f..39aeb500 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -2020,9 +2020,9 @@ fastify.post( '/api/orders/:installationId/:customerId', { schema: { - tags: ["Installation"], - description: "Update the work status", - summary: "Update the work status", + tags: ["Installation"], + description: "Update the work status", + summary: "Update the work status", params: { type: 'object', required: ['installationId', 'customerId'], @@ -2033,25 +2033,29 @@ fastify.post( }, body: { type: 'object', - required: ['work_status'], + required: ['work_status', 'hardwareId'], properties: { work_status: { type: 'string', enum: ['active', 'pending', 'complete'], description: 'The new work status', }, - }, - }, - response: { - 200: { - type: 'object', - properties: { - status_code: { type: 'integer' }, - message: { type: 'string' }, - data: { type: 'array' }, // you can make this stricter too if needed + hardwareId: { + type: 'string', + description: 'Hardware ID to update orders for', }, }, }, + // response: { + // 200: { + // type: 'object', + // properties: { + // status_code: { type: 'integer' }, + // message: { type: 'string' }, + // data: { type: 'array' }, + // }, + // }, + // }, }, handler: storeController.updateWorkStatusByInstallationId, }