From 82f4b6c62ac7406ea26f3c92eb128535fb91066f Mon Sep 17 00:00:00 2001 From: Varun Date: Tue, 8 Jul 2025 11:21:42 +0530 Subject: [PATCH] api for lcd,get orders of customer --- src/controllers/tanksController.js | 71 ++++++++++++++++++++++++++++++ src/controllers/userController.js | 23 ++++++++++ src/routes/usersRoute.js | 24 +++++++++- 3 files changed, 117 insertions(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 90b580be..1c0b917e 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -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); + diff --git a/src/controllers/userController.js b/src/controllers/userController.js index e4d361f9..6e940856 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -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); + } +}; diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index 1fb10064..cd8970a3 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -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(); };