From 2656722e59152d404126180557847a121ad70f7d Mon Sep 17 00:00:00 2001 From: varun Date: Tue, 30 Jul 2024 03:13:10 -0400 Subject: [PATCH] creating batch numbers --- src/controllers/storeController.js | 59 +++++++++++++++++++++++++++++- src/models/store.js | 9 +---- src/routes/storeRoute.js | 27 +++++++++++++- 3 files changed, 84 insertions(+), 11 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 4ebfd302..7e4e912c 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -973,10 +973,47 @@ exports.getpumpswitchqc = async (req, reply) => { }; + + +const generateBatchNo = (type, hardwareIdCompany) => { + const date = new Date(); + const ddmmyy = `${date.getDate().toString().padStart(2, '0')}${(date.getMonth() + 1).toString().padStart(2, '0')}${date.getFullYear().toString().slice(-2)}`; + const companyPrefix = hardwareIdCompany.slice(0, 2).toUpperCase(); + const randomNumbers = Math.floor(100 + Math.random() * 900).toString(); // 3 random digits + + if (type === 'slave') { + return `SL${ddmmyy}${companyPrefix}${randomNumbers}`; + } + if (type === 'master') { + return `MA${ddmmyy}${companyPrefix}${randomNumbers}`; + } + if (type === 'motor_switch') { + return `MS${ddmmyy}${companyPrefix}${randomNumbers}`; + } + // Add other type conditions if needed + return null; +}; + exports.createSensor = async (req, reply) => { try { const storeId = req.params.storeId; - const { indate, batchno, hardwareId_company, quantity } = req.body; + const { indate, batchno, hardwareId_company, quantity, model, type } = req.body; + + let finalBatchNo = batchno; + + if (batchno === 'new') { + let isUnique = false; + + while (!isUnique) { + finalBatchNo = generateBatchNo(type, hardwareId_company); + + // Check for uniqueness + const existingBatchNo = await Insensors.findOne({ batchno: finalBatchNo }); + if (!existingBatchNo) { + isUnique = true; + } + } + } const date = moment().format('MM-DD'); let entries = []; @@ -984,7 +1021,8 @@ exports.createSensor = async (req, reply) => { for (let i = 0; i < quantity; i++) { const newSensor = { storeId, - batchno, + model, + batchno: finalBatchNo, type, indate, hardwareId_company @@ -999,3 +1037,20 @@ exports.createSensor = async (req, reply) => { reply.code(500).send(err); } }; + + + +exports.getbatchnumbers = async (req, reply) => { + try { + await Insensors.distinct('batchno', { storeId: req.params.storeId }) + .then((batchNumbers) => { + reply.send({ status_code: 200, data: batchNumbers, count: batchNumbers.length }); + }) + .catch((err) => { + console.log(err); + reply.send({ error: err }); + }); + } catch (err) { + reply.send({ error: err }); + } +}; diff --git a/src/models/store.js b/src/models/store.js index 0fc9bb22..36352be9 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -156,14 +156,6 @@ const installationschema = new mongoose.Schema({ updatedBy: ObjectId, }, { versionKey: false }); - - - - - - - - const waterLeverSensorInSchema = new mongoose.Schema({ storeId:{ type: String }, @@ -238,6 +230,7 @@ const insensorsSchema = new mongoose.Schema({ hardwareId: { type: String }, masterId: { type: String, default: null }, type: { type: String }, + model: { type: String }, indate: { type: String }, hardwareId_company: { type: String }, qccheck: { type: String, default: null }, diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index 4e9d7308..7c5814d8 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -711,6 +711,8 @@ fastify.post("/api/createwaterlevelSensorintime/:storeId", { properties: { batchno: { type: "string" }, + model:{type:"string"}, + type:{type:"string"}, quantity: { type: "string" }, indate: { type: "string" }, hardwareId_company: { type: "string" } @@ -720,7 +722,30 @@ fastify.post("/api/createwaterlevelSensorintime/:storeId", { handler: storeController.createSensor, }) - +fastify.get("/api/getbatchnumbers/:storeId", { + schema: { + tags: ["Store-Data"], + description: "This is to Get batch numbers", + summary: "This is to Get batch numbers", + params: { + required: ["storeId"], + type: "object", + properties: { + storeId: { + type: "string", + description: "storeId", + }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: fastify.auth([fastify.authenticate]), + handler: storeController.getbatchnumbers, +});