-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into ft-rating-pagination-167313406
- Loading branch information
Showing
12 changed files
with
226 additions
and
4 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,15 @@ | ||
/* eslint-disable require-jsdoc */ | ||
import StatsService from '../services/db.service'; | ||
import { success } from '../helpers/responses'; | ||
|
||
const { getStat } = StatsService; | ||
|
||
class statsController { | ||
static async getStats(req, res) { | ||
const readerId = req.auth.id; | ||
const stats = await getStat({ readerId }, 'Stats'); | ||
return success('your reading stats', stats).send(res); | ||
} | ||
} | ||
|
||
export default statsController; |
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,18 @@ | ||
import Util from './util'; | ||
|
||
const util = new Util(); | ||
|
||
export const notFound = (msg) => { | ||
util.setError(404, `${msg} not found`); | ||
return util; | ||
}; | ||
|
||
export const error = (msg, code = 400) => { | ||
util.setError(code, msg); | ||
return util; | ||
}; | ||
|
||
export const success = (msg, data = null) => { | ||
util.setSuccess(200, msg, data); | ||
return util; | ||
}; |
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,37 @@ | ||
/* eslint-disable require-jsdoc */ | ||
|
||
import jwt from 'jsonwebtoken'; | ||
|
||
import StatsService from '../services/db.service'; | ||
import { notFound, error } from '../helpers/responses'; | ||
|
||
const { getStat } = StatsService; | ||
|
||
class statsWare { | ||
static async checkStats(req, res, next) { | ||
if (!req.auth) { | ||
return error('you are not logged in').send(res); | ||
} | ||
const readerId = req.auth.id; | ||
const stats = await getStat({ readerId }, 'Stats'); | ||
if (!stats.length) { | ||
return notFound('reading stats').send(res); | ||
} | ||
next(); | ||
} | ||
|
||
static async saveStat(req, res, next) { | ||
try { | ||
let token = req.headers['x-access-token'] || req.headers.authorization; | ||
token = token.slice(7, token.length); | ||
jwt.verify(token, process.env.SECRET_KEY, (err, decode) => { | ||
req.auth = decode; | ||
next(); | ||
}); | ||
} catch (err) { | ||
next(); | ||
} | ||
} | ||
} | ||
|
||
export default statsWare; |
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 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Stats', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
slug: { | ||
type: Sequelize.STRING | ||
}, | ||
item: { | ||
type: Sequelize.STRING | ||
}, | ||
readerId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('Stats'); | ||
} | ||
}; |
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,12 @@ | ||
'use strict'; | ||
module.exports = (sequelize, DataTypes) => { | ||
const Stats = sequelize.define('Stats', { | ||
slug: DataTypes.STRING, | ||
item: DataTypes.STRING, | ||
readerId: DataTypes.INTEGER | ||
}, {}); | ||
Stats.associate = function (models) { | ||
// associations can be defined here | ||
} | ||
return Stats; | ||
}; |
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
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,18 @@ | ||
/* eslint-disable require-jsdoc */ | ||
import models from '../models'; | ||
|
||
const { Stats, Article } = models; | ||
const Models = { Stats, Article }; | ||
const conditon = where => ({ where }); | ||
|
||
class StatsService { | ||
static async createStat(where, model) { | ||
return Models[model].create(where); | ||
} | ||
|
||
static async getStat(where, model) { | ||
return Models[model].findAll(conditon(where)); | ||
} | ||
} | ||
|
||
export default StatsService; |
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,67 @@ | ||
import { chai, expect, server } from './test-setup'; | ||
|
||
let token; | ||
describe('Test user stats', () => { | ||
before((done) => { | ||
chai.request(server) | ||
.post('/api/v1/users/login') | ||
.send({ email: 'admin@gmail.com', password: 'ASqw12345' }) | ||
.end((error, res) => { | ||
token = `Bearer ${res.body.token}`; | ||
done(); | ||
}); | ||
}); | ||
describe('test getting stats', () => { | ||
it('should return correct response if none', (done) => { | ||
chai.request(server) | ||
.get('/api/v1/users/stats') | ||
.set('Authorization', token) | ||
.end((err, res) => { | ||
expect(res.status).to.be.equal(404); | ||
expect(res.body.message).to.include('not found'); | ||
done(); | ||
}); | ||
}); | ||
it('should return correct response if user not logged in', (done) => { | ||
chai.request(server) | ||
.get('/api/v1/users/stats') | ||
.end((err, res) => { | ||
expect(res.status).to.be.equal(400); | ||
expect(res.body.message).to.include('not logged in'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('test stats gathering', () => { | ||
it('should not populate stats if user unauthenticated', (done) => { | ||
chai.request(server) | ||
.get('/api/v1/articles/fakeslug2') | ||
.end(() => { | ||
chai.request(server) | ||
.get('/api/v1/users/stats') | ||
.set('Authorization', token) | ||
.end((err, res) => { | ||
expect(res.status).to.be.equal(404); | ||
expect(res.body.message).to.include('not found'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('should log every read', (done) => { | ||
chai.request(server) | ||
.get('/api/v1/articles/fakeslug2') | ||
.set('Authorization', token) | ||
.end(() => { | ||
chai.request(server) | ||
.get('/api/v1/users/stats') | ||
.set('Authorization', token) | ||
.end((err, res) => { | ||
expect(res.status).to.be.equal(200); | ||
expect(res.body).to.have.deep.property('message', 'your reading stats'); | ||
expect(res.body.data).to.be.a('Array'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |