diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 8be96192..45cc2722 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -1228,7 +1228,28 @@ exports.generateHardwareMasterId = async (req, reply) => { } }; +exports.getSensorByHardwareId = async (req, reply) => { + try { + + const { storeId } = req.params; + const { hardwareId } = req.body; + + if (!hardwareId) { + return reply.code(400).send({ message: 'hardwareId is required' }); + } + + const sensor = await Insensors.findOne({ storeId, hardwareId }); + if (!sensor) { + return reply.code(404).send({ message: 'Sensor not found' }); + } + + return reply.code(200).send(sensor); + } catch (error) { + console.error('Error fetching sensor by hardwareId:', error); + return reply.code(500).send({ message: 'Internal Server Error' }); + } +}; exports.updateSensorById = async (req, reply) => { try { @@ -1283,7 +1304,7 @@ exports.updateSensorQC = async (req, reply) => { const { _id } = req.params; let updateData = req.body; - const allowedFields = ["qccheck", "qcby", "comment", "status"]; + const allowedFields = ['qccheck', 'qcby', 'comment', 'status', 'quality_check_details']; // Filter only allowed fields const filteredUpdateData = Object.keys(updateData) @@ -1296,11 +1317,11 @@ exports.updateSensorQC = async (req, reply) => { // Ensure qccheck is handled properly if (filteredUpdateData.qccheck) { const qccheckLower = filteredUpdateData.qccheck.toLowerCase(); - filteredUpdateData.status = qccheckLower === "ok" ? "available" : "qcfailed"; + filteredUpdateData.status = qccheckLower === 'ok' ? 'available' : 'qcfailed'; } // Update qccheckdate with the current date in "DD-MMM-YYYY - HH:MM" format - filteredUpdateData.qccheckdate = moment().format("DD-MMM-YYYY - HH:mm"); + filteredUpdateData.qccheckdate = moment().format('DD-MMM-YYYY - HH:mm'); // Find the sensor by ID const updatedSensor = await Insensors.findByIdAndUpdate( @@ -1310,14 +1331,14 @@ exports.updateSensorQC = async (req, reply) => { ); if (!updatedSensor) { - return reply.code(404).send({ message: "Sensor not found" }); + return reply.code(404).send({ message: 'Sensor not found' }); } // Update stock based on QC result const stockRecord = await SensorStock.findOne({ storeId: updatedSensor.storeId, type: updatedSensor.type }); if (stockRecord) { - if (filteredUpdateData.qccheck && filteredUpdateData.qccheck.toLowerCase() === "ok") { + if (filteredUpdateData.qccheck && filteredUpdateData.qccheck.toLowerCase() === 'ok') { // If QC is "ok", move 1 from total_count_before_qc to total_available await SensorStock.updateOne( { storeId: updatedSensor.storeId, type: updatedSensor.type }, @@ -1334,8 +1355,8 @@ exports.updateSensorQC = async (req, reply) => { return reply.code(200).send(updatedSensor); } catch (error) { - console.error("Error updating QC fields:", error); - return reply.code(500).send({ message: "Internal Server Error" }); + console.error('Error updating QC fields:', error); + return reply.code(500).send({ message: 'Internal Server Error' }); } }; diff --git a/src/models/store.js b/src/models/store.js index de06ea30..6fe2dc64 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -336,12 +336,12 @@ const motorSwitchSensorInSchema = new mongoose.Schema({ const insensorsSchema = new mongoose.Schema({ storeId: { type: String }, - hardwareId: { type: String ,default:null}, + hardwareId: { type: String, default: null }, masterId: { type: String, default: null }, type: { type: String }, model: { type: String }, indate: { type: String }, - hardwareId_company: { type: String,default: null }, + hardwareId_company: { type: String, default: null }, qccheck: { type: String, default: null }, qccheckdate: { type: String, default: null }, qcby: { type: String, default: null }, @@ -355,11 +355,34 @@ const insensorsSchema = new mongoose.Schema({ comments: { type: String, default: "0" }, quantity: { type: Number, default: 0 }, batchno: { type: String, default: null }, - sensor_type: { type: String, enum: ['slaves', 'motorswitch', 'master'] }, // adding sensor_type field + sensor_type: { type: String }, // adding sensor_type field status: { type: String, default: "pending" }, + quality_check_details: [{ + damage_check: { result: String }, + stickering_check: { result: String }, + power_check: { result: String }, + master_connecting_gsm: { result: String }, + slave_connecting: { result: String }, + motor_starting: { + result: String, + steps: [ + { step: Number, result: String } + ] + }, + connecting_to_sensor: { result: String }, + connecting_to_slave: { result: String }, + data_sending: { result: String }, + distance_check: { + result: String, + steps: [ + { step: Number, result: String } + ] + } + }] }); + const iotpriceSchema = new mongoose.Schema({ name: { type: String }, type: { type: String ,default:null}, diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index bed69c81..fd1ca988 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -1158,6 +1158,30 @@ fastify.post('/api/generateHardwareMasterId/:storeId', { handler: storeController.generateHardwareMasterId, }); + +fastify.post('/api/getSensorByHardwareId/:storeId', { + schema: { + description: 'Fetch details of a specific sensor using hardwareId', + tags: ['Store-Data'], + summary: 'Retrieve sensor details by hardwareId', + params: { + required: ['storeId'], + type: 'object', + properties: { + storeId: { type: 'string', description: 'Store ID' }, + }, + }, + body: { + type: 'object', + required: ['hardwareId'], + properties: { + hardwareId: { type: 'string', description: 'Hardware ID of the sensor' }, + }, + }, + }, + handler: storeController.getSensorByHardwareId, +}); + fastify.post("/api/updateSensorById/:_id", { schema: { description: "Edit specific sensor fields", @@ -1189,30 +1213,75 @@ fastify.post("/api/updateSensorById/:_id", { }); -fastify.post("/api/updateSensorQC/:_id", { +fastify.post('/api/updateSensorQC/:_id', { schema: { - description: "Edit specific sensor QC fields", - tags: ["Store-Data"], - summary: "Update QC fields of a sensor", + description: 'Edit specific sensor QC fields', + tags: ['Store-Data'], + summary: 'Update QC fields of a sensor', params: { - required: ["_id"], - type: "object", + required: ['_id'], + type: 'object', properties: { - _id: { - type: "string", - description: "Sensor ID", - }, + _id: { type: 'string', description: 'Sensor ID' }, }, }, body: { - type: "object", + type: 'object', properties: { - qccheck: { type: "string", description: "QC check status" }, - qcby: { type: "string", description: "QC checked by" }, - comments: { type: "string", description: "QC comment" }, - - }, - }, + qccheck: { type: 'string', description: 'QC check status' }, + qcby: { type: 'string', description: 'QC checked by' }, + comments: { type: 'string', description: 'QC comment' }, + quality_check_details: { + type: 'array', + description: 'Detailed quality check results', + items: { + type: 'object', + properties: { + damage_check: { type: 'string' }, + stickering_check: { type: 'string' }, + power_check: { type: 'string' }, + master_connecting_gsm: { type: 'string' }, + slave_connecting: { type: 'string' }, + motor_starting: { + type: 'object', + properties: { + result: { type: 'string' }, + steps: { + type: 'array', + items: { + type: 'object', + properties: { + step: { type: 'number' }, + result: { type: 'string' } + } + } + } + } + }, + connecting_to_sensor: { type: 'string' }, + connecting_to_slave: { type: 'string' }, + data_sending: { type: 'string' }, + distance_check: { + type: 'object', + properties: { + result: { type: 'string' }, + steps: { + type: 'array', + items: { + type: 'object', + properties: { + step: { type: 'number' }, + result: { type: 'string' } + } + } + } + } + } + } + } + } + } + } }, handler: storeController.updateSensorQC, });