ashok 4 months ago
commit c50aad8b74

@ -5532,8 +5532,6 @@ exports.getRemoveConnectedMastersWithSlaves = async (req, reply) => {
} }
}; };
// const moment = require("moment-timezone");
exports.getDisconnectedCustomerDetails = async (req, reply) => { exports.getDisconnectedCustomerDetails = async (req, reply) => {
try { try {
const { supportId } = req.params; const { supportId } = req.params;
@ -5567,44 +5565,32 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
} }
const hardwareIdsArray = Array.from(allHardwareIds); const hardwareIdsArray = Array.from(allHardwareIds);
console.log("hardwareIdsArray",hardwareIdsArray)
// 4. Find disconnected insensors using connected_to match // 4. Find disconnected insensors using connected_to match
// const disconnectedSensors = await Insensors.find({
// connected_to: { $in: hardwareIdsArray },
// connected_status: "disconnected"
// }).lean();
const disconnectedSensors = await Insensors.find({ const disconnectedSensors = await Insensors.find({
connected_to: { $in: hardwareIdsArray }, connected_to: { $in: hardwareIdsArray },
connected_status: "disconnected" connected_status: "disconnected",
}).lean();
if (!disconnectedSensors.length) {
return reply.code(404).send({ message: "No disconnected issues found" });
}
// 4.5 Exclude customers who have any sensor with support_issue_status = "active"
const disconnectedCustomerIds = [...new Set(disconnectedSensors.map(s => s.customerId))];
const activeSensors = await Insensors.find({
customerId: { $in: disconnectedCustomerIds },
support_issue_status: "active" support_issue_status: "active"
}).lean(); }).lean();
const activeCustomerIds = new Set(activeSensors.map(s => s.customerId)); console.log("disconnectedSensors",disconnectedSensors)
// Filter disconnectedSensors to exclude customers with active sensors
const filteredDisconnectedSensors = disconnectedSensors.filter(
s => !activeCustomerIds.has(s.customerId)
);
if (filteredDisconnectedSensors.length === 0) { if (!disconnectedSensors.length) {
return reply.code(404).send({ message: "No disconnected issues found after filtering active sensors" }); return reply.code(404).send({ message: "No disconnected issues found" });
} }
// 5. Get unique customerIds from filtered disconnected sensors // 5. Get unique customerIds from disconnected sensors
const customerIds = [...new Set(filteredDisconnectedSensors.map(s => s.customerId))]; const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))];
const customers = await User.find({ customerId: { $in: customerIds } }).lean(); const customers = await User.find({ customerId: { $in: customerIds } }).lean();
// 6. For each customer, calculate total unique hardwareIds involved in their disconnected sensors // 6. For each customer, calculate total unique hardwareIds involved in their disconnected sensors
const customerHardwareMap = {}; const customerHardwareMap = {};
for (const sensor of filteredDisconnectedSensors) { for (const sensor of disconnectedSensors) {
const custId = sensor.customerId; const custId = sensor.customerId;
if (!customerHardwareMap[custId]) { if (!customerHardwareMap[custId]) {
customerHardwareMap[custId] = new Set(); customerHardwareMap[custId] = new Set();
@ -5625,9 +5611,12 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
) { ) {
customerHardwareMap[custId].add(issue.hardwareId); customerHardwareMap[custId].add(issue.hardwareId);
} }
console.log("allIssueHardwareIds",allIssueHardwareIds)
} }
} }
// 7. Build final response // 7. Build final response
const response = []; const response = [];
@ -5640,9 +5629,12 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
const issueHw = issue.hardwareId?.trim().toLowerCase(); const issueHw = issue.hardwareId?.trim().toLowerCase();
const hardwareIds = issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []; const hardwareIds = issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || [];
const allIds = [issueHw, ...hardwareIds]; const allIds = [issueHw, ...hardwareIds];
console.log("allIds",allIds)
console.log("Array.from(hardwareIdSet).some(hw => allIds.includes(hw?.trim().toLowerCase()))",Array.from(hardwareIdSet).some(hw => allIds.includes(hw?.trim().toLowerCase())))
return Array.from(hardwareIdSet).some(hw => allIds.includes(hw?.trim().toLowerCase())); return Array.from(hardwareIdSet).some(hw => allIds.includes(hw?.trim().toLowerCase()));
});
});
//console.log("relatedIssues",relatedIssues)
let latestIssueTime = null; let latestIssueTime = null;
for (const issue of relatedIssues) { for (const issue of relatedIssues) {
if (issue.lastTicketRaisedAt) { if (issue.lastTicketRaisedAt) {
@ -5706,7 +5698,6 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
// exports.getDisconnectedCustomerDetails = async (req, reply) => { // exports.getDisconnectedCustomerDetails = async (req, reply) => {
// try { // try {
// const { supportId } = req.params; // const { supportId } = req.params;

Loading…
Cancel
Save