From fbc337e1420d93596781e42fcdfa9d1f69146441 Mon Sep 17 00:00:00 2001 From: Varun Date: Fri, 31 Jan 2025 16:52:40 +0530 Subject: [PATCH] changes in motordata --- src/controllers/tanksController.js | 23 ++++++++++++++++------ src/models/tanks.js | 31 ++++++++++++++++++++++++++++++ src/routes/tanksRoute.js | 3 ++- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index ee7677ef..803efe73 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -385,7 +385,7 @@ exports.getTanksofParticularInstaller = async (req, reply) => { exports.getTankmotordata = async (req, reply) => { try { - const { startDate, stopDate } = req.body; + const { startDate, stopDate,pumps,users } = req.body; const { customerId } = req.params; // Validate and format the input dates @@ -406,11 +406,22 @@ exports.getTankmotordata = async (req, reply) => { if (user) { const userName = user.username || "N/A"; - const motordatas = await MotorData.find({ - customerId, - - }); + + let query = { customerId }; + + if (pumps !== "All") { + query.motor_id = pumps; + } + if (users !== "All") { + query.started_by = users; + } + + // Fetch motor data with applied filters + const motordatas = await MotorData.find(query); + + + const filtereddatas = motordatas.filter((record) => { const recordTime = moment(record.startTime, "DD-MMM-YYYY - HH:mm").toDate(); @@ -2706,7 +2717,7 @@ exports.getPumpsAndUsers = async (req, reply) => { const username = user?.username || ""; // Include username at the beginning of staffNames and add "manual" at the end - const updatedStaffNames = [username, ...staffNames, "manual"]; + const updatedStaffNames = [username, ...staffNames, "manual","user"]; // Prepare response const result = { diff --git a/src/models/tanks.js b/src/models/tanks.js index 11bc90b0..ebaebd42 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -125,6 +125,7 @@ const customerautopercentages = ({ const motordataSchema = new mongoose.Schema({ customerId: { type: String, default: null }, motor_id: { type: String, default: null }, + started_by:{ type: String, default: "user" }, start_instance_id:{type:String,default:null}, supplierTank: { type: String, default: null }, receiverTank: { type: String, default: null }, @@ -142,6 +143,36 @@ const motordataSchema = new mongoose.Schema({ }); +const updateMotorData = async () => { + try { + // Fetch all motor data where quantity_delivered is null or needs updating + const motorDataRecords = await MotorData.find({}); + + for (let record of motorDataRecords) { + // Convert string values to numbers by removing commas + const initialLevel = parseInt(record.receiverInitialwaterlevel.replace(/,/g, ""), 10) || 0; + const finalLevel = parseInt(record.receiverfinalwaterlevel.replace(/,/g, ""), 10) || 0; + + // Calculate quantity delivered + const quantityDelivered = finalLevel - initialLevel; + + // Update the record + await MotorData.updateOne( + { _id: record._id }, + { + $set: { + started_by: "user", + quantity_delivered: quantityDelivered.toString(), // Convert back to string for consistency + }, + } + ); + } + + console.log("Motor data updated successfully!"); + } catch (err) { + console.error("Error updating motor data:", err); + } +}; const tankSchema = new mongoose.Schema({ tankhardwareId: { type: String }, diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 96a1f82e..8aa40fa0 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -864,7 +864,8 @@ module.exports = function (fastify, opts, next) { properties: { startDate:{ type: "string" }, stopDate:{type:"string"}, - block:{type:"string"}, + pumps:{type:"string"}, + users:{type:"string"},