diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 5a55656d..e344046c 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/controllers/supplierOrderController.js b/src/controllers/supplierOrderController.js index b771017c..adf428a2 100644 --- a/src/controllers/supplierOrderController.js +++ b/src/controllers/supplierOrderController.js @@ -782,3 +782,21 @@ exports.medicine = async (req, reply) => { } }; + + +exports.getOrdersByCustomerId = async (req, reply) => { + const { customerId } = req.query; + + if (!customerId) { + return reply.status(400).send({ message: "customerId is required" }); + } + + try { + const orders = await Tankerbooking.find({ customerId }).exec(); + reply.send({ status_code: 200, data: orders, count: orders.length }); + } catch (err) { + reply.status(500).send({ message: err.message }); + } +}; + + diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 67ab1693..e53dc116 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 @@ -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; 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/models/store.js b/src/models/store.js index a3c903c0..26e664a7 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: [{ diff --git a/src/routes/supplierOrdersRoutes.js b/src/routes/supplierOrdersRoutes.js index 661ef902..bc00ab2d 100644 --- a/src/routes/supplierOrdersRoutes.js +++ b/src/routes/supplierOrdersRoutes.js @@ -583,6 +583,28 @@ 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)" }, + + }, + + }, + security: [{ basicAuth: [] }] + }, + handler: supplierOrderController.getOrdersByCustomerId + }); + + + // 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(); };