-
Notifications
You must be signed in to change notification settings - Fork 5
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
246 additions
and
11 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
42 changes: 42 additions & 0 deletions
42
src/migrations/20190421143711-create-article-read-history.js
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,42 @@ | ||
|
||
export default { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('article_read_histories', { | ||
id: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
primaryKey: true, | ||
defaultValue: Sequelize.UUIDV4 | ||
}, | ||
articleId: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
references: { | ||
model: 'articles', | ||
key: 'id', | ||
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE, | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
}, | ||
userId: { | ||
type: Sequelize.UUID, | ||
allowNull: false, | ||
references: { | ||
model: 'users', | ||
key: 'id', | ||
deferrable: Sequelize.Deferrable.INITIALLY_IMMEDIATE | ||
}, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE' | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('article_read_histories') | ||
}; |
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,46 @@ | ||
|
||
/** | ||
* @function | ||
* @param {sequelize} sequelize | ||
* @param {DataTypes} DataTypes | ||
* @returns {Model} Returns a database model | ||
*/ | ||
export default (sequelize, DataTypes) => { | ||
const ArticleReadHistory = sequelize.define( | ||
'ArticleReadHistory', | ||
{ | ||
id: { | ||
type: DataTypes.UUID, | ||
primaryKey: true, | ||
defaultValue: DataTypes.UUIDV4, | ||
}, | ||
|
||
articleId: { | ||
type: DataTypes.UUID, | ||
allowNull: true, | ||
}, | ||
|
||
userId: { | ||
type: DataTypes.UUID, | ||
allowNull: true, | ||
}, | ||
}, | ||
{ | ||
tableName: 'article_read_histories' | ||
} | ||
); | ||
ArticleReadHistory.associate = (models) => { | ||
const { Article, User } = models; | ||
ArticleReadHistory.belongsTo(Article, { | ||
foreignKey: 'articleId', | ||
as: 'article', | ||
onDelete: 'CASCADE', | ||
}); | ||
ArticleReadHistory.belongsTo(User, { | ||
foreignKey: 'userId', | ||
as: 'user', | ||
onDelete: 'CASCADE', | ||
}); | ||
}; | ||
return ArticleReadHistory; | ||
}; |
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,20 @@ | ||
export default { | ||
up: queryInterface => queryInterface.bulkInsert('article_read_histories', [ | ||
{ | ||
id: '979eaa2e-5b8f-4103-8192-4639afae2ba1', | ||
articleId: '979eaa2e-5b8f-4103-8192-4639afae2ba8', | ||
userId: '979eaa2e-5b8f-4103-8192-4639afae2ba9', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, | ||
|
||
{ | ||
id: '979eaa2e-5b8f-4103-8192-4639afae2ba2', | ||
articleId: '979eaa2e-5b8f-4103-8192-4639afae2ba4', | ||
userId: '979eaa2e-5b8f-4103-8192-4639afae2ba9', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
} | ||
], {}), | ||
down: queryInterface => queryInterface.bulkDelete('article_read_histories', null, {}) | ||
}; |
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,51 @@ | ||
import 'chai/register-should'; | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import { startServer } from '../../src/server'; | ||
import Auth from '../../src/middlewares/authenticator'; | ||
|
||
chai.use(chaiHttp); | ||
|
||
|
||
describe('User Reading Stats API Test', () => { | ||
let app, agent = null; | ||
|
||
beforeEach(async () => { | ||
app = await startServer(9999); | ||
agent = chai.request(app); | ||
}); | ||
|
||
it('should return the reading stats of a user', (done) => { | ||
agent | ||
.get('/api/v1/user-reading-stats') | ||
.set('Authorization', Auth.generateToken({ id: '979eaa2e-5b8f-4103-8192-4639afae2ba9' })) | ||
.end((err, res) => { | ||
if (err) return done(err); | ||
const { body } = res; | ||
res.should.have.status(200); | ||
body.status.should.equal('success'); | ||
body.data.should.have.property('articles'); | ||
body.data.should.have.property('totalArticleReadCount'); | ||
body.data.totalArticleReadCount.should.be.a('number'); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should return an error if no user is provided', (done) => { | ||
agent | ||
.get('/api/v1/user-reading-stats') | ||
.set('Authorization', Auth.generateToken({ id: '979eaa2e-5b8f-4103-8192-4639afae2ba30' })) | ||
.end((err, res) => { | ||
if (err) return done(err); | ||
const { body } = res; | ||
res.should.have.status(500); | ||
body.status.should.equal('error'); | ||
done(); | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
app = await app.close(); | ||
agent = null; | ||
}); | ||
}); |