From a4b024d2da2d0ac0ce073f0f9ee1ca92f1931cd8 Mon Sep 17 00:00:00 2001
From: Vytor Calixto <vytorcalixto@gmail.com>
Date: Fri, 14 Oct 2016 16:06:44 -0300
Subject: [PATCH] Add sanitize function in enrollment

Sanitize the location in the result
---
 src/libs/middlewares/parseParams.js |  2 +-
 src/libs/routes/enrollment.js       | 55 ++++++++++++++++++-----------
 src/test/test.js                    | 25 +++++++++++--
 3 files changed, 58 insertions(+), 24 deletions(-)

diff --git a/src/libs/middlewares/parseParams.js b/src/libs/middlewares/parseParams.js
index c74b6b49..36361f27 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/routes/enrollment.js b/src/libs/routes/enrollment.js
index f0c81a55..446d7aca 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('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;
diff --git a/src/test/test.js b/src/test/test.js
index 066b7b77..110be7d6 100644
--- a/src/test/test.js
+++ b/src/test/test.js
@@ -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();
+            });
+    });
+
 
 });
 
-- 
GitLab