Bhaskar 9 months ago
commit 6cd325634d

@ -1051,6 +1051,7 @@ let supplier_tanks = [];
exports.consumption = async (request, reply) => { exports.consumption = async (request, reply) => {
try { try {
const { customerId } = request.params; const { customerId } = request.params;
@ -1065,11 +1066,18 @@ exports.consumption = async (request, reply) => {
// Construct the query object // Construct the query object
const tankQuery = { customerId, tankLocation: "overhead" }; const tankQuery = { customerId, tankLocation: "overhead" };
if (block !== "All") tankQuery.blockName = block; if (block !== "All") tankQuery.blockName = block;
if (typeofwater !== "all") tankQuery.typeOfWater = typeofwater;
// Add typeofwater filter
if (typeofwater === "bore") {
tankQuery.typeOfWater = { $in: ["bore", "Bore Water"] };
} else if (typeofwater === "drinking") {
tankQuery.typeOfWater = { $in: ["drinking", "Drinking Water"] };
}
const tanks = await Tank.find(tankQuery); const tanks = await Tank.find(tankQuery);
const tankData = []; const tankData = [];
const tankconsumptionData = []; const tankconsumptionData = [];
let totalConsumptionForSelectedBlockAndTypeOfWater = 0;
let totalBoreConsumptionForSelectedBlockAndTypeOfWater = 0; let totalBoreConsumptionForSelectedBlockAndTypeOfWater = 0;
let totalBoreCapacityForSelectedBlockAndTypeOfWater = 0; let totalBoreCapacityForSelectedBlockAndTypeOfWater = 0;
let totalDrinkingConsumptionForSelectedBlockAndTypeOfWater = 0; let totalDrinkingConsumptionForSelectedBlockAndTypeOfWater = 0;
@ -1087,7 +1095,8 @@ exports.consumption = async (request, reply) => {
tankName: tank.tankName, tankName: tank.tankName,
tankLocation: tank.tankLocation, tankLocation: tank.tankLocation,
...(block !== "All" && { block: tank.blockName }), ...(block !== "All" && { block: tank.blockName }),
...(typeofwater !== "all" && { typeofwater: tank.typeOfWater }), ...(typeofwater === "bore" && { typeofwater: { $in: ["bore", "Bore Water"] } }),
...(typeofwater === "drinking" && { typeofwater: { $in: ["drinking", "Drinking Water"] } }),
}); });
const filteredConsumptions = tankConsumptions.filter((record) => { const filteredConsumptions = tankConsumptions.filter((record) => {
@ -1100,13 +1109,13 @@ exports.consumption = async (request, reply) => {
}, 0); }, 0);
const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records; const consumption = (waterlevel_at_midnight + total_water_added_from_midnight) - waterlevel + total_consumption_from_records;
totalConsumptionForSelectedBlockAndTypeOfWater += consumption;
// Add to the total consumption and capacities based on water type // Add to the total consumption and capacities based on water type
if (tank.typeOfWater === "bore") { if (tank.typeOfWater === "bore" || tank.typeOfWater === "Bore Water") {
totalBoreConsumptionForSelectedBlockAndTypeOfWater += consumption; totalBoreConsumptionForSelectedBlockAndTypeOfWater += consumption;
totalBoreCapacityForSelectedBlockAndTypeOfWater += capacity; totalBoreCapacityForSelectedBlockAndTypeOfWater += capacity;
} } else if (tank.typeOfWater === "drinking" || tank.typeOfWater === "Drinking Water") {
if (tank.typeOfWater === "drinking") {
totalDrinkingConsumptionForSelectedBlockAndTypeOfWater += consumption; totalDrinkingConsumptionForSelectedBlockAndTypeOfWater += consumption;
totalDrinkingCapacityForSelectedBlockAndTypeOfWater += capacity; totalDrinkingCapacityForSelectedBlockAndTypeOfWater += capacity;
} }
@ -1127,7 +1136,7 @@ exports.consumption = async (request, reply) => {
}); });
} }
// Calculate total consumption percentage // Calculate total consumption percentages
const boreConsumptionPercentage = totalBoreCapacityForSelectedBlockAndTypeOfWater const boreConsumptionPercentage = totalBoreCapacityForSelectedBlockAndTypeOfWater
? ((totalBoreConsumptionForSelectedBlockAndTypeOfWater / totalBoreCapacityForSelectedBlockAndTypeOfWater) * 100).toFixed(2) ? ((totalBoreConsumptionForSelectedBlockAndTypeOfWater / totalBoreCapacityForSelectedBlockAndTypeOfWater) * 100).toFixed(2)
: 0; : 0;
@ -1141,7 +1150,10 @@ exports.consumption = async (request, reply) => {
? boreConsumptionPercentage ? boreConsumptionPercentage
: typeofwater === "drinking" : typeofwater === "drinking"
? drinkingConsumptionPercentage ? drinkingConsumptionPercentage
: 0; : ((totalBoreConsumptionForSelectedBlockAndTypeOfWater + totalDrinkingConsumptionForSelectedBlockAndTypeOfWater) /
(totalBoreCapacityForSelectedBlockAndTypeOfWater + totalDrinkingCapacityForSelectedBlockAndTypeOfWater) *
100
).toFixed(2);
// Include the total consumption in the response // Include the total consumption in the response
const response = { const response = {
@ -1149,11 +1161,21 @@ exports.consumption = async (request, reply) => {
tankData, tankData,
consumptiorecordsdatewise: tankconsumptionData, consumptiorecordsdatewise: tankconsumptionData,
totalConsumptionPercentage, totalConsumptionPercentage,
[`total consumption of ${typeofwater} and selected block`]: typeofwater === "bore" ...(typeofwater === "all"
? totalBoreConsumptionForSelectedBlockAndTypeOfWater ? {
: typeofwater === "drinking" totalConsumptionPercentageForBore: boreConsumptionPercentage,
? totalDrinkingConsumptionForSelectedBlockAndTypeOfWater totalConsumptionPercentageForDrinking: drinkingConsumptionPercentage,
: totalBoreConsumptionForSelectedBlockAndTypeOfWater + totalDrinkingConsumptionForSelectedBlockAndTypeOfWater, totalConsumptionForBore: totalBoreConsumptionForSelectedBlockAndTypeOfWater,
totalConsumptionForDrinking: totalDrinkingConsumptionForSelectedBlockAndTypeOfWater,
}
: {
[`total consumption of ${typeofwater} and selected block`]:
typeofwater === "bore"
? totalBoreConsumptionForSelectedBlockAndTypeOfWater
: totalDrinkingConsumptionForSelectedBlockAndTypeOfWater,
}
),
[`total consumption of ${typeofwater} and selected block`]: totalConsumptionForSelectedBlockAndTypeOfWater
}; };
reply.send(response); reply.send(response);
@ -1163,6 +1185,7 @@ exports.consumption = async (request, reply) => {
}; };
exports.consumptiondatewiseofalltanks = async (request, reply) => { exports.consumptiondatewiseofalltanks = async (request, reply) => {
try { try {
const { customerId } = request.params; const { customerId } = request.params;
@ -1814,7 +1837,7 @@ eventEmitter.on(
// Retrieve the user information // Retrieve the user information
const users = await User.find({ fcmIds: { $in: fcmTokens } }); const users = await User.find({ fcmIds: { $in: fcmTokens } });
const userNames = users.map(user => user.username).join(', '); const userNames = users.map(user => user.username).join(', ');
const startMethod = motorOnType === "Forced Manual"; const startMethod = motorOnType.toUpperCase() === "Forced Manual";
// Prepare the message // Prepare the message
const message = const message =
@ -1842,14 +1865,14 @@ eventEmitter.on(
// Retrieve the user information // Retrieve the user information
const users = await User.find({ fcmIds: { $in: fcmTokens } }); const users = await User.find({ fcmIds: { $in: fcmTokens } });
const userNames = users.map(user => user.username).join(', '); const userNames = users.map(user => user.username).join(', ');
const stopMethod = motorOnType === "Forced Manual"; const stopMethod = motorOnType.toUpperCase() === "Forced Manual";
// Prepare the message // Prepare the message
const message = const message =
`🚰 Tank Name: '${tankName}'\n` + `🚰 Tank Name: '${tankName}'\n` +
`🕒 Pump stopped at: '${stopTime}'\n` + `🕒 Pump stopped at: '${stopTime}'\n` +
`👤 Initiated by: ${userNames}\n` + `👤 Initiated by: ${userNames}\n` +
`Motor Off Type: '${stopMethod}'\n`; `🔄 Pump stopped by: '${stopMethod}'\n`;
// Send the notification // Send the notification
await sendNotification(fcmTokens, 'Motor Stopped 🛑', message); await sendNotification(fcmTokens, 'Motor Stopped 🛑', message);
@ -2436,7 +2459,7 @@ exports.motorAction = async (req, reply) => {
const blockName = req.body.from || "Unknown Block"; // Provide a fallback if `from` is missing const blockName = req.body.from || "Unknown Block"; // Provide a fallback if `from` is missing
const tankName = req.body.to || "Unknown Tank"; // Provide a fallback if `to` is missing const tankName = req.body.to || "Unknown Tank"; // Provide a fallback if `to` is missing
const stopTime = req.body.stopTime const stopTime = req.body.stopTime
const motorOnType = req.body.motor_on_type; const motorOnType = "manual";
const manual_threshold_time = req.body.manual_threshold_time; const manual_threshold_time = req.body.manual_threshold_time;
let hasNotifiedStart = false; let hasNotifiedStart = false;
let hasNotifiedStop = false; let hasNotifiedStop = false;
@ -2477,7 +2500,7 @@ exports.motorAction = async (req, reply) => {
{ $set: { "connections.inputConnections.$.motor_stop_status": "2", { $set: { "connections.inputConnections.$.motor_stop_status": "2",
"connections.inputConnections.$.manual_threshold_time": manual_threshold_time, "connections.inputConnections.$.manual_threshold_time": manual_threshold_time,
"connections.inputConnections.$.threshold_type": "time", "connections.inputConnections.$.threshold_type": "time",
"connections.inputConnections.$.motor_on_type": motorOnType } } "connections.inputConnections.$.motor_on_type": "manual" } }
); );
reply.code(200).send({ message: "Motor started successfully." }); reply.code(200).send({ message: "Motor started successfully." });

Loading…
Cancel
Save