diff --git a/src/libs/routes_v1/rateSchoolNew.js b/src/libs/routes_v1/rateSchoolNew.js index 74688eea8bc90ca09bac36012b8791587e7c11ea..a35a1fcac4d9dbca75967b719fbe8a7ab80a5ceb 100644 --- a/src/libs/routes_v1/rateSchoolNew.js +++ b/src/libs/routes_v1/rateSchoolNew.js @@ -134,6 +134,18 @@ rateSchoolNewApp.get('/income_range', (req, res, next) => { next(); }, response('income_range')); +rateSchoolNewApp.get('/age_range', (req, res, next) => { + req.result = [ + {id: 1, name: '0 a 3 anos'}, + {id: 2, name: '4 a 5 anos'}, + {id: 3, name: '6 a 10 anos'}, + {id: 4, name: '11 a 14 anos'}, + {id: 5, name: '15 a 17 anos'}, + {id: 6, name: '18 a 24 anos'} + ]; + next(); +}, response('age_range')); + rqf.addField({ name: 'filter', field: false, @@ -303,46 +315,55 @@ function matchQueries(attendsSchoolObj, populationObj) { } rateSchoolNewApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => { - // As we will need to do two requests, they'll be stored in a list - req.querySet = [] - // Create an object that will store the first request (the sum of all people that attend school) - // and are below a certain age (in this case, 24 yeas) - let attends_school = req.sql.clone(); - attends_school.from('pnad_novo') - .field('round(sum(pnad_novo.peso_domicilio_pessoas_com_cal), 0)', 'total') - .field('pnad_novo.faixa_etaria') - .field('pnad_novo.ano_ref', 'year') - .where('pnad_novo.ano_ref >= 2019 AND frequenta_escola = 1') - .where('pnad_novo.faixa_etaria < 7') - .group('pnad_novo.ano_ref') - .group('pnad_novo.faixa_etaria') - .order('pnad_novo.ano_ref') - .order('pnad_novo.faixa_etaria') - req.querySet.push(attends_school); - - // Then, the second object is created and stores the sum of all people below a certain age (24 years) - let full_population = req.sql.clone(); - full_population.from('pnad_novo') - .field('round(sum(pnad_novo.peso_domicilio_pessoas_com_cal), 0)', 'total') - .field('pnad_novo.faixa_etaria') - .field('pnad_novo.ano_ref', 'year') - .where('pnad_novo.ano_ref >= 2019') - .where('pnad_novo.faixa_etaria < 7') - .group('pnad_novo.ano_ref') - .group('pnad_novo.faixa_etaria') - .order('pnad_novo.ano_ref') - .order('pnad_novo.faixa_etaria') - req.querySet.push(full_population); + if ("age_range" in req.filter || "age_range" in req.dims) { + // As we will need to do two requests, they'll be stored in a list + req.querySet = [] + + // Create an object that will store the first request (the sum of all people that attend school) + // and are below a certain age (in this case, 24 yeas) + let attends_school = req.sql.clone(); + attends_school.from('pnad_novo') + .field('round(sum(pnad_novo.peso_domicilio_pessoas_com_cal), 0)', 'total') + .field('pnad_novo.faixa_etaria') + .field('pnad_novo.ano_ref', 'year') + .where('pnad_novo.ano_ref >= 2019 AND frequenta_escola = 1') + .where('pnad_novo.faixa_etaria < 7') + .group('pnad_novo.ano_ref') + .group('pnad_novo.faixa_etaria') + .order('pnad_novo.ano_ref') + .order('pnad_novo.faixa_etaria') + req.querySet.push(attends_school); + + // Then, the second object is created and stores the sum of all people below a certain age (24 years) + let full_population = req.sql.clone(); + full_population.from('pnad_novo') + .field('round(sum(pnad_novo.peso_domicilio_pessoas_com_cal), 0)', 'total') + .field('pnad_novo.faixa_etaria') + .field('pnad_novo.ano_ref', 'year') + .where('pnad_novo.ano_ref >= 2019') + .where('pnad_novo.faixa_etaria < 7') + .group('pnad_novo.ano_ref') + .group('pnad_novo.faixa_etaria') + .order('pnad_novo.ano_ref') + .order('pnad_novo.faixa_etaria') + req.querySet.push(full_population); + } next(); }, multiQuery, (req, res, next) => { - - // The multiple requests are made. Then we need to calculate the percetange. So the function - // below is used - let newObj = matchQueries(req.result[0], req.result[1]); - req.result = newObj; - + if ("age_range" in req.filter || "age_range" in req.dims) { + // The multiple requests are made. Then we need to calculate the percetange. So the function + // below is used + let newObj = matchQueries(req.result[0], req.result[1]); + req.result = newObj; + } else { + res.status(400); + next({ + status: 400, + message: 'Wrong/No filter specified' + }); + } next(); }, id2str.transform(false), response('rateSchoolNew'));