diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 08c4f695..aa57becf 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -6618,6 +6618,124 @@ 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" }); +// } + +// const categorizedHardwareIds = []; +// const resolvedHardwareIds = []; + +// for (const issue of supportRecord.categorizedIssues || []) { +// if (issue.hardwareId) categorizedHardwareIds.push(issue.hardwareId); +// if (Array.isArray(issue.hardwareIds)) categorizedHardwareIds.push(...issue.hardwareIds); +// } + +// for (const issue of supportRecord.resolvedIssues || []) { +// if (issue.hardwareId) resolvedHardwareIds.push(issue.hardwareId); +// if (Array.isArray(issue.hardwareIds)) resolvedHardwareIds.push(...issue.hardwareIds); +// } + +// const allHardwareIds = [...new Set([...categorizedHardwareIds, ...resolvedHardwareIds])]; +// if (allHardwareIds.length === 0) { +// return reply.code(404).send({ message: "No hardware IDs in support issues" }); +// } + +// const allSensors = await Insensors.find({ +// $or: [ +// { hardwareId: { $in: allHardwareIds } }, +// { connected_to: { $in: allHardwareIds } } +// ] +// }).lean(); + +// if (allSensors.length === 0) { +// return reply.code(404).send({ message: "No sensors found for support issues" }); +// } + +// const customerStatusMap = {}; + +// for (const sensor of allSensors) { +// const cid = sensor.customerId; +// if (!cid) continue; + +// if (!customerStatusMap[cid]) { +// customerStatusMap[cid] = { status: "unknown" }; +// } + +// if (resolvedHardwareIds.includes(sensor.hardwareId) || resolvedHardwareIds.includes(sensor.connected_to)) { +// customerStatusMap[cid].status = "resolved"; +// } else if (sensor.connected_status === "disconnected") { +// customerStatusMap[cid].status = "disconnected"; +// } else if (sensor.connected_status === "connected" && customerStatusMap[cid].status !== "disconnected" && customerStatusMap[cid].status !== "resolved") { +// 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; @@ -6631,35 +6749,29 @@ 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) const categorizedHardwareIds = []; - const resolvedHardwareIds = []; - for (const issue of supportRecord.categorizedIssues || []) { if (issue.hardwareId) categorizedHardwareIds.push(issue.hardwareId); if (Array.isArray(issue.hardwareIds)) categorizedHardwareIds.push(...issue.hardwareIds); } - for (const issue of supportRecord.resolvedIssues || []) { - if (issue.hardwareId) resolvedHardwareIds.push(issue.hardwareId); - if (Array.isArray(issue.hardwareIds)) resolvedHardwareIds.push(...issue.hardwareIds); - } - - const allHardwareIds = [...new Set([...categorizedHardwareIds, ...resolvedHardwareIds])]; - if (allHardwareIds.length === 0) { - return reply.code(404).send({ message: "No hardware IDs in support issues" }); + if (categorizedHardwareIds.length === 0) { + return reply.code(404).send({ message: "No hardware IDs in categorized issues" }); } const allSensors = await Insensors.find({ $or: [ - { hardwareId: { $in: allHardwareIds } }, - { connected_to: { $in: allHardwareIds } } + { hardwareId: { $in: categorizedHardwareIds } }, + { connected_to: { $in: categorizedHardwareIds } } ] }).lean(); if (allSensors.length === 0) { - return reply.code(404).send({ message: "No sensors found for support issues" }); + 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) { @@ -6670,11 +6782,9 @@ exports.getDisconnectedMoveCustomerDetails = async (req, reply) => { customerStatusMap[cid] = { status: "unknown" }; } - if (resolvedHardwareIds.includes(sensor.hardwareId) || resolvedHardwareIds.includes(sensor.connected_to)) { - customerStatusMap[cid].status = "resolved"; - } else if (sensor.connected_status === "disconnected") { + if (sensor.connected_status === "disconnected") { customerStatusMap[cid].status = "disconnected"; - } else if (sensor.connected_status === "connected" && customerStatusMap[cid].status !== "disconnected" && customerStatusMap[cid].status !== "resolved") { + } else if (sensor.connected_status === "connected" && customerStatusMap[cid].status !== "disconnected") { customerStatusMap[cid].status = "connected"; } } @@ -6685,7 +6795,7 @@ exports.getDisconnectedMoveCustomerDetails = async (req, reply) => { const combinedCustomerList = users.map(user => { const cid = user.customerId; return { - customer : { + customer: { customerId: cid, connectionStatus: customerStatusMap[cid]?.status || "unknown", username: user.username || "", @@ -6719,10 +6829,8 @@ exports.getDisconnectedMoveCustomerDetails = async (req, reply) => { stripeSubscriptionStatus: user.stripeSubscriptionStatus || false, createdAt: user.createdAt, updatedAt: user.updatedAt - - } - }; + }; }); return reply.send({ @@ -6736,7 +6844,6 @@ exports.getDisconnectedMoveCustomerDetails = async (req, reply) => { } }; - exports.getResolvedCustomerDetails = async (req, reply) => { try { const { supportId } = req.params;