combined get tanks with sensor and slave,switch count

master
Varun 10 months ago
parent 332ea45589
commit 58368736c8

@ -208,19 +208,67 @@ exports.getConnectionsInfoOfParticularTank = async (req, reply) => {
//get tanks data by passing username //get tanks data by passing username
exports.getTank = async (req, reply) => { exports.getTank = async (req, reply) => {
try { try {
await Tank.find({customerId: req.query.customerId}) const { customerId } = req.params;
.exec()
.then((docs) => { // Aggregate to count need_sensor values
reply.send({ status_code: 200, data: docs, count: docs.length }); const needSensorCounts = await Tank.aggregate([
}) { $match: { customerId } },
.catch((err) => { {
console.log(err); $group: {
reply.send({ error: err }); _id: "$need_sensor",
}); count: { $sum: 1 },
} catch (err) { },
throw boom.boomify(err); },
]);
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 (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 // Process each tank
const tankResults = tanks.map(tank => { const tankResults = tanks.map(tank => {
const needSensorCount = { 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({ reply.send({
alltanks:tanks,
customerId, customerId,
tanks: tankResults, tanks: tankResults,
}); });

Loading…
Cancel
Save