ashok 4 months ago
commit 20afc3277f

@ -8039,6 +8039,170 @@ 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" });
// // Filter issues by category
// 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" });
// // Order map
// 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
// };
// });
// });
// // Create map for issue -> movedAt/resolvedAt
// 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();
// // Get latest IoT data for master
// const latestIotData = await IotData.findOne({ hardwareId: master.hardwareId }).sort({ date: -1 }).lean();
// // GSM Status
// const now = moment.tz("Asia/Kolkata");
// let gsm_last_check_time = null;
// let gsmConnected = false;
// if (latestIotData?.date) {
// const gsmTime = moment.tz(latestIotData.date, "Asia/Kolkata");
// const gsmDiff = now.diff(gsmTime, "minutes");
// gsmConnected = gsmDiff <= 1;
// gsm_last_check_time = latestIotData.date;
// }
// // Prepare slave details with LORA check
// 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;
// let lora_last_check_time = null;
// if (matchedTank?.date && matchedTank?.tankHeight !== "0") {
// const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
// const loraDiff = now.diff(tankTime, "minutes");
// loraConnected = loraDiff <= 1;
// lora_last_check_time = matchedTank.date;
// }
// 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);
// return {
// hardwareId: slave.tankhardwareId,
// tankName: slave.tankName || "",
// location: slave.tankLocation || "",
// connected_status: slave.connected_status,
// connected_lora_date: slave.connected_lora_date || "",
// connected_lora_time: slave.connected_lora_time || "",
// loraConnected,
// lora_last_check_time,
// lora_last_disconnect_time: slave.lora_last_disconnect_time || null,
// connected_to: slave.connected_to || "",
// masterName: orderMap[master.hardwareId]?.masterName || "",
// type: "slave",
// typeOfWater: tankInfo?.typeOfWater || "",
// support_lora_last_check_time: slave.support_lora_last_check_time || null,
// team_member_support_lora_last_check_time: slave.team_member_support_lora_last_check_time || null,
// comments: slaveComments
// };
// }));
// const masterComments = (support.comments || [])
// .filter(comment => comment.hardwareId === master.hardwareId)
// .map(c => c.text);
// const issue = issueMap[master.hardwareId];
// disconnectedIssues.push({
// hardwareId: master.hardwareId,
// masterName: orderMap[master.hardwareId]?.masterName || "",
// location: orderMap[master.hardwareId]?.location || "",
// type: "master",
// connected_status: master.connected_status,
// gsmConnected,
// gsm_last_check_time,
// gsm_last_disconnect_time: master.gsm_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_gsm_last_check_time: master.support_gsm_last_check_time || null,
// team_member_support_gsm_last_check_time: master.team_member_support_gsm_last_check_time || null,
// connected_slave_count: slaveDetails.length,
// connected_slaves: slaveDetails,
// comments: masterComments,
// movedAt: category !== "Resolved" ? (issue?.movedAt || null) : null,
// resolvedAt: category === "Resolved" ? (issue?.resolvedAt || null) : 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) => {
try {
const { supportId, category } = req.params;
@ -8051,7 +8215,6 @@ exports.particularCategory = async (req, reply) => {
const support = await Support.findOne({ supportId }).lean();
if (!support) return reply.code(404).send({ message: "Support record not found" });
// Filter issues by category
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}` });
@ -8073,7 +8236,6 @@ exports.particularCategory = async (req, reply) => {
if (!allRelatedSensors.length) return reply.code(404).send({ message: "No sensors found" });
// Order map
const orders = await Order.find({ customerId }).lean();
const orderMap = {};
orders.forEach(order => {
@ -8085,49 +8247,51 @@ exports.particularCategory = async (req, reply) => {
});
});
// Create map for issue -> movedAt/resolvedAt
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();
// Get latest IoT data for master
const latestIotData = await IotData.findOne({ hardwareId: master.hardwareId }).sort({ date: -1 }).lean();
// GSM Status
const now = moment.tz("Asia/Kolkata");
let gsm_last_check_time = null;
let gsmConnected = false;
if (latestIotData?.date) {
const gsmTime = moment.tz(latestIotData.date, "Asia/Kolkata");
const gsmDiff = now.diff(gsmTime, "minutes");
gsmConnected = gsmDiff <= 1;
gsm_last_check_time = latestIotData.date;
}
// Prepare slave details with LORA check
// 🔄 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;
let lora_last_check_time = null;
if (matchedTank?.date && matchedTank?.tankHeight !== "0") {
const tankTime = moment.tz(matchedTank.date, "Asia/Kolkata");
const loraDiff = now.diff(tankTime, "minutes");
loraConnected = loraDiff <= 1;
lora_last_check_time = matchedTank.date;
}
// 🔄 Update slave connected_status
await Insensors.updateOne(
{ tankhardwareId: slaveHardwareId },
{ $set: { connected_status: loraConnected ? "connected" : "disconnected" } }
);
const tankInfo = await Tank.findOne({
$or: [
{ hardwareId: slaveHardwareId },
@ -8135,33 +8299,52 @@ exports.particularCategory = async (req, reply) => {
]
}).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)
.map(c => c.text);
.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: slave.connected_status,
connected_lora_date: slave.connected_lora_date || "",
connected_lora_time: slave.connected_lora_time || "",
loraConnected,
lora_last_check_time,
lora_last_disconnect_time: slave.lora_last_disconnect_time || null,
connected_status: loraConnected ? "connected" : "disconnected",
connected_to: slave.connected_to || "",
masterName: orderMap[master.hardwareId]?.masterName || "",
type: "slave",
typeOfWater: tankInfo?.typeOfWater || "",
support_lora_last_check_time: slave.support_lora_last_check_time || null,
team_member_support_lora_last_check_time: slave.team_member_support_lora_last_check_time || null,
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)
.map(c => c.text);
.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];
@ -8170,16 +8353,7 @@ exports.particularCategory = async (req, reply) => {
masterName: orderMap[master.hardwareId]?.masterName || "",
location: orderMap[master.hardwareId]?.location || "",
type: "master",
connected_status: master.connected_status,
gsmConnected,
gsm_last_check_time,
gsm_last_disconnect_time: master.gsm_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_gsm_last_check_time: master.support_gsm_last_check_time || null,
team_member_support_gsm_last_check_time: master.team_member_support_gsm_last_check_time || null,
connected_status: gsmConnected ? "connected" : "disconnected",
connected_slave_count: slaveDetails.length,
connected_slaves: slaveDetails,
comments: masterComments,
@ -8202,8 +8376,6 @@ exports.particularCategory = async (req, reply) => {
}
};
exports.assignCategorizeIssue = async (request, reply) => {
const { supportId } = request.params;
const { support_teamMemberId, startDate, endDate, category, masterHardwareId } = request.body;

Loading…
Cancel
Save