remove all connected issues

master^2
Bhaskar 4 months ago
parent f367785765
commit 13b24f05be

@ -4264,154 +4264,195 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => {
}
};
exports.getRemoveConnectedMastersWithSlaves = async (req, reply) => {
try {
const { supportId } = req.params;
if (!supportId) {
return reply.code(400).send({ error: "supportId is required" });
}
// exports.getRemoveConnectedMastersWithSlaves = async (req, reply) => {
// try {
// const { supportId } = req.params;
// if (!supportId) {
// return reply.code(400).send({ error: "supportId is required" });
// }
const supportRecord = await Support.findOne({ supportId }).lean();
if (!supportRecord) {
return reply.code(404).send({ message: "No support record found for this supportId" });
}
// const supportRecord = await Support.findOne({ supportId }).lean();
// if (!supportRecord) {
// return reply.code(404).send({ message: "No support record found for this supportId" });
// }
const allIssues = supportRecord.issues || [];
// const allIssues = supportRecord.issues || [];
// Gather all unique hardwareIds from issues
const hardwareSet = new Set();
allIssues.forEach(issue => {
if (issue.hardwareId) hardwareSet.add(issue.hardwareId);
if (issue.masterHardwareId) hardwareSet.add(issue.masterHardwareId);
if (issue.hardwareIds && Array.isArray(issue.hardwareIds)) {
issue.hardwareIds.forEach(id => hardwareSet.add(id));
}
});
const hardwareIds = [...hardwareSet];
// // Gather all unique hardwareIds from issues
// const hardwareSet = new Set();
// allIssues.forEach(issue => {
// if (issue.hardwareId) hardwareSet.add(issue.hardwareId);
// if (issue.masterHardwareId) hardwareSet.add(issue.masterHardwareId);
// if (issue.hardwareIds && Array.isArray(issue.hardwareIds)) {
// issue.hardwareIds.forEach(id => hardwareSet.add(id));
// }
// });
// const hardwareIds = [...hardwareSet];
// Fetch all sensors related to these hardwareIds
const sensors = await Insensors.find({
$or: [
{ hardwareId: { $in: hardwareIds } },
{ tankhardwareId: { $in: hardwareIds } }
]
}).lean();
// // Fetch all sensors related to these hardwareIds
// const sensors = await Insensors.find({
// $or: [
// { hardwareId: { $in: hardwareIds } },
// { tankhardwareId: { $in: hardwareIds } }
// ]
// }).lean();
// Map sensors by hardwareId and tankhardwareId
const sensorMap = {};
sensors.forEach(sensor => {
if (sensor.hardwareId) sensorMap[sensor.hardwareId] = sensor;
if (sensor.tankhardwareId) sensorMap[sensor.tankhardwareId] = sensor;
});
// // Map sensors by hardwareId and tankhardwareId
// const sensorMap = {};
// sensors.forEach(sensor => {
// if (sensor.hardwareId) sensorMap[sensor.hardwareId] = sensor;
// if (sensor.tankhardwareId) sensorMap[sensor.tankhardwareId] = sensor;
// });
// Determine customerId from support or sensors
let customerId = supportRecord.customerId;
if (!customerId) {
const firstSensor = sensors.find(sensor => sensor.customerId);
if (firstSensor) {
customerId = firstSensor.customerId;
} else {
return reply.code(404).send({ message: "Unable to determine customerId" });
}
}
// // Determine customerId from support or sensors
// let customerId = supportRecord.customerId;
// if (!customerId) {
// const firstSensor = sensors.find(sensor => sensor.customerId);
// if (firstSensor) {
// customerId = firstSensor.customerId;
// } else {
// return reply.code(404).send({ message: "Unable to determine customerId" });
// }
// }
// Fetch orders for enriching master/slave info
const orders = await Order.find({ customerId }).lean();
// // Fetch orders for enriching master/slave info
// const orders = await Order.find({ customerId }).lean();
const orderMap = {};
orders.forEach(order => {
(order.master_connections || []).forEach(conn => {
if (conn.hardwareId) {
orderMap[conn.hardwareId] = {
masterName: conn.master_name || null,
location: conn.location || null
};
}
});
});
// const orderMap = {};
// orders.forEach(order => {
// (order.master_connections || []).forEach(conn => {
// if (conn.hardwareId) {
// orderMap[conn.hardwareId] = {
// masterName: conn.master_name || null,
// location: conn.location || null
// };
// }
// });
// });
const slaveOrderMap = {};
orders.forEach(order => {
(order.tank_connections || []).forEach(conn => {
if (conn.hardwareId) {
slaveOrderMap[conn.hardwareId] = {
location: conn.location || null,
typeOfWater: conn.typeOfWater || null
};
}
});
});
// const slaveOrderMap = {};
// orders.forEach(order => {
// (order.tank_connections || []).forEach(conn => {
// if (conn.hardwareId) {
// slaveOrderMap[conn.hardwareId] = {
// location: conn.location || null,
// typeOfWater: conn.typeOfWater || null
// };
// }
// });
// });
const connectedMasters = [];
const updatedIssues = [];
// const connectedMasters = [];
// const updatedIssues = [];
// Process each issue to check connection status
for (const issue of allIssues) {
const masterId = issue.masterHardwareId || issue.hardwareId;
const masterSensor = sensorMap[masterId];
if (!masterSensor || masterSensor.type !== "master") {
// If no master sensor found or not a master, keep the issue as is
updatedIssues.push(issue);
continue;
}
// // Process each issue to check connection status
// for (const issue of allIssues) {
// const masterId = issue.masterHardwareId || issue.hardwareId;
// const masterSensor = sensorMap[masterId];
// if (!masterSensor || masterSensor.type !== "master") {
// // If no master sensor found or not a master, keep the issue as is
// updatedIssues.push(issue);
// continue;
// }
// Get connected slaves of this master
const connectedSlaves = await Insensors.find({
connected_to: masterSensor.hardwareId,
type: "slave"
}).lean();
// // Get connected slaves of this master
// const connectedSlaves = await Insensors.find({
// connected_to: masterSensor.hardwareId,
// type: "slave"
// }).lean();
// Check if master is connected
const isMasterConnected = masterSensor.connected_status === "connected";
// // Check if master is connected
// const isMasterConnected = masterSensor.connected_status === "connected";
// Check if all slaves are connected
const allSlavesConnected = connectedSlaves.every(slave => slave.connected_status === "connected");
// // Check if all slaves are connected
// const allSlavesConnected = connectedSlaves.every(slave => slave.connected_status === "connected");
if (isMasterConnected && allSlavesConnected) {
// All connected - prepare connected master object
const enrichedMaster = {
hardwareId: masterSensor.hardwareId,
masterName: orderMap[masterSensor.hardwareId]?.masterName || masterSensor.masterName || "",
location: orderMap[masterSensor.hardwareId]?.location || masterSensor.location || "",
type: "master",
connected_status: masterSensor.connected_status,
connected_slave_count: connectedSlaves.length,
connected_slaves: connectedSlaves.map(slave => ({
hardwareId: slave.tankhardwareId || slave.hardwareId,
tankName: slave.tankName || "",
location: slave.location || slaveOrderMap[slave.tankhardwareId || slave.hardwareId]?.location || "",
connected_status: slave.connected_status,
type: "slave",
typeOfWater: slave.typeOfWater || slaveOrderMap[slave.tankhardwareId || slave.hardwareId]?.typeOfWater || "",
connected_to: slave.connected_to
}))
};
// if (isMasterConnected && allSlavesConnected) {
// // All connected - prepare connected master object
// const enrichedMaster = {
// hardwareId: masterSensor.hardwareId,
// masterName: orderMap[masterSensor.hardwareId]?.masterName || masterSensor.masterName || "",
// location: orderMap[masterSensor.hardwareId]?.location || masterSensor.location || "",
// type: "master",
// connected_status: masterSensor.connected_status,
// connected_slave_count: connectedSlaves.length,
// connected_slaves: connectedSlaves.map(slave => ({
// hardwareId: slave.tankhardwareId || slave.hardwareId,
// tankName: slave.tankName || "",
// location: slave.location || slaveOrderMap[slave.tankhardwareId || slave.hardwareId]?.location || "",
// connected_status: slave.connected_status,
// type: "slave",
// typeOfWater: slave.typeOfWater || slaveOrderMap[slave.tankhardwareId || slave.hardwareId]?.typeOfWater || "",
// connected_to: slave.connected_to
// }))
// };
connectedMasters.push(enrichedMaster);
// Do NOT add this issue to updatedIssues (removing it from issues)
} else {
// Not all connected, keep the issue in support issues
updatedIssues.push(issue);
// connectedMasters.push(enrichedMaster);
// // Do NOT add this issue to updatedIssues (removing it from issues)
// } else {
// // Not all connected, keep the issue in support issues
// updatedIssues.push(issue);
// }
// }
// // Update the Support document issues with filtered updatedIssues
// await Support.updateOne({ supportId }, { $set: { issues: updatedIssues } });
// return reply.send({
// status_code: 200,
// supportId,
// totalConnectedMasters: connectedMasters.length,
// connectedMasters
// });
// } catch (error) {
// console.error("Error in getConnectedMastersWithSlaves:", error);
// return reply.code(500).send({ error: "Internal server error" });
// }
// };
exports.getRemoveConnectedMastersWithSlaves = async (req, reply) => {
try {
const { supportId, hardwareId } = req.params;
if (!supportId || !hardwareId) {
return reply.code(400).send({ error: "supportId and hardwareId are required" });
}
// Find the support record
const supportRecord = await Support.findOne({ supportId }).lean();
if (!supportRecord) {
return reply.code(404).send({ message: "No support record found for this supportId" });
}
// Update the Support document issues with filtered updatedIssues
await Support.updateOne({ supportId }, { $set: { issues: updatedIssues } });
const originalIssues = supportRecord.issues || [];
// Filter out issues where hardwareId matches hardwareId or masterHardwareId
const filteredIssues = originalIssues.filter(issue => {
return !(
issue.hardwareId === hardwareId ||
issue.masterHardwareId === hardwareId
);
});
// Update the support record
await Support.updateOne(
{ supportId },
{ $set: { issues: filteredIssues } }
);
return reply.send({
status_code: 200,
supportId,
totalConnectedMasters: connectedMasters.length,
connectedMasters
message: `Issue(s) with hardwareId "${hardwareId}" removed successfully.`,
remainingIssues: filteredIssues
});
} catch (error) {
console.error("Error in getConnectedMastersWithSlaves:", error);
console.error("Error in removeIssueByHardwareId:", error);
return reply.code(500).send({ error: "Internal server error" });
}
};
exports.getDisconnectedCustomerDetails = async (req, reply) => {
try {
const { supportId } = req.params;

@ -562,7 +562,7 @@ module.exports = function (fastify, opts, next) {
},
handler: installationController.getDisconnectedIssuesBySupportId,
});
fastify.get("/api/getRemoveAllConnectedIsuues/:supportId", {
fastify.get("/api/getRemoveAllConnectedIsuues/:supportId/:hardwareId", {
schema: {
description: "Remove all connected list for Support",
tags: ["Support"],
@ -571,7 +571,7 @@ module.exports = function (fastify, opts, next) {
type: "object",
properties: {
supportId: { type: "string" },
hardwareId: { type: "string" },
},
required: [ "supportId"],
},

Loading…
Cancel
Save