From e39605640c8d0dfbdcee807674e4559f7fe725cc Mon Sep 17 00:00:00 2001
From: Vytor Calixto <vytorcalixto@gmail.com>
Date: Mon, 12 Sep 2016 11:41:28 -0300
Subject: [PATCH] Update census_year to min_year and max_year

---
 src/libs/routes/enrollment.js | 45 +++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 12 deletions(-)

diff --git a/src/libs/routes/enrollment.js b/src/libs/routes/enrollment.js
index facc6c9a..1b007395 100644
--- a/src/libs/routes/enrollment.js
+++ b/src/libs/routes/enrollment.js
@@ -14,8 +14,14 @@ const response = require(`${libs}/middlewares/response`);
 
 // **Temporary** solution to add where clauses that are common to all requests
 function filter(req, q) {
-    if (typeof req.census_year !== 'undefined') {
-        q.where('ano_censo=?', req.census_year);
+    if (typeof req.min_year !== 'undefined') {
+        if (typeof req.max_year === 'undefined') q.where('ano_censo=?', req.min_year);
+        else q.where('ano_censo>=?', req.min_year);
+    }
+
+    if (typeof req.max_year !== 'undefined') {
+        if (typeof req.min_year === 'undefined') q.where('ano_censo=?', req.max_year);
+        else q.where('ano_censo<=?', req.max_year);
     }
 
     if (typeof req.adm_dependency_id !== 'undefined') {
@@ -84,8 +90,13 @@ enrollmentApp.use('/', (req, res, next) => {
         req.paramCnt += 1;
     }
 
-    if (typeof params.census_year !== 'undefined') {
-        req.census_year = parseInt(params.census_year, 10);
+    if (typeof params.min_year !== 'undefined') {
+        req.min_year = parseInt(params.min_year, 10);
+        req.paramCnt += 1;
+    }
+
+    if (typeof params.max_year !== 'undefined') {
+        req.max_year = parseInt(params.max_year, 10);
         req.paramCnt += 1;
     }
 
@@ -101,14 +112,17 @@ enrollmentApp.use('/', (req, res, next) => {
     const params = req.query;
     if (typeof params.aggregate !== 'undefined' && params.aggregate === 'region') {
         log.debug('Using enrollments query for regions');
-        const q = squel.select().from('mat_regioes').field('name').field('SUM(total)', 'total');
+        const q = squel.select().from('mat_regioes')
+            .field('name')
+            .field('SUM(total)', 'total')
+            .field('ano_censo', 'year');
 
         filter(q);
 
         if (typeof req.id !== 'undefined') {
             q.where('pk_regiao_id=?', req.id);
         }
-        req.sql = q.group('name').toParam();
+        req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam();
     }
     next();
 });
@@ -117,14 +131,17 @@ enrollmentApp.use('/', (req, res, next) => {
     const params = req.query;
     if (typeof params.aggregate !== 'undefined' && params.aggregate === 'state') {
         log.debug('Using enrollments query for states');
-        const q = squel.select().from('mat_estados').field('name').field('SUM(total)', 'total');
+        const q = squel.select().from('mat_estados')
+            .field('name')
+            .field('SUM(total)', 'total')
+            .field('ano_censo', 'year');
 
         filter(req, q);
 
         if (typeof req.id !== 'undefined') {
             q.where('pk_estado_id=?', req.id);
         }
-        req.sql = q.group('name').toParam();
+        req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam();
     }
     next();
 });
@@ -133,14 +150,17 @@ enrollmentApp.use('/', (req, res, next) => {
     const params = req.query;
     if (typeof params.aggregate !== 'undefined' && params.aggregate === 'city') {
         log.debug('Using enrollments query for cities');
-        const q = squel.select().from('mat_estados').field('name').field('SUM(total)', 'total');
+        const q = squel.select().from('mat_municipios')
+            .field('name')
+            .field('SUM(total)', 'total')
+            .field('ano_censo', 'year');
 
         filter(req, q);
 
         if (typeof req.id !== 'undefined') {
             q.where('pk_municipio_id=?', req.id);
         }
-        req.sql = q.group('name').toParam();
+        req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam();
     }
     next();
 });
@@ -150,11 +170,12 @@ enrollmentApp.use('/', (req, res, next) => {
     if (typeof params.aggregate === 'undefined') {
         log.debug('Using enrollments query for the whole country');
         const q = squel.select().from('turmas').field("'Brasil'", 'name')
-            .field('COALESCE(SUM(num_matriculas),0)', 'total');
+            .field('COALESCE(SUM(num_matriculas),0)', 'total')
+            .field('ano_censo', 'year');
 
         filter(req, q);
 
-        req.sql = q.toParam();
+        req.sql = q.group('ano_censo').order('ano_censo').toParam();
     }
     next();
 });
-- 
GitLab