ashok 4 months ago
commit 7342e0c7e2

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

Loading…
Cancel
Save