diff --git a/src/libs/middlewares/studentRevenueParser.js b/src/libs/middlewares/studentRevenueParser.js new file mode 100644 index 0000000000000000000000000000000000000000..954c9c8208d7349b104bdc5355e2bddb96a3498a --- /dev/null +++ b/src/libs/middlewares/studentRevenueParser.js @@ -0,0 +1,23 @@ +/* +Middleware que remove o campo "region" quando o campo "city" está ativo nos filtros. +Devido à limitações na tabela de dados, quando ambos campos estão ativos o filtro por +"região" funciona incorretamente. +*/ +module.exports = function studentRevenueParser(req, res, next) { + console.log(req.query); + + if (req.query && req.query.filter) { + const jsonString = `{${req.query.filter.replace(/(\w+):/g, '"$1":')}}`; + const obj = JSON.parse(jsonString); + + if ("city" in obj && "region" in obj) { + delete obj.region; + const customString = Object.entries(obj) + .map(([key, value]) => `${key}:"${value}"`) + .join(','); + req.query.filter = customString; + } + } + + next(); +}; diff --git a/src/libs/routes_v1/api.js b/src/libs/routes_v1/api.js index b9af676949596cb8959df16bf616fc85dee5d419..843060180a8ade07714fc6965b16049f2420d797 100644 --- a/src/libs/routes_v1/api.js +++ b/src/libs/routes_v1/api.js @@ -174,6 +174,7 @@ const adjustedLiquidFrequency = require(`${libs}/routes_v1/adjustedLiquidFrequen const iliteracyRate = require(`${libs}/routes_v1/iliteracyRate`); const studentRevenue = require(`${libs}/routes_v1/studentRevenue`); +const studentRevenueParser = require(`${libs}/middlewares/studentRevenueParser`); api.get('/', (req, res) => { res.json({ msg: 'SimCAQ API v1 is running' }); @@ -249,7 +250,7 @@ api.use('/superior_education_conclusion_tax', superiorEducationConclusionTax) api.use('/basic_education_conclusion', basicEducationConclusion); api.use('/adjusted_liquid_frequency', adjustedLiquidFrequency); api.use('/iliteracy_rate', iliteracyRate); -api.use('/student_revenue', studentRevenue); +api.use('/student_revenue', studentRevenueParser, studentRevenue); //Publication api.use('/publication', publication); diff --git a/src/libs/routes_v1/studentRevenue.js b/src/libs/routes_v1/studentRevenue.js index efce47c509829164c97650901d14f68b1cb86097..4b8c334e97679326bbb6644c413e55d285fd0c54 100644 --- a/src/libs/routes_v1/studentRevenue.js +++ b/src/libs/routes_v1/studentRevenue.js @@ -79,12 +79,26 @@ rqf.addField({ type: 'integer', field: 'ano' } +}).addValue({ + name: 'region', + table: 'estado', + tableField: ['nome_ente', 'cod_ibge', 'id', 'regiao_id'], + resultField: ['state_name', 'state_cod_ibge', 'state_id', 'region_id'], + where: { + relation: '=', + type: 'integer', + field: 'regiao_id', + }, + join: { + primary: 'id', + foreign: 'cod_ibge', + foreignTable: 'receitas' + } }) studentRevenueApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { - if (req.query.dims && req.query.dims.includes('receitas')) { - let whereCondition = ""; + let whereCondition = req.query.filter.includes("region") ? "" : "receitas.cod_ibge = 0"; let filterId; const filters = req.query.filter.split(","); filters.forEach((filter) => {