|
|
|
@ -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;
|
|
|
|
@ -5370,11 +5374,31 @@ 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." });
|
|
|
|
|
|
|
|
|
|
} catch (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
|
|
|
|
|