fetch details categories add customerId

master^2
Bhaskar 5 months ago
parent b2782cdae3
commit d4e0da8051

@ -5056,10 +5056,10 @@ exports.moveIssueToCategory = async (req, reply) => {
exports.particularCategory = async (req, reply) => { exports.particularCategory = async (req, reply) => {
try { try {
const { supportId, category } = req.params; const { customerId, supportId, category } = req.params;
if (!supportId || !category) { if (!customerId || !supportId || !category) {
return reply.code(400).send({ error: "supportId and category are required" }); return reply.code(400).send({ error: "customerId, supportId, and category are required" });
} }
const support = await Support.findOne({ supportId }).lean(); const support = await Support.findOne({ supportId }).lean();
@ -5067,13 +5067,20 @@ exports.particularCategory = async (req, reply) => {
return reply.code(404).send({ message: "Support record not found" }); return reply.code(404).send({ message: "Support record not found" });
} }
const issues = (support.categorizedIssues || []).filter(issue => issue.category === category); // Filter categorizedIssues by category and customerId
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); const hardwareIds = issues.map(issue => issue.hardwareId).filter(Boolean);
// Only fetch sensors that belong to the customerId
const insensors = await Insensors.find({ const insensors = await Insensors.find({
customerId,
hardwareId: { $in: hardwareIds }, hardwareId: { $in: hardwareIds },
connected_status: "disconnected" connected_status: "disconnected"
}).lean(); }).lean();
@ -5083,23 +5090,21 @@ exports.particularCategory = async (req, reply) => {
} }
const orderMap = {}; const orderMap = {};
const customerId = insensors[0]?.customerId; const orders = await Order.find({ customerId }).lean();
if (customerId) { orders.forEach(order => {
const orders = await Order.find({ customerId }).lean(); order.master_connections.forEach(conn => {
orders.forEach(order => { orderMap[conn.hardwareId] = {
order.master_connections.forEach(conn => { masterName: conn.master_name || null,
orderMap[conn.hardwareId] = { location: conn.location || null
masterName: conn.master_name || null, };
location: conn.location || null
};
});
}); });
} });
const disconnectedIssues = []; const disconnectedIssues = [];
for (const master of insensors.filter(i => i.type === "master")) { for (const master of insensors.filter(i => i.type === "master")) {
const slaves = await Insensors.find({ const slaves = await Insensors.find({
customerId,
connected_to: master.hardwareId, connected_to: master.hardwareId,
connected_status: "disconnected" connected_status: "disconnected"
}).lean(); }).lean();
@ -5118,7 +5123,7 @@ exports.particularCategory = async (req, reply) => {
typeOfWater: slave.typeOfWater || "", typeOfWater: slave.typeOfWater || "",
support_lora_last_check_time: null, support_lora_last_check_time: null,
category, category,
assignedTo: slaveIssue?.assignedTo || null // <-- Include assigned details here assignedTo: slaveIssue?.assignedTo || null
}; };
}); });
@ -5135,13 +5140,14 @@ exports.particularCategory = async (req, reply) => {
connected_slave_count: slaveDetails.length, connected_slave_count: slaveDetails.length,
connected_slaves: slaveDetails, connected_slaves: slaveDetails,
category, category,
assignedTo: masterIssue?.assignedTo || null // <-- Include assigned details here assignedTo: masterIssue?.assignedTo || null
}); });
} }
return reply.send({ return reply.send({
status_code: 200, status_code: 200,
supportId, supportId,
customerId,
totalMasters: disconnectedIssues.length, totalMasters: disconnectedIssues.length,
disconnectedIssues disconnectedIssues
}); });
@ -5152,6 +5158,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;

@ -745,7 +745,7 @@ module.exports = function (fastify, opts, next) {
handler: installationController.moveIssueToCategory handler: installationController.moveIssueToCategory
}); });
fastify.get('/api/support/categorizedIssues/:supportId/:category', { fastify.get('/api/support/categorizedIssues/:supportId/:category/:customerId', {
schema: { schema: {
description: 'Get all issues in a particular category for a support record', description: 'Get all issues in a particular category for a support record',
tags: ['Support'], tags: ['Support'],
@ -755,6 +755,7 @@ module.exports = function (fastify, opts, next) {
required: ['supportId', 'category'], required: ['supportId', 'category'],
properties: { properties: {
supportId: { type: 'string' }, supportId: { type: 'string' },
customerId: { type: 'string' },
category: { category: {
type: 'string', type: 'string',
enum: ['Power Outage', 'Level1', 'Pending', 'Onsite Issues'] // your allowed categories enum: ['Power Outage', 'Level1', 'Pending', 'Onsite Issues'] // your allowed categories

Loading…
Cancel
Save