reveert back all disconnected issue

master^2
Bhaskar 4 months ago
parent 9aa292e762
commit 10b482e83a

@ -6219,6 +6219,214 @@ exports.getRemoveConnectedMastersWithSlaves = async (req, reply) => {
// };
// exports.getDisconnectedCustomerDetails = 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" });
// }
// // Step 1: Filter unresolved and not moved issues
// const unresolvedIssues = (supportRecord.issues || []).filter(
// issue => issue.resolved === false && issue.movedToCategory !== true
// );
// // Step 2: Build set of hardwareIds already in categorizedIssues (moved)
// const existingCategorizedHardwareIds = new Set();
// (supportRecord.categorizedIssues || []).forEach(issue => {
// if (issue.hardwareId) existingCategorizedHardwareIds.add(issue.hardwareId.trim().toLowerCase());
// if (Array.isArray(issue.hardwareIds)) {
// issue.hardwareIds.forEach(id => {
// if (typeof id === "string") existingCategorizedHardwareIds.add(id.trim().toLowerCase());
// });
// }
// });
// // Step 3: Build list of hardwareIds in unresolved issues (excluding moved)
// const allHardwareIds = new Set();
// for (const issue of unresolvedIssues) {
// const issueHardwareId = issue.hardwareId?.trim().toLowerCase();
// const issueSlaveIds = issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || [];
// if (issueHardwareId && !existingCategorizedHardwareIds.has(issueHardwareId)) {
// allHardwareIds.add(issueHardwareId);
// }
// for (const slaveId of issueSlaveIds) {
// if (slaveId && !existingCategorizedHardwareIds.has(slaveId)) {
// allHardwareIds.add(slaveId);
// }
// }
// }
// // Debug: Log issue hardware IDs being considered
// console.log("✅ All issue hardwareIds:", Array.from(allHardwareIds));
// // Step 4: If no valid hardware IDs left, stop
// if (allHardwareIds.size === 0) {
// return reply.code(404).send({ message: "No unresolved hardware IDs found in issues" });
// }
// const hardwareIdsArray = Array.from(allHardwareIds);
// // Step 5: Find disconnected sensors
// const disconnectedSensors = await Insensors.find({
// connected_status: "disconnected",
// $or: [
// { connected_to: { $in: hardwareIdsArray } },
// { hardwareId: { $in: hardwareIdsArray } },
// { tankhardwareId: { $in: hardwareIdsArray } }
// ]
// }).lean();
// // Debug: Log disconnected sensors found
// console.log("⚙️ Disconnected sensors matched:", disconnectedSensors.map(s => s.hardwareId));
// if (!disconnectedSensors.length) {
// return reply.code(404).send({ message: "No disconnected issues found" });
// }
// // Step 6: Get relevant customers
// const customerIds = [...new Set(disconnectedSensors.map(s => s.customerId))];
// const customers = await User.find({ customerId: { $in: customerIds } }).lean();
// // Step 7: Group by customer
// const customerHardwareMap = {};
// for (const sensor of disconnectedSensors) {
// const custId = sensor.customerId;
// if (!customerHardwareMap[custId]) {
// customerHardwareMap[custId] = new Set();
// }
// const sensorHw = sensor.tankhardwareId?.trim().toLowerCase();
// const sensorConnected = sensor.connected_to?.trim().toLowerCase();
// // for (const issue of unresolvedIssues) {
// // const allIssueHardwareIds = [
// // ...(issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []),
// // issue.hardwareId?.trim().toLowerCase()
// // ];
// // if (
// // (sensorHw && allIssueHardwareIds.includes(sensorHw)) ||
// // (sensorConnected && allIssueHardwareIds.includes(sensorConnected))
// // ) {
// // if (issue.hardwareId && !existingCategorizedHardwareIds.has(issue.hardwareId.trim().toLowerCase())) {
// // customerHardwareMap[custId].add(issue.hardwareId.trim().toLowerCase());
// // }
// // }
// // }
// const sensorHardwareId = sensor.hardwareId?.trim().toLowerCase();
// for (const issue of unresolvedIssues) {
// const allIssueHardwareIds = [
// ...(issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || []),
// issue.hardwareId?.trim().toLowerCase()
// ];
// if (
// (sensorHw && allIssueHardwareIds.includes(sensorHw)) ||
// (sensorConnected && allIssueHardwareIds.includes(sensorConnected)) ||
// (sensorHardwareId && allIssueHardwareIds.includes(sensorHardwareId))
// ) {
// for (const hw of allIssueHardwareIds) {
// if (hw && !existingCategorizedHardwareIds.has(hw)) {
// customerHardwareMap[custId].add(hw);
// }
// }
// }
// }
// }
// // Debug: Log map of matched customer hardware
// console.log("📌 Customer hardware map:", Object.fromEntries(
// Object.entries(customerHardwareMap).map(([k, v]) => [k, Array.from(v)])
// ));
// // Step 8: Build final response
// const response = [];
// for (const user of customers) {
// const custId = user.customerId;
// const hardwareIdSet = customerHardwareMap[custId] || new Set();
// if (hardwareIdSet.size === 0) continue;
// const relatedIssues = unresolvedIssues.filter(issue => {
// const issueHw = issue.hardwareId?.trim().toLowerCase();
// const hardwareIds = issue.hardwareIds?.map(id => id?.trim().toLowerCase()) || [];
// const allIds = [issueHw, ...hardwareIds];
// return Array.from(hardwareIdSet).some(hw => allIds.includes(hw));
// });
// let latestIssueTime = null;
// for (const issue of relatedIssues) {
// if (issue.lastTicketRaisedAt) {
// const issueTime = new Date(issue.lastTicketRaisedAt);
// if (!latestIssueTime || issueTime > latestIssueTime) {
// latestIssueTime = issueTime;
// }
// }
// }
// response.push({
// customer: {
// customerId: custId,
// 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,
// lastTicketRaisedAt: latestIssueTime ? moment(latestIssueTime).format("YYYY-MM-DD HH:mm:ss") : null,
// totalHardwareIdsCount: hardwareIdSet.size
// }
// });
// }
// 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" });
// }
// };
exports.getDisconnectedCustomerDetails = async (req, reply) => {
try {
const { supportId } = req.params;
@ -6430,6 +6638,7 @@ exports.getDisconnectedCustomerDetails = async (req, reply) => {
exports.getDisconnectedCustomerDetailsByTeamMemberId = async (req, reply) => {
try {
const { support_teamMemberId } = req.params;

Loading…
Cancel
Save