Skip to content

Commit

Permalink
Merge f8bc133 into cca1648
Browse files Browse the repository at this point in the history
  • Loading branch information
nkalyesubula committed Aug 29, 2019
2 parents cca1648 + f8bc133 commit a9877f5
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 9 deletions.
42 changes: 42 additions & 0 deletions src/controllers/articles.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ 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';
import Util from '../helpers/util';


const { notifyViaEmailAndPush } = NotificationServices;
const util = new Util();

const db = models.Article;
/**
Expand Down Expand Up @@ -195,6 +199,44 @@ class Articles {
updatedArticle
});
}

/**
*
*
* @static
* @param {*} req
* @param {*} res
* @returns {Object} share article over email and social media channelds
* @memberof Articles
*/
static async shareArticle(req, res) {
const article = await db.findOne({
where: { slug: req.params.slug }
});

if (!article) {
util.setError(404, 'Article is not found.');
return util.send(res);
}
const location = `${process.env.BACKEND_URL}/api/${process.env.API_VERSION}`;
const url = `${location}/articles/${req.params.slug}`;
switch (req.params.channel) {
case 'facebook':
await OpenUrlHelper.openUrl(`https:www.facebook.com/sharer/sharer.php?u=${url}`);
util.setError(200, `Article shared to ${req.params.channel}`);
return util.send(res);
case 'twitter':
await OpenUrlHelper.openUrl(`https://twitter.com/intent/tweet?url=${url}`);
util.setError(200, `Article shared to ${req.params.channel}`);
return util.send(res);
case 'mail':
await OpenUrlHelper.openUrl(`mailto:?subject=${article.title}&body=${url}`);
util.setError(200, `Article shared to ${req.params.channel}`);
return util.send(res);
default:
break;
}
}
}

export default Articles;
1 change: 1 addition & 0 deletions src/routes/api/article/article.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ router.get('/', checkQuery, articleController.getAllArticles);
router.get('/:slug', articleController.getOneArticle);
router.delete('/:slug', [auth, confirmEmailAuth], articleController.deleteArticle);
router.patch('/:slug', [auth, confirmEmailAuth], imageUpload.array('images', 10), articleController.UpdateArticle);
router.post('/:slug/share/:channel', [auth, confirmEmailAuth], articleController.shareArticle);


// Highlight
Expand Down
34 changes: 27 additions & 7 deletions src/routes/api/article/doc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,31 @@
400:
$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
$ref: "#/responses/Notfound"
/articles:
post:
Expand Down Expand Up @@ -437,7 +462,7 @@ definitions:
400:
$ref: "#/responses/BadRequest"
404:
$ref: "#/responses/Notfound"
$ref: '#/responses/Notfound'
/articles/{slug}/highlight:
post:
produces:
Expand Down Expand Up @@ -531,9 +556,4 @@ definitions:
200:
description: you are ready to share
404:
description: Sorry, you can not share





description: Sorry, you can not share
4 changes: 2 additions & 2 deletions src/services/notification.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class NotificationServices {
.filter(eachUser => eachUser.inAppNotification === true);
const message = `${followedUser.username} just published an article`;
const location = `${process.env.BACKEND_URL}/api/${process.env.API_VERSION}`;
const url = `${location}/api/v1/articles/${slug}`;
const url = `${location}/articles/${slug}`;


if (myFollowersWithEmailSub.length !== 0) {
Expand Down Expand Up @@ -101,7 +101,7 @@ class NotificationServices {
if (usersWhoFavoritedWithEmailSub.length !== 0) {
const emailAddresses = usersWhoFavoritedWithEmailSub.map(each => each.email);
const location = `${process.env.BACKEND_URL}/api/${process.env.API_VERSION}`;
const url = `${location}/api/v1/articles/${slug}`;
const url = `${location}/articles/${slug}`;
const emailTemplate = newCommentOnFavoritedArticlesTemplate(req.auth.username, url);
await sendEmail(emailAddresses, 'Hi there', emailTemplate);
}
Expand Down
50 changes: 50 additions & 0 deletions test/search.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { chai, server, expect } from './test-setup';
import Helper from '../src/helpers/helper';

const usertoken = Helper.generateToken({
id: 2,
email: 'user@gmail.com',
username: 'user',
verified: false
});

describe('search article query builder', () => {
it('List of all articles', (done) => {
chai
.request(server)
.get('/api/v1/articles')
.set('Content-Type', 'application/json')
.set('Authorization', usertoken)
.end((err, res) => {
expect(res.status).to.be.deep.equal(200);
expect(res.body).to.have.deep.property('message', 'List of all articles');
done();
});
});
it('Title and description in the query', (done) => {
chai
.request(server)
.get('/api/v1/articles?title=Title&&keyword=here')
.set('Content-Type', 'application/json')
.set('Authorization', usertoken)
.end((err, res) => {
if (err) done(err);
expect(res.status).to.be.deep.equal(200);
expect(res.body).to.have.deep.property('message', 'List of all articles');
done();
});
});
it('if invalid query key', (done) => {
chai
.request(server)
.get('/api/v1/articles?key=Tilltle')
.set('Content-Type', 'application/json')
.set('Authorization', usertoken)
.end((err, res) => {
if (err) done(err);
expect(res.status).to.be.deep.equal(400);
expect(res.body).to.have.deep.property('error');
done();
});
});
});
55 changes: 55 additions & 0 deletions test/share.article.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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();
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 a9877f5

Please sign in to comment.