From a3f038d027852e582b8c4eb044f3bb77311bdf94 Mon Sep 17 00:00:00 2001 From: varun Date: Thu, 11 Jul 2024 02:52:36 -0400 Subject: [PATCH] qc and get qc and installation for slaves --- src/controllers/storeController.js | 106 +++++++++++++++++++++++++++++ src/routes/storeRoute.js | 96 ++++++++++++++++++++++++++ 2 files changed, 202 insertions(+) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 9a7b5f56..e519b803 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -657,3 +657,109 @@ exports.addStore = async (request, reply) => { reply.code(500).send(err); } }; + + +exports.qccheckwaterlevelSensorSlave = async (request, reply) => { + try { + const { storeId } = request.params; + const updateData = request.body; + + // Find the document by storeId and tankhardwareId, and update the corresponding slave + const updatedSensor = await WaterLeverSensor.findOneAndUpdate( + { + storeId: storeId, + "slaves.tankhardware.tankhardwareId": request.body.tankhardwareId + }, + { + $set: { + "slaves.tankhardware.$.qccheck": updateData.qccheck, + "slaves.tankhardware.$.qccheckdate": updateData.qccheckdate, + "slaves.tankhardware.$.qcby": updateData.qcby, + "slaves.tankhardware.$.comment": updateData.comment, + "slaves.tankhardware.$.outforrepairdate": updateData.outforrepairdate, + "slaves.tankhardware.$.sendto": updateData.sendto, + "slaves.tankhardware.$.repairfeedback": updateData.repairfeedback + } + }, + { new: true } // Return the updated document + ); + + if (!updatedSensor) { + return reply.status(404).send({ error: 'Slave not found' }); + } + + return reply.status(200).send(updatedSensor); + } catch (error) { + console.error(error); + return reply.status(500).send({ error: 'An error occurred while updating the slave' }); + } +}; + + +exports.installwaterlevelSensorSlave = async (request, reply) => { + try { + const { storeId } = request.params; + const { hardwareId, tankhardwareId, dateofinstallation, customerId, installedby } = request.body; + + // Find the document by storeId and hardwareId, then update the specific slave with tankhardwareId + const updatedSensor = await WaterLeverSensor.findOneAndUpdate( + { + storeId: storeId, + hardwareId: hardwareId, + "slaves.tankhardware.tankhardwareId": tankhardwareId + }, + { + $set: { + "slaves.tankhardware.$.dateofinstallation": dateofinstallation, + "slaves.tankhardware.$.customerId": customerId, + "slaves.tankhardware.$.installedby": installedby + } + }, + { new: true } // Return the updated document + ); + + if (!updatedSensor) { + return reply.status(404).send({ error: 'Sensor or Slave not found' }); + } + + return reply.status(200).send(updatedSensor); + } catch (error) { + console.error(error); + return reply.status(500).send({ error: 'An error occurred while installing the sensor' }); + } +}; + +exports.getHardwareqcslave = async (req, reply) => { + try { + const { storeId } = req.params; + const { hardwareId, tankhardwareId } = req.body; + + let query; + if (tankhardwareId) { + // If tankhardwareId is provided, query for the specific slave + query = { + storeId: storeId, + hardwareId: hardwareId, + "slaves.tankhardware.tankhardwareId": tankhardwareId, + }; + } else { + // Otherwise, query for the main hardware + query = { + storeId: storeId, + hardwareId: hardwareId, + }; + } + + const docs = await WaterLeverSensor.find(query).exec(); + + if (docs.length === 0) { + return reply.status(404).send({ error: 'No hardware found' }); + } + + reply.send({ status_code: 200, data: docs, count: docs.length }); + } catch (err) { + console.error(err); + reply.status(500).send({ error: 'An error occurred while retrieving the hardware QC data' }); + } +}; + diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index 6c4346b6..087e9759 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -217,6 +217,40 @@ fastify.post("/api/qccheckwaterlevelSensor/:hardwareId", { handler: storeController.qccheckwaterlevelSensor, }) +fastify.post("/api/qccheckwaterlevelslaveSensor/:storeId", { + schema: { + description: "This is for checking waterlevel Sensor slaves", + tags: ["Store-Data"], + summary: "This is for checking waterlevel Sensor slaves", + params: { + required: ["storeId"], + type: "object", + properties: { + storeId: { + type: "string", + description: "Store ID", + }, + + }, + }, + body: { + type: "object", + properties: { + tankhardwareId: { type: "string" }, + qccheck: { type: "string" }, + qccheckdate: { type: "string" }, + qcby: { type: "string" }, + comment: { type: "string" }, + outforrepairdate: { type: "string" }, + sendto: { type: "string" }, + repairfeedback: { type: "string" }, + }, + }, + }, + handler: storeController.qccheckwaterlevelSensorSlave, +}); + + fastify.get("/api/getHardware/:storeId", { schema: { @@ -305,5 +339,67 @@ fastify.post("/api/addSlave/:hardwareId", { }); +fastify.post("/api/installwaterlevelSensorSlave/:storeId", { + schema: { + description: "This is for installing waterlevel Sensor Slaves", + tags: ["Store-Data"], + summary: "This is for installing waterlevel Sensor Slaves", + params: { + required: ["storeId"], + type: "object", + properties: { + storeId: { + type: "string", + description: "Store ID", + }, + }, + }, + body: { + type: "object", + properties: { + hardwareId: { type: "string" }, + tankhardwareId: { type: "string" }, // Add tankhardwareId in body + dateofinstallation: { type: "string" }, + customerId: { type: "string" }, + installedby: { type: "string" } + }, + }, + }, + handler: storeController.installwaterlevelSensorSlave, +}); + + +fastify.put("/api/getHardwareqcslave/:storeId", { + schema: { + tags: ["Store-Data"], + description: "This is to Get Quality Check Hardware Data Slave", + summary: "This is to Get Quality Check Hardware Data slave", + params: { + required: ["storeId"], + type: "object", + properties: { + storeId: { + type: "string", + description: "Store ID", + }, + }, + }, + body: { + type: "object", + properties: { + hardwareId: { type: "string" }, + tankhardwareId: { type: "string" }, // Optional field for tank hardware + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + handler: storeController.getHardwareqcslave, +}); + + next(); };