|
|
|
@ -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) => {
|
|
|
|
|
try {
|
|
|
|
|
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" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 device (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" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find matching master in order.master_connections
|
|
|
|
|
const matchingMaster = order.master_connections?.find(m => m.hardwareId === hardwareId);
|
|
|
|
|
let enrichedDevice = null;
|
|
|
|
|
|
|
|
|
|
let masterName = null;
|
|
|
|
|
let location = null;
|
|
|
|
|
if (matchingMaster) {
|
|
|
|
|
masterName = matchingMaster.master_name ?? null;
|
|
|
|
|
location = matchingMaster.location ?? null;
|
|
|
|
|
}
|
|
|
|
|
if (device.type === 'master') {
|
|
|
|
|
// Find matching master in order.master_connections
|
|
|
|
|
const matchingMaster = order.master_connections?.find(m => m.hardwareId === hardwareId);
|
|
|
|
|
|
|
|
|
|
// Build enriched master object
|
|
|
|
|
const enrichedMaster = {
|
|
|
|
|
...master,
|
|
|
|
|
masterName,
|
|
|
|
|
location,
|
|
|
|
|
hardwareId: master.hardwareId
|
|
|
|
|
};
|
|
|
|
|
enrichedDevice = {
|
|
|
|
|
...device,
|
|
|
|
|
masterName: matchingMaster?.master_name ?? null,
|
|
|
|
|
location: matchingMaster?.location ?? null,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 👉 Find slaves connected to this master
|
|
|
|
|
const slaves = await Insensors.find({
|
|
|
|
|
customerId,
|
|
|
|
|
connected_to: hardwareId,
|
|
|
|
|
type: 'slave'
|
|
|
|
|
}).lean();
|
|
|
|
|
} else if (device.type === 'slave' && device.connected_to) {
|
|
|
|
|
// Find master device to get masterName and location
|
|
|
|
|
const masterInOrder = order.master_connections?.find(m => m.hardwareId === device.connected_to);
|
|
|
|
|
|
|
|
|
|
// Add masterName & location to each slave
|
|
|
|
|
const enrichedSlaves = slaves.map(slave => ({
|
|
|
|
|
...slave,
|
|
|
|
|
masterName,
|
|
|
|
|
location
|
|
|
|
|
}));
|
|
|
|
|
enrichedDevice = {
|
|
|
|
|
...device,
|
|
|
|
|
masterName: masterInOrder?.master_name ?? null,
|
|
|
|
|
location: masterInOrder?.location ?? null,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
return reply.code(400).send({ success: false, message: "Device type unknown or missing connected_to" });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return reply.send({
|
|
|
|
|
success: true,
|
|
|
|
|
master: enrichedMaster,
|
|
|
|
|
slaves: enrichedSlaves
|
|
|
|
|
device: enrichedDevice
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} 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" });
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
exports.getPendingMasterSlaveSummary = async (req, reply) => {
|
|
|
|
|
try {
|
|
|
|
|
const { customerId } = req.params;
|
|
|
|
|