added get pumps and users

master^2
Varun 9 months ago
parent 47f4522793
commit 2073783c05

@ -2691,6 +2691,47 @@ const checkWaterLevel = async (customerId, motorId, fcmToken, receiverTank) => {
// };
exports.getPumpsAndUsers = async (req, reply) => {
try {
const { customerId } = req.params;
// Fetch motor_id from inputConnections of all tanks for the customer
const tanks = await Tank.find({ customerId }, { "connections.inputConnections.motor_id": 1 });
const motorIds = tanks.flatMap((tank) =>
tank.connections?.inputConnections?.map((conn) => conn.motor_id).filter(Boolean) || []
);
// Fetch username and staff names from User collection
const user = await User.findOne({ customerId }, { username: 1, "staff.staff.name": 1 });
const staffNames = user?.staff?.staff?.map((s) => s.name) || [];
const username = user?.username || "";
// Include username at the beginning of staffNames and add "manual" at the end
const updatedStaffNames = [username, ...staffNames, "manual"];
// Prepare response
const result = {
motorIds,
staffNames: updatedStaffNames,
};
return reply.send({ success: true, data: result });
} catch (error) {
console.error("Error fetching data:", error);
return reply.status(500).send({ success: false, message: "Internal Server Error" });
}
};
const monitorWaterLevels = async () => {
try {
const tanks = await Tank.find({});
@ -2736,6 +2777,8 @@ async function calculateTotalPumpedWater(customerId, motorId, start_instance_id)
}
return 0; // Return 0 if no data found
}
exports.motorAction = async (req, reply) => {
try {
const customerId = req.params.customerId;

@ -881,7 +881,31 @@ module.exports = function (fastify, opts, next) {
});
fastify.get("/api/getPumpsAndUsers/:customerId", {
schema: {
tags: ["Tank"],
description: "This is to Get pumps and users of particular customerId",
summary: "This is to Get pumps and users of particular customerId",
params: {
type: "object",
properties: {
customerId: {
type: "string",
description: "storeId",
},
},
required: ["customerId"],
},
security: [
{
basicAuth: [],
},
],
},
// preHandler: fastify.auth([fastify.authenticate]),
handler: tanksController.getPumpsAndUsers,
});

Loading…
Cancel
Save