From ee8b16d484c33b29a5439d38af9d8c5eae5b2104 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 24 Jun 2025 11:47:59 +0530 Subject: [PATCH] update hardwarelist --- src/controllers/installationController.js | 52 ++++++++++------------- src/models/store.js | 20 ++++++--- src/routes/installationRoute.js | 42 ++++++++++++------ 3 files changed, 66 insertions(+), 48 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 14b1eb16..9d121c50 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -8806,64 +8806,58 @@ exports.updateHardwareList = async (req, reply) => { const { hardwareList } = req.body; if (!supportId || !customerId || !hardwareId) { - return reply.code(400).send({ - error: "supportId, customerId, and hardwareId are required in path params", - }); + return reply.code(400).send({ error: "supportId, customerId, and hardwareId are required" }); } - if (!hardwareList || typeof hardwareList !== "object") { - return reply.code(400).send({ - error: "hardwareList must be a valid object in request body", - }); + if (!Array.isArray(hardwareList)) { + return reply.code(400).send({ error: "hardwareList must be an array of objects" }); } - // ✅ Check if support record exists const support = await Support.findOne({ supportId }).lean(); - if (!support) { - return reply.code(404).send({ error: "Support record not found" }); - } + 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 + 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.", + error: "Escalation issue not found. Cannot update hardware list." }); } - // ✅ 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" }); + return reply.code(404).send({ error: "Insensor not found" }); } - // ✅ Update hardwareList sensor.hardwareList = hardwareList; + sensor.markModified('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 - } - }); + status_code: 200, + message: "Hardware list updated successfully", + data: { + supportId, + customerId, + hardwareId, + hardwareList: sensor.hardwareList // ← array format + } +}); - } catch (error) { - console.error("Error updating hardware list:", error); + } catch (err) { + console.error("Error updating hardwareList:", err); 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 aef4372c..d7c4cbad 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -616,6 +616,15 @@ team_member_support_lora_last_check_time : { type: String, default: null }, lora_last_check_time : { type: String, default: null }, gsm_last_disconnect_time : { type: String, default: null }, lora_last_disconnect_time : { type: String, default: null }, + hardwareList: { + type: [ + { + name: { type: String, required: true }, + value: { type: Number, required: true } + } + ], + default: [] +}, quality_check_details: [{ damage_check: { result: String }, stickering_check: { result: String }, @@ -641,11 +650,12 @@ outDoor_status: { enum: ['inprogress', 'ready to pick up', 'dispatched'], default: 'inprogress' // optional: set default }, - hardwareList: { - type: Map, - of: Number, - default: {} - }, + // hardwareList: { + // type: Map, + // of: Number, + // default: {} + // }, + distance_check: { result: String, diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 0ad3aa14..bdc7c8bb 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -888,11 +888,11 @@ module.exports = function (fastify, opts, next) { fastify.route({ method: 'POST', - url: '/api/updateHarwareList/:supportId/:customerId/:hardwareId', + url: '/api/updateHardwareList/: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', + description: 'Updates the hardwareList (array of name-value objects) in Insensors schema only if issue is categorized as Escalation', params: { type: 'object', required: ['supportId', 'customerId', 'hardwareId'], @@ -907,16 +907,23 @@ fastify.route({ 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 - } + type: 'array', + description: 'List of hardware components as name-value objects', + items: { + type: 'object', + required: ['name', 'value'], + properties: { + name: { type: 'string', description: 'Component name (e.g., masters, slaves)' }, + value: { type: 'number', description: 'Quantity of the component' } + } + }, + example: [ + { name: 'masters', value: 2 }, + { name: 'slaves', value: 2 }, + { name: 'sensors', value: 4 }, + { name: 'plugs', value: 3 }, + { name: 'wires', value: 200 } + ] } } }, @@ -934,8 +941,14 @@ fastify.route({ customerId: { type: 'string' }, hardwareId: { type: 'string' }, hardwareList: { - type: 'object', - additionalProperties: { type: 'number' } + type: 'array', + items: { + type: 'object', + properties: { + name: { type: 'string' }, + value: { type: 'number' } + } + } } } } @@ -974,6 +987,7 @@ fastify.route({ handler: installationController.updateHardwareList }); + fastify.post("/api/my-categorized-issues/:support_teamMemberId/:customerId", { schema: { description: "Get categorized issues by team member and customer",