master Name location added

master^2
Bhaskar 3 months ago
parent 31588fe011
commit 5e5e06d345

@ -3536,47 +3536,47 @@ exports.getCompleteMasterSlaveSummary = async (req, reply) => {
return reply.status(500).send({ error: "Internal Server Error" }); return reply.status(500).send({ error: "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;
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 // if (!installationId || !customerId || !hardwareId) {
const master = await Insensors.findOne({ // return reply.code(400).send({ success: false, message: "installationId, customerId, and hardwareId are required" });
hardwareId, // }
customerId,
}).lean();
if (!master) { // // Find order
return reply.code(404).send({ success: false, message: "Master device not found in Insensors" }); // const order = await Order.findOne({ installationId, customerId }).lean();
} // if (!order) {
// return reply.code(404).send({ success: false, message: "Order not found" });
// }
// // Find slaves connected to this master // // Find master device in Insensors
// const slaves = await Insensors.find({ // const master = await Insensors.findOne({
// connected_to: hardwareId, // hardwareId,
// customerId, // customerId,
// type: 'slave'
// }).lean(); // }).lean();
return reply.send({ // if (!master) {
success: true, // return reply.code(404).send({ success: false, message: "Master device not found in Insensors" });
master, // }
});
} catch (error) { // // // Find slaves connected to this master
console.error("Error fetching master and slaves:", error); // // const slaves = await Insensors.find({
return reply.code(500).send({ success: false, message: "Internal server error" }); // // connected_to: hardwareId,
} // // customerId,
}; // // type: 'slave'
// // }).lean();
// 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" });
// }
// };
// exports.editTankDimensions = async (req, reply) => { // exports.editTankDimensions = async (req, reply) => {
// try { // 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) => { exports.getPendingMasterSlaveSummary = async (req, reply) => {
try { try {

@ -731,61 +731,7 @@ fastify.post(
hardwareId: { type: 'string', description: 'Master hardwareId' }, 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, handler: installationController.getMasterWithSlaves,

Loading…
Cancel
Save