From b4e7fcbe51a989355e3c856bb3ce24dba4501bfc Mon Sep 17 00:00:00 2001 From: Varun Date: Thu, 3 Apr 2025 12:02:29 +0530 Subject: [PATCH 1/6] changes in installation --- src/controllers/installationController.js | 1 + src/models/store.js | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 14bf4a1d..30cda7c0 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -762,6 +762,7 @@ exports.masterConnectedSlaveList = async (req, reply) => { // Step 1: Find all slave tanks where connected_to matches const slaveTanks = await Insensors.find({ connected_to: connectedTo }); + console.log(slaveTanks,"slaveTanks") if (!slaveTanks || slaveTanks.length === 0) { return reply.status(404).send({ success: false, message: "No tanks found for the given connected_to value" }); diff --git a/src/models/store.js b/src/models/store.js index c71292b4..42e7b7e7 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -359,7 +359,7 @@ const insensorsSchema = new mongoose.Schema({ status: { type: String, default: "pending" }, connected_to: { type: String, default: "0" }, tankName: { type: String, default: "0" }, - tankLocation: { type: Number, default: 0 }, + tankLocation: { type: String, default: "0" }, connected_slave: { type: String, default: null }, quality_check_details: [{ From 63356cac31c4252db07bbe5a3b3f9b75a61c6aec Mon Sep 17 00:00:00 2001 From: Varun Date: Fri, 4 Apr 2025 12:37:02 +0530 Subject: [PATCH 2/6] added favorate suppliers to customer and common get for orders based on orders --- src/controllers/supplierOrderController.js | 41 ++++++++++++++ src/controllers/userController.js | 64 ++++++++++++++++++++++ src/models/User.js | 1 + src/routes/supplierOrdersRoutes.js | 26 +++++++++ src/routes/usersRoute.js | 61 +++++++++++++++++++++ 5 files changed, 193 insertions(+) diff --git a/src/controllers/supplierOrderController.js b/src/controllers/supplierOrderController.js index b771017c..da64f0e0 100644 --- a/src/controllers/supplierOrderController.js +++ b/src/controllers/supplierOrderController.js @@ -782,3 +782,44 @@ exports.medicine = async (req, reply) => { } }; + + + +exports.getOrdersByStatus = async (req, reply) => { + const { customerId, orderStatus } = req.query; + + const query = { orderStatus }; + if (customerId) { + query.customerId = customerId; + } + + try { + const orders = await Tankerbooking.find(query).exec(); + + let enrichedOrders = orders; + + if (orderStatus === "accepted") { + enrichedOrders = await Promise.all( + orders.map(async (order) => { + const deliveryBoy = await DeliveryBoy.findOne({ phone: order.delivery_agent_mobile }); + return { + ...order.toJSON(), + deliveryBoyLocation: deliveryBoy + ? { + deliveryboy_latitude: deliveryBoy.latitude, + deliveryboy_longitude: deliveryBoy.longitude, + deliveryboy_address: deliveryBoy.address + } + : null + }; + }) + ); + } + + reply.send({ status_code: 200, data: enrichedOrders, count: enrichedOrders.length }); + } catch (err) { + reply.status(400).send({ message: err.message }); + } +}; + + diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 179ce6e0..5317a902 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -1042,3 +1042,67 @@ exports.blockStaff = async (request, reply) => { reply.status(500).send({ error: 'An error occurred while blocking staff' }); } }; + + + + +exports.addFavoriteSupplier = async (req, reply) => { + const { customerId, supplierId } = req.body; + + try { + const user = await User.findOne({ customerId }); + + if (!user) { + return reply.status(404).send({ message: "User not found" }); + } + + if (!user.favorate_suppliers.includes(supplierId)) { + user.favorate_suppliers.push(supplierId); + await user.save(); + } + + reply.send({ message: "Supplier added to favorites", data: user.favorate_suppliers }); + } catch (err) { + reply.status(500).send({ message: err.message }); + } +}; + +exports.getFavoriteSuppliers = async (req, reply) => { + const { customerId } = req.params; + + try { + const user = await User.findOne({ customerId }) + .populate("favorate_suppliers") // If you want to get full supplier details + .exec(); + + if (!user) { + return reply.status(404).send({ message: "User not found" }); + } + + reply.send({ data: user.favorate_suppliers }); + } catch (err) { + reply.status(500).send({ message: err.message }); + } +}; + +exports.removeFavoriteSupplier = async (req, reply) => { + const { customerId, supplierId } = req.params; + + try { + const user = await User.findOne({ customerId }); + + if (!user) { + return reply.status(404).send({ message: "User not found" }); + } + + user.favorate_suppliers = user.favorate_suppliers.filter( + (id) => id.toString() !== supplierId + ); + + await user.save(); + + reply.send({ message: "Supplier removed from favorites", data: user.favorate_suppliers }); + } catch (err) { + reply.status(500).send({ message: err.message }); + } +}; diff --git a/src/models/User.js b/src/models/User.js index cbd5613f..94f9c376 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -56,6 +56,7 @@ const userSchema = new mongoose.Schema( inchargeName: String, phoneVerified: { type: Boolean, default: false }, phoneVerificationCode: { type: Number, default: 11111 }, + favorate_suppliers: [{ type: mongoose.Schema.Types.ObjectId, ref: "Supplier" }], passwordResetCode: { type: Number, default: code }, oneTimePasswordSetFlag: { type: Boolean, default: false }, emails: [{ email: String, verified: { type: Boolean, default: false } }], diff --git a/src/routes/supplierOrdersRoutes.js b/src/routes/supplierOrdersRoutes.js index 661ef902..c80ecf91 100644 --- a/src/routes/supplierOrdersRoutes.js +++ b/src/routes/supplierOrdersRoutes.js @@ -583,6 +583,32 @@ module.exports = function (fastify, opts, next) { }); + fastify.route({ + method: "GET", + url: "/api/orders", + schema: { + tags: ["Supplier-Order"], + description: "Get orders filtered by status and customerId", + summary: "Get orders filtered by status and customerId", + querystring: { + type: "object", + properties: { + customerId: { type: "string", description: "Customer ID (optional)" }, + orderStatus: { + type: "string", + enum: ["accepted", "rejected", "delivered", "pending"], + description: "Order status to filter by" + } + }, + required: ["orderStatus"] + }, + security: [{ basicAuth: [] }] + }, + handler: supplierOrderController.getOrdersByStatus + }); + + + // fastify.route({ // method: "GET", // url: "/api/billing/:bookingId", diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index 5a2f6b84..29dbba25 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -937,5 +937,66 @@ fastify.route({ }); + +fastify.route({ + method: "POST", + url: "/api/favorites/add", + schema: { + tags: ["User"], + description: "Add a supplier to the customer's favorites", + summary: "Add a supplier to the customer's favorites", + body: { + type: "object", + required: ["customerId", "supplierId"], + properties: { + customerId: { type: "string", description: "Customer ID" }, + supplierId: { type: "string", description: "Supplier ID to be added" } + } + }, + security: [{ basicAuth: [] }] + }, + handler: userController.addFavoriteSupplier +}); + +fastify.route({ + method: "GET", + url: "/api/favorites/:customerId", + schema: { + tags: ["User"], + description: "Get all favorite suppliers of a customer", + summary: "Get all favorite suppliers of a customer", + params: { + type: "object", + required: ["customerId"], + properties: { + customerId: { type: "string", description: "Customer ID" } + } + }, + security: [{ basicAuth: [] }] + }, + handler: userController.getFavoriteSuppliers +}); + +fastify.route({ + method: "DELETE", + url: "/api/favorites/:customerId/:supplierId", + schema: { + tags: ["User"], + description: "Remove a supplier from the customer's favorites", + summary: "Remove a supplier from the customer's favorites", + params: { + type: "object", + required: ["customerId", "supplierId"], + properties: { + customerId: { type: "string", description: "Customer ID" }, + supplierId: { type: "string", description: "Supplier ID to be removed" } + } + }, + security: [{ basicAuth: [] }] + }, + handler: userController.removeFavoriteSupplier +}); + + next(); }; From 29fdcee7e351420d6965d189393d1bda2162de54 Mon Sep 17 00:00:00 2001 From: Varun Date: Fri, 4 Apr 2025 12:44:53 +0530 Subject: [PATCH 3/6] changes --- src/controllers/supplierOrderController.js | 37 ++++------------------ src/routes/supplierOrdersRoutes.js | 8 ++--- 2 files changed, 9 insertions(+), 36 deletions(-) diff --git a/src/controllers/supplierOrderController.js b/src/controllers/supplierOrderController.js index da64f0e0..adf428a2 100644 --- a/src/controllers/supplierOrderController.js +++ b/src/controllers/supplierOrderController.js @@ -784,41 +784,18 @@ exports.medicine = async (req, reply) => { +exports.getOrdersByCustomerId = async (req, reply) => { + const { customerId } = req.query; -exports.getOrdersByStatus = async (req, reply) => { - const { customerId, orderStatus } = req.query; - - const query = { orderStatus }; - if (customerId) { - query.customerId = customerId; + if (!customerId) { + return reply.status(400).send({ message: "customerId is required" }); } try { - const orders = await Tankerbooking.find(query).exec(); - - let enrichedOrders = orders; - - if (orderStatus === "accepted") { - enrichedOrders = await Promise.all( - orders.map(async (order) => { - const deliveryBoy = await DeliveryBoy.findOne({ phone: order.delivery_agent_mobile }); - return { - ...order.toJSON(), - deliveryBoyLocation: deliveryBoy - ? { - deliveryboy_latitude: deliveryBoy.latitude, - deliveryboy_longitude: deliveryBoy.longitude, - deliveryboy_address: deliveryBoy.address - } - : null - }; - }) - ); - } - - reply.send({ status_code: 200, data: enrichedOrders, count: enrichedOrders.length }); + const orders = await Tankerbooking.find({ customerId }).exec(); + reply.send({ status_code: 200, data: orders, count: orders.length }); } catch (err) { - reply.status(400).send({ message: err.message }); + reply.status(500).send({ message: err.message }); } }; diff --git a/src/routes/supplierOrdersRoutes.js b/src/routes/supplierOrdersRoutes.js index c80ecf91..2942ee49 100644 --- a/src/routes/supplierOrdersRoutes.js +++ b/src/routes/supplierOrdersRoutes.js @@ -594,17 +594,13 @@ module.exports = function (fastify, opts, next) { type: "object", properties: { customerId: { type: "string", description: "Customer ID (optional)" }, - orderStatus: { - type: "string", - enum: ["accepted", "rejected", "delivered", "pending"], - description: "Order status to filter by" - } + }, required: ["orderStatus"] }, security: [{ basicAuth: [] }] }, - handler: supplierOrderController.getOrdersByStatus + handler: supplierOrderController.getOrdersByCustomerId }); From d470d627f2fe670fc07d9ef6a7a39980b5990f2a Mon Sep 17 00:00:00 2001 From: Varun Date: Fri, 4 Apr 2025 13:22:58 +0530 Subject: [PATCH 4/6] changes --- src/routes/supplierOrdersRoutes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/supplierOrdersRoutes.js b/src/routes/supplierOrdersRoutes.js index 2942ee49..bc00ab2d 100644 --- a/src/routes/supplierOrdersRoutes.js +++ b/src/routes/supplierOrdersRoutes.js @@ -596,7 +596,7 @@ module.exports = function (fastify, opts, next) { customerId: { type: "string", description: "Customer ID (optional)" }, }, - required: ["orderStatus"] + }, security: [{ basicAuth: [] }] }, From ca968ac483fb6c4a931cdc42e14d2149b7a7abf9 Mon Sep 17 00:00:00 2001 From: Varun Date: Tue, 8 Apr 2025 10:33:15 +0530 Subject: [PATCH 5/6] changes in motordata --- src/controllers/tanksController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 67ab1693..635a2978 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -397,7 +397,7 @@ exports.getTankmotordata = async (req, reply) => { // Convert input dates to ISO 8601 format for Date comparison const start = moment(startDate, "DD-MMM-YYYY - HH:mm").toDate(); - const end = moment(stopDate, "DD-MMM-YYYY - HH:mm").toDate(); + const end = moment(stopDate, "DD-MMM-YYYY - HH:mm").endOf('day').toDate(); // Convert input dates to string format for string-based comparison From a03e4557d3b0e6c1be031d9663d82ddf473a1b8d Mon Sep 17 00:00:00 2001 From: Varun Date: Tue, 8 Apr 2025 12:16:31 +0530 Subject: [PATCH 6/6] changes in motor action,till this the code worked fine --- src/controllers/tanksController.js | 43 ++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 635a2978..e53dc116 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -2857,7 +2857,19 @@ exports.motorAction = async (req, reply) => { const blockName = req.body.from || "Unknown Block"; const tankName = req.body.to || "Unknown Tank"; const stopTime = req.body.stopTime - if (action === "start") { + const inputConnection_1 = receiverTank.connections?.inputConnections?.find( + conn => conn.motor_id === motorId + ); + // Step 2: Check motor_stop_status + if (action === "start" && inputConnection_1.motor_stop_status === "2") { + // ✅ Proceed with motor start logic + return reply.status(400).send({ error: "Motor is already running or blocked from starting." }); + + // ... your logic to handle starting the motor + + } + + if (action === "start" && inputConnection_1.motor_stop_status !== "2") { if (motorIntervals[motorId]) { console.log(`🛑 Clearing old interval for motorId: ${motorId}`); @@ -2879,7 +2891,7 @@ exports.motorAction = async (req, reply) => { { customerId, "connections.inputConnections.motor_id": motorId }, { $set: { "connections.inputConnections.$.motor_stop_status": "2", - "connections.inputConnections.$.manual_threshold_time": manual_threshold_time, + "connections.inputConnections.$.threshold_type": threshold_type, "connections.inputConnections.$.motor_on_type": "manual" }} @@ -2908,6 +2920,7 @@ exports.motorAction = async (req, reply) => { console.log("entered time",motorId,motorStopStatus) // Update the tank connections with start time and threshold time this.publishMotorStopStatus(motorId, motorStopStatus); + const startTime1 = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss'); for await (const tank of Tank.find({ "connections.inputConnections.motor_id": motorId })) { const index = tank.connections.inputConnections.findIndex(connection => connection.motor_id === motorId); @@ -2918,7 +2931,7 @@ exports.motorAction = async (req, reply) => { $set: { [`connections.inputConnections.${index}.manual_threshold_time`]: req.body.manual_threshold_time, [`connections.inputConnections.${index}.threshold_type`]: "time", - [`connections.inputConnections.${index}.startTime`]: req.body.startTime, + [`connections.inputConnections.${index}.startTime`]: startTime1, [`connections.inputConnections.${index}.start_instance_id`]: start_instance_id } } @@ -2979,6 +2992,7 @@ exports.motorAction = async (req, reply) => { const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); + const currentTime1 = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss'); await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, { @@ -2988,7 +3002,7 @@ exports.motorAction = async (req, reply) => { "connections.inputConnections.$.threshold_type": null, "connections.inputConnections.$.manual_threshold_time": null, "connections.inputConnections.$.manual_threshold_percentage": null, - "connections.inputConnections.$.stopTime": currentTime, + "connections.inputConnections.$.stopTime": currentTime1, } } ); @@ -3223,11 +3237,12 @@ exports.motorAction = async (req, reply) => { async function stopMotor(motorId, customerId, start_instance_id,user_name) { console.log(user_name,"user_name in stop2") const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); + const currentTime1 = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss'); await Tank.updateOne( { customerId, "connections.inputConnections.motor_id": motorId }, { $set: { "connections.inputConnections.$.motor_stop_status": "1", - "connections.inputConnections.$.stopTime": currentTime, + "connections.inputConnections.$.stopTime": currentTime1, "connections.inputConnections.$.start_instance_id": null, "connections.inputConnections.$.threshold_type": null, "connections.inputConnections.$.manual_threshold_time": null, @@ -6131,14 +6146,19 @@ async function processIotData(hw_Id, data) { if (inputConnection) { inputConnection.motor_status = status; - if (inputConnection.motor_stop_status === "1" && status === 2 && inputConnection.motor_on_type !== "forced_manual") { + const now1 = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss'); + const nowMoment = moment(now1, 'DD-MMM-YYYY - HH:mm:ss'); + const startMoment = moment(inputConnection.startTime, 'DD-MMM-YYYY - HH:mm:ss'); + const stopMoment = moment(inputConnection.stopTime, 'DD-MMM-YYYY - HH:mm:ss'); + if (inputConnection.motor_stop_status === "1" && status === 2 && nowMoment.diff(stopMoment, 'seconds') >= 15 && inputConnection.motor_on_type !== "forced_manual") { const currentTime = moment().tz('Asia/Kolkata'); const formattedTime = currentTime.format('DD-MMM-YYYY - HH:mm'); + const startTime1 = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss'); const startInstanceId = `${hw_Id}${formattedTime}`; - + inputConnection.motor_stop_status = "2"; inputConnection.motor_on_type = "forced_manual"; - inputConnection.startTime = formattedTime; + inputConnection.startTime = startTime1; inputConnection.start_instance_id = startInstanceId; const newMotorData = new MotorData({ customerId:motorTank.customerId, @@ -6155,14 +6175,15 @@ async function processIotData(hw_Id, data) { await newMotorData.save(); } - - if (inputConnection.motor_stop_status === "2" && status === 1) { + + if (inputConnection.motor_stop_status === "2" && status === 1 && nowMoment.diff(startMoment, 'seconds') >= 15) { const motorData = await MotorData.findOne({ customerId:motorTank.customerId, motor_id: hw_Id, start_instance_id: inputConnection.start_instance_id }); const startinstance = inputConnection.start_instance_id; const currentTime = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm'); + const stopTime1 = moment().tz('Asia/Kolkata').format('DD-MMM-YYYY - HH:mm:ss'); inputConnection.motor_stop_status = "1"; inputConnection.motor_on_type = "manual"; - inputConnection.stopTime = currentTime; + inputConnection.stopTime = stopTime1; inputConnection.start_instance_id = null; const motorId = hw_Id;