Skip to content

Commit

Permalink
feat(userProfile): Add profile page settings
Browse files Browse the repository at this point in the history
- Add profile settings route
- User can update his/her settings
[Start #169258635]
  • Loading branch information
victkarangwa committed Nov 25, 2019
1 parent fbb10cb commit 137c104
Show file tree
Hide file tree
Showing 25 changed files with 855 additions and 34 deletions.
28 changes: 17 additions & 11 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
FACEBOOK_APP_ID=''
FACEBOOK_APP_SECRET=''
GOOGLE_APP_ID=''
GOOGLE_APP_SECRET=''
DATABASE_DEV_URL=postgres://dbuser:.host:port/db_name
DATABASE_TEST_URL=postgres://dbuser:.host:port/db_name
JWT_KEY=your_jwt_secret_key
EMAIL_ADDRESS=email_adress_of_the_sender
EMAIL_PASSWORD=email_password
SUPER_ADMIN_EMAIL=super_admin_email_address
SUPER_ADMIN_REAL_PASSWORD=super_admin_password
DATABASE_DEV_URL=postgres://DBuser:password@host:port/Dev_DBname
DATABASE_TEST_URL=postgres://DBuser:password@host:port/Test_DBname
JWT_KEY=JWT_SECRETE_KEY
MAIL_SERVICE=MAIL_SERVISE
MAIL_HOST=SMTP_EMAIL
MAIL_PORT=MAIL_PORT
EMAIL_ADDRESS=MAIL_ADDRESS
EMAIL_PASSWORD=EMAIL_PASSWORD
FACEBOOK_APP_ID=YOUR_FB_APP_ID
FACEBOOK_APP_SECRET=YOUR_FB_APP_SECRET
GOOGLE_APP_ID=YOUR_GOOGLE_APP_ID
GOOGLE_APP_SECRET=YOUR_GOOGLE_APP_SECRET
MANAGER_EMAIL=YOUR_MANAGER_EMAIL
MANAGER=MANAGER_NAME
SUPER_ADMIN_EMAIL=SUPER_EMAIL
SUPER_ADMIN_REAL_PASSWORD=PASSWORD

3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"es6": true,
"mocha": true
},
"parserOptions": {
"sourceType": "module"
},
"rules": {
"one-var": 0,
"one-var-declaration-per-line": 0,
Expand Down
114 changes: 112 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 109 additions & 1 deletion src/controllers/UserController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dotenv from 'dotenv';
import HttpStatus from 'http-status-codes';
import { users } from '../database/models';
import { users, userProfile } from '../database/models';
import HashPassword from '../helpers/HashPassword';
import Customize from '../helpers/Customize';
import emailHelper from '../helpers/EmailHelper';
Expand Down Expand Up @@ -43,6 +43,10 @@ class UserController {
isVerified: false,
signupType: 'Barefoot'
});
const { id } = newUser.dataValues;
await userProfile.create({
userId: id,
});
const {
password,
...data
Expand Down Expand Up @@ -109,6 +113,7 @@ class UserController {
* @param {object} refreshToken response
* @param {object} profile objet
* @param {object} done callback
* @param {string} type signupType
* @returns {object} object
*/
static async facebookCallBack(accessToken, refreshToken, profile, done) {
Expand Down Expand Up @@ -197,6 +202,109 @@ class UserController {
}
return Customize.successMessage(req, res, 'total Available roles', allUsers, 200);
}

/**
* User can update his/her profile
* @description POST /api/v1/user/profile-settings
* @static
* @param {object} req request object
* @param {object} res response object
* @returns {object} profileUpdate
*/
static async updateProfile(req, res) {
const { id } = req.user;
const updateUser = await users.findOne({
where: {
id
}
});

let { firstName, lastName } = updateUser.dataValues;
const {
gender, birthDate, address, imageURL, department, managerId,
bio, userFirstName, userLastName,
} = req.body;
firstName = userFirstName || firstName;
lastName = userLastName || lastName;
const data = {
firstName,
lastName,
gender,
birthDate,
address,
imageURL,
department,
managerId,
bio
};
try {
const profileUpdate = await userProfile.update(
{
gender,
birthDate,
address,
imageURL,
department,
managerId,
bio
},
{
where: {
userId: id
}
}
);

await users.update(
{
firstName,
lastName
},
{
where: {
id
}
}
);
if (profileUpdate[0]) {
// Check if record exists in db
Customize.successMessage(req, res, 'Your profile updated successfully', data, HttpStatus.OK);
} else {
Customize.errorMessage(req, res, 'Unable to update your profile', HttpStatus.BAD_REQUEST);
}
} catch (e) {
console.log(e);

Customize.errorMessage(req, res, e, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

/**
* User can view his/her profile
* @description POST /api/v1/user/view-profile
* @static
* @param {object} req request object
* @param {object} res response object
* @returns {object} view-profile
*/
static async viewProfile(req, res) {
try {
const { id } = req.user;
const userData = await userProfile.findOne({
attributes: ['gender', 'birthDate', 'address', 'imageURL', 'department', 'managerId', 'bio'],
include: [{
model: users,
as: 'user',
attributes: ['id', 'firstName', 'lastName', 'email']
}],
where: { userId: id },
});

Customize.successMessage(req, res, 'User profile retrieved successfully', userData, HttpStatus.OK);
} catch (e) {
Customize.errorMessage(req, res, 'Server error', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

export default UserController;
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
type: Sequelize.STRING
},
isVerified: {
type: Sequelize.STRING
type: Sequelize.BOOLEAN
},
signupType: {
type: Sequelize.STRING
Expand Down
Loading

0 comments on commit 137c104

Please sign in to comment.