From c51dd7e787fb400712ba9af08b8e8dcfad3bc4e6 Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 30 Jan 2025 11:56:03 +0530 Subject: [PATCH 1/3] changes in auto_percentage --- src/controllers/tanksController.js | 67 ++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 18 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index a3801708..38620081 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -5328,37 +5328,68 @@ exports.update_auto_mode = async (req, reply) => { exports.update_auto_percentage = async (req, reply) => { try { const customerId = req.params.customerId; - const { tankName, tankLocation, auto_min_percentage, auto_max_percentage, auto_mode_type } = req.body; + let { tankName, tankLocation, auto_min_percentage, auto_max_percentage, auto_mode_type } = req.body; - // Build the query filter - const filter = { customerId: customerId }; - if (tankName !== "all") { + // Handle the optional parameters + tankName = tankName ? tankName : null; + tankLocation = tankLocation ? tankLocation.toLowerCase() : null; + + const filter = { customerId }; + + // If tankName is not 'all', add it to the filter + if (tankName && tankName !== "all") { filter.tankName = tankName; } - if (tankLocation) { + + // Only add tankLocation to the filter if tankName is not 'all' + if (tankLocation && tankName !== "all") { filter.tankLocation = tankLocation; } - // Update auto_min_percentage, auto_max_percentage, and auto_mode_type - await Tank.updateMany( - filter, - { - $set: { - "auto_min_percentage": auto_min_percentage, - "auto_max_percentage": auto_max_percentage, - "auto_mode_type": auto_mode_type - } - } - ); + console.log("Update filter:", JSON.stringify(filter, null, 2)); - reply.send({ status_code: 200, message: "Auto mode and percentages updated successfully." }); + // Check if tanks exist + const matchingTanks = await Tank.find(filter); + console.log("Matching tanks:", matchingTanks); + + if (matchingTanks.length === 0) { + return reply.send({ status_code: 400, message: "No matching records found." }); + } + + // Define the update fields + const updateData = { + auto_min_percentage: String(auto_min_percentage || "20"), + auto_max_percentage: String(auto_max_percentage || "80"), + auto_mode_type: auto_mode_type || "default", + }; + + let result; + if (tankName && tankName !== "all") { + // Update only one tank if tankName is specified and not "all" + result = await Tank.updateOne(filter, { $set: updateData }); + } else { + // Update all tanks of the particular customer if tankName is "all" + result = await Tank.updateMany(filter, { $set: updateData }); + } + + console.log("Update result:", result); + + if (result.modifiedCount > 0) { + reply.send({ status_code: 200, message: "Auto mode and percentages updated successfully." }); + } else { + reply.send({ status_code: 400, message: "Values were already up-to-date." }); + } } catch (error) { - throw boom.boomify(error); + console.error(error); + reply.send({ status_code: 500, message: "Internal server error." }); } }; + + + //storing water level for every 15 minutes const getFormattedISTTime = () => { From bf1568c7d44b56fecf163a922001ab42a1a03ee3 Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 30 Jan 2025 11:59:32 +0530 Subject: [PATCH 2/3] changes --- src/controllers/tanksController.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 38620081..6d597a98 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -5374,11 +5374,9 @@ exports.update_auto_percentage = async (req, reply) => { console.log("Update result:", result); - if (result.modifiedCount > 0) { + reply.send({ status_code: 200, message: "Auto mode and percentages updated successfully." }); - } else { - reply.send({ status_code: 400, message: "Values were already up-to-date." }); - } + } catch (error) { console.error(error); reply.send({ status_code: 500, message: "Internal server error." }); From 9f03a62bf1abfa0f733e5dec1e469af9c99ad93c Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 30 Jan 2025 12:29:18 +0530 Subject: [PATCH 3/3] added get for automode default percentages for particular customer --- src/controllers/tanksController.js | 128 +++++++++++++++++++++++++++-- src/models/tanks.js | 11 ++- src/routes/tanksRoute.js | 30 +++++++ 3 files changed, 163 insertions(+), 6 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 6d597a98..9020b91a 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -1,5 +1,5 @@ //const Tank = require("../models/tanks"); -const { Tank, MotorData, IotData,MotorIot,TankWaterLevel,TankConsumptionSchema,TankConsumptionOriginalSchema } = require('../models/tanks') +const { Tank, MotorData, IotData,MotorIot,TankWaterLevel,TankConsumptionSchema,TankConsumptionOriginalSchema,CustomerAutoPercentages } = require('../models/tanks') const {User} = require("../models/User"); const boom = require("boom"); @@ -5325,6 +5325,10 @@ exports.update_auto_mode = async (req, reply) => { } }; + + + + exports.update_auto_percentage = async (req, reply) => { try { const customerId = req.params.customerId; @@ -5335,7 +5339,7 @@ exports.update_auto_percentage = async (req, reply) => { tankLocation = tankLocation ? tankLocation.toLowerCase() : null; const filter = { customerId }; - + // If tankName is not 'all', add it to the filter if (tankName && tankName !== "all") { filter.tankName = tankName; @@ -5370,13 +5374,33 @@ exports.update_auto_percentage = async (req, reply) => { } else { // Update all tanks of the particular customer if tankName is "all" result = await Tank.updateMany(filter, { $set: updateData }); + + // If auto_mode_type is default and tankName is "all", save or update the data in CustomerAutoPercentages + if (auto_mode_type === "default") { + const currentDate = new Date().toLocaleString("en-GB", { timeZone: "UTC" }); // Get current date in UTC + const formattedDate = currentDate.split(",").join(" -"); // Format it like '17-Dec-2024 - 15:56' + + // Use findOneAndUpdate to either update the existing record or create a new one if it doesn't exist + const updateOrCreate = await CustomerAutoPercentages.findOneAndUpdate( + { customerId }, // Search for the record with the customerId + { + $set: { + auto_min_percentage: String(auto_min_percentage || "20"), + auto_max_percentage: String(auto_max_percentage || "80"), + date: formattedDate, + }, + }, + { upsert: true, new: true } // If no record found, create a new one; return the updated record + ); + + console.log("CustomerAutoPercentages updated/created:", updateOrCreate); + } } console.log("Update result:", result); - - reply.send({ status_code: 200, message: "Auto mode and percentages updated successfully." }); - + reply.send({ status_code: 200, message: "Auto mode and percentages updated successfully." }); + } catch (error) { console.error(error); reply.send({ status_code: 500, message: "Internal server error." }); @@ -5386,6 +5410,100 @@ exports.update_auto_percentage = async (req, reply) => { +// Controller function to get CustomerAutoPercentages by customerId +exports.getCustomerAutoPercentages = async (req, reply) => { + try { + const { customerId } = req.params; // Extract customerId from the params + + // Find the record in CustomerAutoPercentages based on customerId + const customerData = await CustomerAutoPercentages.findOne({ customerId }); + + if (!customerData) { + return reply.send({ + status_code: 404, + message: "No data found for the provided customerId." + }); + } + + reply.send({ + status_code: 200, + message: "Customer data retrieved successfully.", + data: customerData + }); + + } catch (error) { + console.error(error); + reply.send({ + status_code: 500, + message: "Internal server error." + }); + } +}; + + + + +// exports.update_auto_percentage = async (req, reply) => { +// try { +// const customerId = req.params.customerId; +// let { tankName, tankLocation, auto_min_percentage, auto_max_percentage, auto_mode_type } = req.body; + +// // Handle the optional parameters +// tankName = tankName ? tankName : null; +// tankLocation = tankLocation ? tankLocation.toLowerCase() : null; + +// const filter = { customerId }; + +// // If tankName is not 'all', add it to the filter +// if (tankName && tankName !== "all") { +// filter.tankName = tankName; +// } + +// // Only add tankLocation to the filter if tankName is not 'all' +// if (tankLocation && tankName !== "all") { +// filter.tankLocation = tankLocation; +// } + +// console.log("Update filter:", JSON.stringify(filter, null, 2)); + +// // Check if tanks exist +// const matchingTanks = await Tank.find(filter); +// console.log("Matching tanks:", matchingTanks); + +// if (matchingTanks.length === 0) { +// return reply.send({ status_code: 400, message: "No matching records found." }); +// } + +// // Define the update fields +// const updateData = { +// auto_min_percentage: String(auto_min_percentage || "20"), +// auto_max_percentage: String(auto_max_percentage || "80"), +// auto_mode_type: auto_mode_type || "default", +// }; + +// let result; +// if (tankName && tankName !== "all") { +// // Update only one tank if tankName is specified and not "all" +// result = await Tank.updateOne(filter, { $set: updateData }); +// } else { +// // Update all tanks of the particular customer if tankName is "all" +// result = await Tank.updateMany(filter, { $set: updateData }); +// } + +// console.log("Update result:", result); + + +// reply.send({ status_code: 200, message: "Auto mode and percentages updated successfully." }); + +// } catch (error) { +// console.error(error); +// reply.send({ status_code: 500, message: "Internal server error." }); +// } +// }; + + + + //storing water level for every 15 minutes diff --git a/src/models/tanks.js b/src/models/tanks.js index 4c42da8e..11bc90b0 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -112,7 +112,14 @@ const tanksSchema = new mongoose.Schema({ } }); +const customerautopercentages = ({ + customerId: { type: String }, + auto_min_percentage: { type: String, required: true }, + auto_max_percentage: { type: String,default:null }, + date: { type: String, required: true }, + +}); const motordataSchema = new mongoose.Schema({ @@ -187,6 +194,8 @@ const tankconsumptionoriginalSchema = new mongoose.Schema({ const Tank = mongoose.model("Tank", tanksSchema); +const CustomerAutoPercentages = mongoose.model("CustomerAutoPercentages", customerautopercentages); + const MotorData = mongoose.model("MotorData", motordataSchema); const TankWaterLevel = mongoose.model("TankWaterLevel", tankWaterLevelSchema); const IotData = mongoose.model("IotData", IOttankSchema); @@ -195,6 +204,6 @@ const TankConsumptionOriginalSchema = mongoose.model("TankConsumptionOriginalSch module.exports = { - Tank, MotorData,IotData,TankWaterLevel,TankConsumptionSchema,TankConsumptionOriginalSchema + Tank, MotorData,IotData,TankWaterLevel,TankConsumptionSchema,TankConsumptionOriginalSchema,CustomerAutoPercentages } diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 65115aa5..a2f201f5 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -1159,6 +1159,36 @@ module.exports = function (fastify, opts, next) { //preHandler: fastify.auth([fastify.authenticate]), handler: tanksController.getBlockData, }); + + + fastify.route({ + method: "GET", + url: "/api/getCustomerAutoPercentages/:customerId", + schema: { + tags: ["Tank"], + summary: "This is to get auto mode default percentages", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + // querystring: { + // tankName: {type: 'string'} + // }, + security: [ + { + basicAuth: [], + }, + ], + }, + //preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.getCustomerAutoPercentages, + }); fastify.route({ method: "PUT",