Bhaskar 8 months ago
commit c77b8c0330

@ -722,50 +722,50 @@ exports.editStore = async (request, reply) => {
}; };
const moment = require('moment'); const moment = require('moment');
exports.createwaterlevelSensor = async (req, reply) => { // exports.createwaterlevelSensor = async (req, reply) => {
try { // try {
const storeId = req.params.storeId // const storeId = req.params.storeId
const { hardwareId,hardwareId_company, type, indate } = req.body; // const { hardwareId,hardwareId_company, type, indate } = req.body;
var mater_seq_id = await generatewaterlevelsensorId(); // var mater_seq_id = await generatewaterlevelsensorId();
const date = moment().format('MM-DD'); // const date = moment().format('MM-DD');
const prefix = 'AS' + date + 'MALOV1'; // const prefix = 'AS' + date + 'MALOV1';
var masterId = `${prefix}${mater_seq_id}`; // var masterId = `${prefix}${mater_seq_id}`;
const newSensor = new WaterLeverSensor({ // const newSensor = new WaterLeverSensor({
storeId, // storeId,
hardwareId, // hardwareId,
masterId, // masterId,
type, // type,
indate // indate
}); // });
const savedSensor = await newSensor.save(); // const savedSensor = await newSensor.save();
reply.code(200).send(savedSensor); // reply.code(200).send(savedSensor);
} catch (err) { // } catch (err) {
reply.code(500).send(err); // reply.code(500).send(err);
} // }
}; // };
exports.editWaterLevelSensor = async (req, reply) => { // exports.editWaterLevelSensor = async (req, reply) => {
try { // try {
const { storeId } = req.params; // const { storeId } = req.params;
const updates = req.body; // const updates = req.body;
const updatedSensor = await WaterLeverSensor.findOneAndUpdate( // const updatedSensor = await WaterLeverSensor.findOneAndUpdate(
{ storeId:storeId,hardwareId: req.body.hardwareId }, // { storeId:storeId,hardwareId: req.body.hardwareId },
updates, // updates,
{ new: true } // { new: true }
); // );
if (!updatedSensor) { // if (!updatedSensor) {
reply.code(404).send({ message: "Sensor not found" }); // reply.code(404).send({ message: "Sensor not found" });
} else { // } else {
reply.code(200).send(updatedSensor); // reply.code(200).send(updatedSensor);
} // }
} catch (err) { // } catch (err) {
reply.code(500).send(err); // reply.code(500).send(err);
} // }
}; // };
exports.deleteWaterLevelSensor = async (req, reply) => { exports.deleteWaterLevelSensor = async (req, reply) => {
try { try {
@ -809,28 +809,28 @@ exports.editStore = async (request, reply) => {
} }
}; };
exports.qccheckwaterlevelSensor = async (request, reply) => { // exports.qccheckwaterlevelSensor = async (request, reply) => {
try { // try {
const { hardwareId } = request.params; // const { hardwareId } = request.params;
const updateData = request.body; // const updateData = request.body;
// Find the document by hardwareId and update it with the fields received in the body // // Find the document by hardwareId and update it with the fields received in the body
const updatedSensor = await WaterLeverSensor.findOneAndUpdate( // const updatedSensor = await WaterLeverSensor.findOneAndUpdate(
{ hardwareId: hardwareId }, // { hardwareId: hardwareId },
{ $set: updateData }, // { $set: updateData },
{ new: true } // Return the updated document // { new: true } // Return the updated document
); // );
if (!updatedSensor) { // if (!updatedSensor) {
return reply.status(404).send({ error: 'Sensor not found' }); // return reply.status(404).send({ error: 'Sensor not found' });
} // }
return reply.status(200).send(updatedSensor); // return reply.status(200).send(updatedSensor);
} catch (error) { // } catch (error) {
console.error(error); // console.error(error);
return reply.status(500).send({ error: 'An error occurred while updating the sensor' }); // return reply.status(500).send({ error: 'An error occurred while updating the sensor' });
} // }
}; // };
exports.getHardware = async (req, reply) => { exports.getHardware = async (req, reply) => {
@ -930,39 +930,39 @@ exports.editStore = async (request, reply) => {
exports.editSlave = async (req, reply) => { // exports.editSlave = async (req, reply) => {
try { // try {
const { hardwareId } = req.params; // const { hardwareId } = req.params;
const updates = req.body; // const updates = req.body;
const mainHardware = await WaterLeverSensor.findOne({ hardwareId }); // const mainHardware = await WaterLeverSensor.findOne({ hardwareId });
if (!mainHardware) { // if (!mainHardware) {
reply.code(404).send({ message: "Main hardware not found" }); // reply.code(404).send({ message: "Main hardware not found" });
return; // return;
} // }
const slaveIndex = mainHardware.slaves.tankhardware.findIndex( // const slaveIndex = mainHardware.slaves.tankhardware.findIndex(
(slave) => slave.tankhardwareId === req.body.tankhardwareId // (slave) => slave.tankhardwareId === req.body.tankhardwareId
); // );
if (slaveIndex === -1) { // if (slaveIndex === -1) {
reply.code(404).send({ message: "Slave not found" }); // reply.code(404).send({ message: "Slave not found" });
return; // return;
} // }
mainHardware.slaves.tankhardware[slaveIndex] = { // mainHardware.slaves.tankhardware[slaveIndex] = {
...mainHardware.slaves.tankhardware[slaveIndex], // ...mainHardware.slaves.tankhardware[slaveIndex],
...updates, // ...updates,
}; // };
const updatedHardware = await mainHardware.save(); // const updatedHardware = await mainHardware.save();
reply.code(200).send(updatedHardware); // reply.code(200).send(updatedHardware);
} catch (err) { // } catch (err) {
reply.code(500).send(err); // reply.code(500).send(err);
} // }
}; // };
exports.deleteSlave = async (req, reply) => { 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) => { exports.getpumpswitchqc = async (req, reply) => {
try { try {
@ -1261,8 +1312,8 @@ const generateBatchNo = (type, hardwareIdCompany) => {
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, model, type } = req.body; const { indate, batchno, hardwareId_company, quantity, model,type } = req.body;
const sensorType = type.toLowerCase();
let finalBatchNo = batchno; let finalBatchNo = batchno;
if (batchno === 'New') { if (batchno === 'New') {
@ -1287,7 +1338,7 @@ exports.createSensor = async (req, reply) => {
storeId, storeId,
model, model,
batchno: finalBatchNo, batchno: finalBatchNo,
type, type: sensorType,
indate, indate,
hardwareId_company hardwareId_company
}; };
@ -1348,7 +1399,7 @@ exports.getbatchnumbers = async (req, reply) => {
exports.getiots = async (req, reply) => { exports.getiots = async (req, reply) => {
try { try {
const storeId = req.params.storeId; 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 }; let query = { storeId: storeId };

@ -250,7 +250,7 @@ const motorSwitchSensorInSchema = new mongoose.Schema({
const insensorsSchema = new mongoose.Schema({ const insensorsSchema = new mongoose.Schema({
storeId: { type: String }, storeId: { type: String },
hardwareId: { type: String }, hardwareId: { type: String ,default:null},
masterId: { type: String, default: null }, masterId: { type: String, default: null },
type: { type: String }, type: { type: String },
model: { type: String }, model: { type: String },

@ -510,67 +510,67 @@ fastify.get("/api/getusersofParticularInstaller", {
handler: storeController.getusersofParticularInstaller, handler: storeController.getusersofParticularInstaller,
}); });
fastify.post("/api/createwaterlevelSensor/:storeId", { // fastify.post("/api/createwaterlevelSensor/:storeId", {
schema: { // schema: {
description: "This is for creating waterlevel Sensor", // description: "This is for creating waterlevel Sensor",
tags: ["Store-Data"], // tags: ["Store-Data"],
summary: "This is for creating waterlevel Sensor", // summary: "This is for creating waterlevel Sensor",
params: { // params: {
required: ["storeId"], // required: ["storeId"],
type: "object", // type: "object",
properties: { // properties: {
storeId: { // storeId: {
type: "string", // type: "string",
description: "storeId", // description: "storeId",
}, // },
}, // },
}, // },
body: { // body: {
type: "object", // type: "object",
properties: { // properties: {
hardwareId: { type: "string" }, // hardwareId: { type: "string" },
type: { type: "string" }, // type: { type: "string" },
indate: { type: "string" }, // indate: { type: "string" },
hardwareId_company: { type: "string" } // hardwareId_company: { type: "string" }
}, // },
}, // },
}, // },
handler: storeController.createwaterlevelSensor, // handler: storeController.createwaterlevelSensor,
}) // })
fastify.put("/api/editwaterlevelSensor/:storeId", { // fastify.put("/api/editwaterlevelSensor/:storeId", {
schema: { // schema: {
description: "This is for editing a water level sensor", // description: "This is for editing a water level sensor",
tags: ["Store-Data"], // tags: ["Store-Data"],
summary: "This is for editing a water level sensor", // summary: "This is for editing a water level sensor",
params: { // params: {
required: ["storeId"], // required: ["storeId"],
type: "object", // type: "object",
properties: { // properties: {
storeId: { // storeId: {
type: "string", // type: "string",
description: "storeId", // description: "storeId",
}, // },
}, // },
}, // },
body: { // body: {
type: "object", // type: "object",
properties: { // properties: {
hardwareId: { type: "string" }, // hardwareId: { type: "string" },
type: { type: "string" }, // type: { type: "string" },
indate: { type: "string" }, // indate: { type: "string" },
hardwareId_company: { type: "string" } // hardwareId_company: { type: "string" }
}, // },
}, // },
}, // },
handler: storeController.editWaterLevelSensor, // handler: storeController.editWaterLevelSensor,
}); // });
fastify.delete("/api/deletewaterlevelSensor/:storeId", { fastify.delete("/api/deletewaterlevelSensor/:storeId", {
@ -625,37 +625,37 @@ fastify.get("/api/getHardware/:storeId", {
handler: storeController.getHardware, handler: storeController.getHardware,
}); });
fastify.post("/api/qccheckwaterlevelSensor/:hardwareId", { // fastify.post("/api/qccheckwaterlevelSensor/:hardwareId", {
schema: { // schema: {
description: "This is for checking waterlevel Sensor", // description: "This is for checking waterlevel Sensor",
tags: ["Store-Data"], // tags: ["Store-Data"],
summary: "This is for checking waterlevel Sensor", // summary: "This is for checking waterlevel Sensor",
params: { // params: {
required: ["hardwareId"], // required: ["hardwareId"],
type: "object", // type: "object",
properties: { // properties: {
hardwareId: { // hardwareId: {
type: "string", // type: "string",
description: "hardwareId", // description: "hardwareId",
}, // },
}, // },
}, // },
body: { // body: {
type: "object", // type: "object",
properties: { // properties: {
qccheck: { type: "string" }, // qccheck: { type: "string" },
qccheckdate: { type: "string" }, // qccheckdate: { type: "string" },
qcby: { type: "string" }, // qcby: { type: "string" },
comment: { type: "string" }, // comment: { type: "string" },
outforrepairdate: { type: "string" }, // outforrepairdate: { type: "string" },
sendto: { type: "string" }, // sendto: { type: "string" },
repairfeedback: { type: "string" }, // repairfeedback: { type: "string" },
}, // },
}, // },
}, // },
handler: storeController.qccheckwaterlevelSensor, // handler: storeController.qccheckwaterlevelSensor,
}) // })
fastify.put("/api/getHardwareqc/:storeId", { fastify.put("/api/getHardwareqc/:storeId", {
@ -752,45 +752,45 @@ fastify.post("/api/addSlave/:hardwareId", {
}); });
fastify.put("/api/editSlave/:hardwareId", { // fastify.put("/api/editSlave/:hardwareId", {
schema: { // schema: {
description: "This is for editing a slave of a water level sensor", // description: "This is for editing a slave of a water level sensor",
tags: ["Store-Data"], // tags: ["Store-Data"],
summary: "This is for editing a slave of a water level sensor", // summary: "This is for editing a slave of a water level sensor",
params: { // params: {
required: ["hardwareId"], // required: ["hardwareId"],
type: "object", // type: "object",
properties: { // properties: {
hardwareId: { // hardwareId: {
type: "string", // type: "string",
description: "Main hardware ID", // description: "Main hardware ID",
}, // },
}, // },
}, // },
body: { // body: {
type: "object", // type: "object",
properties: { // properties: {
tankhardwareId: { type: "string" }, // tankhardwareId: { type: "string" },
type: { type: "string" }, // type: { 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 },
qccheckdate: { type: "string", default: null }, // qccheckdate: { type: "string", default: null },
qcby: { type: "string", default: null }, // qcby: { type: "string", default: null },
comment: { type: "string", default: "0" }, // comment: { type: "string", default: "0" },
outforrepairdate: { type: "string", default: "0" }, // outforrepairdate: { type: "string", default: "0" },
sendto: { type: "string", default: null }, // sendto: { type: "string", default: null },
repairfeedback: { type: "string", default: "0" }, // repairfeedback: { type: "string", default: "0" },
dateofinstallation: { type: "string", default: null }, // dateofinstallation: { type: "string", default: null },
installedby: { type: "string", default: "0" }, // installedby: { type: "string", default: "0" },
customerId: { type: "string", default: "0" }, // customerId: { type: "string", default: "0" },
comments: { type: "string", default: "0" }, // comments: { type: "string", default: "0" },
}, // },
}, // },
}, // },
handler: storeController.editSlave, // handler: storeController.editSlave,
}); // });
fastify.delete("/api/deleteSlave/:hardwareId", { fastify.delete("/api/deleteSlave/:hardwareId", {
@ -1109,6 +1109,25 @@ fastify.post("/api/createwaterlevelSensorintime/:storeId", {
handler: storeController.createSensor, 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", { fastify.post("/api/updateSensorById/:_id", {
schema: { 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", { fastify.get("/api/getbatchnumbers/:storeId/:type", {
schema: { schema: {

Loading…
Cancel
Save