added acxtive and inactive in tanks

master^2
Varun 8 months ago
parent 440833dde5
commit f3a120c1d9

@ -160,23 +160,53 @@ exports.updateTanksInfo = async (req, reply) => {
} }
}; };
//delete selected tank
exports.deleteTanksInfo = async (req, reply) => { exports.deleteTanksInfo = async (req, reply) => {
try { try {
const customerId = req.params.customerId; const { customerId } = req.params;
const { tankName } = req.query;
const tankName = req.query.tankName;
const tankLocation = req.body.tankLocation.toLowerCase(); 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}); // Convert tankLocation to lowercase (for case-insensitive match)
// return tank; const normalizedTankLocation = tankLocation.toLowerCase();
} catch (err) {
throw boom.boomify(err); // 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) => { exports.getConnectionsInfoOfParticularTank = async (req, reply) => {
try { try {
const customerId = req.params.customerId; const customerId = req.params.customerId;
@ -6315,3 +6345,72 @@ cron.schedule(
// // Call the function to update stopTime // // Call the function to update stopTime
// updateStopTimeFormat(); // 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" });
}
};

@ -63,7 +63,7 @@ const tanksSchema = new mongoose.Schema({
notificationSentVeryHigh: { type: Boolean }, notificationSentVeryHigh: { type: Boolean },
notificationSentHigh: { type: Boolean }, notificationSentHigh: { type: Boolean },
all_motor_status: { type: Boolean }, all_motor_status: { type: Boolean },
status:{ type: String, default: "active" },
connections: { connections: {
source: { type: String }, source: { type: String },
@ -81,7 +81,7 @@ const tanksSchema = new mongoose.Schema({
water_level: { type: String, default: null }, water_level: { type: String, default: null },
manual_threshold_percentage: { type: String, default: "90" }, manual_threshold_percentage: { type: String, default: "90" },
manual_threshold_time: { type: String, default: null }, manual_threshold_time: { type: String, default: null },
status:{ type: String, default: "active" },
stop_threshold_time: { type: String, default: null }, stop_threshold_time: { type: String, default: null },
threshold_type: { type: String, default: "percentage" }, threshold_type: { type: String, default: "percentage" },
startTime: { type: String, default: null }, startTime: { type: String, default: null },
@ -103,7 +103,8 @@ const tanksSchema = new mongoose.Schema({
manual_threshold_percentage: { type: String, default: "90" }, manual_threshold_percentage: { type: String, default: "90" },
manual_threshold_time: { type: String, default: null }, manual_threshold_time: { type: String, default: null },
threshold_type: { type: String, default: "percentage" }, 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 }, inputWaterlevelPercentage: { type: String, default: null },

@ -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(); next();
} }

Loading…
Cancel
Save