From ca4dc04f0b0853ba8a2410a34a6a792c1eb6447c Mon Sep 17 00:00:00 2001
From: Glenda <gpt16@inf.ufpr.br>
Date: Mon, 25 Jun 2018 10:37:18 -0300
Subject: [PATCH] Add auxiliar indicator

---
 src/libs/routes/api.js      |   3 +
 src/libs/routes/auxiliar.js | 334 ++++++++++++++++++++++++++++++++++++
 2 files changed, 337 insertions(+)
 create mode 100644 src/libs/routes/auxiliar.js

diff --git a/src/libs/routes/api.js b/src/libs/routes/api.js
index 70311ae0..0a1b047f 100644
--- a/src/libs/routes/api.js
+++ b/src/libs/routes/api.js
@@ -66,6 +66,8 @@ const classroomCount = require(`${libs}/routes/classroomCount`);
 
 const transport = require(`./transport`);
 
+const auxiliar = require(`${libs}/routes/auxiliar`);
+
 const dailyChargeAmount = require(`${libs}/routes/dailyChargeAmount`);
 
 api.get('/', (req, res) => {
@@ -104,5 +106,6 @@ api.use('/out_of_school', outOfSchool);
 api.use('/classroom_count', classroomCount);
 api.use('/daily_charge_amount', dailyChargeAmount);
 api.use('/transport', transport);
+api.use('/auxiliar', auxiliar);
 
 module.exports = api;
diff --git a/src/libs/routes/auxiliar.js b/src/libs/routes/auxiliar.js
new file mode 100644
index 00000000..7136eb5c
--- /dev/null
+++ b/src/libs/routes/auxiliar.js
@@ -0,0 +1,334 @@
+const express = require('express');
+
+const auxiliarApp = express.Router();
+
+const libs = `${process.cwd()}/libs`;
+
+const log = require(`${libs}/log`)(module);
+
+const squel = require('squel');
+
+const query = require(`${libs}/middlewares/query`).query;
+
+const response = require(`${libs}/middlewares/response`);
+
+const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`);
+
+const id2str = require(`${libs}/middlewares/id2str`);
+
+const config = require(`${libs}/config`);
+
+const passport = require('passport');
+
+const download = require(`${libs}/middlewares/downloadDatabase`);
+
+const addMissing = require(`${libs}/middlewares/addMissing`);
+
+const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]}  }).middleware;
+
+let rqf = new ReqQueryFields();
+
+auxiliarApp.use(cache('15 day'));
+
+auxiliarApp.get('/year_range', (req, res, next) => {
+    req.sql.from('docente')
+    .field('MIN(docente.ano_censo)', 'start_year')
+    .field('MAX(docente.ano_censo)', 'end_year');
+    next();
+}, query, response('range'));
+
+auxiliarApp.get('/years', (req, res, next) => {
+    req.sql.from('docente').
+    field('DISTINCT docente.ano_censo', 'year');
+    next();
+}, query, response('years'));
+
+auxiliarApp.get('/source', (req, res, next) => {
+    req.sql.from('fonte')
+    .field('fonte', 'source')
+    .where('tabela = \'docente\'');
+    next();
+}, query, response('source'))
+
+// Dependência administrativa
+auxiliarApp.get('/adm_dependency_detailed', (req, res, next) => {
+    req.result = [];
+    for(let i = 1; i <= 6; ++i) {
+        req.result.push({
+            id: i,
+            name: id2str.admDependencyPriv(i)
+        });
+    };
+    next();
+}, response('adm_dependency_detailed'));
+
+// Dependência administrativa com detalhamento das escolas privadas
+auxiliarApp.get('/adm_dependency', (req, res, next) => {
+    req.result = [];
+    for(let i = 1; i <= 4; ++i) {
+        req.result.push({
+            id: i,
+            name: id2str.admDependency(i)
+        });
+    };
+    next();
+}, response('adm_dependency'));
+
+// Área da localidade
+auxiliarApp.get('/location', (req, res, next) => {
+    req.result = [];
+    for(let i = 1; i <= 2; ++i) {
+        req.result.push({
+            id: i,
+            name: id2str.location(i)
+        });
+    };
+    next();
+}, response('location'));
+
+// Área da localidade por tipo de área rural
+auxiliarApp.get('/rural_location', (req, res, next) => {
+    req.result = [
+        {id: 1, name: "Urbana"},
+        {id: 2, name: "Rural"},
+        {id: 3, name: "Rural - Área de assentamento"},
+        {id: 4, name: "Rural - Terra indígena"},
+        {id: 5, name: "Rural - Área remanescente de quilombos"},
+        {id: 6, name: "Rural - Unidade de uso sustentável"}
+    ];
+    next();
+}, response('rural_location'));
+
+// Etapa e modalidade por segmento
+auxiliarApp.get('/education_level_mod', (req, res, next) => {
+    req.result = [];
+    for(let i = 1; i <= 11; ++i) {
+        req.result.push({
+            id: i,
+            name: id2str.educationLevelMod(i)
+        });
+    }
+
+    req.result.push({
+        id: 99,
+        name: id2str.educationLevelMod(99)
+    });
+    next();
+}, response('education_level_mod'));
+
+// Sexo
+auxiliarApp.get('/gender', (req, res, next) => {
+    req.result = [
+        {id: 1, name: 'Masculino'},
+        {id: 2, name: 'Feminino'}
+    ];
+    next();
+}, response('gender'));
+
+// Raça/Cor
+auxiliarApp.get('/ethnic_group', (req, res, next) => {
+    req.result = [];
+    for(let i = 0; i <=5; ++i) {
+        req.result.push({
+            id: i,
+            name: id2str.ethnicGroup(i)
+        });
+    }
+    next();
+}, response('ethnic_group'));
+
+// Filtros
+rqf.addField({
+    name: 'filter',
+    field: false,
+    where: true
+}).addField({
+    name: 'dims',
+    field: true,
+    where: false
+// Dependência administrativa [CEBES013N0]
+}).addValue({
+    name: 'adm_dependency',
+    table: 'docente',
+    tableField: 'dependencia_adm_id',
+    resultField: 'adm_dependency_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'dependencia_adm_id'
+    }
+// Dependência administrativa com detalhamento das escolas privadas [CEBES013T1]
+}).addValue({
+    name: 'adm_dependency_detailed',
+    table: 'docente',
+    tableField: 'dependencia_adm_priv',
+    resultField: 'adm_dependency_detailed_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'dependencia_adm_priv'
+    }
+// Etapa e modalidade por segmento [CEBDO009N0T1]
+}).addValue({
+    name: 'education_level_mod',
+    table: 'docente',
+    tableField: 'etapas_mod_ensino_segmento_id',
+    resultField: 'education_level_mod_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'etapas_mod_ensino_segmento_id'
+    }
+}).addValue({
+  name: 'region',
+  table: 'regiao',
+  tableField: 'nome',
+  resultField: 'region_name',
+  where: {
+      relation: '=',
+      type: 'integer',
+      field: 'id'
+  },
+  join: {
+      primary: 'id',
+      foreign: 'escola_regiao_id',
+      foreignTable: 'docente'
+  }
+}).addValue({
+    name: 'state',
+    table: 'estado',
+    tableField: 'nome',
+    resultField: 'state_name',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'escola_estado_id',
+        foreignTable: 'docente'
+    }
+}).addValueToField({
+    name: 'city',
+    table: 'municipio',
+    tableField: ['nome', 'id'],
+    resultField: ['city_name', 'city_id'],
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'escola_municipio_id',
+        foreignTable: 'docente'
+    }
+}, 'dims').addValueToField({
+    name: 'city',
+    table: 'municipio',
+    tableField: 'nome',
+    resultField: 'city_name',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'escola_municipio_id',
+        foreignTable: 'docente'
+    }
+// Área da localidade [CEBES014N0]
+}, 'filter').addValue({
+    name: 'location',
+    table: 'docente',
+    tableField: 'cod_localizacao',
+    resultField: 'location_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'cod_localizacao'
+    }
+// Área da localidade por tipo de área rural [CEBES014T1]
+}).addValue({
+    name: 'rural_location',
+    table: 'docente',
+    tableField: 'localidade_area_rural',
+    resultField: 'rural_location_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'localidade_area_rural'
+    }
+}).addValue({
+    name: 'min_year',
+    table: 'docente',
+    tableField: 'ano_censo',
+    resultField: 'year',
+    where: {
+        relation: '>=',
+        type: 'integer',
+        field: 'ano_censo'
+    }
+}).addValue({
+    name: 'max_year',
+    table: 'docente',
+    tableField: 'ano_censo',
+    resultField: 'year',
+    where: {
+        relation: '<=',
+        type: 'integer',
+        field: 'ano_censo'
+    }
+// Sexo [CEBDO008N0]
+}).addValue({
+    name: 'gender',
+    table: 'docente',
+    tableField: 'sexo',
+    resultField: 'gender_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'sexo'
+    }
+// Cor/Raça [CEBDO009N0]
+}).addValue({
+    name: 'ethnic_group',
+    table: 'docente',
+    tableField: 'cor_raca',
+    resultField: 'ethnic_group_id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'cor_raca'
+    }
+});
+
+// Lab dados
+auxiliarApp.get('/', rqf.parse(), (req, res, next) => {
+    req.sql.field('COUNT(DISTINCT docente.id)', 'total')
+    .field("'Brasil'", 'name')
+    .field('docente.ano_censo', 'year')
+    .from('docente')
+    .group('docente.ano_censo')
+    .order('docente.ano_censo')
+    .where('((docente.tipo_turma_id <= 3) AND (docente.dependencia_adm_id = 2 OR docente.dependencia_adm_id = 3) AND (docente.tipo_docente = 2))');
+    next();
+}, rqf.build(), query, addMissing(rqf), id2str.transform(), response('auxiliar'));
+
+// SimCAQ
+auxiliarApp.get('/count', rqf.parse(), (req, res, next) => {
+    req.sql.field('COUNT(DISTINCT docente.id)', 'total')
+    .field("'Brasil'", 'name')
+    .field('docente.ano_censo', 'year')
+    .from('docente')
+    .group('docente.ano_censo')
+    .order('docente.ano_censo')
+    .where('(docente.tipo_turma_id <= 3 AND docente.tipo_docente = 2)');
+    next();
+}, rqf.build(), query, addMissing(rqf), id2str.transform(), response('auxiliar'));
+
+auxiliarApp.get('/download', passport.authenticate('bearer', { session: false }), rqf.parse(), rqf.build(), download('docente', 'mapping_docente'));
+
+module.exports = auxiliarApp;
-- 
GitLab