diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 5317a902..5e6a8c10 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -890,6 +890,102 @@ 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 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"; + + // 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); + throw boom.boomify(err); + } +}; + +exports.editFavoriteSupplier = async (req, reply) => { + try { + const { customerId } = req.params; + const { oldSupplierId, newSupplierId } = req.query; + + const user = await User.findOne({ customerId }); + if (!user) return reply.code(404).send({ status_code: 404, message: "User not found" }); + + const index = user.favorate_suppliers.indexOf(oldSupplierId); + if (index === -1) + return reply.code(400).send({ status_code: 400, message: "Old supplier not found in favorites" }); + + user.favorate_suppliers[index] = newSupplierId; + await user.save(); + + reply.send({ + status_code: 200, + message: "Favorite supplier updated", + data: user.favorate_suppliers, + }); + } catch (err) { + throw boom.boomify(err); + } +}; + +exports.deleteFavoriteSupplier = async (req, reply) => { + try { + const { customerId } = req.params; + const { supplierId } = req.query; + + const user = await User.findOne({ customerId }); + if (!user) return reply.code(404).send({ status_code: 404, message: "User not found" }); + + const initialLength = user.favorate_suppliers.length; + user.favorate_suppliers = user.favorate_suppliers.filter(id => id !== supplierId); + + if (user.favorate_suppliers.length === initialLength) + return reply.code(400).send({ status_code: 400, message: "Supplier not found in favorites" }); + + await user.save(); + + reply.send({ + status_code: 200, + message: "Favorite supplier removed", + data: user.favorate_suppliers, + }); + } catch (err) { + throw boom.boomify(err); + } +}; + + + exports.updateTeamMember = async (req, reply) => { try { var customerId = req.params.customerId; diff --git a/src/models/User.js b/src/models/User.js index 94f9c376..82a854ab 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -62,6 +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:String,default: null }], staff: { diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index 29dbba25..5e2ee285 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -656,6 +656,8 @@ module.exports = function (fastify, opts, next) { }); + + fastify.route({ method: "POST", url: "/api/sendSms", @@ -752,6 +754,82 @@ module.exports = function (fastify, opts, next) { preHandler: fastify.auth([fastify.authenticate]), handler: userController.deleteTeamMember, // Ensure this line points to the handler }); + + fastify.route({ + method: "PUT", + url: "/api/addingfavoratesupplier/:customerId", + schema: { + tags: ["User"], + summary: "This is for adding favorate supplier", + description: "This is for adding favorate supplier", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + querystring: { + supplierId: { type: "string" }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: fastify.auth([fastify.authenticate]), + handler: userController.addingfavoratesupplier, // Ensure this line points to the handler + }); + + fastify.route({ + method: "PUT", + url: "/api/editfavoratesupplier/:customerId", + schema: { + tags: ["User"], + summary: "Edit a favorite supplier", + description: "Replace an existing supplierId with a new one", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { type: "string" }, + }, + }, + querystring: { + oldSupplierId: { type: "string" }, + newSupplierId: { type: "string" }, + }, + security: [{ basicAuth: [] }], + }, + handler: userController.editFavoriteSupplier, + }); + + fastify.route({ + method: "DELETE", + url: "/api/deletefavoratesupplier/:customerId", + schema: { + tags: ["User"], + summary: "Delete a favorite supplier", + description: "Remove a supplierId from favorite suppliers", + params: { + required: ["customerId"], + type: "object", + properties: { + customerId: { type: "string" }, + }, + }, + querystring: { + supplierId: { type: "string" }, + }, + security: [{ basicAuth: [] }], + }, + handler: userController.deleteFavoriteSupplier, + }); + fastify.route({ method: "PUT",