diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 2fcf8be4..d65a2ca0 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -7109,47 +7109,81 @@ 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" }); } + // Find support record 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 = (support.categorizedIssues || []).filter(issue => issue.category === category); if (issues.length === 0) { return reply.code(404).send({ message: `No issues found for category: ${category}` }); } - const hardwareIds = issues.map(issue => issue.hardwareId).filter(Boolean); - const allRelatedSensors = await Insensors.find({ hardwareId: { $in: hardwareIds } }).lean(); + // Get unique hardwareIds from the issues + 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) { - 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 customerId = allRelatedSensors[0]?.customerId; - if (customerId) { - const orders = await Order.find({ customerId }).lean(); - orders.forEach(order => { - order.master_connections.forEach(conn => { - orderMap[conn.hardwareId] = { - masterName: conn.master_name || null, - location: conn.location || null - }; - }); + orders.forEach(order => { + order.master_connections.forEach(conn => { + orderMap[conn.hardwareId] = { + masterName: conn.master_name || null, + location: conn.location || null + }; }); - } + }); const disconnectedIssues = []; + // For each master sensor 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 slaveHardwareId = slave.tankhardwareId; @@ -7159,7 +7193,7 @@ exports.particularCategory = async (req, reply) => { { tankhardwareId: slaveHardwareId } ] }).lean(); - console.log("tankInfo",tankInfo) + const slaveComments = (support.comments || []) .filter(comment => comment.hardwareId === slave.hardwareId) .map(c => c.text); @@ -7223,6 +7257,7 @@ exports.particularCategory = async (req, reply) => { + // exports.assignCategorizeIssue = async (request, reply) => { // const { supportId } = request.params; // const { support_teamMemberId, startDate, endDate, category, masterHardwareId } = request.body;