diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index 47396428..d9bb1cbf 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -1024,16 +1024,16 @@ exports.getPendingSuppliers = async (req, reply) => { .skip(startindex) .exec() .then((docs) => { - // const supplierDataWithTimestamp = docs.map((doc, index) => ({ - // data: doc, - // //timestamp: timestamps[index], - // })); - // reply.send({ - // status_code: 200, - // data: supplierDataWithTimestamp, - // count: docs.length, - // }); - reply.send({ status_code: 200, data: docs, count: docs.length }); + 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); @@ -1076,39 +1076,79 @@ exports.getRejectSuppliers = async (req, reply) => { } }; +// exports.getPendingCustomers = async (req, reply) => { +// const limit = parseInt(req.query.limit) || 100; +// const page = parseInt(req.query.page) || 1; +// const startindex = (page - 1) * limit; +// const supplierId = req.params.supplierId; // Assuming you have already authenticated the user and stored their ID in the request object + +// try { +// const friendRequests = await FriendRequest.find({ +// supplierId, +// status: ["pending"], +// }); +// console.log(friendRequests, supplierId, "su...."); +// const supplierIdsToInclude = friendRequests.map( +// (request) => request.customerId +// ); +// console.log(supplierIdsToInclude, "supplierIdsToInclude.."); +// const timestamps = friendRequests.map((request) => request.timestamp); +// console.log(timestamps, "timestamps"); +// await User.find({ customerId: { $in: supplierIdsToInclude } }) +// .limit(limit) +// .skip(startindex) +// .exec() +// .then((docs) => { +// // const customerDataWithTimestamp = docs.map((doc, index) => ({ +// // data: doc, +// // //timestamp: timestamps[index], +// // })); +// reply.send({ status_code: 200, data: docs, count: docs.length }); +// // reply.send({ +// // status_code: 200, +// // data: customerDataWithTimestamp, +// // count: docs.length, +// // }); +// }) +// .catch((err) => { +// console.log(err); +// reply.send({ error: err }); +// }); +// } catch (err) { +// throw boom.boomify(err); +// } +// }; + exports.getPendingCustomers = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; const startindex = (page - 1) * limit; - const supplierId = req.params.supplierId; // Assuming you have already authenticated the user and stored their ID in the request object + const supplierId = req.params.supplierId; try { const friendRequests = await FriendRequest.find({ supplierId, status: ["pending"], }); - console.log(friendRequests, supplierId, "su...."); const supplierIdsToInclude = friendRequests.map( (request) => request.customerId ); - console.log(supplierIdsToInclude, "supplierIdsToInclude.."); const timestamps = friendRequests.map((request) => request.timestamp); - console.log(timestamps, "timestamps"); + await User.find({ customerId: { $in: supplierIdsToInclude } }) .limit(limit) .skip(startindex) .exec() .then((docs) => { - // const customerDataWithTimestamp = docs.map((doc, index) => ({ - // data: doc, - // //timestamp: timestamps[index], - // })); - reply.send({ status_code: 200, data: docs, count: docs.length }); - // reply.send({ - // status_code: 200, - // data: customerDataWithTimestamp, - // count: docs.length, - // }); + const customerDataWithTimestamp = docs.map((doc, index) => ({ + ...doc._doc, + timestamp: timestamps[index], + })); + reply.send({ + status_code: 200, + data: customerDataWithTimestamp, + count: docs.length, + }); }) .catch((err) => { console.log(err);