master^2
Varun 5 months ago
parent e38f47f4e3
commit 6398cb0e75

@ -984,48 +984,7 @@ 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);
}
};
@ -1186,26 +1145,7 @@ exports.blockStaff = async (request, reply) => {
exports.addFavoriteSupplier = async (req, reply) => {
const { customerId, supplierId } = req.body;
try {
const user = await User.findOne({ customerId });
if (!user) {
return reply.status(404).send({ message: "User not found" });
}
if (!user.favorate_suppliers.includes(supplierId)) {
user.favorate_suppliers.push(supplierId);
await user.save();
}
reply.send({ message: "Supplier added to favorites", data: user.favorate_suppliers });
} catch (err) {
reply.status(500).send({ message: err.message });
}
};
exports.getFavoriteSuppliers = async (req, reply) => {
const { customerId } = req.params;
@ -1225,24 +1165,3 @@ exports.getFavoriteSuppliers = async (req, reply) => {
}
};
exports.removeFavoriteSupplier = async (req, reply) => {
const { customerId, supplierId } = req.params;
try {
const user = await User.findOne({ customerId });
if (!user) {
return reply.status(404).send({ message: "User not found" });
}
user.favorate_suppliers = user.favorate_suppliers.filter(
(id) => id.toString() !== supplierId
);
await user.save();
reply.send({ message: "Supplier removed from favorites", data: user.favorate_suppliers });
} catch (err) {
reply.status(500).send({ message: err.message });
}
};

@ -830,24 +830,7 @@ module.exports = function (fastify, opts, next) {
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,
});
@ -1036,25 +1019,7 @@ fastify.route({
fastify.route({
method: "POST",
url: "/api/favorites/add",
schema: {
tags: ["User"],
description: "Add a supplier to the customer's favorites",
summary: "Add a supplier to the customer's favorites",
body: {
type: "object",
required: ["customerId", "supplierId"],
properties: {
customerId: { type: "string", description: "Customer ID" },
supplierId: { type: "string", description: "Supplier ID to be added" }
}
},
security: [{ basicAuth: [] }]
},
handler: userController.addFavoriteSupplier
});
fastify.route({
method: "GET",
@ -1075,25 +1040,7 @@ fastify.route({
handler: userController.getFavoriteSuppliers
});
fastify.route({
method: "DELETE",
url: "/api/favorites/:customerId/:supplierId",
schema: {
tags: ["User"],
description: "Remove a supplier from the customer's favorites",
summary: "Remove a supplier from the customer's favorites",
params: {
type: "object",
required: ["customerId", "supplierId"],
properties: {
customerId: { type: "string", description: "Customer ID" },
supplierId: { type: "string", description: "Supplier ID to be removed" }
}
},
security: [{ basicAuth: [] }]
},
handler: userController.removeFavoriteSupplier
});
next();

Loading…
Cancel
Save