diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 33878549..75d89e40 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -4379,9 +4379,127 @@ const generateTicketId = () => { // console.log(`✅ New ticket raised for ${masterHardwareId}`); // }; +// const raiseATicketLikeLogic = async (supportRecord, masterHardwareId, slaveData = []) => { +// const now = new Date(); +// const formattedNow = new Date(now.getTime() + 19800000) +// .toISOString() +// .replace("T", " ") +// .substring(0, 19); + +// // Check if already categorized +// const alreadyCategorized = supportRecord.categorizedIssues.some( +// (catIssue) => catIssue.hardwareId === masterHardwareId +// ); +// if (alreadyCategorized) { +// console.log(`⛔ Ticket for ${masterHardwareId} already categorized. Skipping.`); +// return; +// } + +// // Prepare slave hardwareIds and names +// const slaveHardwareIds = slaveData.map((slave) => slave.tankhardwareId).sort(); +// const slaveNames = slaveData.map((slave) => slave.sensorName || slave.tankName || "").sort(); + +// // Check for existing unresolved and not moved-to-category issue +// const duplicateExists = supportRecord.issues.some((issue) => { +// const existingSlaveIds = (issue.hardwareIds || []).sort(); +// const existingSlaveNames = (issue.slaveNames || []).sort(); + +// return ( +// issue.hardwareId === masterHardwareId && +// issue.masterHardwareId === masterHardwareId && +// issue.resolved === false && +// issue.movedToCategory === false && +// JSON.stringify(existingSlaveIds) === JSON.stringify(slaveHardwareIds) && +// JSON.stringify(existingSlaveNames) === JSON.stringify(slaveNames) +// ); +// }); + +// if (duplicateExists) { +// console.log(`⛔ Duplicate unresolved issue already exists for ${masterHardwareId}. Skipping.`); +// return; +// } + +// // Create new issue +// const newIssue = { +// ticketId: generateTicketId(), +// type: "GSM or LoRa Disconnected", +// masterHardwareId, +// hardwareId: masterHardwareId, +// hardwareIds: slaveHardwareIds, +// slaveNames: slaveNames, +// resolved: false, +// movedToCategory: false, +// lastTicketRaisedAt: formattedNow, +// createdAt: formattedNow, +// }; + +// console.log("newIssue", newIssue); + +// // ✅ Atomic update +// await Support.findByIdAndUpdate( +// supportRecord._id, +// { +// $push: { issues: newIssue }, +// $set: { lastTicketRaisedAt: formattedNow } +// }, +// { new: true } +// ); + +// console.log(`✅ New ticket raised for ${masterHardwareId}`); +// }; + +// cron.schedule("*/1 * * * *", async () => { +// try { +// console.log("🔁 Running auto-disconnect ticket check..."); + +// // Step 1: Get all support profiles +// const allSupportProfiles = await Support.find({}); + +// for (const supportRecord of allSupportProfiles) { +// const supportId = supportRecord.supportId; +// if (!supportId) continue; + +// // Step 2: Find all master sensors +// const allMasters = await Insensors.find({ type: "master" }).lean(); + +// for (const master of allMasters) { +// const customerId = master.customerId; +// const hardwareId = master.hardwareId; + +// if (!customerId || !hardwareId) continue; + +// // ✅ Update GSM and LoRa connection statuses +// await updateConnectedStatusOnly(customerId, hardwareId); + +// // 🔄 Re-fetch updated master and slaves +// const updatedMaster = await Insensors.findOne({ hardwareId, customerId }).lean(); +// const connectedSlaves = await Insensors.find({ +// connected_to: hardwareId, +// type: "slave" +// }).lean(); + +// // Step 3: Check disconnections +// const disconnectedSlaves = connectedSlaves.filter( +// (s) => s.connected_status === "disconnected" +// ); +// const masterIsDisconnected = updatedMaster.connected_status === "disconnected"; + +// // Step 4: Raise ticket if needed +// if (masterIsDisconnected || disconnectedSlaves.length > 0) { +// await raiseATicketLikeLogic(supportRecord, hardwareId, disconnectedSlaves); +// } +// } +// } + +// console.log("✅ Auto ticket check completed."); +// } catch (err) { +// console.error("❌ Cron error:", err); +// } +// }); + const raiseATicketLikeLogic = async (supportRecord, masterHardwareId, slaveData = []) => { const now = new Date(); - const formattedNow = new Date(now.getTime() + 19800000) + const formattedNow = new Date(now.getTime() + 19800000) // +05:30 offset in ms .toISOString() .replace("T", " ") .substring(0, 19); @@ -4421,7 +4539,6 @@ const raiseATicketLikeLogic = async (supportRecord, masterHardwareId, slaveData // Create new issue const newIssue = { - ticketId: generateTicketId(), type: "GSM or LoRa Disconnected", masterHardwareId, hardwareId: masterHardwareId, @@ -4433,21 +4550,14 @@ const raiseATicketLikeLogic = async (supportRecord, masterHardwareId, slaveData createdAt: formattedNow, }; - console.log("newIssue", newIssue); - - // ✅ Atomic update - await Support.findByIdAndUpdate( - supportRecord._id, - { - $push: { issues: newIssue }, - $set: { lastTicketRaisedAt: formattedNow } - }, - { new: true } - ); + supportRecord.issues.push(newIssue); + supportRecord.lastTicketRaisedAt = formattedNow; + await supportRecord.save(); console.log(`✅ New ticket raised for ${masterHardwareId}`); }; + cron.schedule("*/1 * * * *", async () => { try { console.log("🔁 Running auto-disconnect ticket check..."); @@ -8179,7 +8289,7 @@ exports.moveIssueToCategory = async (req, reply) => { hardwareId: issue.hardwareId, masterHardwareId: issue.masterHardwareId || issue.hardwareId, category, - ticketId : issue.ticketId, + //ticketId : issue.ticketId, movedAt: nowTime, movedToCategory: true, }); @@ -8197,7 +8307,7 @@ exports.moveIssueToCategory = async (req, reply) => { masterHardwareId: issue.masterHardwareId || issue.hardwareId, slaveName, category, - ticketId : issue.ticketId, + //ticketId : issue.ticketId, movedAt: nowTime, movedToCategory: true, }); @@ -9271,7 +9381,7 @@ exports.resolvedIssuesForSupport = async (req, reply) => { type: issue.type, hardwareId: issue.hardwareId, masterHardwareId, - ticketId: issue.ticketId, + //ticketId: issue.ticketId, category: "Resolved", resolvedAt: nowTime, originalMovedAt: issue.movedAt || null, @@ -9310,7 +9420,7 @@ exports.resolvedIssuesForSupport = async (req, reply) => { type: issue.type, hardwareId: issue.hardwareId, masterHardwareId, - ticketId: issue.ticketId, + //ticketId: issue.ticketId, category: category, movedAt: nowTime, movedToCategory: true, diff --git a/src/models/store.js b/src/models/store.js index 505953aa..4c29970e 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -206,7 +206,7 @@ const installationschema = new mongoose.Schema({ }); const IssueSchema = new Schema({ - ticketId: { type: String, unique: true }, + //ticketId: { type: String, unique: true }, type: { type: String, enum: ["GSM Disconnected", "LoRa Disconnected", "GSM or LoRa Disconnected"], @@ -271,7 +271,7 @@ const installationschema = new mongoose.Schema({ enum: ["Power Outage", "Resolved", "Escalation","Pending"], required: true }, - ticketId: String, + // ticketId: String, movedAt: { type: String, required: true @@ -334,7 +334,7 @@ const installationschema = new mongoose.Schema({ category: String, // will be 'Resolved' resolvedAt: String, // ISO string or Date originalMovedAt: String, // store original movedAt for reference - ticketId: String, + // ticketId: String, } ],