From 46d0a258a14f82117ddea47c41cda47f4b1e317b Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 21 May 2025 11:40:40 +0530 Subject: [PATCH 1/9] added favorate supplier ,edit and delete --- src/controllers/userController.js | 96 +++++++++++++++++++++++++++++++ src/models/User.js | 1 + src/routes/usersRoute.js | 78 +++++++++++++++++++++++++ 3 files changed, 175 insertions(+) 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", From e38f47f4e3568054f8caaf216b9df91d1bbc0387 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 21 May 2025 12:05:53 +0530 Subject: [PATCH 2/9] added get for favorate suppliers --- src/controllers/userController.js | 44 +++++++++++++++++++++++++++++++ src/handlers/supplierHandler.js | 24 ++++++++++++----- src/routes/usersRoute.js | 20 ++++++++++++++ 3 files changed, 81 insertions(+), 7 deletions(-) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 5e6a8c10..5e6d564d 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -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) => { diff --git a/src/handlers/supplierHandler.js b/src/handlers/supplierHandler.js index 7d3cf100..74097f72 100644 --- a/src/handlers/supplierHandler.js +++ b/src/handlers/supplierHandler.js @@ -1021,17 +1021,22 @@ exports.getConnectedSuppliers = async (req, reply) => { const limit = parseInt(req.query.limit) || 100; const page = parseInt(req.query.page) || 1; 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 { + // 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({ customerId, status: "accepted", }); - const supplierIdsToInclude = friendRequests.map( - (request) => request.supplierId - ); + const supplierIdsToInclude = friendRequests.map(req => req.supplierId); + // Get suppliers const suppliers = await Supplier.find({ supplierId: { $in: supplierIdsToInclude } }) @@ -1039,19 +1044,24 @@ exports.getConnectedSuppliers = async (req, reply) => { .skip(startindex) .exec(); - const supplierIds = suppliers.map((supplier) => supplier.supplierId); + const supplierIds = suppliers.map(s => s.supplierId); + // Get profile pictures const profilePictures = await profilePictureSupplier.find({ supplierId: { $in: supplierIds } - }).exec(); + }); + // Construct final response const data = suppliers.map((supplier) => { const profilePicture = profilePictures.find( - (picture) => picture.supplierId === supplier.supplierId + (pic) => pic.supplierId === supplier.supplierId ); + const isFavorate = favorateSuppliers.includes(supplier.supplierId); + return { ...supplier.toObject(), picture: profilePicture ? profilePicture.picture : null, + favorate: isFavorate, }; }); diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index 5e2ee285..9db838ef 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -829,6 +829,26 @@ 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, + }); + fastify.route({ From 6398cb0e75b87c92cf27b899273237c83c006d09 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 21 May 2025 13:19:43 +0530 Subject: [PATCH 3/9] changes --- src/controllers/userController.js | 81 ------------------------------- src/routes/usersRoute.js | 59 ++-------------------- 2 files changed, 3 insertions(+), 137 deletions(-) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 5e6d564d..a40a224e 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -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 }); - } -}; diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index 9db838ef..91cf7594 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -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(); From a08759903c0d0cdd794a6ff1791c3281aca1c442 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 21 May 2025 13:22:19 +0530 Subject: [PATCH 4/9] changes --- src/models/User.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models/User.js b/src/models/User.js index 82a854ab..8bd09d8f 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -56,7 +56,7 @@ const userSchema = new mongoose.Schema( inchargeName: String, phoneVerified: { type: Boolean, default: false }, phoneVerificationCode: { type: Number, default: 11111 }, - favorate_suppliers: [{ type: mongoose.Schema.Types.ObjectId, ref: "Supplier" }], + passwordResetCode: { type: Number, default: code }, oneTimePasswordSetFlag: { type: Boolean, default: false }, emails: [{ email: String, verified: { type: Boolean, default: false } }], From 3ccc9686ac0ac796eff5a8c3f21e5285ebdb11f1 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 21 May 2025 13:24:17 +0530 Subject: [PATCH 5/9] changes --- src/controllers/userController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index a40a224e..82059d51 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -1152,7 +1152,7 @@ exports.getFavoriteSuppliers = async (req, reply) => { try { const user = await User.findOne({ customerId }) - .populate("favorate_suppliers") // If you want to get full supplier details + .populate("favorate_suppliers") // This will now work properly .exec(); if (!user) { From 0c9fa6430e7ef488d0ec2cebe9a6274056819bc0 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 21 May 2025 13:27:22 +0530 Subject: [PATCH 6/9] changes --- src/controllers/userController.js | 11 +++++++---- src/models/User.js | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 82059d51..48a69c0a 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -1151,17 +1151,20 @@ exports.getFavoriteSuppliers = async (req, reply) => { const { customerId } = req.params; try { - const user = await User.findOne({ customerId }) - .populate("favorate_suppliers") // This will now work properly - .exec(); + const user = await User.findOne({ customerId }).exec(); if (!user) { return reply.status(404).send({ message: "User not found" }); } - reply.send({ data: user.favorate_suppliers }); + const suppliers = await Supplier.find({ + supplierId: { $in: user.favorate_suppliers } + }); + + reply.send({ data: suppliers }); } catch (err) { reply.status(500).send({ message: err.message }); } }; + diff --git a/src/models/User.js b/src/models/User.js index 8bd09d8f..92301f63 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -62,7 +62,8 @@ 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 }], + favorate_suppliers: [{ type: mongoose.Schema.Types.ObjectId, ref: "Supplier", default: null }], + staff: { From 40a3b205a333eee232d2e9553539eae9efce5195 Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 21 May 2025 13:30:13 +0530 Subject: [PATCH 7/9] Changes --- src/controllers/userController.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) 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; From 518c0b5c2a2ee4ba4241509dc3d5b5f4b7f2ae9d Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 21 May 2025 13:34:30 +0530 Subject: [PATCH 8/9] changes --- src/controllers/userController.js | 91 ++++++++++++++----------------- src/models/User.js | 2 +- 2 files changed, 41 insertions(+), 52 deletions(-) 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: { From 07f2011e252e3968b6b908938d86b1f39b28f95c Mon Sep 17 00:00:00 2001 From: Varun Date: Wed, 21 May 2025 13:37:01 +0530 Subject: [PATCH 9/9] changes --- src/controllers/userController.js | 21 ++++++++++++++++----- src/routes/usersRoute.js | 1 + 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index bff69ccb..3f54f255 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -1151,20 +1151,31 @@ exports.getFavoriteSuppliers = async (req, reply) => { const { customerId } = req.params; try { - const user = await User.findOne({ customerId }).exec(); + // Find the user by customerId + const user = await User.findOne({ customerId }); if (!user) { - return reply.status(404).send({ message: "User not found" }); + return reply.status(404).send({ status_code: 404, message: "User not found" }); } + const supplierIds = user.favorate_suppliers || []; + + // Get full supplier details for those supplierIds const suppliers = await Supplier.find({ - supplierId: { $in: user.favorate_suppliers } + supplierId: { $in: supplierIds } + }).exec(); + + reply.send({ + status_code: 200, + data: suppliers, + count: suppliers.length }); - reply.send({ data: suppliers }); } catch (err) { - reply.status(500).send({ message: err.message }); + console.error("Error fetching favorite suppliers:", err); + reply.status(500).send({ status_code: 500, message: "Internal server error" }); } }; + diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index 91cf7594..0c09ded8 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -1043,5 +1043,6 @@ fastify.route({ + next(); };