duplicates are added in get all issues removed

master^2
Bhaskar 5 months ago
parent b28580bbd6
commit 53c7476e79

@ -5125,9 +5125,12 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
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" });
} }
// 2. Collect all hardwareIds (hardwareId + hardwareIds[]) // 2. Filter only unresolved issues
const unresolvedIssues = (supportRecord.issues || []).filter(issue => issue.resolved === false);
// 3. Collect all hardwareIds (hardwareId + hardwareIds[] from unresolved issues)
const allHardwareIds = new Set(); const allHardwareIds = new Set();
for (const issue of supportRecord.issues) { for (const issue of unresolvedIssues) {
if (issue.hardwareId) allHardwareIds.add(issue.hardwareId.trim().toLowerCase()); if (issue.hardwareId) allHardwareIds.add(issue.hardwareId.trim().toLowerCase());
if (Array.isArray(issue.hardwareIds)) { if (Array.isArray(issue.hardwareIds)) {
issue.hardwareIds.forEach(id => { issue.hardwareIds.forEach(id => {
@ -5137,12 +5140,12 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
} }
if (allHardwareIds.size === 0) { if (allHardwareIds.size === 0) {
return reply.code(404).send({ message: "No hardware IDs found in issues" }); return reply.code(404).send({ message: "No unresolved hardware IDs found in issues" });
} }
const hardwareIdsArray = Array.from(allHardwareIds); const hardwareIdsArray = Array.from(allHardwareIds);
// 3. Find disconnected insensors using connected_to match (normalize connected_to) // 4. Find disconnected insensors using connected_to match
const disconnectedSensors = await Insensors.find({ const disconnectedSensors = await Insensors.find({
connected_to: { $in: hardwareIdsArray } connected_to: { $in: hardwareIdsArray }
}).lean(); }).lean();
@ -5151,11 +5154,11 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
return reply.code(404).send({ message: "No disconnected issues found" }); return reply.code(404).send({ message: "No disconnected issues found" });
} }
// 4. Get unique customerIds from disconnected sensors // 5. Get unique customerIds from disconnected sensors
const customerIds = [...new Set(disconnectedSensors.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();
// 5. 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 disconnectedSensors) { for (const sensor of disconnectedSensors) {
@ -5167,7 +5170,7 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
const sensorHw = sensor.tankhardwareId?.trim().toLowerCase(); const sensorHw = sensor.tankhardwareId?.trim().toLowerCase();
const sensorConnected = sensor.connected_to?.trim().toLowerCase(); const sensorConnected = sensor.connected_to?.trim().toLowerCase();
for (const issue of supportRecord.issues) { for (const issue of unresolvedIssues) {
const issueHw = issue.hardwareId?.trim().toLowerCase(); const issueHw = issue.hardwareId?.trim().toLowerCase();
if ( if (
@ -5188,15 +5191,15 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
} }
} }
// 6. Build final response // 7. Build final response
const response = []; const response = [];
for (const user of customers) { for (const user of customers) {
const custId = user.customerId; const custId = user.customerId;
const hardwareIdSet = customerHardwareMap[custId] || new Set(); const hardwareIdSet = customerHardwareMap[custId] || new Set();
// Extract latest issue-specific lastTicketRaisedAt // Extract latest unresolved issue-specific lastTicketRaisedAt
const relatedIssues = supportRecord.issues.filter(issue => { const relatedIssues = unresolvedIssues.filter(issue => {
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];
@ -5269,6 +5272,8 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => { exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => {
try { try {
const { support_teamMemberId } = req.params; const { support_teamMemberId } = req.params;

Loading…
Cancel
Save