Skip to content

Commit

Permalink
Merge branch 'develop' into 165378746-feature/suggested-followers
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotimi Babalola committed Apr 17, 2019
2 parents dac21bf + 0712cbe commit 0c4a7c5
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 45 deletions.
6 changes: 4 additions & 2 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import session from 'express-session';
import dotenv from 'dotenv';
import routes from './routes/index';
import swaggerSpec from './documentation/swagger';

import middlewares from './middlewares';
import {
facebookStrategy,
twitterStrategy,
googleStrategy,
} from './config/passport.service';

const { trimmerMiddleware } = middlewares;

const app = express();

app.use(express.json());

app.use(trimmerMiddleware);
const port = process.env.PORT || 6000;

const baseUrl = '/api/v1';
Expand Down
6 changes: 1 addition & 5 deletions server/controllers/article.controllers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import model from '../models';
import validations from '../helpers/validations';
import slugMaker from '../helpers/slug-maker';
import spaceTrimmer from '../helpers/space-trimmer';
import tagsHelpers from '../helpers/tags-helpers';
import serverError from '../helpers/server-error';
import serchDatabase from '../helpers/search-database';
Expand Down Expand Up @@ -83,7 +82,6 @@ const createArticle = async (req, res) => {
const { userObj } = req.user;
req.body.user_id = userObj.id;

req.body = spaceTrimmer(req.body);
req.body.reading_time = readingTime(req.body.abstract, req.body.body);
const article = await Article.create(req.body);

Expand Down Expand Up @@ -195,7 +193,6 @@ const editAticle = async (req, res) => {
},
});
}
const updateBody = spaceTrimmer(req.body);

const articleToBeUpdated = await Article.findOne({
where: {
Expand All @@ -222,7 +219,7 @@ const editAticle = async (req, res) => {
});
}

const updatedArticle = await articleToBeUpdated.update(updateBody);
const updatedArticle = await articleToBeUpdated.update(req.body);

return res.status(200).json({
message: 'Article Updated Successfully',
Expand All @@ -244,7 +241,6 @@ const createHighlight = async (req, res) => {
});
}

req.body = spaceTrimmer(req.body);
req.body.article_id = req.params.articleId;
req.body.user_id = req.user.userObj.id;

Expand Down
14 changes: 5 additions & 9 deletions server/controllers/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,11 @@ const signupController = async (req, res) => {
password,
});

if (!user) return res.status(401).json({ errors: { body: error } });
if (!user) {
return res.status(204).json({ errors: { body: ['User not created'] } });
}

const {
id,
is_admin: isAdmin,
bio,
image_url: image,
first_name: firstName,
} = user;
const { id, is_admin: isAdmin, first_name: firstName } = user;
const token = authHelper.encode({ id, isAdmin });

const verificationToken = authHelper.encode({ email });
Expand All @@ -98,7 +94,7 @@ const signupController = async (req, res) => {
await notifications.signupEmail(email, verificationLink, firstName);

return res.status(200).json({
user: { email, token, bio, image },
user: { email, token },
});
} catch (err) {
return res.status(500).json({
Expand Down
17 changes: 12 additions & 5 deletions server/controllers/get-articles.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import models from '../models';
import validations from '../helpers/validations';
import serverError from '../helpers/server-error';

const { Article } = models;
const { Article, User } = models;

const getArticles = async (req, res) => {
if (validations.validateArticlePage(req.params.page)) {
Expand All @@ -22,7 +22,7 @@ const getArticles = async (req, res) => {
const offset = pageSize * page - pageSize;

try {
const articles = await Article.findAll({
const articles = await Article.findAndCountAll({
offset,
limit: pageSize,
order: ['title'],
Expand All @@ -34,10 +34,17 @@ const getArticles = async (req, res) => {
'updatedAt',
'likes_count',
],
include: [
{
model: User,
as: 'author',
attributes: ['first_name', 'last_name', 'bio', 'image_url'],
},
],
});

return res.status(200).json({
articles,
articles: articles.rows,
articlesCount: articles.count,
});
} catch (e) {
return res.status(500).json({
Expand All @@ -47,7 +54,7 @@ const getArticles = async (req, res) => {
}
return res.status(400).json({
errors: {
body: ['cannot be anything but numbers'],
body: ['Page number cannot be anything but numbers'],
},
});
};
Expand Down
13 changes: 10 additions & 3 deletions server/controllers/search-article.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const searchArticles = async (req, res) => {
},
});
}
const searchFilter = req.query.search;
const searchFilter = req.query.filter;
const articles = Article.findAll({
where: {
title: {
Expand All @@ -32,7 +32,7 @@ const searchArticles = async (req, res) => {
{
model: User,
as: 'author',
attributes: ['first_name', 'last_name'],
attributes: ['first_name', 'last_name', 'bio', 'image_url'],
},
],
});
Expand All @@ -56,6 +56,13 @@ const searchArticles = async (req, res) => {
'updatedAt',
'likes_count',
],
include: [
{
model: User,
as: 'author',
attributes: ['first_name', 'last_name', 'bio', 'image_url'],
},
],
},
],
});
Expand All @@ -66,7 +73,7 @@ const searchArticles = async (req, res) => {
[sequelize.Op.iLike]: `%${searchFilter}%`,
},
},
attributes: ['first_name', 'last_name'],
attributes: ['first_name', 'last_name', 'bio', 'image_url'],
include: [
{
model: Article,
Expand Down
11 changes: 0 additions & 11 deletions server/helpers/space-trimmer.js

This file was deleted.

3 changes: 1 addition & 2 deletions server/joiSchema/articleSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const articleSchema = () => {
const schema = Joi.object().keys({
title: Joi.string()
.min(5)
.max(100)
.required(),
.max(100),
abstract: Joi.string().min(5),
body: Joi.string().min(5),
is_draft: Joi.boolean().required(),
Expand Down
2 changes: 2 additions & 0 deletions server/middlewares/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import commentMiddleware from './comment.middlewares';
import adminMiddleware from './admin.middlewares';
import uuidMiddleware from './uuid.middleware';
import userIdMiddleware from './user-id-is-correct.middleware';
import trimmerMiddleware from './trimmer.middlewares';

export default {
authValidator,
Expand All @@ -20,4 +21,5 @@ export default {
adminMiddleware,
uuidMiddleware,
userIdMiddleware,
trimmerMiddleware,
};
12 changes: 12 additions & 0 deletions server/middlewares/trimmer.middlewares.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const trimmer = (req, res, next) => {
const { body } = req;

Object.keys(body).forEach(val => {
if (typeof body[val] === 'string') {
body[val] = body[val].trim();
}
});
return next();
};

export default trimmer;
10 changes: 7 additions & 3 deletions tests/article-pagination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ describe('Display articles in pages', () => {
.request(app)
.get('/api/v1/articles/1')
.end((err, res) => {
const { articles } = JSON.parse(res.text);
expect(articles).to.be.a('array');
expect(res).to.have.status(200);
expect(res.body.articles).to.be.a('array');
expect(res.body.articlesCount).to.be.a('number');

done();
});
Expand All @@ -20,7 +21,10 @@ describe('Display articles in pages', () => {
.get('/api/v1/articles/a')
.end((err, res) => {
const errorMessage = res.body.errors.body[0];
expect(errorMessage).to.be.equal('cannot be anything but numbers');
expect(res).to.have.status(400);
expect(errorMessage).to.be.equal(
'Page number cannot be anything but numbers'
);

done();
});
Expand Down
6 changes: 3 additions & 3 deletions tests/article-search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Article Search', () => {
it('Display articles by search filter', done => {
chai
.request(app)
.get(`/api/v1/search?search=${articleSearchFilter}`)
.get(`/api/v1/search?filter=${articleSearchFilter}`)
.end((err, res) => {
const { articleFilter } = res.body;
expect(res.status).to.equal(200);
Expand All @@ -22,7 +22,7 @@ describe('Article Search', () => {
it('Display author by search filter', done => {
chai
.request(app)
.get(`/api/v1/search?search=${authorSearchFilter}`)
.get(`/api/v1/search?filter=${authorSearchFilter}`)
.end((err, res) => {
const { authorFilter } = res.body;
expect(res.status).to.equal(200);
Expand All @@ -35,7 +35,7 @@ describe('Article Search', () => {
it('Display keyword by search filter', done => {
chai
.request(app)
.get(`/api/v1/search?search=${keywordSearchFilter}`)
.get(`/api/v1/search?filter=${keywordSearchFilter}`)
.end((err, res) => {
const { keywordFilter } = res.body;
expect(res.status).to.equal(200);
Expand Down
4 changes: 2 additions & 2 deletions tests/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('SIGNUP CONTROLLER TEST', () => {
expect(res).to.have.status(200);
expect(res).to.be.a('object');
expect(user).to.be.a('object');
expect(user).to.have.keys('email', 'token', 'bio', 'image');
expect(user).to.have.keys('email', 'token');
done();
});
});
Expand All @@ -87,7 +87,7 @@ describe('SIGNUP CONTROLLER TEST', () => {
expect(res).to.have.status(200);
expect(res).to.be.a('object');
expect(user).to.be.a('object');
expect(user).to.have.keys('email', 'token', 'bio', 'image');
expect(user).to.have.keys('email', 'token');
done();
});
});
Expand Down

0 comments on commit 0c4a7c5

Please sign in to comment.