From 46ec234fb67daa532ccf37e9ab874aa6ef4cda21 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 30 Apr 2025 11:28:15 +0530 Subject: [PATCH] changes --- src/controllers/installationController.js | 89 ++++++++++++----------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index c1eb95bd..6f88f57b 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -2736,7 +2736,7 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { return reply.code(404).send({ message: "No hardware IDs found in issues" }); } - // 3. Find disconnected Insensors (matching hardwareId or connected_to) + // 3. Find disconnected Insensors const disconnectedSensors = await Insensors.find({ $or: [ { hardwareId: { $in: hardwareIds } }, @@ -2753,50 +2753,49 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))]; const customers = await User.find({ customerId: { $in: customerIds } }).lean(); - // 5. Build enriched response - const response = disconnectedSensors.map(sensor => { - const user = customers.find(u => u.customerId === sensor.customerId); + // 5. Map unique customers + const uniqueCustomerMap = {}; + for (const user of customers) { + if (!uniqueCustomerMap[user.customerId]) { + uniqueCustomerMap[user.customerId] = { + customer: { + customerId: user.customerId, + username: user.username || "", + firstName: user.profile?.firstName || "", + lastName: user.profile?.lastName || "", + phone: user.phone || user.profile?.contactNumber || "", + email: user.emails?.[0]?.email || "", + phoneVerified: user.phoneVerified || false, + address1: user.profile?.address1 || "", + address2: user.profile?.address2 || "", + city: user.profile?.city || "", + state: user.profile?.state || "", + country: user.profile?.country || "", + zip: user.profile?.zip || "", + notes: user.profile?.notes || "", + latitude: user.latitude, + longitude: user.longitude, + fcmIds: (user.fcmIds || []).filter(fcm => typeof fcm === "string" && fcm.startsWith("d")), + installationId: user.installationId || "", + notificationPreferences: { + allowNotifications: user.allowNotifications || false, + automaticStartAndStopNotify: user.automaticStartAndStopNotify || false, + manualStartAndStopNotify: user.manualStartAndStopNotify || false, + criticalLowWaterAlert: user.criticalLowWaterAlert || false, + lowWaterAlert: user.lowWaterAlert || false, + notificationPreference: user.notificationPreference || "never" + }, + surveyStatus: user.survey_status || "pending", + stripePaymentStatus: user.stripePaymentStatus || false, + stripeSubscriptionStatus: user.stripeSubscriptionStatus || false, + createdAt: user.createdAt, + updatedAt: user.updatedAt + } + }; + } + } - return { - // hardwareId: sensor.hardwareId, - // masterName: sensor.masterName, - // location: sensor.location, - // disconnectedAt: sensor.disconnectedAt || new Date().toISOString(), - customer: user ? { - customerId: user.customerId, - username: user.username || "", - firstName: user.profile?.firstName || "", - lastName: user.profile?.lastName || "", - phone: user.phone || user.profile?.contactNumber || "", - email: user.emails?.[0]?.email || "", - phoneVerified: user.phoneVerified || false, - address1: user.profile?.address1 || "", - address2: user.profile?.address2 || "", - city: user.profile?.city || "", - state: user.profile?.state || "", - country: user.profile?.country || "", - zip: user.profile?.zip || "", - notes: user.profile?.notes || "", - latitude: user.latitude, - longitude: user.longitude, - fcmIds: user.fcmIds || [], - installationId: user.installationId || "", - notificationPreferences: { - allowNotifications: user.allowNotifications || false, - automaticStartAndStopNotify: user.automaticStartAndStopNotify || false, - manualStartAndStopNotify: user.manualStartAndStopNotify || false, - criticalLowWaterAlert: user.criticalLowWaterAlert || false, - lowWaterAlert: user.lowWaterAlert || false, - notificationPreference: user.notificationPreference || "never" - }, - surveyStatus: user.survey_status || "pending", - stripePaymentStatus: user.stripePaymentStatus || false, - stripeSubscriptionStatus: user.stripeSubscriptionStatus || false, - createdAt: user.createdAt, - updatedAt: user.updatedAt - } : null - }; - }); + const response = Object.values(uniqueCustomerMap); return reply.send({ status_code: 200, @@ -2810,3 +2809,5 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { }; + +