all connected remove from the support

master^2
Bhaskar 5 months ago
parent 95fb53ca1c
commit 145b40e04d

@ -4270,6 +4270,152 @@ 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" });
}
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 || [];
// 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();
// 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" });
}
}
// 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 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 = [];
// 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();
// 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");
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);
}
}
// 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.getDisconnectedCustomerDetails = async (req, reply) => { exports.getDisconnectedCustomerDetails = async (req, reply) => {

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

Loading…
Cancel
Save