Skip to content
Snippets Groups Projects
auxiliar.js 8.53 KiB
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'))

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'));

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'));

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'));

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'));

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'));

auxiliarApp.get('/gender', (req, res, next) => {
    req.result = [
        {id: 1, name: 'Masculino'},
        {id: 2, name: 'Feminino'}
    ];
    next();
}, response('gender'));

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'));

rqf.addField({
    name: 'filter',
    field: false,
    where: true
}).addField({
    name: 'dims',
    field: true,
    where: false
}).addValue({
    name: 'adm_dependency',
    table: 'docente',
    tableField: 'dependencia_adm_id',
    resultField: 'adm_dependency_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'dependencia_adm_id'
    }
}).addValue({
    name: 'adm_dependency_detailed',
    table: 'docente',
    tableField: 'dependencia_adm_priv',
    resultField: 'adm_dependency_detailed_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'dependencia_adm_priv'
    }
}).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'
    }
}).addValue({
    name: 'rural_location',
    table: 'docente',
    tableField: 'localidade_area_rural',
    resultField: 'rural_location_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'localidade_area_rural'
    }
}).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'
    }
}, 'filter').addValueToField({
    name: 'school',
    table: 'escola',
    tableField: ['nome_escola', 'id'],
    resultField: ['school_name', 'school_id'],
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: ['id', 'ano_censo'],
        foreign: ['escola_id', 'ano_censo'],
        foreignTable: 'docente'
    }
}, 'dims').addValueToField({
    name: 'school',
    table: 'escola',
    tableField: 'nome_escola',
    resultField: 'school_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: ['id', 'ano_censo'],
        foreign: ['escola_id', 'ano_censo'],
        foreignTable: 'docente'
    }
}, 'filter').addValue({
    name: 'location',
    table: 'docente',
    tableField: 'cod_localizacao',
    resultField: 'location_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'cod_localizacao'
    }
}).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'
    }
}).addValue({
    name: 'gender',
    table: 'docente',
    tableField: 'sexo',
    resultField: 'gender_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'sexo'
    }
}).addValue({
    name: 'ethnic_group',
    table: 'docente',
    tableField: 'cor_raca',
    resultField: 'ethnic_group_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'cor_raca'
    }
});

// LDE
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.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.dependencia_adm_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;