From e26b2fee31fa7f3ff20368fccf93aa0c7c07551a Mon Sep 17 00:00:00 2001 From: Bhaskara Kishore Date: Thu, 23 Mar 2023 15:23:32 +0530 Subject: [PATCH 1/3] connected status list --- src/controllers/tankersController.js | 26 +++++++++++++++++ src/models/tankers.js | 2 +- src/routes/tankersRoute.js | 42 +++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/src/controllers/tankersController.js b/src/controllers/tankersController.js index 43f7fc00..cc272a9f 100644 --- a/src/controllers/tankersController.js +++ b/src/controllers/tankersController.js @@ -1,4 +1,5 @@ const { Tanker, Tankerbooking,Bore,GovtPipeLine } = require('../models/tankers') +const { FriendRequest } = require('../models/supplier') const { User,Counter, generateBookingId,resetCounter,generateCustomerId } = require('../models/User') @@ -461,3 +462,28 @@ exports.status = async (req, reply) => { } }; +exports.connectionStatus = async (req, reply) => { + try { + // query the database to check if the customer and supplier are connected + const isConnected = await FriendRequest.findOne({ customerId: req.query.customerId }) + .populate('supplier', null, { supplierId: req.query.supplierId }) + .exec() + .then(customer => customer.supplier !== null); + + console.log("isconne..", isConnected) + + if (isConnected) { + // if customer and supplier are connected, return list of tankers + const tankers = await Tanker.find({ status : 'connected'}).exec(); + console.log("tankers..", tankers) + reply.send({ tankers }); + } else { + // if customer and supplier are not connected, return error + reply.status(403).send({ error: 'Forbidden' });8 + } + //res.send({ tankers }); + } catch (err) { + console.error(err); + reply.status(500).send({ error: 'Internal server error' }); + } +} \ No newline at end of file diff --git a/src/models/tankers.js b/src/models/tankers.js index 6c0b6c9f..58954b48 100644 --- a/src/models/tankers.js +++ b/src/models/tankers.js @@ -24,7 +24,7 @@ const tankersSchema = new mongoose.Schema({ type: [String], default: [] }, - status: { type: String} + status: { type: String, default: 'disconnected'} }); diff --git a/src/routes/tankersRoute.js b/src/routes/tankersRoute.js index c0ca27db..06c51de2 100644 --- a/src/routes/tankersRoute.js +++ b/src/routes/tankersRoute.js @@ -54,7 +54,7 @@ module.exports = function (fastify, opts, next) { }, capacity: { type: "string"}, price: {type : "string"}, - status: {type: "string"} + // status: {type: "string"} }, }, @@ -568,6 +568,46 @@ module.exports = function (fastify, opts, next) { preHandler: fastify.auth([fastify.authenticate]), handler: tankersController.status, }); + + + fastify.route({ + method: 'GET', + url: '/connection-status', + schema: { + tags: ["Supplier"], + description: "This is for Get Tank Data for status connected or not", + summary: "This is for to Get Tank Data for status connected or not", + querystring: { + type: 'object', + properties: { + customerId: { type: 'string' }, + supplierId: { type: 'string' } + }, + required: ['customerId', 'supplierId'] + }, + // response: { + // 200: { + // type: 'object', + // properties: { + // isConnected: { type: 'boolean' } + // } + // }, + // 500: { + // type: 'object', + // properties: { + // error: { type: 'string' } + // } + // } + // } + security: [ + { + basicAuth: [], + }, + ], + }, + handler: tankersController.connectionStatus + }); + next(); } From 3bd6b0e953e0ce74769ba8a15deba46f932b17ff Mon Sep 17 00:00:00 2001 From: raj Date: Fri, 24 Mar 2023 05:01:06 +0000 Subject: [PATCH 2/3] updating tankers --- src/tankers.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/tankers.js diff --git a/src/tankers.js b/src/tankers.js new file mode 100644 index 00000000..e69de29b From f9ce6aa9bec59a8a77093eb1414b3cc955c7d70d Mon Sep 17 00:00:00 2001 From: Bhaskara Kishore Date: Fri, 24 Mar 2023 11:00:13 +0530 Subject: [PATCH 3/3] connection-status --- src/controllers/tankersController.js | 27 ++++++++++++++++++++++++++- src/controllers/tanksController.js | 4 ++-- src/routes/tankersRoute.js | 25 ++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/controllers/tankersController.js b/src/controllers/tankersController.js index 54c1c2c0..3023aa07 100644 --- a/src/controllers/tankersController.js +++ b/src/controllers/tankersController.js @@ -1,7 +1,7 @@ const { Tanker, Tankerbooking,Bore,GovtPipeLine } = require('../models/tankers') const { User,Counter, generateBookingId,resetCounter,generateCustomerId } = require('../models/User') - +const { FriendRequest } = require('../models/supplier') //const User = require("../models/User"); const boom = require("boom"); @@ -527,3 +527,28 @@ exports.status = async (req, reply) => { } }; +exports.connectionStatus = async (req, reply) => { + try { + // query the database to check if the customer and supplier are connected + const isConnected = await User.findOne({ customerId: req.query.customerId }) + .populate('supplier', null, { supplierId: req.query.supplierId }) + .exec() + .then(customer => customer.supplier !== null); + + console.log("isconne..", isConnected) + + if (isConnected) { + // if customer and supplier are connected, return list of tankers + const tankers = await Tanker.find({ }).exec(); + console.log("tankers..", tankers) + reply.send({ tankers }); + } else { + // if customer and supplier are not connected, return error + reply.status(403).send({ error: 'Forbidden' }); + } + //res.send({ tankers }); + } catch (err) { + console.error(err); + reply.status(500).send({ error: 'Internal server error' }); + } +} \ No newline at end of file diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index b3015698..4c7a7fcf 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -6,7 +6,7 @@ const boom = require("boom"); const fastify = require("fastify")({ logger: true, }); -const tanksController = require("./tanksController") +// const tanksController = require("./tanksController") @@ -510,4 +510,4 @@ exports.consumption = async (req, reply) => { } catch (err) { throw boom.boomify(err); } -}; +}; \ No newline at end of file diff --git a/src/routes/tankersRoute.js b/src/routes/tankersRoute.js index b4ca6de0..599cbc02 100644 --- a/src/routes/tankersRoute.js +++ b/src/routes/tankersRoute.js @@ -583,7 +583,30 @@ module.exports = function (fastify, opts, next) { }); - + fastify.route({ + method: 'GET', + url: '/connection-status', + schema: { + tags: ["Supplier"], + description: "This is for Get Tank Data for status connected or not", + summary: "This is for to Get Tank Data for status connected or not", + querystring: { + type: 'object', + properties: { + customerId: { type: 'string' }, + //supplierId: { type: 'string' } + }, + required: ['customerId'] + }, + security: [ + { + basicAuth: [], + }, + ], + }, + handler: tankersController.connectionStatus + }); + next(); }