ashok 10 months ago
commit 07b776b97d

@ -208,19 +208,67 @@ exports.getConnectionsInfoOfParticularTank = async (req, reply) => {
//get tanks data by passing username
exports.getTank = async (req, reply) => {
try {
await Tank.find({customerId: req.query.customerId})
.exec()
.then((docs) => {
reply.send({ status_code: 200, data: docs, count: docs.length });
})
.catch((err) => {
console.log(err);
reply.send({ error: err });
const { customerId } = req.params;
// Aggregate to count need_sensor values
const needSensorCounts = await Tank.aggregate([
{ $match: { customerId } },
{
$group: {
_id: "$need_sensor",
count: { $sum: 1 },
},
},
]);
const inputIsMotorCounts = await Tank.aggregate([
{ $match: { customerId } },
{ $unwind: "$connections.inputConnections" },
{
$group: {
_id: "$connections.inputConnections.inputismotor",
count: { $sum: 1 },
},
},
]);
// Retrieve all tank documents for the customerId
const tanks = await Tank.find({ customerId }).exec();
// Format the aggregation results
const needSensorResult = needSensorCounts.reduce((acc, item) => {
if (item._id === "yes") {
acc["yes"] = item.count;
}
return acc;
}, {});
const inputIsMotorResult = inputIsMotorCounts.reduce((acc, item) => {
if (item._id === true) {
acc["true"] = item.count;
}
return acc;
}, {});
// Return the combined response
reply.send({
status_code: 200,
customerId,
needSensor: {
yes: needSensorResult["YES"] || 0,
},
inputIsMotor: {
true: inputIsMotorResult["true"] || 0,
},
tanks: {
data: tanks,
count: tanks.length,
},
});
} catch (err) {
throw boom.boomify(err);
} catch (error) {
reply.status(500).send({ error: "An error occurred while fetching the data." });
}
};
},
@ -242,7 +290,7 @@ exports.getTanksensorcount = async (req, reply) => {
// Process each tank
const tankResults = tanks.map(tank => {
const needSensorCount = {
YES: tank.need_sensor === "yes" ? 1 : 0,
YES: tank.need_sensor === "YES" ? 1 : 0,
};
@ -268,6 +316,7 @@ exports.getTanksensorcount = async (req, reply) => {
});
reply.send({
alltanks:tanks,
customerId,
tanks: tankResults,
});

Loading…
Cancel
Save