ashok 4 months ago
commit 8adf15053b

@ -7640,6 +7640,224 @@ 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" });
// }
// console.log("support",support)
// // Choose the appropriate array based on category
// let issues = [];
// if (category === "Resolved") {
// issues = (support.resolvedIssues || []).filter(issue => issue.category === "Resolved");
// } else {
// issues = (support.categorizedIssues || []).filter(issue => issue.category === category);
// }
// console.log("issues",issues)
// if (issues.length === 0) {
// 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 === 0) {
// return reply.code(404).send({ message: "No hardware IDs found for these issues" });
// }
// let customerId = queryCustomerId;
// if (!customerId) {
// const sensors = await Insensors.find({ hardwareId: { $in: hardwareIds } }).lean();
// if (!sensors.length) {
// return reply.code(404).send({ message: "No sensors found matching these hardware IDs" });
// }
// customerId = sensors[0].customerId;
// if (!customerId) {
// return reply.code(404).send({ message: "Customer ID not found for these sensors" });
// }
// }
// const allRelatedSensors = await Insensors.find({
// customerId,
// hardwareId: { $in: hardwareIds }
// }).lean();
// if (!allRelatedSensors.length) {
// return reply.code(404).send({ message: "No sensors found for the provided customer and hardware IDs" });
// }
// 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 disconnectedIssues = [];
// // for (const master of allRelatedSensors.filter(i => i.type === "master")) {
// 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 slaveDetails = await Promise.all(slaves.map(async (slave) => {
// const slaveHardwareId = slave.tankhardwareId;
// 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_time: slave.connected_lora_time || "",
// connected_lora_date: slave.connected_lora_date || "",
// lora_last_check_time: slave.lora_last_check_time || null,
// 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);
// disconnectedIssues.push({
// hardwareId: master.hardwareId,
// masterName: orderMap[master.hardwareId]?.masterName || "",
// location: orderMap[master.hardwareId]?.location || "",
// type: "master",
// connected_status: master.connected_status,
// gsm_last_check_time: master.gsm_last_check_time || null,
// 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
// });
// }
// 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.assignCategorizeIssue = async (request, reply) => {
// const { supportId } = request.params;
// const { support_teamMemberId, startDate, endDate, category, masterHardwareId } = request.body;
// if (!support_teamMemberId || !startDate || !endDate || !category || !masterHardwareId) {
// return reply.code(400).send({ error: 'support_teamMemberId, startDate, endDate, category, and masterHardwareId are required' });
// }
// const support = await Support.findOne({ supportId });
// if (!support) {
// return reply.code(404).send({ error: 'Support record not found' });
// }
// const teamMembers = support.team_member?.team_member || [];
// const teamMember = teamMembers.find(m => m.support_teamMemberId === support_teamMemberId);
// if (!teamMember) {
// return reply.code(400).send({ error: `Team member ID ${support_teamMemberId} not found` });
// }
// const start = new Date(startDate);
// const end = new Date(endDate);
// if (isNaN(start) || isNaN(end)) {
// return reply.code(400).send({ error: 'Invalid startDate or endDate' });
// }
// // ✅ Format to "DD-MM-YYYY HH:mm:ss"
// const formattedStart = moment(start).format("DD-MM-YYYY HH:mm:ss");
// const formattedEnd = moment(end).format("DD-MM-YYYY HH:mm:ss");
// let assignedCount = 0;
// support.categorizedIssues.forEach(issue => {
// if (
// issue.masterHardwareId === masterHardwareId &&
// issue.category === category
// ) {
// issue.assignedTo = {
// name: teamMember.name,
// support_teamMemberId: teamMember.support_teamMemberId,
// phone: teamMember.phone,
// email: teamMember.email,
// startDate: formattedStart, // 👈 use formatted date
// endDate: formattedEnd // 👈 use formatted date
// };
// assignedCount++;
// }
// });
// if (assignedCount === 0) {
// return reply.code(404).send({ message: 'No matching issues found for assignment' });
// }
// await support.save();
// return reply.send({
// message: `Assigned ${assignedCount} categorized issue(s) to ${teamMember.name}`,
// assignedTo: teamMember.support_teamMemberId
// });
// };
// const moment = require('moment'); // Ensure moment is installed
exports.particularCategory = async (req, reply) => { exports.particularCategory = async (req, reply) => {
try { try {
const { supportId, category } = req.params; const { supportId, category } = req.params;
@ -7650,39 +7868,21 @@ exports.particularCategory = async (req, reply) => {
} }
const support = await Support.findOne({ supportId }).lean(); const support = await Support.findOne({ supportId }).lean();
if (!support) { if (!support) return reply.code(404).send({ message: "Support record not found" });
return reply.code(404).send({ message: "Support record not found" });
}
console.log("support",support)
// Choose the appropriate array based on category
let issues = [];
if (category === "Resolved") {
issues = (support.resolvedIssues || []).filter(issue => issue.category === "Resolved");
} else {
issues = (support.categorizedIssues || []).filter(issue => issue.category === category);
}
console.log("issues",issues)
if (issues.length === 0) {
return reply.code(404).send({ message: `No issues found for category: ${category}` });
}
const hardwareIds = [...new Set(issues.map(issue => issue.hardwareId).filter(Boolean))]; // 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}` });
if (hardwareIds.length === 0) { const hardwareIds = [...new Set(issues.map(issue => issue.hardwareId).filter(Boolean))];
return reply.code(404).send({ message: "No hardware IDs found for these issues" }); if (!hardwareIds.length) return reply.code(404).send({ message: "No hardware IDs found for these issues" });
}
let customerId = queryCustomerId; let customerId = queryCustomerId;
if (!customerId) { if (!customerId) {
const sensors = await Insensors.find({ hardwareId: { $in: hardwareIds } }).lean(); const sensorDoc = await Insensors.findOne({ hardwareId: { $in: hardwareIds } }).lean();
if (!sensors.length) { if (!sensorDoc || !sensorDoc.customerId) return reply.code(404).send({ message: "Customer ID not found" });
return reply.code(404).send({ message: "No sensors found matching these hardware IDs" }); customerId = sensorDoc.customerId;
}
customerId = sensors[0].customerId;
if (!customerId) {
return reply.code(404).send({ message: "Customer ID not found for these sensors" });
}
} }
const allRelatedSensors = await Insensors.find({ const allRelatedSensors = await Insensors.find({
@ -7690,10 +7890,9 @@ exports.particularCategory = async (req, reply) => {
hardwareId: { $in: hardwareIds } hardwareId: { $in: hardwareIds }
}).lean(); }).lean();
if (!allRelatedSensors.length) { if (!allRelatedSensors.length) return reply.code(404).send({ message: "No sensors found" });
return reply.code(404).send({ message: "No sensors found for the provided customer and hardware IDs" });
}
// Order map
const orders = await Order.find({ customerId }).lean(); const orders = await Order.find({ customerId }).lean();
const orderMap = {}; const orderMap = {};
orders.forEach(order => { orders.forEach(order => {
@ -7705,19 +7904,48 @@ exports.particularCategory = async (req, reply) => {
}); });
}); });
// Create map for issue -> movedAt/resolvedAt
const issueMap = {};
issues.forEach(issue => {
issueMap[issue.hardwareId] = issue;
});
const disconnectedIssues = []; const disconnectedIssues = [];
// for (const master of allRelatedSensors.filter(i => i.type === "master")) {
const allMasters = allRelatedSensors.filter(i => i.type === "master"); const allMasters = allRelatedSensors.filter(i => i.type === "master");
for (const master of allMasters) { for (const master of allMasters) {
const slaves = await Insensors.find({ const slaves = await Insensors.find({ connected_to: master.hardwareId, customerId }).lean();
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 slaveDetails = await Promise.all(slaves.map(async (slave) => {
const slaveHardwareId = slave.tankhardwareId; 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({ const tankInfo = await Tank.findOne({
$or: [ $or: [
@ -7735,9 +7963,10 @@ exports.particularCategory = async (req, reply) => {
tankName: slave.tankName || "", tankName: slave.tankName || "",
location: slave.tankLocation || "", location: slave.tankLocation || "",
connected_status: slave.connected_status, connected_status: slave.connected_status,
connected_lora_time: slave.connected_lora_time || "",
connected_lora_date: slave.connected_lora_date || "", connected_lora_date: slave.connected_lora_date || "",
lora_last_check_time: slave.lora_last_check_time || null, connected_lora_time: slave.connected_lora_time || "",
loraConnected,
lora_last_check_time,
lora_last_disconnect_time: slave.lora_last_disconnect_time || null, lora_last_disconnect_time: slave.lora_last_disconnect_time || null,
connected_to: slave.connected_to || "", connected_to: slave.connected_to || "",
masterName: orderMap[master.hardwareId]?.masterName || "", masterName: orderMap[master.hardwareId]?.masterName || "",
@ -7753,13 +7982,16 @@ exports.particularCategory = async (req, reply) => {
.filter(comment => comment.hardwareId === master.hardwareId) .filter(comment => comment.hardwareId === master.hardwareId)
.map(c => c.text); .map(c => c.text);
const issue = issueMap[master.hardwareId];
disconnectedIssues.push({ disconnectedIssues.push({
hardwareId: master.hardwareId, hardwareId: master.hardwareId,
masterName: orderMap[master.hardwareId]?.masterName || "", masterName: orderMap[master.hardwareId]?.masterName || "",
location: orderMap[master.hardwareId]?.location || "", location: orderMap[master.hardwareId]?.location || "",
type: "master", type: "master",
connected_status: master.connected_status, connected_status: master.connected_status,
gsm_last_check_time: master.gsm_last_check_time || null, gsmConnected,
gsm_last_check_time,
gsm_last_disconnect_time: master.gsm_last_disconnect_time || null, gsm_last_disconnect_time: master.gsm_last_disconnect_time || null,
connected_gsm_date: master.connected_gsm_date || "", connected_gsm_date: master.connected_gsm_date || "",
connected_gsm_time: master.connected_gsm_time || "", connected_gsm_time: master.connected_gsm_time || "",
@ -7769,7 +8001,9 @@ exports.particularCategory = async (req, reply) => {
team_member_support_gsm_last_check_time: master.team_member_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_slave_count: slaveDetails.length,
connected_slaves: slaveDetails, connected_slaves: slaveDetails,
comments: masterComments comments: masterComments,
movedAt: category !== "Resolved" ? (issue?.movedAt || null) : null,
resolvedAt: category === "Resolved" ? (issue?.resolvedAt || null) : null
}); });
} }
@ -7789,73 +8023,6 @@ exports.particularCategory = async (req, reply) => {
// exports.assignCategorizeIssue = async (request, reply) => {
// const { supportId } = request.params;
// const { support_teamMemberId, startDate, endDate, category, masterHardwareId } = request.body;
// if (!support_teamMemberId || !startDate || !endDate || !category || !masterHardwareId) {
// return reply.code(400).send({ error: 'support_teamMemberId, startDate, endDate, category, and masterHardwareId are required' });
// }
// const support = await Support.findOne({ supportId });
// if (!support) {
// return reply.code(404).send({ error: 'Support record not found' });
// }
// const teamMembers = support.team_member?.team_member || [];
// const teamMember = teamMembers.find(m => m.support_teamMemberId === support_teamMemberId);
// if (!teamMember) {
// return reply.code(400).send({ error: `Team member ID ${support_teamMemberId} not found` });
// }
// const start = new Date(startDate);
// const end = new Date(endDate);
// if (isNaN(start) || isNaN(end)) {
// return reply.code(400).send({ error: 'Invalid startDate or endDate' });
// }
// // ✅ Format to "DD-MM-YYYY HH:mm:ss"
// const formattedStart = moment(start).format("DD-MM-YYYY HH:mm:ss");
// const formattedEnd = moment(end).format("DD-MM-YYYY HH:mm:ss");
// let assignedCount = 0;
// support.categorizedIssues.forEach(issue => {
// if (
// issue.masterHardwareId === masterHardwareId &&
// issue.category === category
// ) {
// issue.assignedTo = {
// name: teamMember.name,
// support_teamMemberId: teamMember.support_teamMemberId,
// phone: teamMember.phone,
// email: teamMember.email,
// startDate: formattedStart, // 👈 use formatted date
// endDate: formattedEnd // 👈 use formatted date
// };
// assignedCount++;
// }
// });
// if (assignedCount === 0) {
// return reply.code(404).send({ message: 'No matching issues found for assignment' });
// }
// await support.save();
// return reply.send({
// message: `Assigned ${assignedCount} categorized issue(s) to ${teamMember.name}`,
// assignedTo: teamMember.support_teamMemberId
// });
// };
// const moment = require('moment'); // Ensure moment is installed
exports.assignCategorizeIssue = async (request, reply) => { exports.assignCategorizeIssue = async (request, reply) => {
const { supportId } = request.params; const { supportId } = request.params;
const { support_teamMemberId, startDate, endDate, category, masterHardwareId } = request.body; const { support_teamMemberId, startDate, endDate, category, masterHardwareId } = request.body;

Loading…
Cancel
Save