particular issue changes

master^2
Bhaskar 4 months ago
parent e828566d42
commit 243a1532c3

@ -7109,47 +7109,81 @@ exports.moveIssueToCategory = async (req, reply) => {
exports.particularCategory = async (req, reply) => { exports.particularCategory = async (req, reply) => {
try { try {
const { supportId, category } = req.params; const { supportId, category } = req.params;
const { customerId: queryCustomerId } = req.query;
if (!supportId || !category) { if (!supportId || !category) {
return reply.code(400).send({ error: "supportId and category are required" }); return reply.code(400).send({ error: "supportId and category are required" });
} }
// Find support record
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" });
} }
// Filter issues by category
const issues = (support.categorizedIssues || []).filter(issue => issue.category === category); const issues = (support.categorizedIssues || []).filter(issue => issue.category === category);
if (issues.length === 0) { if (issues.length === 0) {
return reply.code(404).send({ message: `No issues found for category: ${category}` }); return reply.code(404).send({ message: `No issues found for category: ${category}` });
} }
const hardwareIds = issues.map(issue => issue.hardwareId).filter(Boolean); // Get unique hardwareIds from the issues
const allRelatedSensors = await Insensors.find({ hardwareId: { $in: hardwareIds } }).lean(); 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) {
// Fetch sensors for these hardwareIds to find the customerId(s)
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" });
}
// Assuming all sensors belong to the same customer, get the first customerId
customerId = sensors[0].customerId;
if (!customerId) {
return reply.code(404).send({ message: "Customer ID not found for these sensors" });
}
}
// Filter sensors only for this customer and hardwareIds (in case sensors contain mixed customers)
const allRelatedSensors = await Insensors.find({
customerId,
hardwareId: { $in: hardwareIds }
}).lean();
if (!allRelatedSensors.length) { if (!allRelatedSensors.length) {
return reply.code(404).send({ message: "No matching devices found for this category." }); return reply.code(404).send({ message: "No sensors found for the provided customer and hardware IDs" });
} }
// Map hardwareId to masterName and location from Orders
const orders = await Order.find({ customerId }).lean();
const orderMap = {}; const orderMap = {};
const customerId = allRelatedSensors[0]?.customerId; orders.forEach(order => {
if (customerId) { order.master_connections.forEach(conn => {
const orders = await Order.find({ customerId }).lean(); orderMap[conn.hardwareId] = {
orders.forEach(order => { masterName: conn.master_name || null,
order.master_connections.forEach(conn => { location: conn.location || null
orderMap[conn.hardwareId] = { };
masterName: conn.master_name || null,
location: conn.location || null
};
});
}); });
} });
const disconnectedIssues = []; const disconnectedIssues = [];
// For each master sensor
for (const master of allRelatedSensors.filter(i => i.type === "master")) { for (const master of allRelatedSensors.filter(i => i.type === "master")) {
const slaves = await Insensors.find({ connected_to: master.hardwareId }).lean(); // Get slaves connected to this master for the same customer
const slaves = await Insensors.find({
connected_to: master.hardwareId,
customerId
}).lean();
// Prepare slave details
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;
@ -7159,7 +7193,7 @@ exports.particularCategory = async (req, reply) => {
{ tankhardwareId: slaveHardwareId } { tankhardwareId: slaveHardwareId }
] ]
}).lean(); }).lean();
console.log("tankInfo",tankInfo)
const slaveComments = (support.comments || []) const slaveComments = (support.comments || [])
.filter(comment => comment.hardwareId === slave.hardwareId) .filter(comment => comment.hardwareId === slave.hardwareId)
.map(c => c.text); .map(c => c.text);
@ -7223,6 +7257,7 @@ exports.particularCategory = async (req, reply) => {
// 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