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