-
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.
bug(fix-live-app): The live application should be running smoothly [F…
…inishes #167938441]
- Loading branch information
Showing
23 changed files
with
1,045 additions
and
581 deletions.
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
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
Large diffs are not rendered by default.
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,129 @@ | ||
import db from '../models/index'; | ||
import RateService from '../services/rate.service'; | ||
/** | ||
* | ||
* | ||
* @class rateController | ||
*/ | ||
class rateController { | ||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {Object} return rating information to user | ||
* @memberof UserController | ||
*/ | ||
static async setArticleRating(req, res) { | ||
try { | ||
// Initialize rating data | ||
const userEmail = req.auth.email; | ||
const { rate } = req.body; | ||
const { articleSlug } = req.params; | ||
const rateSchema = { userEmail, articleSlug, rate }; | ||
|
||
// check if user is trying to rate his/her own article | ||
const article = await db.Article.findOne({ | ||
where: { slug: articleSlug } | ||
}); | ||
const user = await db.user.findOne({ | ||
where: { id: article.authorId } | ||
}); | ||
if (user.email === userEmail) { | ||
return res.status(400).send({ | ||
status: 400, | ||
message: 'You cannot rate your own article' | ||
}); | ||
} | ||
|
||
// create rating | ||
const createdRate = await RateService.create(rateSchema); | ||
return res.status(200).send({ | ||
status: 200, | ||
message: 'Thank you for rating this article', | ||
data: createdRate | ||
}); | ||
} catch (error) { | ||
return res.status(404).send({ | ||
status: 404, | ||
message: error.message | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {Object} return rating information to user | ||
* @memberof UserController | ||
*/ | ||
static async updateArticleRating(req, res) { | ||
try { | ||
// Initialize rating data | ||
const userEmail = req.auth.email; | ||
const { rate } = req.body; | ||
const { articleSlug } = req.params; | ||
|
||
// update article rate | ||
const updatedRate = await RateService.update(articleSlug, userEmail, rate); | ||
if (!updatedRate) { | ||
return res.status(400).send({ | ||
status: 400, | ||
error: `Rating for article with Slug: ${articleSlug} not found` | ||
}); | ||
} | ||
return res.status(200).send({ | ||
status: 200, | ||
message: 'Thank you for rating this article', | ||
newRating: updatedRate | ||
}); | ||
} catch (error) { | ||
return res.status(404).send({ | ||
status: 404, | ||
message: error.message | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* | ||
* @static | ||
* @param {*} req | ||
* @param {*} res | ||
* @returns {Object} return rating information to user | ||
* @memberof rateController | ||
*/ | ||
static async getArticleRating(req, res) { | ||
try { | ||
// Initialize rating data | ||
const userEmail = req.auth.email; | ||
const { articleSlug } = req.params; | ||
|
||
// find particular rating | ||
const userRate = await RateService.findOne(articleSlug, userEmail); | ||
if (!userRate) { | ||
return res.status(400).send({ | ||
status: 400, | ||
error: `Rating for article with slug ${articleSlug} not found`, | ||
}); | ||
} | ||
return res.status(200).send({ | ||
status: 200, | ||
message: `Rating for article with slug ${articleSlug} found`, | ||
data: userRate | ||
}); | ||
} catch (error) { | ||
return res.status(404).send({ | ||
status: 404, | ||
message: error.message | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default rateController; |
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,21 @@ | ||
import Joi from 'joi'; | ||
import Util from '../../helpers/util'; | ||
|
||
const util = new Util(); | ||
|
||
export default (req, res, next) => { | ||
const { rate } = req.body; | ||
|
||
const schema = { | ||
rate: Joi.number().integer().min(1).max(5), | ||
}; | ||
const { error } = Joi.validate({ | ||
rate | ||
}, schema); | ||
|
||
if (!error) return next(); | ||
const errorMessageFromJoi = error.details[0].message; | ||
const removeEscapeCharacters = errorMessageFromJoi.replace(/['"]+/g, ''); | ||
util.setError(400, removeEscapeCharacters); | ||
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
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); | ||
} | ||
}; |
Oops, something went wrong.