ashok 3 months ago
commit e6903a912d

@ -9316,6 +9316,200 @@ exports.moveIssueToCategory = async (req, reply) => {
// }; // };
// exports.particularCategory = async (req, reply) => {
// try {
// const { supportId, category } = req.params;
// const { customerId: queryCustomerId } = req.query;
// if (!supportId || !category) {
// return reply.code(400).send({ error: "supportId and category are required" });
// }
// const support = await Support.findOne({ supportId }).lean();
// if (!support) return reply.code(404).send({ message: "Support record not found" });
// const issues = (category === "Resolved" ? support.resolvedIssues : support.categorizedIssues || [])
// .filter(issue => issue.category === category);
// if (!issues.length) return reply.code(404).send({ message: `No issues found for category: ${category}` });
// const hardwareIds = [...new Set(issues.map(issue => issue.hardwareId).filter(Boolean))];
// if (!hardwareIds.length) return reply.code(404).send({ message: "No hardware IDs found for these issues" });
// let customerId = queryCustomerId;
// if (!customerId) {
// const sensorDoc = await Insensors.findOne({ hardwareId: { $in: hardwareIds } }).lean();
// if (!sensorDoc || !sensorDoc.customerId) return reply.code(404).send({ message: "Customer ID not found" });
// customerId = sensorDoc.customerId;
// }
// const allRelatedSensors = await Insensors.find({
// customerId,
// hardwareId: { $in: hardwareIds }
// }).lean();
// if (!allRelatedSensors.length) return reply.code(404).send({ message: "No sensors found" });
// const orders = await Order.find({ customerId }).lean();
// const orderMap = {};
// orders.forEach(order => {
// order.master_connections.forEach(conn => {
// orderMap[conn.hardwareId] = {
// masterName: conn.master_name || null,
// location: conn.location || null
// };
// });
// });
// const issueMap = {};
// issues.forEach(issue => {
// issueMap[issue.hardwareId] = issue;
// });
// const disconnectedIssues = [];
// const allMasters = allRelatedSensors.filter(i => i.type === "master");
// for (const master of allMasters) {
// const slaves = await Insensors.find({ connected_to: master.hardwareId, customerId }).lean();
// const latestIotData = await IotData.findOne({ hardwareId: master.hardwareId }).sort({ date: -1 }).lean();
// const now = moment.tz("Asia/Kolkata");
// let gsmConnected = false;
// if (latestIotData?.date) {
// const gsmTime = moment.tz(latestIotData.date, "Asia/Kolkata");
// const gsmDiff = now.diff(gsmTime, "minutes");
// gsmConnected = gsmDiff <= 1;
// }
// // 🔄 Update master connected_status
// await Insensors.updateOne(
// { hardwareId: master.hardwareId },
// { $set: { connected_status: gsmConnected ? "connected" : "disconnected" } }
// );
// const slaveDetails = await Promise.all(slaves.map(async (slave) => {
// const slaveHardwareId = slave.tankhardwareId?.trim();
// const matchedTank = latestIotData?.tanks?.find(tank => tank.tankhardwareId === slaveHardwareId);
// let loraConnected = false;
// if (matchedTank?.date && matchedTank?.tankHeight !== "0") {
// const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
// const loraDiff = now.diff(tankTime, "minutes");
// loraConnected = loraDiff <= 1;
// }
// // 🔄 Update slave connected_status
// await Insensors.updateOne(
// { tankhardwareId: slaveHardwareId },
// { $set: { connected_status: loraConnected ? "connected" : "disconnected" } }
// );
// const tankInfo = await Tank.findOne({
// $or: [
// { hardwareId: slaveHardwareId },
// { tankhardwareId: slaveHardwareId }
// ]
// }).lean();
// // const slaveComments = (support.comments || [])
// // .filter(comment => comment.hardwareId === slave.hardwareId)
// // .map(c => c.text);
// const slaveComments = (support.comments || [])
// .filter(comment =>
// comment.hardwareId === slave.hardwareId &&
// comment.customerId === customerId // ✅ filter by customer
// )
// .map(c => ({
// text: c.text,
// commentsTime: c.createdAt
// ? moment(c.createdAt).tz("Asia/Kolkata").format("DD-MM-YYYY HH:mm")
// : null
// }));
// return {
// hardwareId: slave.tankhardwareId,
// tankName: slave.tankName || "",
// location: slave.tankLocation || "",
// connected_status: loraConnected ? "connected" : "disconnected",
// connected_to: slave.connected_to || "",
// gsm_last_check_time: slave.gsm_last_check_time || null,
// gsm_last_disconnect_time: slave.gsm_last_disconnect_time || null,
// lora_last_disconnect_time: slave.lora_last_disconnect_time || null,
// connected_gsm_date: slave.connected_gsm_date || "",
// connected_gsm_time: slave.connected_gsm_time || "",
// connected_lora_date: slave.connected_lora_date || "",
// connected_lora_time: slave.connected_lora_time || "",
// support_lora_last_check_time: slave.support_lora_last_check_time || null,
// masterName: orderMap[master.hardwareId]?.masterName || "",
// type: "slave",
// typeOfWater: tankInfo?.typeOfWater || "",
// outDoor_status: slave.outDoor_status || "inprogress",
// //comments: slaveComments
// };
// }));
// // const masterComments = (support.comments || [])
// // .filter(comment => comment.hardwareId === master.hardwareId)
// // .map(c => c.text);
// const masterComments = (support.comments || [])
// .filter(comment =>
// comment.hardwareId === master.hardwareId &&
// comment.customerId === customerId // ✅ filter by customer
// )
// .map(c => ({
// text: c.text,
// commentsTime: c.createdAt
// ? moment(c.createdAt).tz("Asia/Kolkata").format("DD-MM-YYYY HH:mm")
// : null
// }));
// const issue = issueMap[master.hardwareId];
// disconnectedIssues.push({
// hardwareId: master.hardwareId,
// masterName: orderMap[master.hardwareId]?.masterName || "",
// location: orderMap[master.hardwareId]?.location || "",
// type: "master",
// connected_status: gsmConnected ? "connected" : "disconnected",
// connected_slave_count: slaveDetails.length,
// gsm_last_check_time: master.gsm_last_check_time || null,
// gsm_last_disconnect_time: master.gsm_last_disconnect_time || null,
// lora_last_disconnect_time: master.lora_last_disconnect_time || null,
// connected_gsm_date: master.connected_gsm_date || "",
// connected_gsm_time: master.connected_gsm_time || "",
// connected_lora_date: master.connected_lora_date || "",
// connected_lora_time: master.connected_lora_time || "",
// support_gm_last_check_time: master.support_gsm_last_check_time || null,
// connected_slaves: slaveDetails,
// comments: masterComments,
// outDoor_status: master.outDoor_status || "inprogress",
// movedAt: category !== "Resolved" ? (issue?.movedAt || null) : null,
// resolvedAt: category === "Resolved" ? (issue?.resolvedAt || null) : null,
// category: issue?.category || category,
// hardwareList: master.hardwareList || {},
// assignedTo: issue?.assignedTo || null,
// });
// }
// return reply.send({
// status_code: 200,
// supportId,
// customerId,
// totalMasters: disconnectedIssues.length,
// disconnectedIssues
// });
// } catch (err) {
// console.error("Error in particularCategory:", err);
// return reply.code(500).send({ error: "Internal server error" });
// }
// };
exports.particularCategory = async (req, reply) => { exports.particularCategory = async (req, reply) => {
try { try {
const { supportId, category } = req.params; const { supportId, category } = req.params;
@ -9349,17 +9543,31 @@ exports.particularCategory = async (req, reply) => {
if (!allRelatedSensors.length) return reply.code(404).send({ message: "No sensors found" }); if (!allRelatedSensors.length) return reply.code(404).send({ message: "No sensors found" });
const orders = await Order.find({ customerId }).lean(); //const orders = await Order.find({ customerId }).lean();
let orders = await Order.find({ customerId }).lean();
if (!orders.length) {
const fallbackOrder = await Order.findOne({ "master_connections.hardwareId": { $in: hardwareIds } }).lean();
if (fallbackOrder) {
orders = [fallbackOrder];
console.log("⚠️ Used fallback Order based on hardwareId match");
}
}
const orderMap = {}; const orderMap = {};
orders.forEach(order => { orders.forEach(order => {
if (!Array.isArray(order.master_connections)) return;
order.master_connections.forEach(conn => { order.master_connections.forEach(conn => {
orderMap[conn.hardwareId] = { const trimmedId = (conn.hardwareId || "").trim();
masterName: conn.master_name || null, if (!trimmedId) return;
location: conn.location || null orderMap[trimmedId] = {
masterName: conn.master_name?.trim() || "",
location: conn.location?.trim() || ""
}; };
}); });
}); });
console.log("🧭 Mapped orderMap keys:", Object.keys(orderMap));
const issueMap = {}; const issueMap = {};
issues.forEach(issue => { issues.forEach(issue => {
issueMap[issue.hardwareId] = issue; issueMap[issue.hardwareId] = issue;
@ -9377,11 +9585,9 @@ exports.particularCategory = async (req, reply) => {
if (latestIotData?.date) { if (latestIotData?.date) {
const gsmTime = moment.tz(latestIotData.date, "Asia/Kolkata"); const gsmTime = moment.tz(latestIotData.date, "Asia/Kolkata");
const gsmDiff = now.diff(gsmTime, "minutes"); gsmConnected = now.diff(gsmTime, "minutes") <= 1;
gsmConnected = gsmDiff <= 1;
} }
// 🔄 Update master connected_status
await Insensors.updateOne( await Insensors.updateOne(
{ hardwareId: master.hardwareId }, { hardwareId: master.hardwareId },
{ $set: { connected_status: gsmConnected ? "connected" : "disconnected" } } { $set: { connected_status: gsmConnected ? "connected" : "disconnected" } }
@ -9392,14 +9598,11 @@ exports.particularCategory = async (req, reply) => {
const matchedTank = latestIotData?.tanks?.find(tank => tank.tankhardwareId === slaveHardwareId); const matchedTank = latestIotData?.tanks?.find(tank => tank.tankhardwareId === slaveHardwareId);
let loraConnected = false; let loraConnected = false;
if (matchedTank?.date && matchedTank?.tankHeight !== "0") { if (matchedTank?.date && matchedTank?.tankHeight !== "0") {
const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata"); const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
const loraDiff = now.diff(tankTime, "minutes"); loraConnected = now.diff(tankTime, "minutes") <= 1;
loraConnected = loraDiff <= 1;
} }
// 🔄 Update slave connected_status
await Insensors.updateOne( await Insensors.updateOne(
{ tankhardwareId: slaveHardwareId }, { tankhardwareId: slaveHardwareId },
{ $set: { connected_status: loraConnected ? "connected" : "disconnected" } } { $set: { connected_status: loraConnected ? "connected" : "disconnected" } }
@ -9412,14 +9615,10 @@ exports.particularCategory = async (req, reply) => {
] ]
}).lean(); }).lean();
// const slaveComments = (support.comments || [])
// .filter(comment => comment.hardwareId === slave.hardwareId)
// .map(c => c.text);
const slaveComments = (support.comments || []) const slaveComments = (support.comments || [])
.filter(comment => .filter(comment =>
comment.hardwareId === slave.hardwareId && comment.hardwareId === slave.hardwareId &&
comment.customerId === customerId // ✅ filter by customer comment.customerId === customerId
) )
.map(c => ({ .map(c => ({
text: c.text, text: c.text,
@ -9428,7 +9627,6 @@ exports.particularCategory = async (req, reply) => {
: null : null
})); }));
return { return {
hardwareId: slave.tankhardwareId, hardwareId: slave.tankhardwareId,
tankName: slave.tankName || "", tankName: slave.tankName || "",
@ -9443,22 +9641,17 @@ exports.particularCategory = async (req, reply) => {
connected_lora_date: slave.connected_lora_date || "", connected_lora_date: slave.connected_lora_date || "",
connected_lora_time: slave.connected_lora_time || "", connected_lora_time: slave.connected_lora_time || "",
support_lora_last_check_time: slave.support_lora_last_check_time || null, support_lora_last_check_time: slave.support_lora_last_check_time || null,
masterName: orderMap[master.hardwareId]?.masterName || "", masterName: orderMap[master.hardwareId?.trim()]?.masterName || "",
type: "slave", type: "slave",
typeOfWater: tankInfo?.typeOfWater || "", typeOfWater: tankInfo?.typeOfWater || "",
outDoor_status: slave.outDoor_status || "inprogress", outDoor_status: slave.outDoor_status || "inprogress"
//comments: slaveComments
}; };
})); }));
// const masterComments = (support.comments || [])
// .filter(comment => comment.hardwareId === master.hardwareId)
// .map(c => c.text);
const masterComments = (support.comments || []) const masterComments = (support.comments || [])
.filter(comment => .filter(comment =>
comment.hardwareId === master.hardwareId && comment.hardwareId === master.hardwareId &&
comment.customerId === customerId // ✅ filter by customer comment.customerId === customerId
) )
.map(c => ({ .map(c => ({
text: c.text, text: c.text,
@ -9467,13 +9660,16 @@ exports.particularCategory = async (req, reply) => {
: null : null
})); }));
const trimmedMasterId = (master.hardwareId || "").trim();
const orderDetails = orderMap[trimmedMasterId] || {};
console.log("📦 Resolved orderDetails for", trimmedMasterId, ":", orderDetails);
const issue = issueMap[master.hardwareId]; const issue = issueMap[master.hardwareId];
disconnectedIssues.push({ disconnectedIssues.push({
hardwareId: master.hardwareId, hardwareId: master.hardwareId,
masterName: orderMap[master.hardwareId]?.masterName || "", masterName: orderDetails.masterName || "",
location: orderMap[master.hardwareId]?.location || "", location: orderDetails.location || "",
type: "master", type: "master",
connected_status: gsmConnected ? "connected" : "disconnected", connected_status: gsmConnected ? "connected" : "disconnected",
connected_slave_count: slaveDetails.length, connected_slave_count: slaveDetails.length,
@ -9492,7 +9688,7 @@ exports.particularCategory = async (req, reply) => {
resolvedAt: category === "Resolved" ? (issue?.resolvedAt || null) : null, resolvedAt: category === "Resolved" ? (issue?.resolvedAt || null) : null,
category: issue?.category || category, category: issue?.category || category,
hardwareList: master.hardwareList || {}, hardwareList: master.hardwareList || {},
assignedTo: issue?.assignedTo || null, assignedTo: issue?.assignedTo || null
}); });
} }
@ -9505,11 +9701,12 @@ exports.particularCategory = async (req, reply) => {
}); });
} catch (err) { } catch (err) {
console.error("Error in particularCategory:", err); console.error("Error in particularCategory:", err);
return reply.code(500).send({ error: "Internal server error" }); return reply.code(500).send({ error: "Internal server error" });
} }
}; };
exports.updateHardwareList = async (req, reply) => { exports.updateHardwareList = async (req, reply) => {
try { try {
const { supportId, customerId, hardwareId } = req.params; const { supportId, customerId, hardwareId } = req.params;

Loading…
Cancel
Save