diff --git a/src/controllers/userController.js b/src/controllers/userController.js index bbbdbe46..07109959 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -340,28 +340,21 @@ exports.uploadProfilePicture = async (req, reply) => { try { const customerId = req.params.customerId; const picture = new Buffer.from(req.body.picture, 'base64'); + + let profilePicture = await ProfilePicture.findOne({ customerId }); - const profilePicture = new ProfilePicture({ customerId, picture }); - await profilePicture.save(); - - return { message: 'Profile picture uploaded successfully' }; - } catch (error) { - reply.status(500).send({ error: error.message }); - } -}; - -exports.updateProfilePicture = async (req, reply) => { - try { - const customerId = req.params.customerId; - const picture = new Buffer.from(req.body.picture, 'base64'); - - const profilePicture = await ProfilePicture.findOneAndUpdate({ customerId }, { picture }, { new: true }); if (!profilePicture) { - reply.status(404).send({ error: 'Profile picture not found' }); - return; + profilePicture = new ProfilePicture({ + customerId, + picture, + }); + } else { + profilePicture. picture = picture; } - return { message: 'Profile picture updated successfully' }; + await profilePicture.save(); + + reply.send({ message: 'Profile picture uploaded successfully' }); } catch (error) { reply.status(500).send({ error: error.message }); } @@ -370,3 +363,4 @@ exports.updateProfilePicture = async (req, reply) => { + diff --git a/src/models/User.js b/src/models/User.js index 5035efe0..b59a853e 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -113,7 +113,7 @@ const userSchema = new mongoose.Schema( const profilePictureSchema = new Schema({ customerId: { - type: String, + type: String,unique: true, required: true }, picture: { diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index 71e0de81..11998138 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -412,40 +412,7 @@ module.exports = function (fastify, opts, next) { }, handler: userController.uploadProfilePicture, }); - fastify.route({ - method: "POST", - url: "/api/users/update-profile-picture/:customerId", - schema: { - tags: ["User"], - description: "This is for updating profile picture.", - summary: "This is for updating profile picture.", - params: { - type: "object", - properties: { - customerId: { - type: "string", - description: "customerId", - }, - }, - }, - body: { - type: "object", - required: ["picture"], - properties: { - picture: { - type: 'string' - } - }, - }, - security: [ - { - basicAuth: [], - }, - ], - }, - handler: userController.updateProfilePicture, - }); - +