|
|
@ -1021,17 +1021,22 @@ exports.getConnectedSuppliers = async (req, reply) => {
|
|
|
|
const limit = parseInt(req.query.limit) || 100;
|
|
|
|
const limit = parseInt(req.query.limit) || 100;
|
|
|
|
const page = parseInt(req.query.page) || 1;
|
|
|
|
const page = parseInt(req.query.page) || 1;
|
|
|
|
const startindex = (page - 1) * limit;
|
|
|
|
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
|
|
|
|
const customerId = req.params.customerId;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// Get user's favorite suppliers
|
|
|
|
|
|
|
|
const user = await User.findOne({ customerId }, 'favorate_suppliers');
|
|
|
|
|
|
|
|
const favorateSuppliers = user?.favorate_suppliers || [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get accepted friend requests
|
|
|
|
const friendRequests = await FriendRequest.find({
|
|
|
|
const friendRequests = await FriendRequest.find({
|
|
|
|
customerId,
|
|
|
|
customerId,
|
|
|
|
status: "accepted",
|
|
|
|
status: "accepted",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const supplierIdsToInclude = friendRequests.map(
|
|
|
|
const supplierIdsToInclude = friendRequests.map(req => req.supplierId);
|
|
|
|
(request) => request.supplierId
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get suppliers
|
|
|
|
const suppliers = await Supplier.find({
|
|
|
|
const suppliers = await Supplier.find({
|
|
|
|
supplierId: { $in: supplierIdsToInclude }
|
|
|
|
supplierId: { $in: supplierIdsToInclude }
|
|
|
|
})
|
|
|
|
})
|
|
|
@ -1039,19 +1044,24 @@ exports.getConnectedSuppliers = async (req, reply) => {
|
|
|
|
.skip(startindex)
|
|
|
|
.skip(startindex)
|
|
|
|
.exec();
|
|
|
|
.exec();
|
|
|
|
|
|
|
|
|
|
|
|
const supplierIds = suppliers.map((supplier) => supplier.supplierId);
|
|
|
|
const supplierIds = suppliers.map(s => s.supplierId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get profile pictures
|
|
|
|
const profilePictures = await profilePictureSupplier.find({
|
|
|
|
const profilePictures = await profilePictureSupplier.find({
|
|
|
|
supplierId: { $in: supplierIds }
|
|
|
|
supplierId: { $in: supplierIds }
|
|
|
|
}).exec();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Construct final response
|
|
|
|
const data = suppliers.map((supplier) => {
|
|
|
|
const data = suppliers.map((supplier) => {
|
|
|
|
const profilePicture = profilePictures.find(
|
|
|
|
const profilePicture = profilePictures.find(
|
|
|
|
(picture) => picture.supplierId === supplier.supplierId
|
|
|
|
(pic) => pic.supplierId === supplier.supplierId
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
const isFavorate = favorateSuppliers.includes(supplier.supplierId);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
return {
|
|
|
|
...supplier.toObject(),
|
|
|
|
...supplier.toObject(),
|
|
|
|
picture: profilePicture ? profilePicture.picture : null,
|
|
|
|
picture: profilePicture ? profilePicture.picture : null,
|
|
|
|
|
|
|
|
favorate: isFavorate,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|