From b3f3b8e9be9ec52b244b0b155bc44bbc74b816ec Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Mon, 23 Jun 2025 15:20:13 +0530 Subject: [PATCH] update hardwareList items --- src/controllers/installationController.js | 64 +++++++++++++++++ src/models/store.js | 8 ++- src/routes/installationRoute.js | 87 +++++++++++++++++++++++ 3 files changed, 158 insertions(+), 1 deletion(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 0b735bc9..93fb54ed 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -8799,6 +8799,70 @@ exports.particularCategory = async (req, reply) => { } }; +exports.updateHardwareList = async (req, reply) => { + try { + const { supportId, customerId, hardwareId } = req.params; + const { hardwareList } = req.body; + + if (!supportId || !customerId || !hardwareId) { + return reply.code(400).send({ + error: "supportId, customerId, and hardwareId are required in path params", + }); + } + + if (!hardwareList || typeof hardwareList !== "object") { + return reply.code(400).send({ + error: "hardwareList must be a valid object in request body", + }); + } + + // ✅ Check if support record exists + const support = await Support.findOne({ supportId }).lean(); + if (!support) { + return reply.code(404).send({ error: "Support record not found" }); + } + + // ✅ Ensure at least one escalation issue exists for that hardware and customer + const escalationIssue = (support.categorizedIssues || []).find(issue => + issue.category === "Escalation" && + issue.hardwareId === hardwareId + ); + + if (!escalationIssue) { + return reply.code(403).send({ + error: "Update not allowed. Escalation issue not found for this supportId and hardwareId.", + }); + } + + // ✅ Check if the Insensors document exists + const sensor = await Insensors.findOne({ customerId, hardwareId }); + if (!sensor) { + return reply.code(404).send({ message: "Insensor not found for the given customerId and hardwareId" }); + } + + // ✅ Update hardwareList + sensor.hardwareList = hardwareList; + await sensor.save(); + + return reply.send({ + status_code: 200, + message: "Hardware list updated successfully for escalated issue", + data: { + supportId, + customerId, + hardwareId, + hardwareList: sensor.hardwareList + } + }); + + } catch (error) { + console.error("Error updating hardware list:", error); + 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; diff --git a/src/models/store.js b/src/models/store.js index 13dbb1b6..53c7c870 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -640,7 +640,13 @@ outDoor_status: { type: String, enum: ['inprogress', 'ready to pick up', 'dispatched'], default: 'inprogress' // optional: set default - }, + }, + hardwareList: { + type: Map, + of: Number, + default: {} + }, + distance_check: { result: String, steps: [ diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 717c8469..0ad3aa14 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -886,6 +886,93 @@ module.exports = function (fastify, opts, next) { handler: installationController.assignCategorizeIssue }); +fastify.route({ + method: 'POST', + url: '/api/updateHarwareList/:supportId/:customerId/:hardwareId', + schema: { + tags: ['Support'], + summary: 'Update hardware list for an escalated issue', + description: 'Updates the hardwareList in Insensors schema only if issue is categorized as Escalation', + params: { + type: 'object', + required: ['supportId', 'customerId', 'hardwareId'], + properties: { + supportId: { type: 'string', description: 'Support record ID' }, + customerId: { type: 'string', description: 'Customer ID' }, + hardwareId: { type: 'string', description: 'Hardware ID of the master/slave' } + } + }, + body: { + type: 'object', + required: ['hardwareList'], + properties: { + hardwareList: { + type: 'object', + description: 'Hardware summary object (e.g. { masters: 2, slaves: 2 })', + additionalProperties: { type: 'number' }, + example: { + masters: 2, + slaves: 2, + sensors: 4, + plugs: 3, + wires: 200 + } + } + } + }, + response: { + 200: { + description: 'Successfully updated hardwareList', + type: 'object', + properties: { + status_code: { type: 'number' }, + message: { type: 'string' }, + data: { + type: 'object', + properties: { + supportId: { type: 'string' }, + customerId: { type: 'string' }, + hardwareId: { type: 'string' }, + hardwareList: { + type: 'object', + additionalProperties: { type: 'number' } + } + } + } + } + }, + 400: { + description: 'Missing or invalid input', + type: 'object', + properties: { + error: { type: 'string' } + } + }, + 403: { + description: 'Not allowed (issue is not escalated)', + type: 'object', + properties: { + error: { type: 'string' } + } + }, + 404: { + description: 'Support or sensor not found', + type: 'object', + properties: { + error: { type: 'string' } + } + }, + 500: { + description: 'Internal server error', + type: 'object', + properties: { + error: { type: 'string' } + } + } + } + }, + handler: installationController.updateHardwareList +}); fastify.post("/api/my-categorized-issues/:support_teamMemberId/:customerId", { schema: {