diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 772eb819..0a035954 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -722,50 +722,50 @@ exports.editStore = async (request, reply) => { }; const moment = require('moment'); - exports.createwaterlevelSensor = async (req, reply) => { - try { - const storeId = req.params.storeId - const { hardwareId,hardwareId_company, type, indate } = req.body; - var mater_seq_id = await generatewaterlevelsensorId(); - const date = moment().format('MM-DD'); - const prefix = 'AS' + date + 'MALOV1'; - var masterId = `${prefix}${mater_seq_id}`; - const newSensor = new WaterLeverSensor({ - storeId, - hardwareId, - masterId, - type, - indate - }); - const savedSensor = await newSensor.save(); - reply.code(200).send(savedSensor); - } catch (err) { - reply.code(500).send(err); - } - }; + // exports.createwaterlevelSensor = async (req, reply) => { + // try { + // const storeId = req.params.storeId + // const { hardwareId,hardwareId_company, type, indate } = req.body; + // var mater_seq_id = await generatewaterlevelsensorId(); + // const date = moment().format('MM-DD'); + // const prefix = 'AS' + date + 'MALOV1'; + // var masterId = `${prefix}${mater_seq_id}`; + // const newSensor = new WaterLeverSensor({ + // storeId, + // hardwareId, + // masterId, + // type, + // indate + // }); + // const savedSensor = await newSensor.save(); + // reply.code(200).send(savedSensor); + // } catch (err) { + // reply.code(500).send(err); + // } + // }; - exports.editWaterLevelSensor = async (req, reply) => { - try { - const { storeId } = req.params; - const updates = req.body; + // exports.editWaterLevelSensor = async (req, reply) => { + // try { + // const { storeId } = req.params; + // const updates = req.body; - const updatedSensor = await WaterLeverSensor.findOneAndUpdate( - { storeId:storeId,hardwareId: req.body.hardwareId }, - updates, - { new: true } - ); + // const updatedSensor = await WaterLeverSensor.findOneAndUpdate( + // { storeId:storeId,hardwareId: req.body.hardwareId }, + // updates, + // { new: true } + // ); - if (!updatedSensor) { - reply.code(404).send({ message: "Sensor not found" }); - } else { - reply.code(200).send(updatedSensor); - } - } catch (err) { - reply.code(500).send(err); - } - }; + // if (!updatedSensor) { + // reply.code(404).send({ message: "Sensor not found" }); + // } else { + // reply.code(200).send(updatedSensor); + // } + // } catch (err) { + // reply.code(500).send(err); + // } + // }; exports.deleteWaterLevelSensor = async (req, reply) => { try { @@ -809,28 +809,28 @@ exports.editStore = async (request, reply) => { } }; - exports.qccheckwaterlevelSensor = async (request, reply) => { - try { - const { hardwareId } = request.params; - const updateData = request.body; + // exports.qccheckwaterlevelSensor = async (request, reply) => { + // try { + // const { hardwareId } = request.params; + // const updateData = request.body; - // Find the document by hardwareId and update it with the fields received in the body - const updatedSensor = await WaterLeverSensor.findOneAndUpdate( - { hardwareId: hardwareId }, - { $set: updateData }, - { new: true } // Return the updated document - ); + // // Find the document by hardwareId and update it with the fields received in the body + // const updatedSensor = await WaterLeverSensor.findOneAndUpdate( + // { hardwareId: hardwareId }, + // { $set: updateData }, + // { new: true } // Return the updated document + // ); - if (!updatedSensor) { - return reply.status(404).send({ error: 'Sensor not found' }); - } + // if (!updatedSensor) { + // return reply.status(404).send({ error: 'Sensor 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 sensor' }); - } - }; + // return reply.status(200).send(updatedSensor); + // } catch (error) { + // console.error(error); + // return reply.status(500).send({ error: 'An error occurred while updating the sensor' }); + // } + // }; exports.getHardware = async (req, reply) => { @@ -930,39 +930,39 @@ exports.editStore = async (request, reply) => { -exports.editSlave = async (req, reply) => { - try { - const { hardwareId } = req.params; - const updates = req.body; +// exports.editSlave = async (req, reply) => { +// try { +// const { hardwareId } = req.params; +// const updates = req.body; - const mainHardware = await WaterLeverSensor.findOne({ hardwareId }); +// const mainHardware = await WaterLeverSensor.findOne({ hardwareId }); - if (!mainHardware) { - reply.code(404).send({ message: "Main hardware not found" }); - return; - } +// if (!mainHardware) { +// reply.code(404).send({ message: "Main hardware not found" }); +// return; +// } - const slaveIndex = mainHardware.slaves.tankhardware.findIndex( - (slave) => slave.tankhardwareId === req.body.tankhardwareId - ); +// const slaveIndex = mainHardware.slaves.tankhardware.findIndex( +// (slave) => slave.tankhardwareId === req.body.tankhardwareId +// ); - if (slaveIndex === -1) { - reply.code(404).send({ message: "Slave not found" }); - return; - } +// if (slaveIndex === -1) { +// reply.code(404).send({ message: "Slave not found" }); +// return; +// } - mainHardware.slaves.tankhardware[slaveIndex] = { - ...mainHardware.slaves.tankhardware[slaveIndex], - ...updates, - }; +// mainHardware.slaves.tankhardware[slaveIndex] = { +// ...mainHardware.slaves.tankhardware[slaveIndex], +// ...updates, +// }; - const updatedHardware = await mainHardware.save(); +// const updatedHardware = await mainHardware.save(); - reply.code(200).send(updatedHardware); - } catch (err) { - reply.code(500).send(err); - } -}; +// reply.code(200).send(updatedHardware); +// } catch (err) { +// reply.code(500).send(err); +// } +// }; exports.deleteSlave = async (req, reply) => { @@ -1217,6 +1217,57 @@ exports.updateSensorById = async (req, reply) => { } }; +exports.deleteSensorById = async (req, reply) => { + try { + const { _id } = req.params; + + const deletedSensor = await Insensors.findByIdAndDelete(_id); + + if (!deletedSensor) { + return reply.code(404).send({ message: "Sensor not found" }); + } + + return reply.code(200).send({ message: "Sensor deleted successfully" }); + } catch (error) { + console.error("Error deleting sensor:", error); + return reply.code(500).send({ message: "Internal Server Error" }); + } +}; + +exports.updateSensorQC = async (req, reply) => { + try { + const { _id } = req.params; + let updateData = req.body; + + const allowedFields = ["qccheck", "qcby", "comment"]; + + // Filter only allowed fields + const filteredUpdateData = Object.keys(updateData) + .filter((key) => allowedFields.includes(key)) + .reduce((obj, key) => { + obj[key] = updateData[key]; + return obj; + }, {}); + + // Update qccheckdate with the current date in "DD-MMM-YYYY - HH:MM" format + filteredUpdateData.qccheckdate = moment().format("DD-MMM-YYYY - HH:mm"); + + const updatedSensor = await Insensors.findByIdAndUpdate( + _id, + filteredUpdateData, + { new: true } + ); + + if (!updatedSensor) { + return reply.code(404).send({ message: "Sensor not found" }); + } + + return reply.code(200).send(updatedSensor); + } catch (error) { + console.error("Error updating QC fields:", error); + return reply.code(500).send({ message: "Internal Server Error" }); + } +}; exports.getpumpswitchqc = async (req, reply) => { try { @@ -1261,8 +1312,8 @@ const generateBatchNo = (type, hardwareIdCompany) => { exports.createSensor = async (req, reply) => { try { const storeId = req.params.storeId; - const { indate, batchno, hardwareId_company, quantity, model, type } = req.body; - + const { indate, batchno, hardwareId_company, quantity, model,type } = req.body; + const sensorType = type.toLowerCase(); let finalBatchNo = batchno; if (batchno === 'New') { @@ -1287,7 +1338,7 @@ exports.createSensor = async (req, reply) => { storeId, model, batchno: finalBatchNo, - type, + type: sensorType, indate, hardwareId_company }; @@ -1348,7 +1399,7 @@ exports.getbatchnumbers = async (req, reply) => { exports.getiots = async (req, reply) => { try { const storeId = req.params.storeId; - let type = req.params.type ? req.params.type.toUpperCase() : null; // Convert type to uppercase + let type = req.params.type ? req.params.type.toLowerCase() : null; // Convert type to uppercase let query = { storeId: storeId }; diff --git a/src/models/store.js b/src/models/store.js index 90df39a5..04b2633f 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -250,7 +250,7 @@ const motorSwitchSensorInSchema = new mongoose.Schema({ const insensorsSchema = new mongoose.Schema({ storeId: { type: String }, - hardwareId: { type: String }, + hardwareId: { type: String ,default:null}, masterId: { type: String, default: null }, type: { type: String }, model: { type: String }, diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index f4d75e3e..b7a4301e 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -510,67 +510,67 @@ fastify.get("/api/getusersofParticularInstaller", { handler: storeController.getusersofParticularInstaller, }); -fastify.post("/api/createwaterlevelSensor/:storeId", { - schema: { - description: "This is for creating waterlevel Sensor", - tags: ["Store-Data"], - summary: "This is for creating waterlevel Sensor", - params: { - required: ["storeId"], - type: "object", - properties: { - storeId: { - type: "string", - description: "storeId", - }, - }, - }, +// fastify.post("/api/createwaterlevelSensor/:storeId", { +// schema: { +// description: "This is for creating waterlevel Sensor", +// tags: ["Store-Data"], +// summary: "This is for creating waterlevel Sensor", +// params: { +// required: ["storeId"], +// type: "object", +// properties: { +// storeId: { +// type: "string", +// description: "storeId", +// }, +// }, +// }, - body: { - type: "object", +// body: { +// type: "object", - properties: { +// properties: { - hardwareId: { type: "string" }, - type: { type: "string" }, - indate: { type: "string" }, - hardwareId_company: { type: "string" } - }, - }, - }, - handler: storeController.createwaterlevelSensor, -}) - - - -fastify.put("/api/editwaterlevelSensor/:storeId", { - schema: { - description: "This is for editing a water level sensor", - tags: ["Store-Data"], - summary: "This is for editing a water level sensor", - params: { - required: ["storeId"], - type: "object", - properties: { - storeId: { - type: "string", - description: "storeId", - }, +// hardwareId: { type: "string" }, +// type: { type: "string" }, +// indate: { type: "string" }, +// hardwareId_company: { type: "string" } +// }, +// }, +// }, +// handler: storeController.createwaterlevelSensor, +// }) + + + +// fastify.put("/api/editwaterlevelSensor/:storeId", { +// schema: { +// description: "This is for editing a water level sensor", +// tags: ["Store-Data"], +// summary: "This is for editing a water level sensor", +// params: { +// required: ["storeId"], +// type: "object", +// properties: { +// storeId: { +// type: "string", +// description: "storeId", +// }, - }, - }, - body: { - type: "object", - properties: { - hardwareId: { type: "string" }, - type: { type: "string" }, - indate: { type: "string" }, - hardwareId_company: { type: "string" } - }, - }, - }, - handler: storeController.editWaterLevelSensor, -}); +// }, +// }, +// body: { +// type: "object", +// properties: { +// hardwareId: { type: "string" }, +// type: { type: "string" }, +// indate: { type: "string" }, +// hardwareId_company: { type: "string" } +// }, +// }, +// }, +// handler: storeController.editWaterLevelSensor, +// }); fastify.delete("/api/deletewaterlevelSensor/:storeId", { @@ -625,37 +625,37 @@ fastify.get("/api/getHardware/:storeId", { handler: storeController.getHardware, }); -fastify.post("/api/qccheckwaterlevelSensor/:hardwareId", { - schema: { - description: "This is for checking waterlevel Sensor", - tags: ["Store-Data"], - summary: "This is for checking waterlevel Sensor", - params: { - required: ["hardwareId"], - type: "object", - properties: { - hardwareId: { - type: "string", - description: "hardwareId", - }, - }, - }, - body: { - type: "object", +// fastify.post("/api/qccheckwaterlevelSensor/:hardwareId", { +// schema: { +// description: "This is for checking waterlevel Sensor", +// tags: ["Store-Data"], +// summary: "This is for checking waterlevel Sensor", +// params: { +// required: ["hardwareId"], +// type: "object", +// properties: { +// hardwareId: { +// type: "string", +// description: "hardwareId", +// }, +// }, +// }, +// body: { +// type: "object", - properties: { - qccheck: { type: "string" }, - qccheckdate: { type: "string" }, - qcby: { type: "string" }, - comment: { type: "string" }, - outforrepairdate: { type: "string" }, - sendto: { type: "string" }, - repairfeedback: { type: "string" }, - }, - }, - }, - handler: storeController.qccheckwaterlevelSensor, -}) +// properties: { +// qccheck: { type: "string" }, +// qccheckdate: { type: "string" }, +// qcby: { type: "string" }, +// comment: { type: "string" }, +// outforrepairdate: { type: "string" }, +// sendto: { type: "string" }, +// repairfeedback: { type: "string" }, +// }, +// }, +// }, +// handler: storeController.qccheckwaterlevelSensor, +// }) fastify.put("/api/getHardwareqc/:storeId", { @@ -752,45 +752,45 @@ fastify.post("/api/addSlave/:hardwareId", { }); -fastify.put("/api/editSlave/:hardwareId", { - schema: { - description: "This is for editing a slave of a water level sensor", - tags: ["Store-Data"], - summary: "This is for editing a slave of a water level sensor", - params: { - required: ["hardwareId"], - type: "object", - properties: { - hardwareId: { - type: "string", - description: "Main hardware ID", - }, +// fastify.put("/api/editSlave/:hardwareId", { +// schema: { +// description: "This is for editing a slave of a water level sensor", +// tags: ["Store-Data"], +// summary: "This is for editing a slave of a water level sensor", +// params: { +// required: ["hardwareId"], +// type: "object", +// properties: { +// hardwareId: { +// type: "string", +// description: "Main hardware ID", +// }, - }, - }, - body: { - type: "object", - properties: { - tankhardwareId: { type: "string" }, - type: { type: "string" }, - indate: { type: "string" }, - hardwareId_company: { type: "string" }, - qccheck: { type: "string", default: null }, - qccheckdate: { type: "string", default: null }, - qcby: { type: "string", default: null }, - comment: { type: "string", default: "0" }, - outforrepairdate: { type: "string", default: "0" }, - sendto: { type: "string", default: null }, - repairfeedback: { type: "string", default: "0" }, - dateofinstallation: { type: "string", default: null }, - installedby: { type: "string", default: "0" }, - customerId: { type: "string", default: "0" }, - comments: { type: "string", default: "0" }, - }, - }, - }, - handler: storeController.editSlave, -}); +// }, +// }, +// body: { +// type: "object", +// properties: { +// tankhardwareId: { type: "string" }, +// type: { type: "string" }, +// indate: { type: "string" }, +// hardwareId_company: { type: "string" }, +// qccheck: { type: "string", default: null }, +// qccheckdate: { type: "string", default: null }, +// qcby: { type: "string", default: null }, +// comment: { type: "string", default: "0" }, +// outforrepairdate: { type: "string", default: "0" }, +// sendto: { type: "string", default: null }, +// repairfeedback: { type: "string", default: "0" }, +// dateofinstallation: { type: "string", default: null }, +// installedby: { type: "string", default: "0" }, +// customerId: { type: "string", default: "0" }, +// comments: { type: "string", default: "0" }, +// }, +// }, +// }, +// handler: storeController.editSlave, +// }); fastify.delete("/api/deleteSlave/:hardwareId", { @@ -1109,6 +1109,25 @@ fastify.post("/api/createwaterlevelSensorintime/:storeId", { handler: storeController.createSensor, }) +fastify.delete("/api/deleteSensorById/:_id", { + schema: { + description: "Delete a sensor by ID", + tags: ["Store-Data"], + summary: "Delete a sensor based on its _id", + params: { + required: ["_id"], + type: "object", + properties: { + _id: { + type: "string", + description: "Sensor ID", + }, + }, + }, + }, + handler: storeController.deleteSensorById, +}); + fastify.post("/api/updateSensorById/:_id", { schema: { @@ -1141,6 +1160,33 @@ fastify.post("/api/updateSensorById/:_id", { }); +fastify.post("/api/updateSensorQC/:_id", { + schema: { + description: "Edit specific sensor QC fields", + tags: ["Store-Data"], + summary: "Update QC fields of a sensor", + params: { + required: ["_id"], + type: "object", + properties: { + _id: { + type: "string", + description: "Sensor ID", + }, + }, + }, + body: { + type: "object", + properties: { + qccheck: { type: "string", description: "QC check status" }, + qcby: { type: "string", description: "QC checked by" }, + comment: { type: "string", description: "QC comment" }, + }, + }, + }, + handler: storeController.updateSensorQC, +}); + fastify.get("/api/getbatchnumbers/:storeId/:type", { schema: {