issuecount added

master^2
Bhaskar 4 months ago
parent f698d345df
commit 6fccf490fc

@ -6684,7 +6684,6 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
(issue) => issue.resolved === false && issue.movedToCategory === false (issue) => issue.resolved === false && issue.movedToCategory === false
) || []; ) || [];
// Create set of categorized hardwareIds (masters and slaves)
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(issue.hardwareId.trim().toLowerCase());
@ -6695,7 +6694,6 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
} }
}); });
// Extract all unresolved hardwareIds
const hardwareIdsArray = new Set(); const hardwareIdsArray = new Set();
unresolvedIssues.forEach((issue) => { unresolvedIssues.forEach((issue) => {
if (issue.hardwareId) hardwareIdsArray.add(issue.hardwareId.trim()); if (issue.hardwareId) hardwareIdsArray.add(issue.hardwareId.trim());
@ -6708,7 +6706,6 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
const allHardwareIds = [...hardwareIdsArray]; const allHardwareIds = [...hardwareIdsArray];
// Fetch disconnected sensors, excluding those already categorized
const disconnectedSensorsRaw = await Insensors.find({ const disconnectedSensorsRaw = await Insensors.find({
connected_status: "disconnected", connected_status: "disconnected",
$or: [ $or: [
@ -6727,7 +6724,6 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
return !ids.some(id => existingCategorizedHardwareIds.has(id)); return !ids.some(id => existingCategorizedHardwareIds.has(id));
}); });
// Map customerId -> Set of affected hardwareIds
const customerHardwareMap = {}; const customerHardwareMap = {};
for (const sensor of disconnectedSensors) { for (const sensor of disconnectedSensors) {
const custId = sensor.customerId; const custId = sensor.customerId;
@ -6768,53 +6764,25 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
customerId: { $in: Object.keys(customerHardwareMap) } customerId: { $in: Object.keys(customerHardwareMap) }
}).lean(); }).lean();
// const customerResults = customerDetails.map((customer) => {
// const affectedHardwareSet = customerHardwareMap[customer.customerId] || new Set();
// return {
// customerId: customer.customerId,
// buildingName: customer.buildingName || "",
// location: customer.location || "",
// username: customer.username || "",
// firstName: customer.profile?.firstName || "",
// lastName: customer.profile?.lastName || "",
// phone: customer.phone || user.profile?.contactNumber || "",
// email: customer.emails?.[0]?.email || "",
// phoneVerified: customer.phoneVerified || false,
// address1: customer.profile?.address1 || "",
// address2: customer.profile?.address2 || "",
// city: customer.profile?.city || "",
// latitude: customer.latitude,
// longitude: customer.longitude,
// totalHardwareIdsCount: affectedHardwareSet.size,
// hardwareIds: [...affectedHardwareSet]
// };
// });
const customerResults = customerDetails.map((customer) => { const customerResults = customerDetails.map((customer) => {
const customerId = customer.customerId; const customerId = customer.customerId;
const affectedHardwareSet = customerHardwareMap[customerId] || new Set(); const affectedHardwareSet = customerHardwareMap[customerId] || new Set();
const affectedLowerSet = new Set([...affectedHardwareSet].map(id => id.toLowerCase().trim())); const affectedLowerSet = new Set([...affectedHardwareSet].map(id => id.toLowerCase().trim()));
// Filter only sensors that belong to this customer and are disconnected const disconnectedSensorsForCustomer = disconnectedSensors.filter(
const disconnectedSensorsForCustomer = disconnectedSensors.filter(sensor => s => s.customerId === customerId && s.connected_status === "disconnected"
sensor.customerId === customerId
); );
// Collect actual disconnected hardwareIds (only masters and slaves) const disconnectedSlaves = disconnectedSensorsForCustomer.filter(s => s.type === 'slave');
const uniqueDisconnectedHardwareIds = new Set(); const disconnectedMasters = disconnectedSensorsForCustomer.filter(s => s.type === 'master');
for (const sensor of disconnectedSensorsForCustomer) { console.log("disconnectedMasters",disconnectedMasters)
if ( const uniqueDisconnectedHardwareIds = new Set([
(sensor.type === 'master' || sensor.type === 'slave') && ...disconnectedMasters.map(s => s.hardwareId?.trim()),
sensor.connected_status === 'disconnected' ...disconnectedSlaves.map(s => s.tankhardwareId?.trim() || s.hardwareId?.trim())
) { ].filter(Boolean));
const hw = sensor.tankhardwareId || sensor.hardwareId || sensor.connected_to;
if (hw) { const disconnectedCount = disconnectedMasters.length + disconnectedSlaves.length;
uniqueDisconnectedHardwareIds.add(hw.trim());
}
}
}
// Get all unresolved issues related to this customer's affected hardwareIds
const customerIssues = unresolvedIssues.filter(issue => { const customerIssues = unresolvedIssues.filter(issue => {
const allIssueHardwareIds = [ const allIssueHardwareIds = [
...(issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []), ...(issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []),
@ -6823,7 +6791,6 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
return allIssueHardwareIds.some(hw => affectedLowerSet.has(hw)); return allIssueHardwareIds.some(hw => affectedLowerSet.has(hw));
}); });
// Get latest ticket raise timestamp
const lastTicketRaisedAt = customerIssues.reduce((latest, issue) => { const lastTicketRaisedAt = customerIssues.reduce((latest, issue) => {
const issueTime = new Date(issue.lastTicketRaisedAt); const issueTime = new Date(issue.lastTicketRaisedAt);
if (!isNaN(issueTime)) { if (!isNaN(issueTime)) {
@ -6849,11 +6816,11 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
longitude: customer.longitude, longitude: customer.longitude,
totalHardwareIdsCount: uniqueDisconnectedHardwareIds.size, totalHardwareIdsCount: uniqueDisconnectedHardwareIds.size,
hardwareIds: [...uniqueDisconnectedHardwareIds], hardwareIds: [...uniqueDisconnectedHardwareIds],
lastTicketRaisedAt: lastTicketRaisedAt || null lastTicketRaisedAt: lastTicketRaisedAt || null,
disconnectedCount
}; };
}); });
return reply.code(200).send({ return reply.code(200).send({
success: true, success: true,
totalCustomers: customerResults.length, totalCustomers: customerResults.length,
@ -6873,6 +6840,7 @@ 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