support auto raise ticket issues

master^2
Bhaskar 3 months ago
parent 5a12daeb6b
commit 27bb7989da

@ -4379,9 +4379,127 @@ const generateTicketId = () => {
// console.log(`✅ New ticket raised for ${masterHardwareId}`); // 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 raiseATicketLikeLogic = async (supportRecord, masterHardwareId, slaveData = []) => {
const now = new Date(); const now = new Date();
const formattedNow = new Date(now.getTime() + 19800000) const formattedNow = new Date(now.getTime() + 19800000) // +05:30 offset in ms
.toISOString() .toISOString()
.replace("T", " ") .replace("T", " ")
.substring(0, 19); .substring(0, 19);
@ -4421,7 +4539,6 @@ const raiseATicketLikeLogic = async (supportRecord, masterHardwareId, slaveData
// Create new issue // Create new issue
const newIssue = { const newIssue = {
ticketId: generateTicketId(),
type: "GSM or LoRa Disconnected", type: "GSM or LoRa Disconnected",
masterHardwareId, masterHardwareId,
hardwareId: masterHardwareId, hardwareId: masterHardwareId,
@ -4433,21 +4550,14 @@ const raiseATicketLikeLogic = async (supportRecord, masterHardwareId, slaveData
createdAt: formattedNow, createdAt: formattedNow,
}; };
console.log("newIssue", newIssue); supportRecord.issues.push(newIssue);
supportRecord.lastTicketRaisedAt = formattedNow;
// ✅ Atomic update
await Support.findByIdAndUpdate(
supportRecord._id,
{
$push: { issues: newIssue },
$set: { lastTicketRaisedAt: formattedNow }
},
{ new: true }
);
await supportRecord.save();
console.log(`✅ New ticket raised for ${masterHardwareId}`); console.log(`✅ New ticket raised for ${masterHardwareId}`);
}; };
cron.schedule("*/1 * * * *", async () => { cron.schedule("*/1 * * * *", async () => {
try { try {
console.log("🔁 Running auto-disconnect ticket check..."); console.log("🔁 Running auto-disconnect ticket check...");
@ -8179,7 +8289,7 @@ exports.moveIssueToCategory = async (req, reply) => {
hardwareId: issue.hardwareId, hardwareId: issue.hardwareId,
masterHardwareId: issue.masterHardwareId || issue.hardwareId, masterHardwareId: issue.masterHardwareId || issue.hardwareId,
category, category,
ticketId : issue.ticketId, //ticketId : issue.ticketId,
movedAt: nowTime, movedAt: nowTime,
movedToCategory: true, movedToCategory: true,
}); });
@ -8197,7 +8307,7 @@ exports.moveIssueToCategory = async (req, reply) => {
masterHardwareId: issue.masterHardwareId || issue.hardwareId, masterHardwareId: issue.masterHardwareId || issue.hardwareId,
slaveName, slaveName,
category, category,
ticketId : issue.ticketId, //ticketId : issue.ticketId,
movedAt: nowTime, movedAt: nowTime,
movedToCategory: true, movedToCategory: true,
}); });
@ -9271,7 +9381,7 @@ exports.resolvedIssuesForSupport = async (req, reply) => {
type: issue.type, type: issue.type,
hardwareId: issue.hardwareId, hardwareId: issue.hardwareId,
masterHardwareId, masterHardwareId,
ticketId: issue.ticketId, //ticketId: issue.ticketId,
category: "Resolved", category: "Resolved",
resolvedAt: nowTime, resolvedAt: nowTime,
originalMovedAt: issue.movedAt || null, originalMovedAt: issue.movedAt || null,
@ -9310,7 +9420,7 @@ exports.resolvedIssuesForSupport = async (req, reply) => {
type: issue.type, type: issue.type,
hardwareId: issue.hardwareId, hardwareId: issue.hardwareId,
masterHardwareId, masterHardwareId,
ticketId: issue.ticketId, //ticketId: issue.ticketId,
category: category, category: category,
movedAt: nowTime, movedAt: nowTime,
movedToCategory: true, movedToCategory: true,

@ -206,7 +206,7 @@ const installationschema = new mongoose.Schema({
}); });
const IssueSchema = new Schema({ const IssueSchema = new Schema({
ticketId: { type: String, unique: true }, //ticketId: { type: String, unique: true },
type: { type: {
type: String, type: String,
enum: ["GSM Disconnected", "LoRa Disconnected", "GSM or LoRa Disconnected"], enum: ["GSM Disconnected", "LoRa Disconnected", "GSM or LoRa Disconnected"],
@ -271,7 +271,7 @@ const installationschema = new mongoose.Schema({
enum: ["Power Outage", "Resolved", "Escalation","Pending"], enum: ["Power Outage", "Resolved", "Escalation","Pending"],
required: true required: true
}, },
ticketId: String, // ticketId: String,
movedAt: { movedAt: {
type: String, type: String,
required: true required: true
@ -334,7 +334,7 @@ const installationschema = new mongoose.Schema({
category: String, // will be 'Resolved' category: String, // will be 'Resolved'
resolvedAt: String, // ISO string or Date resolvedAt: String, // ISO string or Date
originalMovedAt: String, // store original movedAt for reference originalMovedAt: String, // store original movedAt for reference
ticketId: String, // ticketId: String,
} }
], ],

Loading…
Cancel
Save