diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 29e33c37..dc77382a 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -3868,6 +3868,103 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { }; +exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => { + try { + const { support_teamMemberId } = req.params; + + if (!support_teamMemberId) { + return reply.code(400).send({ error: "support_teamMemberId is required" }); + } + + // 1. Find support document that includes the given team member ID + const supportRecord = await Support.findOne({ + "team_member.team_member.support_teamMemberId": support_teamMemberId + }).lean(); + + if (!supportRecord) { + return reply.code(404).send({ message: "No support record found for this team member ID" }); + } + + // 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 connected_to + const disconnectedSensors = await Insensors.find({ + connected_to: { $in: hardwareIds }, + connected_status: "disconnected" + }).lean(); + + if (disconnectedSensors.length === 0) { + return reply.code(404).send({ message: "No disconnected issues found" }); + } + + // 4. Get unique customerIds + const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))]; + + // 5. Get User data + const customers = await User.find({ customerId: { $in: customerIds } }).lean(); + + // 6. Format response + 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", + buildingName: user.buildingName, + stripePaymentStatus: user.stripePaymentStatus || false, + stripeSubscriptionStatus: user.stripeSubscriptionStatus || false, + createdAt: user.createdAt, + updatedAt: user.updatedAt + } + }; + } + } + + return reply.send({ + status_code: 200, + data: Object.values(uniqueCustomerMap) + }); + + } catch (error) { + console.error("Error in getDisconnectedCustomerDetailsByTeamMemberId:", error); + return reply.code(500).send({ error: "Internal server error" }); + } +}; exports.getDisconnectedMoveCustomerDetails = async (req, reply) => { diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 36226769..775119c9 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -548,6 +548,24 @@ module.exports = function (fastify, opts, next) { }, handler: installationController.getDisconnectedCustomerDetails, }); + + fastify.get("/api/disconnectedCustomersbyTeamMember/:support_teamMemberId", { + schema: { + description: "Get disconnected customer details by team member ID", + tags: ["Support"], + summary: "Disconnected Customer Details by Team Member", + params: { + type: "object", + properties: { + support_teamMemberId: { type: "string" } + }, + required: ["support_teamMemberId"] + } + }, + handler: installationController.getDisconnectedCustomerDetailsByTeamMemberId + }); + + fastify.get("/api/moveisseshebuildingdetails/:supportId", { schema: { description: "Move the ticket Get building details move for Support",