From 5e5e06d345550444338954fbd930348529a037f6 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 15 Jul 2025 17:53:55 +0530 Subject: [PATCH] master Name location added --- src/controllers/installationController.js | 121 ++++++++++++++++------ src/routes/installationRoute.js | 56 +--------- 2 files changed, 88 insertions(+), 89 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index ce1177d9..5cb5a754 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -3536,47 +3536,47 @@ exports.getCompleteMasterSlaveSummary = async (req, reply) => { return reply.status(500).send({ error: "Internal Server Error" }); } }; -exports.getMasterWithSlaves = async (req, reply) => { - try { - const { installationId, customerId, hardwareId } = req.params; +// exports.getMasterWithSlaves = async (req, reply) => { +// try { +// const { installationId, customerId, hardwareId } = req.params; - if (!installationId || !customerId || !hardwareId) { - return reply.code(400).send({ success: false, message: "installationId, customerId, and hardwareId are required" }); - } +// if (!installationId || !customerId || !hardwareId) { +// return reply.code(400).send({ success: false, message: "installationId, customerId, and hardwareId are required" }); +// } - // Find order - const order = await Order.findOne({ installationId, customerId }).lean(); - if (!order) { - return reply.code(404).send({ success: false, message: "Order not found" }); - } +// // Find order +// const order = await Order.findOne({ installationId, customerId }).lean(); +// if (!order) { +// return reply.code(404).send({ success: false, message: "Order not found" }); +// } - // Find master device in Insensors - const master = await Insensors.findOne({ - hardwareId, - customerId, - }).lean(); +// // Find master device in Insensors +// const master = await Insensors.findOne({ +// hardwareId, +// customerId, +// }).lean(); - if (!master) { - return reply.code(404).send({ success: false, message: "Master device not found in Insensors" }); - } +// if (!master) { +// return reply.code(404).send({ success: false, message: "Master device not found in Insensors" }); +// } - // // Find slaves connected to this master - // const slaves = await Insensors.find({ - // connected_to: hardwareId, - // customerId, - // type: 'slave' - // }).lean(); +// // // Find slaves connected to this master +// // const slaves = await Insensors.find({ +// // connected_to: hardwareId, +// // customerId, +// // type: 'slave' +// // }).lean(); - return reply.send({ - success: true, - master, - }); +// return reply.send({ +// success: true, +// master, +// }); - } catch (error) { - console.error("Error fetching master and slaves:", error); - return reply.code(500).send({ success: false, message: "Internal server error" }); - } -}; +// } catch (error) { +// console.error("Error fetching master and slaves:", error); +// return reply.code(500).send({ success: false, message: "Internal server error" }); +// } +// }; // exports.editTankDimensions = async (req, reply) => { // try { @@ -3709,6 +3709,59 @@ exports.getMasterWithSlaves = async (req, reply) => { // } // }; +exports.getMasterWithSlaves = async (req, reply) => { + try { + const { installationId, customerId, hardwareId } = req.params; + + if (!installationId || !customerId || !hardwareId) { + return reply.code(400).send({ success: false, message: "installationId, customerId, and hardwareId are required" }); + } + + // Find order + const order = await Order.findOne({ installationId, customerId }).lean(); + if (!order) { + return reply.code(404).send({ success: false, message: "Order not found" }); + } + + // Find master device in Insensors + const master = await Insensors.findOne({ + hardwareId, + customerId, + }).lean(); + + if (!master) { + return reply.code(404).send({ success: false, message: "Master device not found in Insensors" }); + } + + // Find matching master in order.master_connections + const matchingMaster = order.master_connections?.find(m => m.hardwareId === hardwareId); + + let masterName = null; + let location = null; + if (matchingMaster) { + masterName = matchingMaster.master_name ?? null; + location = matchingMaster.location ?? null; + } + + // Build enriched master object + const enrichedMaster = { + ...master, + masterName, + location, + hardwareId: master.hardwareId // keep hardwareId here explicitly if needed + }; + + return reply.send({ + success: true, + master: enrichedMaster + }); + + } catch (error) { + console.error("Error fetching master and slaves:", error); + return reply.code(500).send({ success: false, message: "Internal server error" }); + } +}; + exports.getPendingMasterSlaveSummary = async (req, reply) => { try { diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index f12633bb..62dbd91e 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -731,61 +731,7 @@ fastify.post( hardwareId: { type: 'string', description: 'Master hardwareId' }, } }, - // response: { - // 200: { - // type: 'object', - // properties: { - // success: { type: 'boolean' }, - // master: { - // type: 'object', - // description: 'Master device details', - // properties: { - // _id: { type: 'string' }, - // hardwareId: { type: 'string' }, - // customerId: { type: 'string' }, - // type: { type: 'string' }, - // // Add other master fields as needed... - // } - // }, - // slaves: { - // type: 'array', - // description: 'List of connected slave devices', - // items: { - // type: 'object', - // properties: { - // _id: { type: 'string' }, - // hardwareId: { type: 'string' }, - // connected_to: { type: 'string' }, - // customerId: { type: 'string' }, - // type: { type: 'string' }, - // // Add other slave fields as needed... - // } - // } - // } - // } - // }, - // 400: { - // type: 'object', - // properties: { - // success: { type: 'boolean' }, - // message: { type: 'string' } - // } - // }, - // 404: { - // type: 'object', - // properties: { - // success: { type: 'boolean' }, - // message: { type: 'string' } - // } - // }, - // 500: { - // type: 'object', - // properties: { - // success: { type: 'boolean' }, - // message: { type: 'string' } - // } - // } - // } + }, handler: installationController.getMasterWithSlaves,