diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index cccaa384..742cfc4e 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -7863,6 +7863,114 @@ exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => { // } // }; +// exports.getDisconnectedMoveCustomerDetails = 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" }); +// } + +// // Extract hardwareIds from categorizedIssues only (exclude resolvedIssues) +// 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" }); +// } + +// const allSensors = await Insensors.find({ +// $or: [ +// { hardwareId: { $in: categorizedHardwareIds } }, +// { connected_to: { $in: categorizedHardwareIds } } +// ] +// }).lean(); + +// if (allSensors.length === 0) { +// return reply.code(404).send({ message: "No sensors found for categorized hardware IDs" }); +// } + +// // Determine connection status per customer +// const customerStatusMap = {}; + +// for (const sensor of allSensors) { +// const cid = sensor.customerId; +// if (!cid) continue; + +// if (!customerStatusMap[cid]) { +// customerStatusMap[cid] = { status: "unknown" }; +// } + +// if (sensor.connected_status === "disconnected") { +// customerStatusMap[cid].status = "disconnected"; +// } else if (sensor.connected_status === "connected" && customerStatusMap[cid].status !== "disconnected") { +// customerStatusMap[cid].status = "connected"; +// } +// } + +// const customerIds = Object.keys(customerStatusMap); +// const users = await User.find({ customerId: { $in: customerIds } }).lean(); + +// const combinedCustomerList = users.map(user => { +// const cid = user.customerId; +// return { +// customer: { +// customerId: cid, +// connectionStatus: customerStatusMap[cid]?.status || "unknown", +// 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 +// } +// }; +// }); + +// return reply.send({ +// status_code: 200, +// data: combinedCustomerList +// }); + +// } catch (error) { +// console.error("Error in getDisconnectedMoveCustomerDetails:", error); +// return reply.code(500).send({ error: "Internal server error" }); +// } +// }; + exports.getDisconnectedMoveCustomerDetails = async (req, reply) => { try { const { supportId } = req.params; @@ -7876,15 +7984,19 @@ exports.getDisconnectedMoveCustomerDetails = async (req, reply) => { return reply.code(404).send({ message: "No support record found for this supportId" }); } - // Extract hardwareIds from categorizedIssues only (exclude resolvedIssues) + // ✅ Filter only "Power Outage" or "OutDoor Escalation" issues + const validCategories = ["Power Outage", "OutDoor Escalation"]; const categorizedHardwareIds = []; + for (const issue of supportRecord.categorizedIssues || []) { + if (!validCategories.includes(issue.category)) continue; + 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" }); + return reply.code(404).send({ message: "No hardware IDs in categorized issues with Power Outage or OutDoor Escalation" }); } const allSensors = await Insensors.find({ @@ -7971,6 +8083,7 @@ exports.getDisconnectedMoveCustomerDetails = async (req, reply) => { } }; + exports.getResolvedCustomerDetails = async (req, reply) => { try { const { supportId } = req.params;