changes in switch count

master
Varun 10 months ago
parent f2fc3a7327
commit 332ea45589

@ -229,55 +229,54 @@ exports.getTanksensorcount = async (req, reply) => {
try {
const { customerId } = req.params;
// Aggregate to count need_sensor values
const needSensorCounts = await Tank.aggregate([
{ $match: { customerId } },
{
$group: {
_id: "$need_sensor",
count: { $sum: 1 },
},
},
]);
// Find all tanks for the given customerId
const tanks = await Tank.find({ customerId });
const inputIsMotorCounts = await Tank.aggregate([
{ $match: { customerId } },
{ $unwind: "$connections.inputConnections" },
{
$group: {
_id: "$connections.inputConnections.inputismotor",
count: { $sum: 1 },
if (!tanks || tanks.length === 0) {
return reply.send({
message: "No tanks found for the given customerId",
data: [],
});
}
// Process each tank
const tankResults = tanks.map(tank => {
const needSensorCount = {
YES: tank.need_sensor === "yes" ? 1 : 0,
};
const inputConnections = tank.connections.inputConnections || [];
const inputIsMotorCounts = inputConnections.reduce(
(acc, connection) => {
if (connection.inputismotor === true) acc.true += 1;
return acc;
},
},
]);
{ true: 0 }
);
// Format the results
const needSensorResult = needSensorCounts.reduce((acc, item) => {
acc[item._id] = item.count;
return acc;
}, {});
const totalInputConnections = inputConnections.length;
const inputIsMotorResult = inputIsMotorCounts.reduce((acc, item) => {
acc[item._id ? "true" : "false"] = item.count;
return acc;
}, {});
return {
tankName: tank.tankName,
needSensorCount,
inputIsMotorCounts,
totalInputConnections,
inputConnections,
};
});
// Return the response
reply.send({
customerId,
needSensor: {
YES: needSensorResult["YES"] || 0,
NO: needSensorResult["NO"] || 0,
},
inputIsMotor: {
true: inputIsMotorResult["true"] || 0,
false: inputIsMotorResult["false"] || 0,
},
tanks: tankResults,
});
} catch (error) {
reply.status(500).send({ error: "An error occurred while fetching the data." });
}
};
},
exports.getTanksofParticularInstaller = async (req, reply) => {

Loading…
Cancel
Save