diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bc0c1575b968932e3a14cb588c029cf1cd795f4..9fd4af3db07b78bb5fe5b03f50e8cc84af1ad67a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased +- Change parseParams default value from `null` to `true` +- Query middleware throws a 404 when the result is empty +- Change filters and dimensions names! No more `_id`. **Breaks compability** +- Add a basic sanitize function ## 0.1.0 - 2016-10-10 ### Added diff --git a/gulpfile.babel.js b/gulpfile.babel.js index b2758b0a6abe55329d30fb9658e0547e5fcb150d..f9436ee9077a570858e2368e75c2c0ed370d0022 100644 --- a/gulpfile.babel.js +++ b/gulpfile.babel.js @@ -74,11 +74,11 @@ gulp.task('pre-test', () => { gulp.task('test', ['pre-test'], () => { process.chdir('build'); gulp.src('test/test.js', {read: false}) - .pipe(mocha()) + .pipe(mocha({timeout: 15000})) .pipe(istanbul.writeReports()) .pipe(istanbul.enforceThresholds({ thresholds: { - global: 80 + global: 80 } })) .on('error', () => { diff --git a/src/libs/app.js b/src/libs/app.js index fa685039b525ff430eca6841908666b40d8b0300..18b46cf3f195879ce25d1edf2bcaa39a08afb6f3 100644 --- a/src/libs/app.js +++ b/src/libs/app.js @@ -42,7 +42,7 @@ app.use('/api/v1', api); // Catch 404 and forward to error handler app.use((req, res, next) => { res.status(404); - log.debug('%s %d %s', req.method, res.statusCode, req.url); + log.error('%s %d %s', req.method, res.statusCode, req.url); res.json({ error: 'Not found' }).end(); }); diff --git a/src/libs/middlewares/parseParams.js b/src/libs/middlewares/parseParams.js index c74b6b491bc445b22cd480c85d9f232494a139de..36361f274b13ebeaeff709bc2b48e4869682a47c 100644 --- a/src/libs/middlewares/parseParams.js +++ b/src/libs/middlewares/parseParams.js @@ -36,7 +36,7 @@ function parseParams(queryParam, arr) { // Get the key and the value - state:41 is key 'state' whith value 41 const kv = param.split(':'); // Check if there is a value. If there isn't, assign null - obj[kv[0]] = (typeof kv[1] === 'undefined') ? null : kv[1]; + obj[kv[0]] = (typeof kv[1] === 'undefined') ? true : kv[1]; } // If the array exists and is not empty we intersect diff --git a/src/libs/middlewares/query.js b/src/libs/middlewares/query.js index a4f20e3ba5bdd31cf7dab85a44121b8e8e456fc0..70a9aab689b5bf1c3827af59b09594d7a3ab787a 100644 --- a/src/libs/middlewares/query.js +++ b/src/libs/middlewares/query.js @@ -9,6 +9,7 @@ function query(req, res, next) { execQuery(sql.text, sql.values).then((result) => { log.debug(result); req.result = result; + if(result.length === 0) next({status: 404, message: 'Not Found'}); next(); }, (error) => { next(error); diff --git a/src/libs/routes/enrollment.js b/src/libs/routes/enrollment.js index 9fe6e903bdbe9bc10b89181866a920792b832533..446d7aca2fd0a3b10d43894f81e3fea7c0365e2d 100644 --- a/src/libs/routes/enrollment.js +++ b/src/libs/routes/enrollment.js @@ -52,17 +52,17 @@ enrollmentApp.get('/adm_dependency', (req, res, next) => { enrollmentApp.use('/', parseParams('filter', [ 'min_year', 'max_year', - 'adm_dependency_id', - 'location_id', - 'education_level_id', + 'adm_dependency', + 'location', + 'education_level', 'region', 'state', 'city', 'school' ]), parseParams('dims', [ - 'adm_dependency_id', - 'location_id', - 'education_level_id', + 'adm_dependency', + 'location', + 'education_level', 'region', 'state', 'city', @@ -72,13 +72,13 @@ enrollmentApp.use('/', parseParams('filter', [ log.debug(req.dims); // Do the joins - if(typeof req.filter.adm_dependency_id !== 'undefined' - || typeof req.dims.adm_dependency_id !== 'undefined') { + if(typeof req.filter.adm_dependency !== 'undefined' + || typeof req.dims.adm_dependency !== 'undefined') { req.sql.join('dependencia_adms', null, 'fk_dependencia_adm_id=dependencia_adms.pk_dependencia_adm_id'); } - if(typeof req.filter.education_level_id !== 'undefined' - || typeof req.dims.education_level_id !== 'undefined') { + if(typeof req.filter.education_level !== 'undefined' + || typeof req.dims.education_level !== 'undefined') { req.sql.join('etapa_ensino', null, 'fk_etapa_ensino_id=etapa_ensino.pk_etapa_ensino_id'); } @@ -112,7 +112,7 @@ enrollmentApp.use('/', parseParams('filter', [ // Dimensions (add fields) - if(typeof req.dims.education_level_id !== 'undefined') { + if(typeof req.dims.education_level !== 'undefined') { req.sql.field('desc_etapa', 'education_level') .group('desc_etapa') .order('desc_etapa'); @@ -142,14 +142,14 @@ enrollmentApp.use('/', parseParams('filter', [ .order('escolas.nome_entidade'); } - if(typeof req.dims.adm_dependency_id !== 'undefined') { + if(typeof req.dims.adm_dependency !== 'undefined') { req.sql.field('dependencia_adms.nome', 'adm_dependency_name') .group('dependencia_adms.nome') .order('dependencia_adms.nome'); } - if(typeof req.dims.location_id !== 'undefined') { - req.sql.field('turmas.id_localizacao', 'location') + if(typeof req.dims.location !== 'undefined') { + req.sql.field('turmas.id_localizacao', 'location_name') .group('turmas.id_localizacao') .order('turmas.id_localizacao'); } @@ -171,16 +171,16 @@ enrollmentApp.use('/', parseParams('filter', [ req.sql.where('turmas.ano_censo<=?', parseInt(req.filter.max_year, 10)); } - if (typeof req.filter.adm_dependency_id !== 'undefined') { - req.sql.where('pk_dependencia_adm_id=?', parseInt(req.filter.adm_dependency_id, 10)); + if (typeof req.filter.adm_dependency !== 'undefined') { + req.sql.where('pk_dependencia_adm_id=?', parseInt(req.filter.adm_dependency, 10)); } - if (typeof req.filter.location_id !== 'undefined') { - req.sql.where('turmas.id_localizacao=?', parseInt(req.filter.location_id, 10)); + if (typeof req.filter.location !== 'undefined') { + req.sql.where('turmas.id_localizacao=?', parseInt(req.filter.location, 10)); } - if (typeof req.filter.education_level_id !== 'undefined') { - req.sql.where('pk_etapa_ensino_id=?', parseInt(req.filter.education_level_id, 10)); + if (typeof req.filter.education_level !== 'undefined') { + req.sql.where('pk_etapa_ensino_id=?', parseInt(req.filter.education_level, 10)); } if (typeof req.filter.region !== 'undefined') { @@ -209,6 +209,19 @@ enrollmentApp.get('/', (req, res, next) => { .group('turmas.ano_censo') .order('turmas.ano_censo'); next(); -}, query, response('test')); +}, query, (req, res, next) => { + // 'Sanitize' result + let r = req.result; + r.forEach((data) => { + if(req.dims.location) { + if(data.location_name === 1) { + data.location_name = 'Urbana'; + } else { + data.location_name = 'Rural'; + } + } + }); + next(); +}, response('enrollment')); module.exports = enrollmentApp; diff --git a/src/test/test.js b/src/test/test.js index 5529e056fa9303f731a416d2845b096f434a28a8..58a06c165b96fca90baffe8b0bd297e9d2e19844 100644 --- a/src/test/test.js +++ b/src/test/test.js @@ -127,7 +127,7 @@ describe('request enrollments', () => { it('should list enrollments with valid dimensions', (done) => { chai.request(server) - .get('/api/v1/enrollment?dims=region,state,adm_dependency_id,location_id&filter=min_year:2014,region:4') + .get('/api/v1/enrollment?dims=region,state,adm_dependency,location&filter=min_year:2014,region:4') .end((err, res) => { res.should.have.status(200); res.should.be.json; @@ -136,6 +136,7 @@ describe('request enrollments', () => { res.body.result[0].should.have.property('region_name'); res.body.result[0].should.have.property('state_name'); res.body.result[0].should.have.property('adm_dependency_name'); + res.body.result[0].should.have.property('location_name'); res.body.result[0].should.have.property('total'); done(); }); @@ -157,7 +158,7 @@ describe('request enrollments', () => { it('should list enrollments with valid dimensions and filters', (done) => { chai.request(server) - .get('/api/v1/enrollment?dims=region,state,education_level_id,school&filter=min_year:2013,max_year:2014,city:3287') + .get('/api/v1/enrollment?dims=region,state,education_level,school&filter=min_year:2013,max_year:2014,city:3287') .end((err, res) => { res.should.have.status(200); res.should.be.json; @@ -173,6 +174,26 @@ describe('request enrollments', () => { }); }); + it('should list enrollments using all dimensions and filters', (done) => { + chai.request(server) + .get('/api/v1/enrollment?dims=region,state,city,education_level,school,adm_dependency,location&filter=min_year:2013,max_year:2014,city:3287,adm_dependency:3,location:1,education_level:99') + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('result'); + res.body.result.should.be.a('array'); + res.body.result[0].should.have.property('region_name'); + res.body.result[0].should.have.property('state_name'); + res.body.result[0].should.have.property('school_name'); + res.body.result[0].should.have.property('education_level'); + res.body.result[0].should.have.property('location_name'); + res.body.result[0].should.have.property('adm_dependency_name'); + res.body.result[0].should.have.property('total'); + res.body.result[0].should.have.property('year'); + done(); + }); + }); + });