From 9b19ac6f30a8ad0545f461131f1f78f381fabed2 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 30 Apr 2025 11:00:11 +0530 Subject: [PATCH] chnages --- src/controllers/installationController.js | 98 +++++++++++++++++++++++ src/routes/installationRoute.js | 17 ++++ 2 files changed, 115 insertions(+) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 99e146cd..4af13907 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -2711,4 +2711,102 @@ exports.getDisconnectedIssuesBySupportId = async (req, reply) => { return reply.code(500).send({ error: "Internal server error" }); } }; +exports.getDisconnectedCustomerDetails = async (req, reply) => { + try { + const { supportId } = req.params; + + if (!supportId) { + return reply.code(400).send({ error: "supportId is required" }); + } + + // 1. Get the support record by supportId + const supportRecord = await Support.findOne({ supportId }).lean(); + if (!supportRecord) { + return reply.code(404).send({ message: "No support record found for this supportId" }); + } + + // 2. Extract all hardwareIds from issues + const hardwareIds = []; + for (const issue of supportRecord.issues) { + if (issue.hardwareId) hardwareIds.push(issue.hardwareId); + if (Array.isArray(issue.hardwareIds)) hardwareIds.push(...issue.hardwareIds); + } + + if (hardwareIds.length === 0) { + return reply.code(404).send({ message: "No hardware IDs found in issues" }); + } + + // 3. Find disconnected Insensors (matching hardwareId or connected_to) + const disconnectedSensors = await Insensors.find({ + $or: [ + { hardwareId: { $in: hardwareIds } }, + { connected_to: { $in: hardwareIds } } + ], + connected_status: "disconnected" + }).lean(); + + if (disconnectedSensors.length === 0) { + return reply.code(404).send({ message: "No disconnected issues found" }); + } + + // 4. Get all unique customerIds + 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); + + 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 + }; + }); + + return reply.send({ + status_code: 200, + disconnectedIssues: response + }); + + } catch (error) { + console.error("Error fetching disconnected customer details:", error); + return reply.code(500).send({ error: "Internal server error" }); + } +}; + diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 7a8b7fee..a53105b7 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -499,6 +499,23 @@ module.exports = function (fastify, opts, next) { }, handler: installationController.getDisconnectedIssuesBySupportId, }); + + fastify.get("/api/fetchthebuildingdetails/:supportId", { + schema: { + description: "Get building details for Support", + tags: ["Support"], + summary: "Get building details list for Support", + params: { + type: "object", + properties: { + supportId: { type: "string" }, + + }, + required: [ "supportId"], + }, + }, + handler: installationController.getDisconnectedCustomerDetails, + }); next(); } \ No newline at end of file