diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index da48a7a9..7d3cf100 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -987,6 +987,36 @@ exports.getSingleSupplier = async (req, reply) => { } }; +// 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 +// try { +// 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() +// .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.getConnectedSuppliers = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; @@ -997,26 +1027,85 @@ exports.getConnectedSuppliers = async (req, reply) => { customerId, status: "accepted", }); - // console.log(friendRequests,customerId) + const supplierIdsToInclude = friendRequests.map( (request) => request.supplierId ); - await Supplier.find({ supplierId: { $in: supplierIdsToInclude } }) + + const suppliers = await Supplier.find({ + supplierId: { $in: supplierIdsToInclude } + }) .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); } }; + +// exports.getPendingSuppliers = 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, +// status: ["pending"], +// }); +// // console.log(friendRequests,customerId) +// const supplierIdsToInclude = friendRequests.map( +// (request) => request.supplierId +// ); +// console.log(supplierIdsToInclude, "SUPLIERINCLUDE"); + +// const timestamps = friendRequests.map(request => request.timestamp); +// console.log(timestamps, "timestamps"); +// await Supplier.find({ supplierId: { $in: supplierIdsToInclude } }) +// .limit(limit) +// .skip(startindex) +// .exec() +// .then((docs) => { +// const customerDataWithTimestamp = docs.map((doc, index) => ({ +// ...doc._doc, +// timestamp: timestamps[index], +// })); +// reply.send({ +// status_code: 200, +// data: customerDataWithTimestamp, +// count: docs.length, +// }); +// //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.getPendingSuppliers = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; @@ -1028,39 +1117,76 @@ exports.getPendingSuppliers = async (req, reply) => { customerId, status: ["pending"], }); - // console.log(friendRequests,customerId) + const supplierIdsToInclude = friendRequests.map( (request) => request.supplierId ); - console.log(supplierIdsToInclude, "SUPLIERINCLUDE"); - - const timestamps = friendRequests.map(request => request.timestamp); - console.log(timestamps, "timestamps"); - await Supplier.find({ supplierId: { $in: supplierIdsToInclude } }) + + const timestamps = friendRequests.map((request) => request.timestamp); + + const suppliers = await Supplier.find({ + supplierId: { $in: supplierIdsToInclude }, + }) .limit(limit) .skip(startindex) - .exec() - .then((docs) => { - const customerDataWithTimestamp = docs.map((doc, index) => ({ - ...doc._doc, - timestamp: timestamps[index], - })); - reply.send({ - status_code: 200, - data: customerDataWithTimestamp, - count: docs.length, - }); - //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, index) => { + const profilePicture = profilePictures.find( + (picture) => picture.supplierId === supplier.supplierId + ); + return { + ...supplier.toObject(), + timestamp: timestamps[index], + picture: profilePicture ? profilePicture.picture : null, + }; + }); + + reply.send({ status_code: 200, data, count: data.length }); } catch (err) { throw boom.boomify(err); } }; + +// exports.getRejectSuppliers = 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, +// status: ["rejected"], +// }); +// // console.log(friendRequests,customerId) +// const supplierIdsToInclude = friendRequests.map( +// (request) => request.supplierId +// ); +// // console.log(supplierIdsToInclude) +// await Supplier.find({ supplierId: { $in: supplierIdsToInclude } }) +// .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.getRejectSuppliers = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; @@ -1072,22 +1198,35 @@ exports.getRejectSuppliers = async (req, reply) => { customerId, status: ["rejected"], }); - // console.log(friendRequests,customerId) + const supplierIdsToInclude = friendRequests.map( (request) => request.supplierId ); - // console.log(supplierIdsToInclude) - await Supplier.find({ supplierId: { $in: supplierIdsToInclude } }) + + const suppliers = await Supplier.find({ + supplierId: { $in: supplierIdsToInclude }, + }) .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); } @@ -1095,6 +1234,7 @@ exports.getRejectSuppliers = async (req, reply) => { + exports.getPendingCustomers = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1;