diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 072def60..7baea1a3 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -8137,6 +8137,130 @@ for (const issue of resolvedIssues) { } }; +exports.getLongTermCustomerDetails = async (req, reply) => { + try { + const { supportId } = req.params; + + if (!supportId) { + return reply.code(400).send({ error: "supportId is required" }); + } + + const supportRecord = await Support.findOne({ supportId }).lean(); + if (!supportRecord) { + return reply.code(404).send({ message: "No support record found for this supportId" }); + } + + const categorizedIssues = supportRecord.categorizedIssues || []; + const longTermIssues = categorizedIssues.filter(issue => issue.category === "LongTerm Issues"); + + if (!longTermIssues.length) { + return reply.code(404).send({ message: "No Long Term Issues to process" }); + } + + const hardwareIds = []; + for (const issue of longTermIssues) { + if (issue.hardwareId) hardwareIds.push(issue.hardwareId); + if (Array.isArray(issue.hardwareIds)) hardwareIds.push(...issue.hardwareIds); + } + + if (!hardwareIds.length) { + return reply.code(404).send({ message: "No hardware IDs in Long Term Issues" }); + } + + // Fetch related sensors to get customerId + const sensors = await Insensors.find({ + $or: [ + { hardwareId: { $in: hardwareIds } }, + { connected_to: { $in: hardwareIds } } + ] + }).lean(); + + if (!sensors.length) { + return reply.code(404).send({ message: "No sensors found for Long Term hardware" }); + } + + const customerIds = [...new Set(sensors.map(s => s.customerId))]; + + const customers = await User.find({ customerId: { $in: customerIds } }).lean(); + + // Build map of movedAt per hardwareId + const movedAtMap = {}; + for (const issue of longTermIssues) { + const movedAt = issue.movedAt; + if (issue.hardwareId) movedAtMap[issue.hardwareId] = movedAt; + if (Array.isArray(issue.hardwareIds)) { + issue.hardwareIds.forEach(hid => { + movedAtMap[hid] = movedAt; + }); + } + } + + const uniqueCustomerMap = {}; + + for (const user of customers) { + const cid = user.customerId; + + if (!uniqueCustomerMap[cid]) { + const customerSensorHardwareIds = sensors + .filter(s => s.customerId === cid) + .map(s => s.hardwareId || s.tankhardwareId || s.connected_to) + .filter(Boolean); + + const movedTimes = customerSensorHardwareIds + .map(hid => movedAtMap[hid]) + .filter(Boolean) + .sort((a, b) => new Date(b) - new Date(a)); // Descending + + 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, + movedAt: movedTimes[0] || null // ⏳ Latest movedAt + } + }; + } + } + + return reply.send({ + status_code: 200, + data: Object.values(uniqueCustomerMap) + }); + + } catch (error) { + console.error("❌ Error in getLongTermCustomerDetails:", 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 8390282f..75c70ba2 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -689,7 +689,22 @@ module.exports = function (fastify, opts, next) { }, handler: installationController.getResolvedCustomerDetails, }); - + fastify.get("/api/longTermissusesbuildingdetails/:supportId", { + schema: { + description: "Long Term Issues the ticket Get building details move for Support", + tags: ["Support"], + summary: "Long Term Issues the ticket Get building details move for Support", + params: { + type: "object", + properties: { + supportId: { type: "string" }, + + }, + required: [ "supportId"], + }, + }, + handler: installationController.getLongTermCustomerDetails, + }); fastify.route({ method: 'POST', url: '/api/supportCreateTeamMember/:supportId',