Bhaskar 8 months ago
commit af814bd203

@ -1,5 +1,5 @@
//const Tank = require("../models/tanks"); //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 {User} = require("../models/User");
const boom = require("boom"); const boom = require("boom");
@ -5325,40 +5325,187 @@ exports.update_auto_mode = async (req, reply) => {
} }
}; };
exports.update_auto_percentage = async (req, reply) => { exports.update_auto_percentage = async (req, reply) => {
try { try {
const customerId = req.params.customerId; 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;
// Handle the optional parameters
tankName = tankName ? tankName : null;
tankLocation = tankLocation ? tankLocation.toLowerCase() : null;
const filter = { customerId };
// Build the query filter // If tankName is not 'all', add it to the filter
const filter = { customerId: customerId }; if (tankName && tankName !== "all") {
if (tankName !== "all") {
filter.tankName = tankName; filter.tankName = tankName;
} }
if (tankLocation) {
// Only add tankLocation to the filter if tankName is not 'all'
if (tankLocation && tankName !== "all") {
filter.tankLocation = tankLocation; filter.tankLocation = tankLocation;
} }
// Update auto_min_percentage, auto_max_percentage, and auto_mode_type console.log("Update filter:", JSON.stringify(filter, null, 2));
await Tank.updateMany(
filter, // Check if tanks exist
{ const matchingTanks = await Tank.find(filter);
$set: { console.log("Matching tanks:", matchingTanks);
"auto_min_percentage": auto_min_percentage,
"auto_max_percentage": auto_max_percentage, if (matchingTanks.length === 0) {
"auto_mode_type": auto_mode_type 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 });
// 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) { } catch (error) {
throw boom.boomify(error); console.error(error);
reply.send({ status_code: 500, message: "Internal server error." });
} }
}; };
// 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 //storing water level for every 15 minutes
const getFormattedISTTime = () => { const getFormattedISTTime = () => {

@ -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({ const motordataSchema = new mongoose.Schema({
@ -187,6 +194,8 @@ const tankconsumptionoriginalSchema = new mongoose.Schema({
const Tank = mongoose.model("Tank", tanksSchema); const Tank = mongoose.model("Tank", tanksSchema);
const CustomerAutoPercentages = mongoose.model("CustomerAutoPercentages", customerautopercentages);
const MotorData = mongoose.model("MotorData", motordataSchema); const MotorData = mongoose.model("MotorData", motordataSchema);
const TankWaterLevel = mongoose.model("TankWaterLevel", tankWaterLevelSchema); const TankWaterLevel = mongoose.model("TankWaterLevel", tankWaterLevelSchema);
const IotData = mongoose.model("IotData", IOttankSchema); const IotData = mongoose.model("IotData", IOttankSchema);
@ -195,6 +204,6 @@ const TankConsumptionOriginalSchema = mongoose.model("TankConsumptionOriginalSch
module.exports = { module.exports = {
Tank, MotorData,IotData,TankWaterLevel,TankConsumptionSchema,TankConsumptionOriginalSchema Tank, MotorData,IotData,TankWaterLevel,TankConsumptionSchema,TankConsumptionOriginalSchema,CustomerAutoPercentages
} }

@ -1159,6 +1159,36 @@ module.exports = function (fastify, opts, next) {
//preHandler: fastify.auth([fastify.authenticate]), //preHandler: fastify.auth([fastify.authenticate]),
handler: tanksController.getBlockData, 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({ fastify.route({
method: "PUT", method: "PUT",

Loading…
Cancel
Save