diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 2ed19ea1..5078c5f8 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -160,23 +160,53 @@ exports.updateTanksInfo = async (req, reply) => { } }; -//delete selected tank + + + + exports.deleteTanksInfo = async (req, reply) => { try { - const customerId = req.params.customerId; - - const tankName = req.query.tankName; + const { customerId } = req.params; + const { tankName } = req.query; const tankLocation = req.body.tankLocation.toLowerCase(); - const tank = await Tank.findOneAndDelete({ tankName: tankName,customerId:customerId,tankLocation:tankLocation }); + if (!tankName || !tankLocation) { + return reply.code(400).send({ message: "Tank name and location are required" }); + } - reply.send({ status_code: 200, data: tank}); - // return tank; - } catch (err) { - throw boom.boomify(err); + // Convert tankLocation to lowercase (for case-insensitive match) + const normalizedTankLocation = tankLocation.toLowerCase(); + + // Find and delete the main tank + const deletedTank = await Tank.findOneAndDelete({ + customerId, + tankName, + tankLocation: normalizedTankLocation + }); + + if (!deletedTank) { + return reply.code(404).send({ message: "Tank not found" }); + } + + // Remove the deleted tank from inputConnections and outputConnections in all other tanks + await Tank.updateMany( + { customerId }, + { + $pull: { + "connections.inputConnections": { inputConnections: tankName }, + "connections.outputConnections": { outputConnections: tankName } + } + } + ); + + return reply.send({ message: "Tank deleted successfully" }); + } catch (error) { + console.error("Error deleting tank:", error); + return reply.code(500).send({ message: "Internal Server Error" }); } }; + exports.getConnectionsInfoOfParticularTank = async (req, reply) => { try { const customerId = req.params.customerId; @@ -6315,3 +6345,72 @@ cron.schedule( // // Call the function to update stopTime // updateStopTimeFormat(); + + + +exports.updatetankstatus = async (req, reply) => { + try { + const { customerId } = req.params; + const { tankName, tankLocation, status } = req.body; + + if (!["active", "inactive"].includes(status)) { + return reply.code(400).send({ message: "Invalid status value" }); + } + + // Find the main tank + const mainTank = await Tank.findOneAndUpdate( + { customerId, tankName, tankLocation }, + { $set: { status } }, + { new: true } + ); + + if (!mainTank) { + return reply.code(404).send({ message: "Tank not found" }); + } + + // Update status in related outputConnections tanks + await Tank.updateMany( + { + customerId, + "connections.outputConnections.outputConnections": tankName, + }, + { $set: { "connections.outputConnections.$.status": status } } + ); + + // Update status in related inputConnections tanks + await Tank.updateMany( + { + customerId, + "connections.inputConnections.inputConnections": tankName, + }, + { $set: { "connections.inputConnections.$.status": status } } + ); + + return reply.send({ message: "Tank status updated successfully" }); + } catch (error) { + console.error("Error updating tank status:", error); + return reply.code(500).send({ message: "Internal Server Error" }); + } +}; + + + + +exports.listofactiveandinactivetankstatus = async (req, reply) => { + try { + const { customerId } = req.params; + const { status } = req.query; + + if (!["active", "inactive"].includes(status)) { + return reply.code(400).send({ message: "Invalid status value" }); + } + + // Find tanks based on customerId and status + const tanks = await Tank.find({ customerId, status }); + + return reply.send({ tanks }); + } catch (error) { + console.error("Error fetching tank list:", error); + return reply.code(500).send({ message: "Internal Server Error" }); + } +}; diff --git a/src/models/tanks.js b/src/models/tanks.js index d53a79cb..4c42da8e 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -63,7 +63,7 @@ const tanksSchema = new mongoose.Schema({ notificationSentVeryHigh: { type: Boolean }, notificationSentHigh: { type: Boolean }, all_motor_status: { type: Boolean }, - + status:{ type: String, default: "active" }, connections: { source: { type: String }, @@ -81,7 +81,7 @@ const tanksSchema = new mongoose.Schema({ water_level: { type: String, default: null }, manual_threshold_percentage: { type: String, default: "90" }, manual_threshold_time: { type: String, default: null }, - + status:{ type: String, default: "active" }, stop_threshold_time: { type: String, default: null }, threshold_type: { type: String, default: "percentage" }, startTime: { type: String, default: null }, @@ -103,7 +103,8 @@ const tanksSchema = new mongoose.Schema({ manual_threshold_percentage: { type: String, default: "90" }, manual_threshold_time: { type: String, default: null }, threshold_type: { type: String, default: "percentage" }, - waterlevelPercentage: { type: String, default: null } + waterlevelPercentage: { type: String, default: null } , + status:{ type: String, default: "active" }, } ], inputWaterlevelPercentage: { type: String, default: null }, diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index 4a904302..3ee25576 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -1231,6 +1231,70 @@ module.exports = function (fastify, opts, next) { }); + fastify.route({ + method: "PUT", + url: "/api/updatetankstatus/:customerId", + schema: { + tags: ["Tank"], + summary: "This is for updating tank status to active or inactive", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + + body: { + type: "object", + // required: ['phone'], + properties: { + tankName:{ type: "string" }, + tankLocation:{type:"string"}, + status:{type:"string"}, + + + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + //preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.updatetankstatus, + }); + + + fastify.route({ + method: "GET", + url: "/api/listofactiveandinactivetankstatus/:customerId", + schema: { + tags: ["Tank"], + summary: "Get list of active or inactive tanks", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { type: "string", description: "Customer ID" }, + }, + }, + querystring: { + type: "object", + properties: { + status: { type: "string", enum: ["active", "inactive"] }, + }, + }, + security: [{ basicAuth: [] }], + }, + handler: tanksController.listofactiveandinactivetankstatus, + }); + + next(); }