Skip to content
Snippets Groups Projects
Commit a4b024d2 authored by Vytor Calixto's avatar Vytor Calixto :space_invader:
Browse files

Add sanitize function in enrollment

Sanitize the location in the result
parent 55b92b7b
No related branches found
No related tags found
1 merge request!116Release v1.0.0
Pipeline #
...@@ -36,7 +36,7 @@ function parseParams(queryParam, arr) { ...@@ -36,7 +36,7 @@ function parseParams(queryParam, arr) {
// Get the key and the value - state:41 is key 'state' whith value 41 // Get the key and the value - state:41 is key 'state' whith value 41
const kv = param.split(':'); const kv = param.split(':');
// Check if there is a value. If there isn't, assign null // 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 // If the array exists and is not empty we intersect
......
...@@ -52,17 +52,17 @@ enrollmentApp.get('/adm_dependency', (req, res, next) => { ...@@ -52,17 +52,17 @@ enrollmentApp.get('/adm_dependency', (req, res, next) => {
enrollmentApp.use('/', parseParams('filter', [ enrollmentApp.use('/', parseParams('filter', [
'min_year', 'min_year',
'max_year', 'max_year',
'adm_dependency_id', 'adm_dependency',
'location_id', 'location',
'education_level_id', 'education_level',
'region', 'region',
'state', 'state',
'city', 'city',
'school' 'school'
]), parseParams('dims', [ ]), parseParams('dims', [
'adm_dependency_id', 'adm_dependency',
'location_id', 'location',
'education_level_id', 'education_level',
'region', 'region',
'state', 'state',
'city', 'city',
...@@ -72,13 +72,13 @@ enrollmentApp.use('/', parseParams('filter', [ ...@@ -72,13 +72,13 @@ enrollmentApp.use('/', parseParams('filter', [
log.debug(req.dims); log.debug(req.dims);
// Do the joins // Do the joins
if(typeof req.filter.adm_dependency_id !== 'undefined' if(typeof req.filter.adm_dependency !== 'undefined'
|| typeof req.dims.adm_dependency_id !== 'undefined') { || typeof req.dims.adm_dependency !== 'undefined') {
req.sql.join('dependencia_adms', null, 'fk_dependencia_adm_id=dependencia_adms.pk_dependencia_adm_id'); req.sql.join('dependencia_adms', null, 'fk_dependencia_adm_id=dependencia_adms.pk_dependencia_adm_id');
} }
if(typeof req.filter.education_level_id !== 'undefined' if(typeof req.filter.education_level !== 'undefined'
|| typeof req.dims.education_level_id !== 'undefined') { || typeof req.dims.education_level !== 'undefined') {
req.sql.join('etapa_ensino', null, 'fk_etapa_ensino_id=etapa_ensino.pk_etapa_ensino_id'); req.sql.join('etapa_ensino', null, 'fk_etapa_ensino_id=etapa_ensino.pk_etapa_ensino_id');
} }
...@@ -112,7 +112,7 @@ enrollmentApp.use('/', parseParams('filter', [ ...@@ -112,7 +112,7 @@ enrollmentApp.use('/', parseParams('filter', [
// Dimensions (add fields) // 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') req.sql.field('desc_etapa', 'education_level')
.group('desc_etapa') .group('desc_etapa')
.order('desc_etapa'); .order('desc_etapa');
...@@ -142,14 +142,14 @@ enrollmentApp.use('/', parseParams('filter', [ ...@@ -142,14 +142,14 @@ enrollmentApp.use('/', parseParams('filter', [
.order('escolas.nome_entidade'); .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') req.sql.field('dependencia_adms.nome', 'adm_dependency_name')
.group('dependencia_adms.nome') .group('dependencia_adms.nome')
.order('dependencia_adms.nome'); .order('dependencia_adms.nome');
} }
if(typeof req.dims.location_id !== 'undefined') { if(typeof req.dims.location !== 'undefined') {
req.sql.field('turmas.id_localizacao', 'location') req.sql.field('turmas.id_localizacao', 'location_name')
.group('turmas.id_localizacao') .group('turmas.id_localizacao')
.order('turmas.id_localizacao'); .order('turmas.id_localizacao');
} }
...@@ -171,16 +171,16 @@ enrollmentApp.use('/', parseParams('filter', [ ...@@ -171,16 +171,16 @@ enrollmentApp.use('/', parseParams('filter', [
req.sql.where('turmas.ano_censo<=?', parseInt(req.filter.max_year, 10)); req.sql.where('turmas.ano_censo<=?', parseInt(req.filter.max_year, 10));
} }
if (typeof req.filter.adm_dependency_id !== 'undefined') { if (typeof req.filter.adm_dependency !== 'undefined') {
req.sql.where('pk_dependencia_adm_id=?', parseInt(req.filter.adm_dependency_id, 10)); req.sql.where('pk_dependencia_adm_id=?', parseInt(req.filter.adm_dependency, 10));
} }
if (typeof req.filter.location_id !== 'undefined') { if (typeof req.filter.location !== 'undefined') {
req.sql.where('turmas.id_localizacao=?', parseInt(req.filter.location_id, 10)); req.sql.where('turmas.id_localizacao=?', parseInt(req.filter.location, 10));
} }
if (typeof req.filter.education_level_id !== 'undefined') { if (typeof req.filter.education_level !== 'undefined') {
req.sql.where('pk_etapa_ensino_id=?', parseInt(req.filter.education_level_id, 10)); req.sql.where('pk_etapa_ensino_id=?', parseInt(req.filter.education_level, 10));
} }
if (typeof req.filter.region !== 'undefined') { if (typeof req.filter.region !== 'undefined') {
...@@ -209,6 +209,19 @@ enrollmentApp.get('/', (req, res, next) => { ...@@ -209,6 +209,19 @@ enrollmentApp.get('/', (req, res, next) => {
.group('turmas.ano_censo') .group('turmas.ano_censo')
.order('turmas.ano_censo'); .order('turmas.ano_censo');
next(); next();
}, query, response('enrollment')); }, 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; module.exports = enrollmentApp;
...@@ -124,7 +124,7 @@ describe('request enrollments', () => { ...@@ -124,7 +124,7 @@ describe('request enrollments', () => {
it('should list enrollments with valid dimensions', (done) => { it('should list enrollments with valid dimensions', (done) => {
chai.request(server) 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) => { .end((err, res) => {
res.should.have.status(200); res.should.have.status(200);
res.should.be.json; res.should.be.json;
...@@ -133,6 +133,7 @@ describe('request enrollments', () => { ...@@ -133,6 +133,7 @@ describe('request enrollments', () => {
res.body.result[0].should.have.property('region_name'); 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('state_name');
res.body.result[0].should.have.property('adm_dependency_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'); res.body.result[0].should.have.property('total');
done(); done();
}); });
...@@ -154,7 +155,7 @@ describe('request enrollments', () => { ...@@ -154,7 +155,7 @@ describe('request enrollments', () => {
it('should list enrollments with valid dimensions and filters', (done) => { it('should list enrollments with valid dimensions and filters', (done) => {
chai.request(server) 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) => { .end((err, res) => {
res.should.have.status(200); res.should.have.status(200);
res.should.be.json; res.should.be.json;
...@@ -170,6 +171,26 @@ describe('request enrollments', () => { ...@@ -170,6 +171,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();
});
});
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment