Skip to content
Snippets Groups Projects
idhm.js 4.56 KiB
Newer Older
Fernando Erd's avatar
Fernando Erd committed
const express = require('express');

const idhmApp = 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`);

let rqf = new ReqQueryFields();

idhmApp.get('/year_range', (req, res, next) => {
    req.sql.from('adh_idh')
    .field('MIN(adh_idh.ano_censo)', 'start_year')
    .field('MAX(adh_idh.ano_censo)', 'end_year');
    next();
Fernando Erd's avatar
Fernando Erd committed
}, query, (req, res, next) => {
    req.sql.from('adh_idh_uf')
    .field('MIN(adh_idh_uf.ano_censo)', 'start_year')
    .field('MAX(adh_idh_uf.ano_censo)', 'end_year');
    req.old_result = req.result;
    next();
}, query, (req, res, next) => {
    // console.log(req.old_result[0].start_year);
    // console.log(req.result[0].start_year);
    if (req.old_result[0].start_year < req.result[0].start_year) {
        req.result[0].start_year = req.old_result[0].start_year;
    }
    if (req.old_result[0].end_year > req.result[0].end_year) {
        req.result[0].end_year = req.old_result[0].old_result;
    }
    next();
Fernando Erd's avatar
Fernando Erd committed
}, query, response('range'));

Fernando Erd's avatar
Fernando Erd committed
idhmApp.get('/IDHM_level', (req, res, next) => {
Fernando Erd's avatar
Fernando Erd committed
    req.result = [
        {id: null, name: 'Não Classificado'},
        {id: 1, name: 'Muito Baixa'},
        {id: 2, name: 'Baixo'},
        {id: 3, name: 'Médio'},
        {id: 4, name: 'Alto'},
        {id: 5, name: 'Muito Alto'}
Fernando Erd's avatar
Fernando Erd committed
    ];
Fernando Erd's avatar
Fernando Erd committed
    next();
}, response('IDHM_level'));

Fernando Erd's avatar
Fernando Erd committed
rqf.addField({
    name: 'filter',
    field: false,
    where: true
}).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: 'adh_idh'
    },
    join: {
        primary: 'id',
        foreign: 'municipio_id',
        foreignTable: 'adh_idh'
    }
}).addValue({
    name: 'state',
    table: 'estado',
    tableField: 'nome',
    resultField: 'state_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'estado_id',
Fernando Erd's avatar
Fernando Erd committed
        table: '@'
Fernando Erd's avatar
Fernando Erd committed
    },
    join: {
        primary: 'id',
        foreign: 'estado_id',
Fernando Erd's avatar
Fernando Erd committed
        foreignTable: '@'
Fernando Erd's avatar
Fernando Erd committed
    }
Fernando Erd's avatar
Fernando Erd committed
}).addValue({
    name: 'min_year',
    table: '@',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '>=',
        type: 'integer',
        table: '@',
        field: 'ano_censo'
    }
}).addValue({
    name: 'max_year',
    table: '@',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '<=',
        type: 'integer',
        table: '@',
        field: 'ano_censo'
    }
Fernando Erd's avatar
Fernando Erd committed
}).addValue({
Fernando Erd's avatar
Fernando Erd committed
    name: 'idhm_level',
Fernando Erd's avatar
Fernando Erd committed
    table: '@',
    tableField: 'idhm_nivel',
Fernando Erd's avatar
Fernando Erd committed
    resultField: 'idhm_level_id',
Fernando Erd's avatar
Fernando Erd committed
    where: {
        relation: '=',
        type: 'integer',
        table: '@',
        field: 'idhm_nivel'
    }
Fernando Erd's avatar
Fernando Erd committed
});
Fernando Erd's avatar
Fernando Erd committed
idhmApp.get('/', rqf.parse(), (req, res, next) => {
Fernando Erd's avatar
Fernando Erd committed
  log.debug(req.sql.toParam());
Fernando Erd's avatar
Fernando Erd committed
  if((Object.keys(req.filter).length === 0) && (Object.keys(req.dims).length === 0)) {
Fernando Erd's avatar
Fernando Erd committed
      req.sql.from('adh_idh_uf')
      .field('adh_idh_uf.idhm', 'total')
      .field('adh_idh_uf.ano_censo', 'year')
      .field('adh_idh_uf.estado_id', 'state_id')
      .group('adh_idh_uf.idhm')
      .group('adh_idh_uf.ano_censo')
      .group('adh_idh_uf.estado_id')
  } else if ("state" in req.filter && !("city" in req.filter)) {
      req.sql.from('adh_idh_uf')
      .field('adh_idh_uf.idhm', 'total')
      .field('adh_idh_uf.ano_censo', 'year')
      .field('adh_idh_uf.estado_id', 'state_id')
      .group('adh_idh_uf.idhm')
      .group('adh_idh_uf.ano_censo')
      .group('adh_idh_uf.estado_id')
   } else if ("city" in req.filter) {
      req.sql.from('adh_idh')
      .field('adh_idh.idhm', 'total')
      .field('adh_idh.ano_censo', 'year')
      .field('adh_idh.municipio_id', 'city_id')
      .field('adh_idh.estado_id', 'city_id')
      .group('adh_idh.idhm')
      .group('adh_idh.ano_censo')
      .group('adh_idh.municipio_id')
      .group('adh_idh.estado_id')
    } else {
Fernando Erd's avatar
Fernando Erd committed
      req.sql.from('adh_idh_uf')
      .field('adh_idh_uf.idhm', 'total')
      .field('adh_idh_uf.ano_censo', 'year')
      .field('adh_idh_uf.estado_id', 'state_id')
      .group('adh_idh_uf.idhm')
      .group('adh_idh_uf.ano_censo')
      .group('adh_idh_uf.estado_id')
Fernando Erd's avatar
Fernando Erd committed
   next();
Fernando Erd's avatar
Fernando Erd committed
}, rqf.build(), query, id2str.transform(), response('idhm'));
Fernando Erd's avatar
Fernando Erd committed

module.exports = idhmApp;