diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 9136b376..bff69ccb 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -731,6 +731,46 @@ exports.changePassword = async (req, reply) => { } }; +exports.addingfavoratesupplier = async (req, reply) => { + try { + const { customerId } = req.params; + const { supplierId } = req.query; + + if (!supplierId) { + return reply.code(400).send({ status_code: 400, message: "supplierId is required" }); + } + + // 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); + await user.save(); + } + + // Fetch FriendRequest status + const friendRequest = await FriendRequest.findOne({ customerId, supplierId }); + const status = friendRequest ? friendRequest.status : "not_requested"; + + reply.send({ + status_code: 200, + message: "Supplier added to favorites successfully", + data: { + customerId, + supplierId, + favorate_suppliers: user.favorate_suppliers, + status, + }, + }); + } catch (err) { + console.error(err); + reply.status(500).send({ status_code: 500, message: err.message }); + } +}; exports.forgotPasswordSupplier = async (req, reply) => { @@ -890,58 +930,7 @@ exports.deleteTeamMember = async (req, reply) => { } }; -exports.addingfavoratesupplier = async (req, reply) => { - try { - const { customerId } = req.params; - const { supplierId } = req.query; - - if (!supplierId) { - 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 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(); - } - // Fetch FriendRequest status - const friendRequest = await FriendRequest.findOne({ customerId, supplierId }); - const status = friendRequest ? friendRequest.status : "not_requested"; - - // Send response - reply.send({ - status_code: 200, - message: "Supplier added to favorites successfully", - data: { - customerId, - supplierId, - favorate_suppliers: user.favorate_suppliers, - status, - }, - }); - } catch (err) { - console.error(err); - reply.status(500).send({ status_code: 500, message: err.message }); - } -}; exports.editFavoriteSupplier = async (req, reply) => { diff --git a/src/models/User.js b/src/models/User.js index 92301f63..f5032205 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -62,7 +62,7 @@ const userSchema = new mongoose.Schema( emails: [{ email: String, verified: { type: Boolean, default: false } }], services: { password: { bcrypt: String } }, survey_status:{ type:String,default: "pending" }, - favorate_suppliers: [{ type: mongoose.Schema.Types.ObjectId, ref: "Supplier", default: null }], + favorate_suppliers: [{ type: String, default: null }], staff: {