diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 48a69c0a..9136b376 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -899,16 +899,26 @@ exports.addingfavoratesupplier = async (req, reply) => { return reply.code(400).send({ status_code: 400, message: "supplierId is required" }); } + // Find supplier by supplierId string to get the _id + const supplier = await Supplier.findOne({ supplierId }); + if (!supplier) { + return reply.code(404).send({ status_code: 404, message: "Supplier not found" }); + } + // Find user const user = await User.findOne({ customerId }); - if (!user) { return reply.code(404).send({ status_code: 404, message: "User not found" }); } - // Add supplierId to favorate_suppliers if not already there - if (!user.favorate_suppliers.includes(supplierId)) { - user.favorate_suppliers.push(supplierId); + // Add supplier._id to favorate_suppliers if not already present + const supplierObjectId = supplier._id.toString(); + const isAlreadyFavorite = user.favorate_suppliers.some( + (id) => id.toString() === supplierObjectId + ); + + if (!isAlreadyFavorite) { + user.favorate_suppliers.push(supplier._id); await user.save(); } @@ -929,10 +939,11 @@ exports.addingfavoratesupplier = async (req, reply) => { }); } catch (err) { console.error(err); - throw boom.boomify(err); + reply.status(500).send({ status_code: 500, message: err.message }); } }; + exports.editFavoriteSupplier = async (req, reply) => { try { const { customerId } = req.params;