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) {
// 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
......
......@@ -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('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;
......@@ -124,7 +124,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;
......@@ -133,6 +133,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();
});
......@@ -154,7 +155,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;
......@@ -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