diff --git a/src/libs/routes_v1/adjustedLiquidFrequency.js b/src/libs/routes_v1/adjustedLiquidFrequency.js index a682dac2ca0ad2fa499cb4acabec7b4a2bd87d4b..bb02e81e421d7a543b0bc0c92945b553a473fef3 100644 --- a/src/libs/routes_v1/adjustedLiquidFrequency.js +++ b/src/libs/routes_v1/adjustedLiquidFrequency.js @@ -142,16 +142,6 @@ rqf.addField({ foreign: 'cod_uf', foreignTable: 'pnad_novo' } -}).addValue({ - name: 'years_of_study', - table: 'pnad_novo', - tableField: 'anos_de_estudo', - resultField: 'years_of_study_id', - where: { - relation: '=', - type: 'integer', - field: 'anos_de_estudo' - } }).addValue({ name: 'bolsa_familia', table: 'pnad_novo', @@ -265,57 +255,32 @@ rqf.addField({ } }); -function matchQueries(queryPartial, queryTotal) { - let match = []; - queryTotal.forEach((result) => { - let newObj = {}; - let keys = Object.keys(result); - keys.forEach((key) => { - newObj[key] = result[key]; - }); - let index = keys.indexOf('total'); - if(index > -1) keys.splice(index, 1); - let objMatch = null; - - for(let i = 0; i < queryPartial.length; ++i) { - let partial = queryPartial[i]; - let foundMatch = true; - for(let j = 0; j < keys.length; ++j) { - let key = keys[j]; - if(partial[key] !== result[key]) { - foundMatch = false; - break; - } - } - if(foundMatch) { - objMatch = partial; - break; - } - } - - if(objMatch) { - newObj.denominator = result.total; - newObj.partial = objMatch.total; - newObj.total = (objMatch.total / result.total) * 100; - match.push(newObj); - } - }); - return match; -} - - adjustedLiquidFrequency.get('/', rqf.parse(), (req, res, next) => { + const dimensions = req.dims || {}; // Obter dimensões solicitadas pela requisição + + // Subquery: total_pop let totalPop = squel.select() - .field("ano_ref") - .field("faixa_etaria") - .field("SUM(peso_domicilio_pessoas_com_cal)", "total") - .from("pnad_novo") - .group("ano_ref") - .group("faixa_etaria"); - - // Aplicando filtros dinâmicos à subquery total_pop + .field("ano_ref") + .field("faixa_etaria") + .field("SUM(peso_domicilio_pessoas_com_cal)", "total") + .from("pnad_novo") + .where("faixa_etaria <= 6") + .group("ano_ref") + .group("faixa_etaria"); + + + // Aplicar filtros dinâmicos à subquery total_pop totalPop = rqf.buildQuery(req, totalPop); + + const joinQuery = ` + total_pop.ano_ref = pnad_novo.ano_ref AND + total_pop.faixa_etaria = pnad_novo.apoio_frequencia_ajustada + ${dimensions.gender ? " AND total_pop.gender_id = pnad_novo.sexo" : ""} + ${dimensions.bolsa_familia ? "AND total_pop.bolsa_familia_id = pnad_novo.recebeu_rendimentos_de_programa_bolsa_familia" : ""} + ${dimensions.new_pnad_ethnic_group ? "AND total_pop.new_pnad_ethnic_group_id = pnad_novo.cor_raca" : ""} + ${dimensions.income_range ? "AND total_pop.income_range_id = pnad_novo.faixa_rendimento_aux_tx" : ""} + ` // Subquery: total_apoio_freq let totalApoioFreq = squel.select() @@ -326,26 +291,40 @@ adjustedLiquidFrequency.get('/', rqf.parse(), (req, res, next) => { .from("pnad_novo") .join( totalPop, - "total_pop", - "total_pop.ano_ref = pnad_novo.ano_ref AND total_pop.faixa_etaria = pnad_novo.apoio_frequencia_ajustada" + "total_pop", + joinQuery ) .group("pnad_novo.ano_ref") .group("pnad_novo.apoio_frequencia_ajustada") .group("total_pop.total"); - // Aplicando filtros à subquery total_apoio_freq + // Aplicar filtros dinâmicos à subquery total_apoio_freq totalApoioFreq = rqf.buildQuery(req, totalApoioFreq); - // Query principal - req.sql = squel.select() + // Query Principal + let mainQuery = squel.select() + .from(totalApoioFreq, "total_apoio_freq") .field("total_apoio_freq.ano_ref") - .field("total_apoio_freq.apoio_frequencia_ajustada") + .field("total_apoio_freq.apoio_frequencia_ajustada", "age_range_all_id") .field("ROUND((total_apoio_freq.total_freq / total_apoio_freq.total) * 100, 1)", "total") - .from(totalApoioFreq, "total_apoio_freq") - .where("total_apoio_freq.ano_ref >= 2019") // Corrigido para usar alias correto + .where("total_apoio_freq.ano_ref >= 2019") .order("total_apoio_freq.ano_ref") .order("total_apoio_freq.apoio_frequencia_ajustada"); + if (dimensions.gender) + mainQuery.field("total_apoio_freq.gender_id") + + if (dimensions.bolsa_familia) + mainQuery.field("total_apoio_freq.bolsa_familia_id") + + if (dimensions.new_pnad_ethnic_group) + mainQuery.field("total_apoio_freq.new_pnad_ethnic_group_id") + + if (dimensions.income_range) + mainQuery.field("total_apoio_freq.income_range_id") + + req.sql = mainQuery; + next(); }, query, id2str.transform(false), response('adjusted_liquid_frequency'));