Skip to content
Snippets Groups Projects
class.js 7.90 KiB
const express = require('express');

const classApp = express.Router();

const libs = `${process.cwd()}/libs`;

const log = require(`${libs}/log`)(module);

const squel = require('squel');

const query = require(`${libs}/middlewares/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 cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware;

let rqfCount = new ReqQueryFields();

classApp.use(cache('15 day'));

// Complete range of the enrollments dataset.
// Returns a tuple of start and ending years of the complete enrollments dataset.
classApp.get('/year_range', (req, res, next) => {
    req.sql.from('turma')
    .field('MIN(turma.ano_censo)', 'start_year')
    .field('MAX(turma.ano_censo)', 'end_year');
    next();
}, query, response('range'));

classApp.get('/years', (req, res, next) => {
    req.sql.from('turma')
    .field('DISTINCT turma.ano_censo', 'year');
    next();
}, query, response('years'));

classApp.get('/location', (req, res, next) => {
    req.sql = squel.select()
    .field('id')
    .field('descricao', 'name')
    .from('localizacao')
    .where('localizacao.id <= 2');
    next();
}, query, response('location'));

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

// Returns all adm dependencies
classApp.get('/adm_dependency', (req, res, next) => {
    req.sql.from('dependencia_adm')
    .field('id')
    .field('nome', 'name')
    .where('id <= 4');
    next();
}, query, response('adm_dependency'));

classApp.get('/adm_dependency_detailed', (req, res, next) => {
    req.sql.from('dependencia_adm_priv')
    .field('id', 'id')
    .field('nome', 'name');
    next();
}, query, response('adm_dependency_detailed'));

// Returns all periods avaible
classApp.get('/period', (req, res, next) => {
    req.sql.from('turma_turno')
    .field('id')
    .field('nome', 'name');
    next();
}, query, response('period'));

// Returns integral-time avaible
classApp.get('/integral_time', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Disponível'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('integral_time'));

// Returns all educational levels avaible
classApp.get('/education_level_mod', (req, res, next) => {
    req.sql.from('etapas_mod_ensino_segmento')
    .field('id')
    .field('nome', 'name');
    next();
}, query, response('education_level_mod'));

classApp.get('/education_level_short', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não classificada'},
        {id: 1, name: 'Creche'},
        {id: 2, name: 'Pré-Escola'},
        {id: 3, name: 'Ensino Fundamental - anos iniciais'},
        {id: 4, name: 'Ensino Fundamental - anos finais'},
        {id: 5, name: 'Ensino Médio'},
        {id: 6, name: 'EJA'},
        {id: 7, name: 'EE exclusiva'}
    ];
    next();
}, response('education_level_short'));

rqfCount.addField({
    name: 'filter',
    field: false,
    where: true
}).addField({
    name: 'dims',
    field: true,
    where: false
}).addValueToField({
    name: 'city',
    table: 'municipio',
    tableField: ['nome', 'id'],
    resultField: ['city_name', 'city_id'],
    where: {
        relation: '=',
        type: 'integer',
        field: 'municipio_id',
        table: 'turma'
    },
    join: {
        primary: 'id',
        foreign: 'municipio_id',
        foreignTable: 'turma'
    }
}, 'dims').addValueToField({
    name: 'city',
    table: 'municipio',
    tableField: 'nome',
    resultField: 'city_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'municipio_id',
        table: 'turma'
    },
    join: {
        primary: 'id',
        foreign: 'municipio_id',
        foreignTable: 'turma'
    }
}, 'filter').addValue({
    name: 'state',
    table: 'estado',
    tableField: 'nome',
    resultField: 'state_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'estado_id',
        table: 'turma'
    },
    join: {
        primary: 'id',
        foreign: 'estado_id',
        foreignTable: 'turma'
    }
}).addValue({
    name: 'region',
    table: 'regiao',
    tableField: 'nome',
    resultField: 'region_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: 'id',
        foreign: 'regiao_id',
        foreignTable: 'turma'
    }
}).addValue({
    name: 'min_year',
    table: 'turma',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '>=',
        type: 'integer',
        field: 'ano_censo'
    }
}).addValue({
    name: 'max_year',
    table: 'turma',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '<=',
        type: 'integer',
        field: 'ano_censo'
    }
}).addValue({
    name:'adm_dependency',
    table: 'turma',
    tableField: 'dependencia_adm_id',
    resultField: 'adm_dependency_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'dependencia_adm_id'
    }
}).addValue({
    name: 'location',
    table: 'turma',
    tableField: 'localizacao_id',
    resultField: 'location_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'localizacao_id'
    }
}).addValue({
    name: 'rural_location',
    table: 'turma',
    tableField: 'localidade_area_rural',
    resultField: 'rural_location_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'localidade_area_rural'
    }
}).addValue({
  name:'education_level_mod',
  table: 'turma',
  tableField: 'etapas_mod_ensino_segmento_id',
  resultField: 'education_level_mod_id',
  where: {
    relation: '=',
    type: 'integer',
    field: 'etapas_mod_ensino_segmento_id'
  }
}).addValue({
    name: 'education_level_short',
    table: 'turma',
    tableField: 'etapa_resumida',
    resultField: 'education_level_short_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'etapa_resumida'
    }
}).addValue({
    name: 'adm_dependency_detailed',
    table: 'turma',
    tableField: 'dependencia_adm_priv',
    resultField: 'adm_dependency_detailed_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'dependencia_adm_priv'
    }
}).addValue({
  name:'period',
  table: 'turma',
  tableField: 'turma_turno_id',
  resultField: 'period_id',
  where: {
      relation: '=',
      type: 'integer',
      field: 'turma_turno_id'
  }
}).addValue({
  name:'integral_time',
  table: 'turma',
  tableField: 'tempo_integral',
  resultField: 'integral_time_id',
  where: {
      relation: '=',
      type: 'boolean',
      field: 'tempo_integral'
  }
}).addValue({
    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: 'turma'
    }
});


classApp.get('/', rqfCount.parse(), rqfCount.build(), (req, res, next) => {
  log.debug(req.sql.toParam());
   req.sql.field('COUNT(turma.id)', 'total')
   .field("'Brasil'", 'name')
   .field('turma.ano_censo', 'year')
   .from('turma')
   .group('turma.ano_censo')
   .order('turma.ano_censo')
   .where('turma.tipo_turma_id = 0 OR turma.tipo_turma_id = 1 OR turma.tipo_turma_id = 2 OR turma.tipo_turma_id = 3');
   next();
}, query, id2str.transform(), response('class'));

module.exports = classApp;