From 4f9fd4f4f0c0910ce3ae2b7119819403205d5c34 Mon Sep 17 00:00:00 2001
From: Fernando Erd <fce15@inf.ufpr.br>
Date: Fri, 18 May 2018 10:05:58 -0300
Subject: [PATCH] Change default query

---
 src/libs/routes/transport.js | 96 +++++++++++++++++++++---------------
 1 file changed, 57 insertions(+), 39 deletions(-)

diff --git a/src/libs/routes/transport.js b/src/libs/routes/transport.js
index 0d73d306..f8ec7633 100644
--- a/src/libs/routes/transport.js
+++ b/src/libs/routes/transport.js
@@ -14,6 +14,8 @@ const multiQuery = require(`${libs}/middlewares/multiQuery`);
 
 const response = require(`${libs}/middlewares/response`);
 
+const addMissing = require(`${libs}/middlewares/addMissing`);
+
 const id2str = require(`${libs}/middlewares/id2str`);
 
 const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`);
@@ -110,6 +112,38 @@ rqf.addField({
     name: 'dims',
     field: true,
     where: false
+}).addValue({
+    name: 'city',
+    table: 'municipio',
+    tableField: 'nome',
+    resultField: 'city_name',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'municipio_id',
+        table: 'matricula'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'municipio_id',
+        foreignTable: 'matricula'
+    }
+}).addValue({
+    name: 'state',
+    table: 'estado',
+    tableField: 'nome',
+    resultField: 'state_name',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'estado_id',
+        table: 'matricula'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'estado_id',
+        foreignTable: 'matricula'
+    }
 }).addValue({
     name: 'rural_location',
     table: 'matricula',
@@ -240,42 +274,27 @@ transportApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
     .field("'Brasil'", 'name')
     .field('matricula.ano_censo', 'year')
     .from('matricula')
-    .join('turma', null, 'matricula.turma_id = turma.id AND matricula.ano_censo = turma.ano_censo')
     .group('matricula.ano_censo')
     .order('matricula.ano_censo')
-    .where('turma.tipo_turma_id <= 3');
+    .where('matricula.tipo <= 3 AND matricula.transporte_escolar_publico');
     req.queryIndex.allTransports = req.querySet.push(allTransports) - 1;
 
-
-    // Vans e Kombi
-    let allVansAndKombi = allTransports.clone();
-    allVansAndKombi.where('matricula.transporte_vans_kombi = 1');
-    req.queryIndex.allVansAndKombi = req.querySet.push(allVansAndKombi) - 1;
-
     // Vans e Kombi
     let goVansAndKombi = allTransports.clone();
-    goVansAndKombi.where('matricula.transporte_vans_kombi = 1 OR matricula.transporte_vans_kombi = 0');
+    goVansAndKombi.where('matricula.transporte_vans_kombi = 1');
     req.queryIndex.goVansAndKombi = req.querySet.push(goVansAndKombi) - 1;
 
-    // Micro-ônibus
-    let allMicroBus = allTransports.clone();
-    allMicroBus.where('matricula.transporte_micro_onibus = 1 OR matricula.transporte_micro_onibus = 0');
-    req.queryIndex.allMicroBus = req.querySet.push(allMicroBus) - 1;
-
     // Micro
     let goMicroBus = allTransports.clone();
     goMicroBus.where('matricula.transporte_micro_onibus = 1');
     req.queryIndex.goMicroBus = req.querySet.push(goMicroBus) - 1;
 
-    // Ônibus
-    let allBus = allTransports.clone();
-    allBus.where('matricula.transporte_onibus = 1 OR matricula.transporte_onibus = 0');
-    req.queryIndex.allBus = req.querySet.push(allBus) - 1;
 
+    // Ônibus
     let goBus = allTransports.clone();
     goBus.where('matricula.transporte_onibus = 1');
     req.queryIndex.goBus = req.querySet.push(goBus) - 1;
-    //
+
     // Bicicleta
     let goBikes = allTransports.clone();
     goBikes.where('matricula.transporte_bicicleta = 1');
@@ -296,6 +315,7 @@ transportApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
     goWaterway_5_Students.where('matricula.transporte_embar_0_5 = 1');
     req.queryIndex.goWaterway_5_Students = req.querySet.push(goWaterway_5_Students) - 1;
 
+
     // Aquaviário/ Embarcação (capacidade de 5 até 15 alunos)
     let goWaterway_15_Students = allTransports.clone();
     goWaterway_15_Students.where('matricula.transporte_embar_5_15 = 1');
@@ -317,23 +337,23 @@ transportApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
     req.queryIndex.goSubwayAndTrain = req.querySet.push(goSubwayAndTrain) - 1;
 
     next();
-  }, multiQuery, (req, res, next) => {
-    // let van_and_kombi = matchQueries(req.result[req.queryIndex.goVansAndKombi], req.result[req.queryIndex.allVansAndKombi]);
-    // let microBus = matchQueries(req.result[req.queryIndex.goMicroBus], req.result[req.queryIndex.allMicroBus]);
-    // let Bus = matchQueries(req.result[req.queryIndex.goBus], req.result[req.queryIndex.allBus]);
-    let van_and_kombi = req.result[req.queryIndex.goVansAndKombi]
-    let micro_bus = req.result[req.queryIndex.goMicroBus]
-    let Bus = req.result[req.queryIndex.goBus]
-    let bike = req.result[req.queryIndex.goBikes]
-    let animal_traction = req.result[req.queryIndex.goAnimalTraction]
-    let other_vehicle = req.result[req.queryIndex.goOtherVehicle]
-    let waterway_5_Students = req.result[req.queryIndex.goWaterway_5_Students]
-    let waterway_10_Students = req.result[req.queryIndex.goWaterway_10_Students]
-    let waterway_15_Students = req.result[req.queryIndex.goWaterway_15_Students]
-    let waterway_35_Students = req.result[req.queryIndex.goWaterway_35_Students]
-    let waterway_More_Than_35 = req.result[req.queryIndex.goWaterwayMoreThan_35]
-    let subway_and_train = req.result[req.queryIndex.goSubwayAndTrain]
+}, multiQuery, (req, res, next) => {
+
+    let public_transport = req.result[req.queryIndex.allTransports];
+    let van_and_kombi = req.result[req.queryIndex.goVansAndKombi];
+    let micro_bus = req.result[req.queryIndex.goMicroBus];
+    let Bus = req.result[req.queryIndex.goBus];
+    let bike = req.result[req.queryIndex.goBikes];
+    let animal_traction = req.result[req.queryIndex.goAnimalTraction];
+    let other_vehicle = req.result[req.queryIndex.goOtherVehicle];
+    let waterway_5_Students = req.result[req.queryIndex.goWaterway_5_Students];
+    let waterway_15_Students = req.result[req.queryIndex.goWaterway_15_Students];
+    let waterway_35_Students = req.result[req.queryIndex.goWaterway_35_Students];
+    let waterway_More_Than_35 = req.result[req.queryIndex.goWaterwayMoreThan_35];
+    let subway_and_train = req.result[req.queryIndex.goSubwayAndTrain];
+
     req.result = [{
+          public_transport,
           van_and_kombi,
           micro_bus,
           Bus,
@@ -341,14 +361,12 @@ transportApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
           animal_traction,
           other_vehicle,
           waterway_5_Students,
-          waterway_10_Students,
           waterway_15_Students,
           waterway_35_Students,
           waterway_More_Than_35,
           subway_and_train
-
-    }]
+    }];
     next();
-  }, response('transports'));
+}, id2str.multitransform(false), response('transports'));
 
 module.exports = transportApp;
-- 
GitLab