Skip to content

Commit

Permalink
Merge 3440985 into 394b268
Browse files Browse the repository at this point in the history
  • Loading branch information
salviosage committed Aug 16, 2019
2 parents 394b268 + 3440985 commit a0b244a
Show file tree
Hide file tree
Showing 12 changed files with 439 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ typings/

#ignore vscode settings file
.vscode/settings.json
#ignore DS_Store
.DS_Store

# End of https://www.gitignore.io/api/node

61 changes: 61 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"bcrypt": "^3.0.6",
"body-parser": "^1.19.0",
"cloudinary": "^1.14.0",
"connect-multiparty": "^2.2.0",
"cors": "^2.8.5",
"coverage": "^0.4.1",
"dotenv": "^6.2.0",
Expand All @@ -38,11 +39,13 @@
"express-async-errors": "^3.1.1",
"express-jwt": "^5.3.1",
"express-session": "^1.15.6",
"fs": "0.0.1-security",
"joi": "^14.3.1",
"jsonwebtoken": "^8.5.1",
"method-override": "^2.3.10",
"methods": "^1.1.2",
"multer": "^1.4.2",
"multer-storage-cloudinary": "^2.2.1",
"nyc": "^14.1.1",
"passport": "^0.4.0",
"passport-facebook": "^3.0.0",
Expand Down
99 changes: 99 additions & 0 deletions src/controllers/profile.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import cloudinary from 'cloudinary';
import UserService from '../services/user.service';
import Util from '../helpers/util';

const util = new Util();

/**
* @description User profile
*/
class Profile {
/**
* @description get user profile
* @param {object} req
* @param {object} res
* @returns {object} return object containing user profile
*/
static async getProfile(req, res) {
const userName = req.params.username;

try {
let userfound;
if (userName) {
userfound = await UserService.findOne('', userName);
} else {
userfound = await UserService.findOne(req.auth.email, '');
}
if (!userfound) {
util.setError(404, 'Profile not found');
return util.send(res);
}
const data = {
username: userfound.username,
bio: userfound.bio,
image: userfound.image,
};
util.setSuccess(200, 'Successfully retrieved a user profile', data);
return util.send(res);
} catch (err) {
util.setError(400, 'we are facing some problemsin system, please contact administrator');
return util.send(res);
}
}

/**
* @description update user profile
* @param {object} req
* @param {object} res
* @returns {object} return object containing updated user profile
*/
static async updateProfile(req, res) {
let filename = '';
if (req.files.image) {
filename = req.files.image.path;
}
cloudinary.v2.uploader.upload(filename, { tags: 'CodepiratesAuthors-haven' }, async (err, image) => {
try {
const userName = req.auth.email;
const user = await UserService.findOne(userName, '');
const oldURL = user.image;
if (!user) {
util.setError(400, 'User not found');
return util.send(res);
}
const usernamefound = await UserService.getUserByUserName(req.body.username, userName);
if (usernamefound) {
util.setError(409, 'Sorry! The profile username taken, try another one');
return util.send(res);
}
let imgURL;
if (!err) {
imgURL = image.secure_url;
}
if (!imgURL) {
imgURL = oldURL;
}
const prof = {
email: req.auth.email,
username: req.body.username,
bio: req.body.bio,
image: imgURL,
};
const updateProfile = await UserService.updateProfile(prof);
const newProfile = {
userName: updateProfile[1].username,
email: updateProfile[1].email,
bio: updateProfile[1].bio,
image: updateProfile[1].image,
upadatedAt: updateProfile[1].upadatedAt,
};
util.setSuccess(200, 'Successfully Profile Updated.', newProfile);
return util.send(res);
} catch (error) {
util.setError(400, 'we are having some problems in system, please contact administrator');
return util.send(res);
}
});
}
}
export default Profile;
38 changes: 38 additions & 0 deletions src/middlewares/validators/user.profile.validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Joi from 'joi';
import Util from '../../helpers/util';

const util = new Util();

export default (req, res, next) => {
const {
username, bio
} = req.body;

const schema = {
username: Joi.string()
.trim()
.regex(/^[a-zA-Z]+$/)
.min(3)
.max(20),
bio: Joi.string()
.trim()
.min(20)
};
const { error } = Joi.validate({
bio, username
}, schema);

if (!error) return next();
const errorMessageFromJoi = error.details[0].message;
switch (errorMessageFromJoi) {
case `"bio" with value "${String(bio)}" fails to match the required pattern: /^[a-zA-Z]+$/`:
util.setError(400, 'bio should contain only characters');
return util.send(res);
case `"username" with value "${String(username)}" fails to match the required pattern: /^[a-zA-Z]+$/`:
util.setError(400, 'username should contain only characters');
return util.send(res);
default:
util.setError(400, errorMessageFromJoi);
return util.send(res);
}
};
8 changes: 8 additions & 0 deletions src/migrations/20190805142252-create-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ module.exports = {
type: Sequelize.STRING,
unique:true
},
bio: {
allowNull: true,
type: Sequelize.STRING,
},
image: {
allowNull: true,
type: Sequelize.STRING,
},
role: {
allowNull: false,
type: Sequelize.STRING,
Expand Down
2 changes: 2 additions & 0 deletions src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module.exports = (sequelize, DataTypes) => {
email: DataTypes.STRING,
password: DataTypes.STRING,
username: DataTypes.STRING,
image: DataTypes.STRING,
bio: DataTypes.STRING,
role: {type:DataTypes.STRING, defaultValue: 'normal'},
verified: {type: DataTypes.BOOLEAN, defaultValue: false},
}, {});
Expand Down
6 changes: 5 additions & 1 deletion src/routes/api/index.route.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import express from 'express';
import swaggerui from 'swagger-ui-express';
import path from 'path';
import swaggerSpecification from '../../config/swagger';


import oauth from './oauth/oauth.routes';
import user from './user/user.route';
import article from './article/article.routes';
import profile from './profile/profile.route';

const router = express.Router();
router.use('/images', express.static(path.join(__dirname, 'images')));

router.use(oauth);
router.use('/profile', profile);
router.use('/users', user);
router.use('/', article);
router.use('/api-docs', swaggerui.serve, swaggerui.setup(swaggerSpecification));


export default router;
18 changes: 18 additions & 0 deletions src/routes/api/profile/profile.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import express from 'express';
import connectmultiparty from 'connect-multiparty';
import ProfileController from '../../../controllers/profile.controller';
import validateToken from '../../../middlewares/auth';
import profileVAlidator from '../../../middlewares/validators/user.profile.validator';
import confirmEmaiAuth from '../../../middlewares/emailVarification.middleware';


const connectMulti = connectmultiparty();
const router = express.Router();
// greate edit a viewing profile handlers

router.get('/', validateToken, confirmEmaiAuth, ProfileController.getProfile);
router.get('/:username', validateToken, confirmEmaiAuth, ProfileController.getProfile);
router.put('/', [validateToken, confirmEmaiAuth, connectMulti, profileVAlidator], ProfileController.updateProfile);


export default router;
2 changes: 2 additions & 0 deletions src/routes/api/user/user.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import followController from '../../../controllers/follow.controller';
import resetPasswordValidation from '../../../middlewares/validators/resetpassword.validation';

const router = express.Router();

router.get('/verify', verifyEmail);
router.get('/allusers', [validateToken, admin, confirmEmaiAuth], UserController.getAllUsers);
router.post('/signup', validateUser, UserController.signup);
Expand All @@ -23,6 +24,7 @@ router.post('/profiles/:userId/follow', [validateToken, validateUserId], followC
router.get('/profiles/following', validateToken, followController.listOfFollowedUsers);
router.get('/profiles/followers', validateToken, followController.listOfFollowers);


// reset password route handlers
router.post('/reset', UserController.requestPasswordReset);
router.patch('/reset/:token', resetPasswordValidation, UserController.handlePasswordReset);
Expand Down
Loading

0 comments on commit a0b244a

Please sign in to comment.