added get for favorate suppliers

master^2
Varun 5 months ago
parent 46d0a258a1
commit e38f47f4e3

@ -984,6 +984,50 @@ exports.deleteFavoriteSupplier = async (req, reply) => {
} }
}; };
exports.getFavoriteSuppliers = async (req, reply) => {
try {
const { customerId } = req.params;
// Get user's favorite suppliers list
const user = await User.findOne({ customerId }, 'favorate_suppliers');
if (!user) {
return reply.code(404).send({ status_code: 404, message: "User not found" });
}
const favorateSupplierIds = user.favorate_suppliers || [];
if (favorateSupplierIds.length === 0) {
return reply.send({ status_code: 200, data: [], count: 0 });
}
// Fetch supplier details
const suppliers = await Supplier.find({
supplierId: { $in: favorateSupplierIds }
});
// Fetch profile pictures
const profilePictures = await profilePictureSupplier.find({
supplierId: { $in: favorateSupplierIds }
});
// Combine data
const data = suppliers.map((supplier) => {
const profilePicture = profilePictures.find(
(pic) => pic.supplierId === supplier.supplierId
);
return {
...supplier.toObject(),
picture: profilePicture ? profilePicture.picture : null,
favorate: true,
};
});
reply.send({ status_code: 200, data, count: data.length });
} catch (err) {
throw boom.boomify(err);
}
};
exports.updateTeamMember = async (req, reply) => { exports.updateTeamMember = async (req, reply) => {

@ -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,
}; };
}); });

@ -830,6 +830,26 @@ module.exports = function (fastify, opts, next) {
handler: userController.deleteFavoriteSupplier, handler: userController.deleteFavoriteSupplier,
}); });
fastify.route({
method: "GET",
url: "/api/getfavoratesuppliers/:customerId",
schema: {
tags: ["User"],
summary: "Get favorite suppliers of a user",
description: "Fetch all favorite suppliers based on customerId",
params: {
type: "object",
required: ["customerId"],
properties: {
customerId: { type: "string" },
},
},
security: [{ basicAuth: [] }],
},
handler: userController.getFavoriteSuppliers,
});
fastify.route({ fastify.route({
method: "PUT", method: "PUT",

Loading…
Cancel
Save