-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
439 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.