From e3490c5424f901d0730ad77bb6b49b67ac40b9cb Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 27 Mar 2023 03:39:39 -0400 Subject: [PATCH 1/2] once the connect request is sent it will not show in connection request --- src/handlers/supplierHandler.js | 28 +++++++++++++++++++++++++++- src/routes/supplierRoute.js | 12 +++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index 569d1f0e..8615f467 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -1,5 +1,6 @@ //Get the data models const { Supplier } = require('../models/supplier'); +const { FriendRequest } = require('../models/supplier') const { ProfilePicture } = require('../models/User') const supplierController = require("../controllers/supplierController"); const customJwtAuth = require("../customAuthJwt"); @@ -537,7 +538,7 @@ exports.getCurrentSupplier = async (req, reply) => { } }; // Get all users -exports.getSuppliers = async (req, reply) => { +exports.getSuppliers1 = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; const startindex = (page - 1) * limit; @@ -558,6 +559,31 @@ exports.getSuppliers = async (req, reply) => { } }; +exports.getSuppliers = async (req, reply) => { + const limit = parseInt(req.query.limit) || 100; + const page = parseInt(req.query.page) || 1; + const startindex = (page - 1) * limit; + const customerId = req.params.customerId; // Assuming you have already authenticated the user and stored their ID in the request object + try { + const friendRequests = await FriendRequest.find({ customerId }); + const supplierIdsToExclude = friendRequests.map((request) => request.supplierId); + await Supplier.find({ supplierId: { $nin: supplierIdsToExclude } }) + .limit(limit) + .skip(startindex) + .exec() + .then((docs) => { + reply.send({ status_code: 200, data: docs, count: docs.length }); + }) + .catch((err) => { + console.log(err); + reply.send({ error: err }); + }); + } catch (err) { + throw boom.boomify(err); + } +}; + + // Get single user by ID exports.getSingleSupplier = async (req, reply) => { try { diff --git a/src/routes/supplierRoute.js b/src/routes/supplierRoute.js index 9f4f92ed..2a5cd4d6 100644 --- a/src/routes/supplierRoute.js +++ b/src/routes/supplierRoute.js @@ -7,11 +7,21 @@ const validationHandler = require("../handlers/supplierHandler"); module.exports = function (fastify, opts, next) { - fastify.get("/api/suppliers", { + fastify.get("/api/suppliers/:customerId", { schema: { tags: ["Supplier-Data"], description: "This is for Get All Suppliers", summary: "This is for to Get All Suppliers", + params: { + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + security: [ { basicAuth: [], From cb9fb519eaa2fd5ce4e052bb1617d6448c5270a0 Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 27 Mar 2023 04:31:42 -0400 Subject: [PATCH 2/2] getting connected suppliers --- src/handlers/supplierHandler.js | 62 ++++++++++++++++++++++++--------- src/routes/supplierRoute.js | 27 ++++++++++++++ 2 files changed, 72 insertions(+), 17 deletions(-) diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index 8615f467..568d6e1f 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -537,13 +537,37 @@ exports.getCurrentSupplier = async (req, reply) => { throw boom.boomify(err); } }; -// Get all users -exports.getSuppliers1 = async (req, reply) => { +// Get all suppliers +// exports.getSuppliers = async (req, reply) => { +// const limit = parseInt(req.query.limit) || 100; +// const page = parseInt(req.query.page) || 1; +// const startindex = (page - 1) * limit; +// try { +// await Supplier.find() +// .limit(limit) +// .skip(startindex) +// .exec() +// .then((docs) => { +// reply.send({ status_code: 200, data: docs, count: docs.length }); +// }) +// .catch((err) => { +// console.log(err); +// reply.send({ error: err }); +// }); +// } catch (err) { +// throw boom.boomify(err); +// } +// }; + +exports.getSuppliers = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; const startindex = (page - 1) * limit; + const customerId = req.params.customerId; // Assuming you have already authenticated the user and stored their ID in the request object try { - await Supplier.find() + const friendRequests = await FriendRequest.find({ customerId }); + const supplierIdsToExclude = friendRequests.map((request) => request.supplierId); + await Supplier.find({ supplierId: { $nin: supplierIdsToExclude } }) .limit(limit) .skip(startindex) .exec() @@ -559,15 +583,29 @@ exports.getSuppliers1 = async (req, reply) => { } }; -exports.getSuppliers = async (req, reply) => { + +// Get single user by ID +exports.getSingleSupplier = async (req, reply) => { + try { + const supplierId = req.params.supplierId; + const supplier = await Supplier.findOne({ supplierId: supplierId }); + return supplier; + } catch (err) { + throw boom.boomify(err); + } +}; + +exports.getConnectedSuppliers = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; const startindex = (page - 1) * limit; const customerId = req.params.customerId; // Assuming you have already authenticated the user and stored their ID in the request object + console.log(customerId,"HI") try { - const friendRequests = await FriendRequest.find({ customerId }); - const supplierIdsToExclude = friendRequests.map((request) => request.supplierId); - await Supplier.find({ supplierId: { $nin: supplierIdsToExclude } }) + const friendRequests = await FriendRequest.find({ customerId, status: "accepted" }); + console.log(friendRequests,customerId) + const supplierIdsToInclude = friendRequests.map((request) => request.supplierId); + await Supplier.find({ supplierId: { $in: supplierIdsToInclude } }) .limit(limit) .skip(startindex) .exec() @@ -584,13 +622,3 @@ exports.getSuppliers = async (req, reply) => { }; -// Get single user by ID -exports.getSingleSupplier = async (req, reply) => { - try { - const supplierId = req.params.supplierId; - const supplier = await Supplier.findOne({ supplierId: supplierId }); - return supplier; - } catch (err) { - throw boom.boomify(err); - } -}; diff --git a/src/routes/supplierRoute.js b/src/routes/supplierRoute.js index 2a5cd4d6..986fff0c 100644 --- a/src/routes/supplierRoute.js +++ b/src/routes/supplierRoute.js @@ -31,6 +31,33 @@ module.exports = function (fastify, opts, next) { handler: validationHandler.getSuppliers, }); + + fastify.get("/api/connectedSuppliers/:customerId", { + schema: { + tags: ["Supplier-Data"], + description: "This is for Get All connected Suppliers", + summary: "This is for to Get All connected Suppliers", + params: { + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + + security: [ + { + basicAuth: [], + }, + ], + }, + handler: validationHandler.getConnectedSuppliers, + }); + + + fastify.post("/api/supplierlogin", { schema: { description: "This is for Login Supplier",