diff --git a/src/controllers/createConnectionController.js b/src/controllers/createConnectionController.js index 28540979..0358b71e 100644 --- a/src/controllers/createConnectionController.js +++ b/src/controllers/createConnectionController.js @@ -20,8 +20,28 @@ exports.createConnections = async (req, body) => { const tankInfo = await Tank.findOne({ customerId: customerId.toString(),tankName:tankname }) const usertobeInserted = req.body; tankInfo.connections.source = tankInfo.tankName; - if (usertobeInserted.inputConnections) tankInfo.connections.inputConnections = usertobeInserted.inputConnections; - if (usertobeInserted.outputConnections) tankInfo.connections.outputConnections = usertobeInserted.outputConnections; + if (usertobeInserted.inputConnections) { + // update inputismotor field for each input connection + tankInfo.connections.inputConnections = usertobeInserted.inputConnections.map(connection => { + return { + inputConnections: connection.inputConnections, + input_type: connection.input_type, + motor_status: connection.motor_status || "0", + inputismotor: connection.inputismotor || false // default to false if not specified + }; + }); + } + + if (usertobeInserted.outputConnections) { + // update outputismotor field for each output connection + tankInfo.connections.outputConnections = usertobeInserted.outputConnections.map(connection => { + return { + outputConnections: connection.outputConnections, + output_type: connection.output_type, + outputismotor: connection.outputismotor || false // default to false if not specified + }; + }); + } const connection_data = req.body.outputConnections for (const data of connection_data){ diff --git a/src/models/tanks.js b/src/models/tanks.js index 44c86290..27f8ba83 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -9,6 +9,27 @@ const ObjectId = Schema.Types.ObjectId; // Store a random password reset code const code = Math.floor(100000 + Math.random() * 900000); const RoleSchema = new Schema({ name: String }); +// const tanksSchema = new mongoose.Schema({ +// hardwareId: { type: String }, +// hardwareId_type: { type: String }, +// hardwareId_company: { type: String }, +// customerId: { type: String, default: null }, +// tankName: { type: String, default: null }, +// blockName: { type: String, default: null }, +// capacity: { type: String, default: "0" }, +// typeOfWater: { type: String, default: null }, +// waterlevel: { type: String, default: "0" }, +// tankLocation: { type: String, default: null }, +// motor_status: { type: String, default: 0 }, +// connections: { +// source: { type: String}, +// inputConnections: [{ inputConnections: String,input_type:String,motor_status: { type: String, default: "0" } }], +// outputConnections: [{ outputConnections: String,output_type:String }] +// } + + +// }); + const tanksSchema = new mongoose.Schema({ hardwareId: { type: String }, hardwareId_type: { type: String }, @@ -20,15 +41,28 @@ const tanksSchema = new mongoose.Schema({ typeOfWater: { type: String, default: null }, waterlevel: { type: String, default: "0" }, tankLocation: { type: String, default: null }, - motor_status: { type: String, default: 0 }, + motor_status: { type: String, default: "0" }, connections: { - source: { type: String}, - inputConnections: [{ inputConnections: String,input_type:String,motor_status: { type: String, default: "0" } }], - outputConnections: [{ outputConnections: String,output_type:String }] + source: { type: String }, + inputConnections: [ + { + inputConnections: { type: String }, + input_type: { type: String }, + inputismotor: { type: Boolean, default: false }, + motor_status: { type: String, default: "0" } + } + ], + outputConnections: [ + { + outputConnections: { type: String }, + output_type: { type: String }, + outputismotor: { type: Boolean, default: false }, + motor_status: { type: String, default: "0" } + } + ] } - - -}); + }); + const motordataSchema = new mongoose.Schema({ diff --git a/src/routes/createConnectionsRoute.js b/src/routes/createConnectionsRoute.js index 502ac2b4..ccbc4cc1 100644 --- a/src/routes/createConnectionsRoute.js +++ b/src/routes/createConnectionsRoute.js @@ -3,67 +3,154 @@ const createConnectionController = require("../controllers/createConnectionContr module.exports = function (fastify, opts, next) { + // fastify.route({ + // method: "POST", + // url: "/api/createConnections/:customerId", + // schema: { + // tags: ["Connections"], + // description: "This is for cretae New Connection", + // summary: "This is for cretae New Connection.", + // params: { + // required: ["customerId"], + // type: "object", + // properties: { + // customerId: { + // type: "string", + // description: "customerId", + // }, + // }, + // }, + // // body: { + // // type: "object", + // // properties: { + // // tankname: { type: "string" }, + // // inputConnections: { + // // type: "array", + // // maxItems: 2500, + // // items: { + // // type: "object", + // // properties: { + // // inputConnections: { type: "string", default: null }, + // // input_type: { type: "string", default: null }, + // // }, + // // }, + // // }, + // // outputConnections: { + // // type: "array", + // // maxItems: 2500, + // // items: { + // // type: "object", + // // properties: { + // // outputConnections: { type: "string", default: null }, + // // output_type: { type: "string", default: null }, + // // }, + // // }, + // // }, + // // }, + // // }, + // body: { + // type: "object", + // properties: { + // tankname: { type: "string" }, + // inputConnections: { + // type: "array", + // maxItems: 2500, + // items: { + // type: "object", + // properties: { + // inputConnections: { type: "string", default: null }, + // input_type: { type: "string", default: null }, + // inputismotor: { type: "boolean", default: true } + // }, + // }, + // }, + // outputConnections: { + // type: "array", + // maxItems: 2500, + // items: { + // type: "object", + // properties: { + // outputConnections: { type: "string", default: null }, + // output_type: { type: "string", default: null }, + // outputismotor: { type: "boolean", default: true } + // }, + // }, + // }, + // }, + // }, + // security: [ + // { + // basicAuth: [], + // }, + // ], + + // }, + // preHandler: fastify.auth([fastify.authenticate]), + // handler: createConnectionController.createConnections, + // // onResponse: (request, reply) => { + // // validationHandler.sendPhoneVerificationCode(request, reply); + // // }, + // //onResponse: validationHandler.sendPhoneVerificationCode, + // }); + + fastify.route({ - method: "POST", - url: "/api/createConnections/:customerId", - schema: { - tags: ["Connections"], - description: "This is for cretae New Connection", - summary: "This is for cretae New Connection.", - params: { + method: "POST", + url: "/api/createConnections/:customerId", + schema: { + tags: ["Connections"], + description: "This is for cretae New Connection", + summary: "This is for cretae New Connection.", + params: { required: ["customerId"], type: "object", properties: { - customerId: { - type: "string", - description: "customerId", - }, + customerId: { + type: "string", + description: "customerId", + }, }, - }, - body: { - type: "object", - properties: { + }, + body: { + type: "object", + properties: { tankname: { type: "string" }, - inputConnections: { - type: "array", - maxItems: 2500, - items: { - type: "object", - properties: { - inputConnections: { type: "string", default: null }, - input_type: { type: "string", default: null }, - }, - }, - }, - outputConnections: { - type: "array", - maxItems: 2500, - items: { - type: "object", - properties: { - outputConnections: { type: "string", default: null }, - output_type: { type: "string", default: null }, - }, - }, - }, - }, - }, - security: [ - { - basicAuth: [], - }, - ], - - }, - preHandler: fastify.auth([fastify.authenticate]), - handler: createConnectionController.createConnections, - // onResponse: (request, reply) => { - // validationHandler.sendPhoneVerificationCode(request, reply); - // }, - //onResponse: validationHandler.sendPhoneVerificationCode, - }); - - + inputConnections: { + type: "array", + maxItems: 2500, + items: { + type: "object", + properties: { + inputConnections: { type: "string", default: null }, + input_type: { type: "string", default: null }, + inputismotor: { type: "boolean", default: true }, + }, + }, + }, + outputConnections: { + type: "array", + maxItems: 2500, + items: { + type: "object", + properties: { + outputConnections: { type: "string", default: null }, + output_type: { type: "string", default: null }, + outputismotor: { type: "boolean", default: true }, + }, + }, + }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + preHandler: fastify.auth([fastify.authenticate]), + handler: createConnectionController.createConnections, + }); + fastify.route({ method: "PUT",