Skip to content

Commit

Permalink
pull origin develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Kabalisa committed Jul 13, 2019
2 parents 1cb74c5 + 2f0ea1a commit 88b6783
Show file tree
Hide file tree
Showing 52 changed files with 2,144 additions and 249 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ yarn.lock
# dot env file
.env

postgres-data
postgres-data
34 changes: 22 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import session from 'express-session';
import routes from './routes';
import registerApiDocEndpoint from './config/swagger';
import pass from './config/passport/localstrategy';
import initNotification from './helpers/utils/eventHandlers';
import initEventListener from './helpers/utils/eventHandlers';

initNotification();
initEventListener();

const isProduction = process.env.NODE_ENV === 'production';

Expand All @@ -27,20 +27,19 @@ app.use(
})
);


// Normal express config defaults
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ limit: '500mb', extended: false, parameterLimit: 500000 }));
app.use(bodyParser.json({ limit: '500mb' }));
app.use(passport.initialize());
pass(passport);
app.use(express.static(`${__dirname}/public`));
app.use('/html', express.static(`${__dirname}/html`));

if (!isProduction) {
app.use(errorhandler());
}

registerApiDocEndpoint(app);

registerApiDocEndpoint(app);
app.use(routes);

// / catch 404 and forward to error handler
Expand All @@ -53,13 +52,27 @@ app.use((req, res, next) => {
// Handle application error
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
console.log(err.stack);
let error = {};
if (!isProduction) {
error = err;
}
res.status(err.status || 500);

if (err.message === 'Validation error') {
res.json({
status: 409,
errors: {
body: error.errors[0].message
}
});
}
if (err.message === 'SequelizeDatabaseError') {
res.json({
status: 401,
errors: {
body: 'Unable to access database'
}
});
}
res.json({
errors: {
message: err.message,
Expand All @@ -68,8 +81,5 @@ app.use((err, req, res, next) => {
});
});

// Normal express config defaults
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

export default app;
113 changes: 111 additions & 2 deletions controllers/article.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ class ArticleController {
userId
});

if (!isArticleAuthor && (!hasRead || !hasRead.userId)) {
await statsHelper.saveReadingStats(userId, articleId);
if (!isArticleAuthor && !hasRead) {
await statsHelper.saveReadingStats({ userId, articleId, isDuplicate: false });
} else if (!isArticleAuthor && (hasRead)) {
await statsHelper.saveReadingStats({ userId, articleId, isDuplicate: true });
}

article.dataValues.tagList = tags;
Expand Down Expand Up @@ -256,6 +258,113 @@ class ArticleController {
const bookmarks = await ArticleHelper.getBookmarks(id);
return res.status(200).send({ Bookmarks: bookmarks });
}

/**
* @param {object} req - Request object
* @param {object} res - Response object
* @returns {object} response
* @static
*/
static async highlightText(req, res) {
const highlightAndComment = await ArticleHelper.highlightedText(req);
if (highlightAndComment.error) {
return res.status(400).send({ status: 400, errors: { body: highlightAndComment.error } });
}
return res.status(201).send({ status: 201, data: highlightAndComment });
}

/**
* @param {object} req - Request object
* @param {object} res - Response object
* @returns {object} response
* @static
*/
static async getHighlightText(req, res) {
const highlights = await ArticleHelper.getHighlightedText(req);
if (highlights.error) {
return res.status(404).send({ status: 404, errors: { body: [highlights.error] } });
}
return res.status(200).send({ status: 200, data: highlights });
}

/**
* @param {object} req - Request object
* @param {object} res - Response object
* @returns {object} response
* @static
*/
static async getCommentHighlights(req, res) {
const highlights = await ArticleHelper.getHighlightedTextComment(req);
if (highlights.error) {
return res.status(404).send({ status: 404, errors: { body: [highlights.error] } });
}
return res.status(200).send({ status: 200, data: highlights });
}

/**
* @param {object} req - Request object
* @param {object} res - Response object
* @returns {object} response
* @static
*/
static async addCommentHighlights(req, res) {
const comment = await ArticleHelper.commentHighlighedText(req);
return res.status(201).send({ status: 201, data: comment });
}

/**
* @param {object} req - Request object
* @param {object} res - Response object
* @returns {object} response
* @static
*/
static async reportArticle(req, res) {
const { reason } = req.body;
const userId = req.user.id;
const { reportType, slug } = req.params;

await ArticleHelper.saveReportedArticle({
articleSlug: slug, userId, reportType, reason: reason || null
});
return res.status(201).json({
status: 201,
message: 'Article has successfully reported'
});
}

/**
* @param {object} req - Request object
* @param {object} res - Response object
* @returns {object} response
* @static
*/
static async getAllReportedArticle(req, res) {
const reports = await ArticleHelper.getReportedArticles();
const totalReports = reports.count;
const data = {
total: totalReports,
reports: reports.rows
};

return res.status(200).send({
status: 200,
data
});
}

/**
* @param {object} req - Request object
* @param {object} res - Response object
* @returns {object} response
* @static
*/
static async getAllCommentHighlights(req, res) {
const highlights = await ArticleHelper.getallHighlightedTextComment(req);
if (highlights.error) {
return res.status(404).send({ status: 404, errors: { body: [highlights.error] } });
}
return res.status(200).send({ status: 200, data: highlights });
}
}

export default ArticleController;
30 changes: 30 additions & 0 deletions controllers/message.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import messageHelper from '../helpers/messageHelper';

/**
* @class Message
*/
class Message {
/**
* @param {object} req - Request object
* @param {object} res - Response object
* @returns {object} response
*/

/**
* @param {object} req - Request object
* @param {object} res - Response object
* @returns {object} response
*/
static async getMessages(req, res) {
try {
const messages = await messageHelper.getChats();
return res.status(200).send({
status: 200,
messages
});
} catch (error) {
return res.status(400).send(error);
}
}
}
export default Message;
21 changes: 11 additions & 10 deletions controllers/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@ class Users {
static async user(req, res) {
const { username } = req.params;
try {
const foundUser = await User.findOne({ where: { username } });
const foundUser = await User.findOne({
where: { username },
attributes: ['id', 'firstName', 'lastName', 'username', 'bio', 'image', 'email']
});
if (!foundUser) {
return res.status(404).json({
status: 404,
message: 'User not found!'
});
}
const profileData = {
username: foundUser.username,
bio: foundUser.bio,
image: foundUser.image
};
return res.status(200).json({
status: 200,
data: profileData
data: foundUser
});
} catch (error) {
return res.status(500).json({
Expand All @@ -47,15 +45,18 @@ class Users {
static async editProfile(req, res) {
try {
const { username } = req.params;
const {
firstName, lastName, bio, image
} = req.body;
const updateProfile = await User.update(
{
username: req.body.username,
bio: req.body.bio,
image: req.body.image
firstName, lastName, bio, image,
},
{ where: { username }, returning: true, plain: true }
);
const newProfile = {
firstName: updateProfile[1].firstName,
lastName: updateProfile[1].lastName,
username: updateProfile[1].username,
email: updateProfile[1].email,
image: updateProfile[1].image,
Expand Down
3 changes: 2 additions & 1 deletion controllers/resetPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class Password {
id: user.id
};
const token = Helper.generateToken(userDetails);
const info = { email, subject: 'Authorshaven Reset Password', html: `<html>User with the following email <strong>${email}</strong> has required to reset their password please copy this token <strong>${token}</strong></html>` };
const url = `${process.env.FRONTEND_URL}/completReset/${token}`;
const info = { email, subject: 'Authorshaven Reset Password', html: `<html>User with the following email <strong>${email}</strong> has required to reset their password please use <a href=${url}> this link</a></html>` };
mailSender.send(info).then(result => res.status(200).send({ Result: result, Token: token })).catch(() => res.status(400).send({ ERROR: 'issue with sending email' }));
}).catch(error => res.status(400).send({ ERROR: error }));
}
Expand Down
31 changes: 11 additions & 20 deletions controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,13 @@ class UserController {
);
if (isValidPassword) {
const token = helper.generateToken(userExist.dataValues);
return res.status(200).send({
status: res.statusCode,
token,
data: {
username: userExist.dataValues.username,
email: userExist.dataValues.email
}
});
return res.redirect(
`${process.env.FRONTEND_URL}/articles?token=${token}&username=${
req.user.username
}`
);
}
return res.status(422).send({
status: res.statusCode,
message: 'Email Already registered with other platform'
});
return res.redirect(`${process.env.FRONTEND_URL}/verify/409`);
}
const encryptedPassword = await helper.hashPassword(req.user.password);

Expand All @@ -57,14 +51,11 @@ class UserController {
return res.status(500).send('Internal error');
}
const token = helper.generateToken(newUser.dataValues);
return res.status(201).send({
status: res.statusCode,
token,
data: {
username: newUser.dataValues.username,
email: newUser.dataValues.email
}
});
return res.redirect(
`${process.env.FRONTEND_URL}/articles?token=${token}&username=${
req.user.username
}`
);
}

/**
Expand Down
Loading

0 comments on commit 88b6783

Please sign in to comment.