From 834e68e2fb96eb8e3a91ab6251374641dd5ed799 Mon Sep 17 00:00:00 2001 From: Bhaskara Kishore Date: Mon, 8 May 2023 14:31:14 +0530 Subject: [PATCH] add filed profilepicture in get all suppliers --- src/handlers/supplierHandler.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index b8824f61..90addfb3 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -937,6 +937,8 @@ exports.getCurrentSupplier = async (req, reply) => { // } // }; + + exports.getSuppliers = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; @@ -947,17 +949,27 @@ exports.getSuppliers = async (req, reply) => { const supplierIdsToExclude = friendRequests.map( (request) => request.supplierId ); - await Supplier.find({ supplierId: { $nin: supplierIdsToExclude } }) + const suppliers = 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 }); - }); + .exec(); + + const supplierIds = suppliers.map((supplier) => supplier.supplierId); + const profilePictures = await profilePictureSupplier.find({ + supplierId: { $in: supplierIds }, + }).exec(); + + const data = suppliers.map((supplier) => { + const profilePicture = profilePictures.find( + (picture) => picture.supplierId === supplier.supplierId + ); + return { + ...supplier.toObject(), + picture: profilePicture ? profilePicture.picture : null, + }; + }); + + reply.send({ status_code: 200, data, count: data.length }); } catch (err) { throw boom.boomify(err); }