particular issue changes

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

@ -7109,32 +7109,61 @@ 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" });
}
const orderMap = {};
const customerId = allRelatedSensors[0]?.customerId;
if (customerId) {
// Map hardwareId to masterName and location from Orders
const orders = await Order.find({ customerId }).lean();
const orderMap = {};
orders.forEach(order => {
order.master_connections.forEach(conn => {
orderMap[conn.hardwareId] = {
@ -7143,13 +7172,18 @@ exports.particularCategory = async (req, reply) => {
};
});
});
}
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;

Loading…
Cancel
Save