From b06f48fca9feb8a8bebe1c6a8dd5cccc09272451 Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 13 Feb 2023 04:22:57 -0500 Subject: [PATCH 1/3] added pprofile picture in user and writeen update function --- src/controllers/userController.js | 37 +++++++++++++++- src/models/User.js | 15 ++++++- src/routes/usersRoute.js | 71 +++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 2 deletions(-) diff --git a/src/controllers/userController.js b/src/controllers/userController.js index 97a46a80..bbbdbe46 100644 --- a/src/controllers/userController.js +++ b/src/controllers/userController.js @@ -9,7 +9,7 @@ const libphonenumberjs = require("libphonenumber-js"); const boom = require("boom"); // Get Data Models -const { User,Counter, generateBookingId,resetCounter,generateCustomerId } = require('../models/User') +const { User,Counter, generateBookingId,resetCounter,generateCustomerId,ProfilePicture} = require('../models/User') //const User = require("../models/User"); const customJwtAuth = require("../customAuthJwt"); @@ -335,3 +335,38 @@ exports.delPhoneUser = async (req, reply) => { throw boom.boomify(err); } }; + +exports.uploadProfilePicture = async (req, reply) => { + try { + const customerId = req.params.customerId; + const picture = new Buffer.from(req.body.picture, 'base64'); + + 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; + } + + return { message: 'Profile picture updated successfully' }; + } catch (error) { + reply.status(500).send({ error: error.message }); + } +}; + + + + diff --git a/src/models/User.js b/src/models/User.js index 3f69bbbc..5035efe0 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -110,6 +110,19 @@ const userSchema = new mongoose.Schema( }, { versionKey: false } ); + +const profilePictureSchema = new Schema({ + customerId: { + type: String, + required: true + }, + picture: { + type: Buffer, + required: true + } +}); + +const ProfilePicture = mongoose.model('ProfilePicture', profilePictureSchema); const Counter = mongoose.model('Counter', CounterSchema); const User = mongoose.model("User", userSchema); @@ -120,4 +133,4 @@ const User = mongoose.model("User", userSchema); //module.exports = mongoose.model("User", userSchema); -module.exports = { User,Counter, generateCustomerId,generateBookingId ,resetCounter}; +module.exports = { User,Counter, generateCustomerId,generateBookingId ,resetCounter,ProfilePicture}; diff --git a/src/routes/usersRoute.js b/src/routes/usersRoute.js index d3b0ce94..71e0de81 100644 --- a/src/routes/usersRoute.js +++ b/src/routes/usersRoute.js @@ -379,5 +379,76 @@ module.exports = function (fastify, opts, next) { // Login for a user is in the main index.js file. // fastify-jwt used to create the token was throwing exceptions and requierd // it be called before the route is loaded. + fastify.route({ + method: "POST", + url: "/api/users/profile-picture/:customerId", + schema: { + tags: ["User"], + description: "This is for uploading profile picture.", + summary: "This is for uploading profile picture.", + params: { + type: "object", + properties: { + customerId: { + type: "string", + description: "customerId", + }, + }, + }, + body: { + type: "object", + required: ["picture"], + properties: { + picture: { + type: 'string' + } + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + 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, + }); + + + + + next(); }; From 95e7309696259bdeb52f305d5c2498690ca5567b Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 13 Feb 2023 04:23:04 -0500 Subject: [PATCH 2/3] added pprofile picture in user and writeen update function --- src/controllers/userController.js | 30 +++++++++++--------------- src/models/User.js | 2 +- src/routes/usersRoute.js | 35 +------------------------------ 3 files changed, 14 insertions(+), 53 deletions(-) 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, - }); - + From 45cfc5917eaae79431cb17b42275f6d754cf0df0 Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 13 Feb 2023 05:08:24 -0500 Subject: [PATCH 3/3] created a field named tanklevel in tanks.js --- src/models/tanks.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models/tanks.js b/src/models/tanks.js index 11eacc37..48e20d3c 100644 --- a/src/models/tanks.js +++ b/src/models/tanks.js @@ -14,7 +14,7 @@ const tanksSchema = new mongoose.Schema({ blockName: { type: String, default: null }, capacity: { type: String, default: null }, typeOfWater: { type: String, default: null }, - + water_level: { type: String, default: null }, tankLocation: { type: String, default: null }, connections: { source: { type: String},