Skip to content

Commit

Permalink
feature(share-articles): Users should be able to share articles acros…
Browse files Browse the repository at this point in the history
…s different channels [Finishes #167313416]
  • Loading branch information
nkalyesubula committed Aug 28, 2019
1 parent 883dce9 commit 45cf47f
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import articleService from '../services/article.service';
import Helper from '../helpers/helper';
import NotificationServices from '../services/notification.service';
import cloudinaryHelper from '../helpers/cloudinaryHelper';
import OpenUrlHelper from '../helpers/share.article.helper';

const { notifyViaEmailAndPush } = NotificationServices;

Expand Down Expand Up @@ -221,11 +222,11 @@ class Articles {
* @memberof Articles
*/
static async shareArticle(req, res) {
const articleSlug = await db.findOne({
const article = await db.findOne({
where: { slug: req.params.slug }
});

if (!articleSlug) {
if (!article) {
return res.status(404).json({
status: 404,
message: 'Article is not found.'
Expand All @@ -235,26 +236,23 @@ class Articles {
const url = `${location}/articles/${req.params.slug}`;
switch (req.params.channel) {
case 'facebook':
if (process.env.NODE_ENV !== 'test') open(`https:www.facebook.com/sharer/sharer.php?u=${url}`);
res.status(200).json({
await OpenUrlHelper.openUrl(`https:www.facebook.com/sharer/sharer.php?u=${url}`);
return res.status(200).json({
status: 200,
message: `Article shared to ${req.params.channel}`,
});
break;
case 'twitter':
if (process.env.NODE_ENV !== 'test') { open(`https://twitter.com/intent/tweet?url=${url}`); }
res.status(200).json({
await OpenUrlHelper.openUrl(`https://twitter.com/intent/tweet?url=${url}`);
return res.status(200).json({
status: 200,
message: `Article shared to ${req.params.channel}`,
});
break;
case 'mail':
if (process.env.NODE_ENV !== 'test') open(`mailto:?subject=${slug}&body=${url}`);
res.status(200).json({
await OpenUrlHelper.openUrl(`mailto:?subject=${article.title}&body=${url}`);
return res.status(200).json({
status: 200,
message: `Article shared to ${req.params.channel}`,
});
break;
default:
break;
}
Expand Down
7 changes: 7 additions & 0 deletions src/helpers/share.article.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import open from 'open';

const openUrl = url => open(url);

const OpenUrlHelper = { openUrl };

export default OpenUrlHelper;
26 changes: 26 additions & 0 deletions src/routes/api/article/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,30 @@
$ref: '#/responses/BadRequest'
404:
$ref: '#/responses/Notfound'
/articles/{slug}/share/{channel}:
post:
description: >
Share an article on mail and social media channels
parameters:
- name: slug
in: path
description: Slug for article
type: string
required: true
- name: channel
in: path
description: Channel to share to
type: string
required: true
- name: x-access-token
in: header
schema:
type: string
required:
- authorization
responses:
200:
description: Article successfully shared
/articles:
post:
summary: Create an article
Expand Down Expand Up @@ -438,3 +462,5 @@ definitions:
$ref: '#/responses/BadRequest'
404:
$ref: '#/responses/Notfound'


56 changes: 56 additions & 0 deletions test/share.article.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import sinon from 'sinon';
import { expect } from './test-setup';
import Article from '../src/controllers/articles.controller';
import OpenUrlHelper from '../src/helpers/share.article.helper';

describe('test for sharing an article', () => {
it('test for sharing an article via facebook', async () => {
const req = {
params: { slug: 'fakeslug', channel: 'facebook' },
auth: {
email: 'user@gmail.com'
}
};
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
sinon.stub(OpenUrlHelper, 'openUrl').returns(true);
await Article.shareArticle(req, res);
expect(res.status).to.have.been.calledWith(200);
});
it('test for sharing an article via twitter', async () => {
const req = {
params: { slug: 'fakeslug', channel: 'twitter' },
auth: {
email: 'user@gmail.com'
}
};
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await Article.shareArticle(req, res);
expect(res.status).to.have.been.calledWith(200);
});
it('test for sharing an article via mail', async () => {
const req = {
params: { slug: 'fakeslug', channel: 'twitter' },
auth: {
email: 'user@gmail.com'
}
};
const res = {
status() { },
send() { },
json() { }
};
sinon.stub(res, 'status').returnsThis();
await Article.shareArticle(req, res);
expect(res.status).to.have.been.calledWith(200);
});
});

0 comments on commit 45cf47f

Please sign in to comment.