From 40b192f818227f6175fb1dcb39f9c9cd85dd8830 Mon Sep 17 00:00:00 2001 From: Vytor Calixto <vytorcalixto@gmail.com> Date: Thu, 16 Nov 2017 11:41:48 -0200 Subject: [PATCH] Create route distributionFactor --- src/libs/routes/distributionFactor.js | 190 ++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 src/libs/routes/distributionFactor.js diff --git a/src/libs/routes/distributionFactor.js b/src/libs/routes/distributionFactor.js new file mode 100644 index 00000000..f7d40502 --- /dev/null +++ b/src/libs/routes/distributionFactor.js @@ -0,0 +1,190 @@ +const express = require('express'); + +const distributionApp = express.Router(); + +const libs = `${process.cwd()}/libs`; + +const squel = require('squel'); + +const query = require(`${libs}/middlewares/query`); + +const multiQuery = require(`${libs}/middlewares/multiQuery`); + +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 rqf = new ReqQueryFields(); + +distributionApp.use(cache('15 day')); + +rqf.addField({ + name: 'dims', + field: true, + where: false +}).addField({ + name: 'filter', + field: false, + where: true +}).addValueToField({ + name: 'city', + table: 'municipio', + tableField: ['nome', 'id'], + resultField: ['city_name', 'city_id'], + where: { + relation: '=', + type: 'integer', + field: 'municipio_id', + table: 'fatores_matricula' + }, + join: { + primary: 'id', + foreign: 'municipio_id', + foreignTable: 'fatores_matricula' + } +}, 'dims').addValueToField({ + name: 'city', + table: 'municipio', + tableField: 'nome', + resultField: 'city_name', + where: { + relation: '=', + type: 'integer', + field: 'municipio_id', + table: 'fatores_matricula' + }, + join: { + primary: 'id', + foreign: 'municipio_id', + foreignTable: 'fatores_matricula' + } +}, 'filter').addValue({ + name: 'state', + table: 'estado', + tableField: 'nome', + resultField: 'state_name', + where: { + relation: '=', + type: 'integer', + field: 'estado_id', + table: 'fatores_matricula' + }, + join: { + primary: 'id', + foreign: 'estado_id', + foreignTable: 'fatores_matricula' + } +}).addValue({ + name: 'region', + table: 'regiao', + tableField: 'nome', + resultField: 'region_name', + where: { + relation: '=', + type: 'integer', + field: 'id' + }, + join: { + primary: 'id', + foreign: 'regiao_id', + foreignTable: 'fatores_matricula' + } +}) + +// Return all cities +distributionApp.get('/', rqf.parse(), (req, res, next) => { + req.querySet = []; + req.queryIndex = {}; + + let relation = req.sql.clone(); + relation.from('relacao_fatores_matricula').field('*'); + req.queryIndex.relation = req.querySet.push(relation) - 1; + + req.sql.from('fatores_matricula') + .field('fatores_matricula.municipio_id', 'municipio_id') + .field('fatores_matricula."mais_CRE_0"') + .field('fatores_matricula."mais_CRE_1"') + .field('fatores_matricula."mais_CRE_2"') + .field('fatores_matricula."mais_CRE_3"') + .field('fatores_matricula."mais_PRE"') + .field('fatores_matricula."mais_EFAI"') + .field('fatores_matricula."mais_EFAF"') + .field('fatores_matricula."mais_EM"') + .field('fatores_matricula."mais_EJA"') + .field('fatores_matricula."menos_CRE_0"') + .field('fatores_matricula."menos_CRE_1"') + .field('fatores_matricula."menos_CRE_2"') + .field('fatores_matricula."menos_CRE_3"') + .field('fatores_matricula."menos_PRE"') + .field('fatores_matricula."menos_EFAI"') + .field('fatores_matricula."menos_EFAF"') + .field('fatores_matricula."menos_EM"') + .field('fatores_matricula."menos_EJA"') + .group('fatores_matricula.municipio_id') + .group('fatores_matricula."mais_CRE_0"') + .group('fatores_matricula."mais_CRE_1"') + .group('fatores_matricula."mais_CRE_2"') + .group('fatores_matricula."mais_CRE_3"') + .group('fatores_matricula."mais_PRE"') + .group('fatores_matricula."mais_EFAI"') + .group('fatores_matricula."mais_EFAF"') + .group('fatores_matricula."mais_EM"') + .group('fatores_matricula."mais_EJA"') + .group('fatores_matricula."menos_CRE_0"') + .group('fatores_matricula."menos_CRE_1"') + .group('fatores_matricula."menos_CRE_2"') + .group('fatores_matricula."menos_CRE_3"') + .group('fatores_matricula."menos_PRE"') + .group('fatores_matricula."menos_EFAI"') + .group('fatores_matricula."menos_EFAF"') + .group('fatores_matricula."menos_EM"') + .group('fatores_matricula."menos_EJA"'); + + if(typeof req.dims.state !== 'undefined' || typeof req.filter.state !== 'undefined') { + req.sql.where('fatores_matricula.nivel = \'UF\''); + } else { + req.sql.where('fatores_matricula.nivel = \'BR\''); + } + + next(); +}, rqf.build(), query, (req, res, next) => { + req.enrollmentFactor = req.result; + next(); +}, multiQuery, (req, res, next) => { + let relation = req.result[req.queryIndex.relation]; + let result = []; + let first = true; + req.enrollmentFactor.forEach((city) => { + if(first) console.log(city); + let obj = { + level: city.nivel, + region_id: city.regiao_id, + region_name: city.region_name, + state_id: city.state_id, + state_name: city.state_name, + city_id: city.municipio_id, + city_name: city.city_name, + series: [] + }; + if(first) console.log(obj); + first = false; + relation.forEach((serie) => { + obj.series.push({ + serie_id: serie.id, + distribution_factor_addition: city[serie.fator_adicao], + distribution_factor_reduction: city[serie.fator_reducao] + }); + }); + result.push(obj); + }); + req.result = result; + next(); +}, response('ditributionFactor')); + +module.exports = distributionApp; -- GitLab