diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 28754f5a..a2893398 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -1251,16 +1251,41 @@ exports.createSensor = async (req, reply) => { exports.getbatchnumbers = async (req, reply) => { try { - await Insensors.distinct('batchno', { storeId: req.params.storeId }) + const { storeId, type } = req.params; + + let prefix = ""; + switch (type.toUpperCase()) { + case "SLAVE": + prefix = "SL"; + break; + case "MASTER": + prefix = "MA"; + break; + case "SENSOR": + prefix = "SN"; + break; + case "MOTOR_SWITCH": + prefix = "MS"; + break; + default: + reply.send({ status_code: 400, message: "Invalid type" }); + return; + } + + await Insensors.distinct('batchno', { + storeId: storeId, + batchno: { $regex: `^${prefix}` }, + }) .then((batchNumbers) => { reply.send({ status_code: 200, data: batchNumbers, count: batchNumbers.length }); }) .catch((err) => { - console.log(err); - reply.send({ error: err }); + console.error(err); + reply.send({ status_code: 500, error: err }); }); } catch (err) { - reply.send({ error: err }); + console.error(err); + reply.send({ status_code: 500, error: err }); } }; diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index fdb814e4..7e097c77 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -1109,7 +1109,7 @@ fastify.post("/api/createwaterlevelSensorintime/:storeId", { handler: storeController.createSensor, }) -fastify.get("/api/getbatchnumbers/:storeId", { +fastify.get("/api/getbatchnumbers/:storeId/:type", { schema: { tags: ["Store-Data"], description: "This is to Get batch numbers", @@ -1124,6 +1124,16 @@ fastify.get("/api/getbatchnumbers/:storeId", { }, }, }, + params: { + required: ["type"], + type: "object", + properties: { + type: { + type: "string", + description: "type", + }, + }, + }, security: [ { basicAuth: [],