ashok 1 year ago
commit d0f381ae1f

@ -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) => { exports.createSensor = async (req, reply) => {
try { try {
const storeId = req.params.storeId; 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'); const date = moment().format('MM-DD');
let entries = []; let entries = [];
@ -984,7 +1021,8 @@ exports.createSensor = async (req, reply) => {
for (let i = 0; i < quantity; i++) { for (let i = 0; i < quantity; i++) {
const newSensor = { const newSensor = {
storeId, storeId,
batchno, model,
batchno: finalBatchNo,
type, type,
indate, indate,
hardwareId_company hardwareId_company
@ -999,3 +1037,20 @@ exports.createSensor = async (req, reply) => {
reply.code(500).send(err); 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 });
}
};

@ -157,14 +157,6 @@ const installationschema = new mongoose.Schema({
}, { versionKey: false }); }, { versionKey: false });
const waterLeverSensorInSchema = new mongoose.Schema({ const waterLeverSensorInSchema = new mongoose.Schema({
storeId:{ type: String }, storeId:{ type: String },
hardwareId: { type: String }, hardwareId: { type: String },
@ -238,6 +230,7 @@ const insensorsSchema = new mongoose.Schema({
hardwareId: { type: String }, hardwareId: { type: String },
masterId: { type: String, default: null }, masterId: { type: String, default: null },
type: { type: String }, type: { type: String },
model: { type: String },
indate: { type: String }, indate: { type: String },
hardwareId_company: { type: String }, hardwareId_company: { type: String },
qccheck: { type: String, default: null }, qccheck: { type: String, default: null },

@ -711,6 +711,8 @@ fastify.post("/api/createwaterlevelSensorintime/:storeId", {
properties: { properties: {
batchno: { type: "string" }, batchno: { type: "string" },
model:{type:"string"},
type:{type:"string"},
quantity: { type: "string" }, quantity: { type: "string" },
indate: { type: "string" }, indate: { type: "string" },
hardwareId_company: { type: "string" } hardwareId_company: { type: "string" }
@ -720,7 +722,30 @@ fastify.post("/api/createwaterlevelSensorintime/:storeId", {
handler: storeController.createSensor, 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,
});

Loading…
Cancel
Save