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",