masterName and location

master^2
Bhaskar 3 months ago
parent e940b98cc9
commit cde1810eb7

@ -3817,6 +3817,178 @@ exports.getCompleteMasterSlaveSummary = 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
// };
// // 👉 Find slaves connected to this master
// const slaves = await Insensors.find({
// customerId,
// connected_to: hardwareId,
// type: 'slave'
// }).lean();
// // Add masterName & location to each slave
// const enrichedSlaves = slaves.map(slave => ({
// ...slave,
// masterName,
// location
// }));
// return reply.send({
// success: true,
// master: enrichedMaster,
// slaves: enrichedSlaves
// });
// } catch (error) {
// console.error("Error fetching master and slaves:", error);
// return reply.code(500).send({ success: false, message: "Internal server error" });
// }
// };
// 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 device in Insensors (could be master or slave)
// const device = await Insensors.findOne({
// hardwareId,
// customerId,
// }).lean();
// if (!device) {
// return reply.code(404).send({ success: false, message: "Device not found in Insensors" });
// }
// let master;
// let masterName = null;
// let location = null;
// let slaves = [];
// if (device.type === 'master') {
// // Case: passed hardwareId is master
// master = device;
// // Find matching master in order.master_connections
// const matchingMaster = order.master_connections?.find(m => m.hardwareId === hardwareId);
// if (matchingMaster) {
// masterName = matchingMaster.master_name ?? null;
// location = matchingMaster.location ?? null;
// }
// // Find slaves connected to this master
// slaves = await Insensors.find({
// customerId,
// connected_to: hardwareId,
// type: 'slave'
// }).lean();
// } else if (device.type === 'slave' && device.connected_to) {
// // Case: passed hardwareId is slave → need to find master
// master = await Insensors.findOne({
// hardwareId: device.connected_to,
// customerId,
// type: 'master'
// }).lean();
// if (!master) {
// return reply.code(404).send({ success: false, message: "Master device linked to this slave not found" });
// }
// // Find matching master in order.master_connections
// const matchingMaster = order.master_connections?.find(m => m.hardwareId === device.connected_to);
// if (matchingMaster) {
// masterName = matchingMaster.master_name ?? null;
// location = matchingMaster.location ?? null;
// }
// // Find other slaves connected to the same master (including this slave)
// slaves = await Insensors.find({
// customerId,
// connected_to: device.connected_to,
// type: 'slave'
// }).lean();
// } else {
// return reply.code(400).send({ success: false, message: "Device type unknown or missing connected_to" });
// }
// // Enrich master object
// const enrichedMaster = {
// ...master,
// masterName,
// location,
// hardwareId: master.hardwareId
// };
// // Enrich each slave with masterName & location
// const enrichedSlaves = slaves.map(slave => ({
// ...slave,
// masterName,
// location
// }));
// return reply.send({
// success: true,
// master: enrichedMaster,
// slaves: enrichedSlaves
// });
// } catch (error) {
// console.error("Error fetching master and slaves:", error);
// return reply.code(500).send({ success: false, message: "Internal server error" });
// }
// };
exports.getMasterWithSlaves = async (req, reply) => { exports.getMasterWithSlaves = async (req, reply) => {
try { try {
const { installationId, customerId, hardwareId } = req.params; const { installationId, customerId, hardwareId } = req.params;
@ -3831,62 +4003,50 @@ exports.getMasterWithSlaves = async (req, reply) => {
return reply.code(404).send({ success: false, message: "Order not found" }); return reply.code(404).send({ success: false, message: "Order not found" });
} }
// Find master device in Insensors // Find device (could be master or slave)
const master = await Insensors.findOne({ const device = await Insensors.findOne({ hardwareId, customerId }).lean();
hardwareId, if (!device) {
customerId, return reply.code(404).send({ success: false, message: "Device not found in Insensors" });
}).lean();
if (!master) {
return reply.code(404).send({ success: false, message: "Master device not found in Insensors" });
} }
let enrichedDevice = null;
if (device.type === 'master') {
// Find matching master in order.master_connections // Find matching master in order.master_connections
const matchingMaster = order.master_connections?.find(m => m.hardwareId === hardwareId); const matchingMaster = order.master_connections?.find(m => m.hardwareId === hardwareId);
let masterName = null; enrichedDevice = {
let location = null; ...device,
if (matchingMaster) { masterName: matchingMaster?.master_name ?? null,
masterName = matchingMaster.master_name ?? null; location: matchingMaster?.location ?? null,
location = matchingMaster.location ?? null;
}
// Build enriched master object
const enrichedMaster = {
...master,
masterName,
location,
hardwareId: master.hardwareId
}; };
// 👉 Find slaves connected to this master } else if (device.type === 'slave' && device.connected_to) {
const slaves = await Insensors.find({ // Find master device to get masterName and location
customerId, const masterInOrder = order.master_connections?.find(m => m.hardwareId === device.connected_to);
connected_to: hardwareId,
type: 'slave'
}).lean();
// Add masterName & location to each slave enrichedDevice = {
const enrichedSlaves = slaves.map(slave => ({ ...device,
...slave, masterName: masterInOrder?.master_name ?? null,
masterName, location: masterInOrder?.location ?? null,
location };
}));
} else {
return reply.code(400).send({ success: false, message: "Device type unknown or missing connected_to" });
}
return reply.send({ return reply.send({
success: true, success: true,
master: enrichedMaster, device: enrichedDevice
slaves: enrichedSlaves
}); });
} catch (error) { } catch (error) {
console.error("Error fetching master and slaves:", error); console.error("Error fetching device:", error);
return reply.code(500).send({ success: false, message: "Internal server error" }); return reply.code(500).send({ success: false, message: "Internal server error" });
} }
}; };
exports.getPendingMasterSlaveSummary = async (req, reply) => { exports.getPendingMasterSlaveSummary = async (req, reply) => {
try { try {
const { customerId } = req.params; const { customerId } = req.params;

Loading…
Cancel
Save