diff --git a/src/libs/middlewares/aggregateData.js b/src/libs/middlewares/aggregateData.js index a721bb1b8d06adf4d48d6ff9251c7a2b0a0f0c9f..73fb93a79fc3c8b4bdfe260d1008feca26a353e1 100644 --- a/src/libs/middlewares/aggregateData.js +++ b/src/libs/middlewares/aggregateData.js @@ -20,43 +20,44 @@ const convert = { } function aggregateData(req, res, next) { - const newResult = [] - const aggregateFields = ['gender', 'age_range', 'ethnic_group', 'education_level_mod_agg', 'integral_time_agg', 'period_agg', 'modality_integral_time', 'especial_education'] - let id; - const fields = req.query.dims.split(','); - let currentAggregateField; - let currentNonAggregateField; - - // Verifica se o filtro passado está presente nos filtros agregados - fields.forEach(field => {if (aggregateFields.includes(field)) currentAggregateField = field; else currentNonAggregateField = field}); - console.log(convert[currentAggregateField]) - if (currentAggregateField) { - req.result.forEach((r) => { - // Alguns filtros começam com o id = 0 outros id = 1 - id = ['ethnic_group', 'integral_time_agg'].includes(currentAggregateField) ? 0 : 1; - for (const property in r) { - // Dados agregados são identificados com a substring "total_" em sua chave - if (property.includes("total_")) { - let data = { - total: r[property], - year: r.year, - [`${currentAggregateField}_id`]: id, - [`${currentAggregateField}_name`]: id2str[convert[currentAggregateField]](id) - } + if (req.query.dims) { + const newResult = [] + const aggregateFields = ['gender', 'age_range', 'ethnic_group', 'education_level_mod_agg', 'integral_time_agg', 'period_agg', 'modality_integral_time', 'especial_education'] + let id; + const fields = req.query.dims.split(','); + let currentAggregateField; + let currentNonAggregateField; - if (currentNonAggregateField) { - data[`${currentNonAggregateField}_id`] = r[`${currentNonAggregateField}_id`]; - data[`${currentNonAggregateField}_name`] = id2str[convert[currentNonAggregateField]](r[`${currentNonAggregateField}_id`]); + // Verifica se o filtro passado está presente nos filtros agregados + fields.forEach(field => {if (aggregateFields.includes(field)) currentAggregateField = field; else currentNonAggregateField = field}); + console.log(convert[currentAggregateField]) + if (currentAggregateField) { + req.result.forEach((r) => { + // Alguns filtros começam com o id = 0 outros id = 1 + id = ['ethnic_group', 'integral_time_agg'].includes(currentAggregateField) ? 0 : 1; + for (const property in r) { + // Dados agregados são identificados com a substring "total_" em sua chave + if (property.includes("total_")) { + let data = { + total: r[property], + year: r.year, + [`${currentAggregateField}_id`]: id, + [`${currentAggregateField}_name`]: id2str[convert[currentAggregateField]](id) + } + + if (currentNonAggregateField) { + data[`${currentNonAggregateField}_id`] = r[`${currentNonAggregateField}_id`]; + data[`${currentNonAggregateField}_name`] = id2str[convert[currentNonAggregateField]](r[`${currentNonAggregateField}_id`]); + } + + newResult.push(data) + ++id; } - - newResult.push(data) - ++id; } - } - }) - req.result = newResult; + }) + req.result = newResult; + } } - console.log(req.result); next(); }