diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 596be2f8..a4e531e0 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -3729,6 +3729,111 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => { +exports.getDisconnectedMoveCustomerDetails = async (req, reply) => { + try { + const { supportId } = req.params; + + if (!supportId) { + return reply.code(400).send({ error: "supportId is required" }); + } + + // 1. Fetch support record + const supportRecord = await Support.findOne({ supportId }).lean(); + if (!supportRecord) { + return reply.code(404).send({ message: "No support record found for this supportId" }); + } + + // 2. Only proceed if categorizedIssues exist + if (!Array.isArray(supportRecord.categorizedIssues) || supportRecord.categorizedIssues.length === 0) { + return reply.code(404).send({ message: "No categorized issues to process" }); + } + + // 3. Collect hardware IDs from categorized issues + const categorizedHardwareIds = []; + for (const issue of supportRecord.categorizedIssues) { + if (issue.hardwareId) categorizedHardwareIds.push(issue.hardwareId); + if (Array.isArray(issue.hardwareIds)) categorizedHardwareIds.push(...issue.hardwareIds); + } + + if (categorizedHardwareIds.length === 0) { + return reply.code(404).send({ message: "No hardware IDs in categorized issues" }); + } + + // 4. Get disconnected sensors from Insensors + const disconnectedSensors = await Insensors.find({ + $or: [ + { hardwareId: { $in: categorizedHardwareIds } }, + { connected_to: { $in: categorizedHardwareIds } } + ], + connected_status: "disconnected" + }).lean(); + + if (disconnectedSensors.length === 0) { + return reply.code(404).send({ message: "No disconnected sensors found" }); + } + + // 5. Get unique customerIds + const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))]; + + // 6. Fetch corresponding customers + const customers = await User.find({ customerId: { $in: customerIds } }).lean(); + + const uniqueCustomerMap = {}; + + for (const user of customers) { + const cid = user.customerId; + if (!uniqueCustomerMap[cid]) { + uniqueCustomerMap[cid] = { + customer: { + customerId: cid, + username: user.username || "", + firstName: user.profile?.firstName || user.firstName || "", + lastName: user.profile?.lastName || user.lastName || "", + phone: user.phone || user.profile?.contactNumber || user.alternativeNumber || "", + email: user.emails?.[0]?.email || user.email || "", + phoneVerified: user.phoneVerified || false, + address1: user.profile?.address1 || user.address1 || "", + address2: user.profile?.address2 || user.address2 || "", + city: user.profile?.city || user.city || "", + state: user.profile?.state || user.state || "", + country: user.profile?.country || user.country || "", + zip: user.profile?.zip || "", + notes: user.profile?.notes || "", + latitude: user.latitude || 0, + longitude: user.longitude || 0, + 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 + } + }; + } + } + + const response = Object.values(uniqueCustomerMap); + + return reply.send({ + status_code: 200, + data: response + }); + + } catch (error) { + console.error("Error fetching disconnected customer details:", error); + return reply.code(500).send({ error: "Internal server error" }); + } +}; // const bcrypt = require("bcrypt"); diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index d8f13693..68842a00 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -548,7 +548,22 @@ module.exports = function (fastify, opts, next) { }, handler: installationController.getDisconnectedCustomerDetails, }); - + fastify.get("/api/moveisseshebuildingdetails/:supportId", { + schema: { + description: "Move the ticket Get building details move for Support", + tags: ["Support"], + summary: "Move the ticket Get building details move for Support", + params: { + type: "object", + properties: { + supportId: { type: "string" }, + + }, + required: [ "supportId"], + }, + }, + handler: installationController.getDisconnectedMoveCustomerDetails, + }); fastify.route({ method: 'POST', url: '/api/supportCreateTeamMember/:supportId',