api for lcd,get orders of customer

master^2
Varun 3 months ago
parent 7971f7a30b
commit 82f4b6c62a

@ -7996,3 +7996,74 @@ exports.compareMeasuredHeight = async (req, reply) => {
// generateAndSaveTankExcel();
async function publishAllTankMotorsForCustomer(customerId) {
try {
const tanks = await Tank.find({ customerId, status: 'active' }).lean();
for (const tank of tanks) {
const { tankName, tankLocation, connections } = tank;
const inputConnections = connections?.inputConnections || [];
for (const conn of inputConnections) {
if (!conn.motor_id || conn.status !== 'active') continue;
let time = 0;
if (conn.motor_status === '2') {
const now = moment().tz('Asia/Kolkata');
if (conn.startTime) {
const start = moment(conn.startTime, 'DD-MMM-YYYY - HH:mm');
if (start.isValid()) {
const hoursElapsed = moment.duration(now.diff(start)).asHours();
if (hoursElapsed <= 12) {
time = Math.floor(moment.duration(now.diff(start)).asMinutes());
} else {
console.log(`⏳ Skipped motor_id ${conn.motor_id} — startTime older than 12 hours`);
}
} else {
console.warn(`⚠️ Invalid startTime format for motor_id ${conn.motor_id}: ${conn.startTime}`);
}
} else {
console.log(`⚠️ startTime is null for motor_id ${conn.motor_id}, sending time = 0`);
}
}
const payload = {
tankName,
tankLocation,
motor_status: conn.motor_status,
customerId,
time,
};
const topic = `water/motor-status/${conn.motor_id}`;
client.publish(topic, JSON.stringify(payload), { qos: 1 }, (err) => {
if (err) {
console.error(`❌ Failed to publish to ${topic}:`, err.message);
} else {
console.log(`📤 Published to ${topic}:`, payload);
}
});
}
}
} catch (err) {
console.error(`❌ Error publishing motor data for customer ${customerId}:`, err.message);
}
}
// 🕒 Call every 30 seconds
// Run every 30 seconds
setInterval(() => {
publishAllTankMotorsForCustomer('AWSUSKY4');
}, 30000);

@ -1461,3 +1461,26 @@ const bookingId = `ARM${datePart}${randomDigit}`;
throw boom.internal("Failed to handle booking action", err);
}
};
exports.getordersofcustomer = async (req, reply) => {
try {
const customerId = req.params.customerId;
// Find the specific tank
const mainTank = await Tankerbooking.find({
customerId: customerId,
});
if (!mainTank) {
return reply.send({ status_code: 404, error: "Main tank not found" });
}
// Send the found tank within a list
reply.send({ status_code: 200, data: [mainTank] });
} catch (err) {
throw boom.boomify(err);
}
};

@ -1184,7 +1184,7 @@ fastify.route({
url: "/api/booking/accept/:supplierId",
schema: {
description: "Accept a requested booking by supplier",
tags: ["Supplier-Data"],
tags: ["User"],
summary: "Accept booking and move to tanker bookings",
params: {
type: "object",
@ -1212,6 +1212,28 @@ fastify.route({
});
fastify.route({
method: "GET",
url: "/api/getordersofcustomer/:customerId",
schema: {
tags: ["User"],
description: "Get tanker orders of customer",
summary: "Get tanker orders of customer",
params: {
type: "object",
properties: {
customerId: { type: "string" }
},
required: ["customerId"]
}
},
handler: userController.getordersofcustomer
});
next();
};

Loading…
Cancel
Save