ashok 4 months ago
commit 0c6aa9a181

@ -6427,14 +6427,17 @@ exports.getRemoveConnectedMastersWithSlaves = async (req, reply) => {
// } // }
// }; // };
exports.getDisconnectedCustomerDetails = async (req, reply) => { exports.getDisconnectedCustomerDetails = 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 normalize = id => (typeof id === "string" ? id.trim().toLowerCase() : "");
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" });
@ -6448,19 +6451,17 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
// Step 2: Build set of hardwareIds already in categorizedIssues (moved) // Step 2: Build set of hardwareIds already in categorizedIssues (moved)
const existingCategorizedHardwareIds = new Set(); const existingCategorizedHardwareIds = new Set();
(supportRecord.categorizedIssues || []).forEach(issue => { (supportRecord.categorizedIssues || []).forEach(issue => {
if (issue.hardwareId) existingCategorizedHardwareIds.add(issue.hardwareId.trim().toLowerCase()); if (issue.hardwareId) existingCategorizedHardwareIds.add(normalize(issue.hardwareId));
if (Array.isArray(issue.hardwareIds)) { if (Array.isArray(issue.hardwareIds)) {
issue.hardwareIds.forEach(id => { issue.hardwareIds.forEach(id => existingCategorizedHardwareIds.add(normalize(id)));
if (typeof id === "string") existingCategorizedHardwareIds.add(id.trim().toLowerCase());
});
} }
}); });
// Step 3: Build list of hardwareIds in unresolved issues (excluding moved) // Step 3: Build list of hardwareIds in unresolved issues (excluding moved)
const allHardwareIds = new Set(); const allHardwareIds = new Set();
for (const issue of unresolvedIssues) { for (const issue of unresolvedIssues) {
const issueHardwareId = issue.hardwareId?.trim().toLowerCase(); const issueHardwareId = normalize(issue.hardwareId);
const issueSlaveIds = issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []; const issueSlaveIds = (issue.hardwareIds || []).map(normalize);
if (issueHardwareId && !existingCategorizedHardwareIds.has(issueHardwareId)) { if (issueHardwareId && !existingCategorizedHardwareIds.has(issueHardwareId)) {
allHardwareIds.add(issueHardwareId); allHardwareIds.add(issueHardwareId);
@ -6473,7 +6474,6 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
} }
} }
// Debug: Log issue hardware IDs being considered
console.log("✅ All issue hardwareIds:", Array.from(allHardwareIds)); console.log("✅ All issue hardwareIds:", Array.from(allHardwareIds));
// Step 4: If no valid hardware IDs left, stop // Step 4: If no valid hardware IDs left, stop
@ -6493,7 +6493,6 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
] ]
}).lean(); }).lean();
// Debug: Log disconnected sensors found
console.log("⚙️ Disconnected sensors matched:", disconnectedSensors.map(s => s.hardwareId)); console.log("⚙️ Disconnected sensors matched:", disconnectedSensors.map(s => s.hardwareId));
if (!disconnectedSensors.length) { if (!disconnectedSensors.length) {
@ -6512,53 +6511,16 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
customerHardwareMap[custId] = new Set(); customerHardwareMap[custId] = new Set();
} }
const sensorHw = sensor.tankhardwareId?.trim().toLowerCase(); const sensorHw = normalize(sensor.tankhardwareId);
const sensorConnected = sensor.connected_to?.trim().toLowerCase(); const sensorConnected = normalize(sensor.connected_to);
const sensorHardwareId = normalize(sensor.hardwareId);
// for (const issue of unresolvedIssues) {
// const allIssueHardwareIds = [
// ...(issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []),
// issue.hardwareId?.trim().toLowerCase()
// ];
// if (
// (sensorHw && allIssueHardwareIds.includes(sensorHw)) ||
// (sensorConnected && allIssueHardwareIds.includes(sensorConnected))
// ) {
// if (issue.hardwareId && !existingCategorizedHardwareIds.has(issue.hardwareId.trim().toLowerCase())) {
// customerHardwareMap[custId].add(issue.hardwareId.trim().toLowerCase());
// }
// }
// }
const sensorHardwareId = sensor.hardwareId?.trim().toLowerCase();
// for (const issue of unresolvedIssues) {
// const allIssueHardwareIds = [
// ...(issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []),
// issue.hardwareId?.trim().toLowerCase()
// ];
// if (
// (sensorHw && allIssueHardwareIds.includes(sensorHw)) ||
// (sensorConnected && allIssueHardwareIds.includes(sensorConnected)) ||
// (sensorHardwareId && allIssueHardwareIds.includes(sensorHardwareId))
// ) {
// for (const hw of allIssueHardwareIds) {
// if (hw && !existingCategorizedHardwareIds.has(hw)) {
// customerHardwareMap[custId].add(hw);
// }
// }
// }
// }
for (const issue of unresolvedIssues) { for (const issue of unresolvedIssues) {
if (issue.movedToCategory === true) continue; // ✅ extra safeguard
const allIssueHardwareIds = [ const allIssueHardwareIds = [
...(issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []), normalize(issue.hardwareId),
issue.hardwareId?.trim().toLowerCase() ...(issue.hardwareIds || []).map(normalize)
]; ];
if ( if (
(sensorHw && allIssueHardwareIds.includes(sensorHw)) || (sensorHw && allIssueHardwareIds.includes(sensorHw)) ||
(sensorConnected && allIssueHardwareIds.includes(sensorConnected)) || (sensorConnected && allIssueHardwareIds.includes(sensorConnected)) ||
@ -6571,12 +6533,8 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
} }
} }
} }
} }
// Debug: Log map of matched customer hardware
console.log("📌 Customer hardware map:", Object.fromEntries( console.log("📌 Customer hardware map:", Object.fromEntries(
Object.entries(customerHardwareMap).map(([k, v]) => [k, Array.from(v)]) Object.entries(customerHardwareMap).map(([k, v]) => [k, Array.from(v)])
)); ));
@ -6590,9 +6548,10 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
if (hardwareIdSet.size === 0) continue; if (hardwareIdSet.size === 0) continue;
const relatedIssues = unresolvedIssues.filter(issue => { const relatedIssues = unresolvedIssues.filter(issue => {
const issueHw = issue.hardwareId?.trim().toLowerCase(); const allIds = [
const hardwareIds = issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []; normalize(issue.hardwareId),
const allIds = [issueHw, ...hardwareIds]; ...(issue.hardwareIds || []).map(normalize)
];
return Array.from(hardwareIdSet).some(hw => allIds.includes(hw)); return Array.from(hardwareIdSet).some(hw => allIds.includes(hw));
}); });
@ -6658,9 +6617,6 @@ 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