remove all connected issues

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

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

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

Loading…
Cancel
Save