-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
255 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
export default { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Shares', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
references: { | ||
model: 'Users', | ||
key: 'id' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
allowNull: false, | ||
unique: false | ||
}, | ||
articleSlug: { | ||
type: Sequelize.STRING, | ||
references: { | ||
model: 'Articles', | ||
key: 'slug' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
allowNull: false, | ||
unique: false | ||
}, | ||
sharedOn: { | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}) | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('Shares'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
export default (sequelize, DataTypes) => { | ||
const Share = sequelize.define('Share', { | ||
userId: | ||
{ | ||
type: DataTypes.INTEGER, | ||
onDelete: 'CASCADE', | ||
allowNull: false, | ||
references: { | ||
model: 'Users', | ||
key: 'id' | ||
} | ||
}, | ||
articleSlug: { | ||
type: DataTypes.STRING, | ||
onDelete: 'CASCADE', | ||
allowNull: false, | ||
references: { | ||
model: 'Articles', | ||
key: 'slug' | ||
} | ||
} , | ||
sharedOn: { | ||
type: DataTypes.STRING, | ||
onDelete: 'CASCADE', | ||
allowNull: false | ||
} | ||
}, {}); | ||
Share.associate = ({ User, Article }) => { | ||
Share.belongsTo(User, { foreignKey: 'userId'}); | ||
Share.belongsTo(Article, { foreignKey: 'articleSlug'}); | ||
}; | ||
return Share; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
import open from 'open'; | ||
import dotenv from 'dotenv'; | ||
import env from '../configs/environments'; | ||
import models from '../api/models'; | ||
|
||
const { Article } = models; | ||
|
||
dotenv.config(); | ||
|
||
const minimumValue = 0; | ||
const { APP_URL_FRONTEND } = env; | ||
const facebookShareURL = `https://web.facebook.com/sharer/sharer.php?u=${APP_URL_FRONTEND}/api/articles/`; | ||
const twitterShareURL = `https://twitter.com/intent/tweet?text=${APP_URL_FRONTEND}/api/articles/`; | ||
const linkedinShareURL = `https://www.linkedin.com/sharing/share-offsite/?url=${APP_URL_FRONTEND}/api/articles/`; | ||
|
||
export default async (req, res, next) => { | ||
const { slug } = req.params; | ||
|
||
const { dataValues: { title } } = await Article.findOne({ where: { slug } }); | ||
req.title = title; | ||
|
||
if (req.url.search(/\/facebook/g) > minimumValue) { | ||
req.sharedOn = 'facebook'; | ||
await open(`${facebookShareURL}${slug}`, { wait: false }); | ||
} else if (req.url.search(/\/twitter/g) > minimumValue) { | ||
req.sharedOn = 'twitter'; | ||
await open(`${twitterShareURL}${slug}`, { wait: false }); | ||
} else if (req.url.search(/\/linkedin/g) > minimumValue) { | ||
req.sharedOn = 'linkedin'; | ||
await open(`${linkedinShareURL}${slug}`, { wait: false }); | ||
} else if (req.url.search(/\/gmail/g) > minimumValue) { | ||
req.sharedOn = 'gmail'; | ||
await open(`mailto:?subject=${title}&body=${APP_URL_FRONTEND}/articles/${slug}`, | ||
{ | ||
wait: false | ||
}); | ||
} | ||
|
||
next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import app from '../src/index'; | ||
import status from '../src/helpers/constants/status.codes'; | ||
import signupUser from '../src/helpers/tests/signup'; | ||
|
||
const { expect } = chai; | ||
chai.use(chaiHttp); | ||
|
||
const userToken = signupUser(); | ||
const slug = 'What-is-a-Version-1-UUID'; | ||
const title = 'What is a Version 1 UUID'; | ||
|
||
describe(' testing the social share endpoints', () => { | ||
it('should share via facebook', (done) => { | ||
chai | ||
.request(app) | ||
.post(`/api/articles/${slug}/share/facebook`) | ||
.set('Authorization', userToken) | ||
.end((err, res) => { | ||
expect(res.body).to.have.property('status').eql(status.CREATED); | ||
expect(res.body).to.have.property('message').eql(`${title} has been shared successfully on facebook, Thanks`); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should share via twitter', (done) => { | ||
chai | ||
.request(app) | ||
.post(`/api/articles/${slug}/share/twitter`) | ||
.set('Authorization', userToken) | ||
.end((err, res) => { | ||
expect(res.body).to.have.property('status').eql(status.CREATED); | ||
expect(res.body).to.have.property('message').eql(`${title} has been shared successfully on twitter, Thanks`); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should share via linkedin', (done) => { | ||
chai | ||
.request(app) | ||
.post(`/api/articles/${slug}/share/linkedin`) | ||
.set('Authorization', userToken) | ||
.end((err, res) => { | ||
expect(res.body).to.have.property('status').eql(status.CREATED); | ||
expect(res.body).to.have.property('message').eql(`${title} has been shared successfully on linkedin, Thanks`); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should share via gmail', (done) => { | ||
chai | ||
.request(app) | ||
.post(`/api/articles/${slug}/share/gmail`) | ||
.set('Authorization', userToken) | ||
.end((err, res) => { | ||
expect(res.body).to.have.property('status').eql(status.CREATED); | ||
expect(res.body).to.have.property('message').eql(`${title} has been shared successfully on gmail, Thanks`); | ||
done(); | ||
}); | ||
}); | ||
}); |