Skip to content

Commit

Permalink
Merge 12e0b58 into d749c51
Browse files Browse the repository at this point in the history
  • Loading branch information
Veraclins committed Aug 27, 2018
2 parents d749c51 + 12e0b58 commit c2f65d3
Show file tree
Hide file tree
Showing 39 changed files with 1,618 additions and 331 deletions.
15 changes: 12 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,27 @@ DB_HOST_TEST=
DB_PORT_TEST=

SENDGRID_API_KEY=

TOKEN_SECRET=

SESSION_KEY=

FACEBOOK_CLIENT_ID=
FACEBOOK_CLIENT_SECRET=
FACEBOOK_CLIENT_CALLBACK_URL=

GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_CLIENT_CALLBACK_URL=

COPYLEAKS_KEY=
COPYLEAKS_EMAIL=

SITE_ADMIN_EMAIL=

PUSHER_APP_ID_DEV =
PUSHER_KEY_DEV =
PUSHER_SECRET_DEV =
PUSHER_CLUSTER_DEV =

PUSHER_APP_ID_PROD =
PUSHER_KEY_PROD =
PUSHER_SECRET_PROD =
PUSHER_CLUSTER_PROD =
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"test": "npm run migrate:test && cross-env NODE_ENV=test nyc --reporter=html --reporter=text mocha --timeout 50000 --exit --require babel-register ./server/tests/**/*.test.js",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"coveralls": "nyc --reporter=lcov --reporter=text-lcov npm test",
"prestart": "sequelize db:migrate",
"start": "babel-node server/index.js --presets babel-preset-env",
"dev": "cross-env NODE_ENV=development nodemon server/index.js --exec babel-node --presets babel-preset-env",
"migrate:test": "sequelize db:migrate:undo:all --env=test && sequelize db:migrate --env=test && sequelize db:seed:all --env=test",
Expand Down Expand Up @@ -49,23 +50,25 @@
"passport-local": "^1.0.0",
"passport-mock-strategy": "^1.1.1",
"passport-twitter": "^1.0.4",
"path": "^0.12.7",
"pg": "^7.4.3",
"pg-hstore": "^2.3.2",
"request": "^2.87.0",
"request-promise": "^4.2.2",
"pusher": "^2.1.2",
"request": "^2.88.0",
"sequelize": "^4.38.0",
"slug": "^0.9.1",
"socket.io": "^2.1.1",
"underscore": "^1.9.1",
"validatorjs": "^3.14.2"
},
"devDependencies": {
"chai": "^4.1.2",
"eslint": "^4.19.1",
"eslint-config-airbnb": "^17.0.0",
"eslint-config-airbnb-base": "^13.0.0",
"eslint-plugin-import": "^2.13.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-jsx-a11y": "^6.1.1",
"eslint-plugin-react": "^7.10.0",
"eslint-plugin-react": "^7.11.1",
"mocha": "^5.2.0",
"nodemon": "^1.17.4",
"sequelize-cli": "^4.0.0"
Expand Down
3 changes: 0 additions & 3 deletions server/config/passport.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import dotenv from 'dotenv';

dotenv.config();

const strategies = {
google: {
Expand Down
19 changes: 19 additions & 0 deletions server/config/pusher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

const config = {
development: {
appId: process.env.PUSHER_APP_ID_DEV,
key: process.env.PUSHER_KEY_DEV,
secret: process.env.PUSHER_SECRET_DEV,
cluster: process.env.PUSHER_CLUSTER_DEV,
},
production: {
appId: process.env.PUSHER_APP_ID_PROD,
key: process.env.PUSHER_KEY_PROD,
secret: process.env.PUSHER_SECRET_PROD,
cluster: process.env.PUSHER_CLUSTER_PROD,
},
};

const pusherConfig = config.development;

export default pusherConfig;
73 changes: 39 additions & 34 deletions server/controllers/ArticleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import models from '../models';
import randomString from '../helpers/randomString';
import dashReplace from '../helpers/replaceDash';
import queryHelper from '../helpers/queryHelper';
import NotificationController from './NotificationController';

const { Article, Tag } = models;
const { Article, Tag, Channel } = models;
const error = {
message: 'Request can not be processed at the moment, please try again shortly,',
status: 400,
Expand Down Expand Up @@ -35,6 +36,7 @@ export default class ArticleController {
categoryId: article.categoryId,
title: article.title,
body: article.body,
channel: article.channel,
imageUrl: article.imageUrl,
tags,
createdAt: new Date(article.createdAt).toLocaleString('en-GB', { hour12: true }),
Expand All @@ -49,44 +51,40 @@ export default class ArticleController {
* @param {object} res the response object
* @returns {object} the article that was created.
*/
static createArticle(req, res) {
static async createArticle(req, res) {
const { id: userId } = req.user;
const slug = `${dashReplace(req.body.title).toLowerCase()}-${randomString(10)}`;
const {
title, body, imageUrl, categoryId, tags
} = req.body;

Article.create({
slug, title, body, imageUrl, categoryId, userId,
})
.then((article) => {
if (tags !== undefined) {
const tagToLowerCase = tags.toLowerCase();
const splitTags = tagToLowerCase.split(',');
const tagsObjectsArray = [];
for (const value of splitTags) {
tagsObjectsArray.push({ title: value, articleId: article.id });
}
Tag.bulkCreate(tagsObjectsArray)
.then(() => Tag.findAll({
where: { articleId: article.id }
}))
.then((newTags) => {
article.tags = splitTags;
article.addTag(newTags);
return ArticleController.articleResponse(article, 201, res);
});
} else {
return ArticleController.articleResponse(article, 201, res);
try {
const newArticle = await Article.create({
slug, title, body, imageUrl, categoryId, userId,
});
if (tags !== undefined) {
const tagToLowerCase = tags.toLowerCase();
const splitTags = tagToLowerCase.split(',');
const tagsObjectsArray = [];
for (const value of splitTags) {
tagsObjectsArray.push({ title: value, articleId: newArticle.id });
}
})
.catch(() => {
res.status(400).json({
status: 400,
success: false,
error: 'Article was not successfully created',
await Tag.bulkCreate(tagsObjectsArray);
const newTags = await Tag.findAll({
where: { articleId: newArticle.id },
});
newArticle.tags = splitTags;
newArticle.addTag(newTags);
}
ArticleController.articleResponse(newArticle, 201, res);
const articleChannel = `article-${slug}`;
return NotificationController.subscribe(articleChannel, userId);
} catch (err) {
res.status(400).json({
status: 'error',
message: 'Article was not successfully created',
err: err.message,
});
}
}
/**
* Update an article and return the Data.
Expand Down Expand Up @@ -121,7 +119,7 @@ export default class ArticleController {
message: 'You can only update an article that belongs to you',
});
}
Article.update({
return Article.update({
title: title || foundArticle.title,
body: body || foundArticle.body,
imageUrl: imageUrl || foundArticle.imageUrl,
Expand All @@ -133,6 +131,11 @@ export default class ArticleController {
})
.then(([, [updatedArticle]]) => {
ArticleController.articleResponse(updatedArticle, 200, res);
return NotificationController.notifyOnUpdate('made an update', {
userId: id,
articleSlug: slug,
resourceId: updatedArticle.id,
});
})
.catch(() => {
res.status(400).json({
Expand Down Expand Up @@ -173,7 +176,7 @@ export default class ArticleController {
message: 'You can only delete an article that belongs to you',
});
}
Article.destroy({
return Article.destroy({
where: { slug },
})
.then(() => {
Expand All @@ -182,6 +185,8 @@ export default class ArticleController {
success: true,
message: `Article with slug: ${slug} has been successfully deleted`,
});
const name = `article-${slug}`;
return Channel.destroy({ where: { name } });
});
})
.catch(() => res.status(400).json({
Expand Down Expand Up @@ -227,7 +232,7 @@ export default class ArticleController {
});
}
userId = Number.parseInt(userId, 10);
Article
return Article
.findAll(Object.assign({}, queryHelper.allArticles, { where: { userId }, offset, limit }))
.then((articles) => {
ArticleController.sendPaginationResponse(res, articles, userId);
Expand Down
Loading

0 comments on commit c2f65d3

Please sign in to comment.