diff --git a/src/libs/routes/enrollment.js b/src/libs/routes/enrollment.js index 0ec49b84abb4dac94d3bce2b838c6c51ac038175..e0ca7238515611d89965fabd9457142f0301e51e 100644 --- a/src/libs/routes/enrollment.js +++ b/src/libs/routes/enrollment.js @@ -12,28 +12,10 @@ const query = require(`${libs}/middlewares/query`); const response = require(`${libs}/middlewares/response`); -// **Temporary** solution to add where clauses that are common to all requests -function filter(req, q) { - if (typeof req.min_year !== 'undefined') { - q.where('ano_censo>=?', req.min_year); - } +const parseParams = require(`${libs}/middlewares/parseParams`); - if (typeof req.max_year !== 'undefined') { - q.where('ano_censo<=?', req.max_year); - } - - if (typeof req.adm_dependency_id !== 'undefined') { - q.where('fk_dependencia_adm_id=?', req.adm_dependency_id); - } - - if (typeof req.location_id !== 'undefined') { - q.where('id_localizacao=?', req.location_id); - } +// **Temporary** solution to add where clauses that are common to all requests - if (typeof req.education_level_id !== 'undefined') { - q.where('fk_etapa_ensino_id=?', req.education_level_id); - } -} /** * Complete range of the enrollments dataset @@ -45,7 +27,7 @@ enrollmentApp.get('/year_range', (req, res, next) => { .from('turmas') .field('MIN(turmas.ano_censo)', 'start_year') .field('MAX(turmas.ano_censo)', 'end_year') - .toParam(); + ; next(); }, query, response('range')); @@ -59,7 +41,7 @@ enrollmentApp.get('/education_level', (req, res, next) => { .from('etapa_ensino') .field('pk_etapa_ensino_id', 'id') .field('desc_etapa', 'name') - .toParam(); + ; next(); }, query, response('education_level')); @@ -73,170 +55,176 @@ enrollmentApp.get('/adm_dependency', (req, res, next) => { .from('dependencia_adms') .field('pk_dependencia_adm_id', 'id') .field('nome', 'name') - .toParam(); + ; next(); }, query, response('adm_dependency')); enrollmentApp.get('/data', (req, res, next) => { - req.sql = squel.select().from('turmas').toParam(); + req.sql = squel.select().from('turmas'); next(); }, query, response('data')); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - req.paramCnt = 0; +// Parse the filters and dimensions parameter in the query +enrollmentApp.use('/', parseParams('filter', [ + 'min_year', + 'max_year', + 'adm_dependency_id', + 'location_id', + 'education_level_id', + 'region', + 'state', + 'city', + 'school' +]), parseParams('dims', [ + 'adm_dependency_id', + 'location_id', + 'education_level_id', + 'region', + 'state', + 'city', + 'school' +]), (req, res, next) => { + log.debug(req.filter); + log.debug(req.dims); - if (typeof params.id !== 'undefined') { - req.id = parseInt(params.id, 10); - req.paramCnt += 1; + // Do the joins + if(typeof req.filter.adm_dependency_id !== 'undefined' + || typeof req.dims.adm_dependency_id !== 'undefined') { + req.sql.join('dependencia_adms', null, 'fk_dependencia_adm_id=dependencia_adms.pk_dependencia_adm_id'); } - if (typeof params.location_id !== 'undefined') { - req.location_id = parseInt(params.location_id, 10); - req.paramCnt += 1; + if(typeof req.filter.education_level_id !== 'undefined' + || typeof req.dims.education_level_id !== 'undefined') { + req.sql.join('etapa_ensino', null, 'fk_etapa_ensino_id=etapa_ensino.pk_etapa_ensino_id'); } - if (typeof params.adm_dependency_id !== 'undefined') { - req.adm_dependency_id = parseInt(params.adm_dependency_id, 10); - req.paramCnt += 1; + if(typeof req.filter.region !== 'undefined' + || typeof req.dims.region !== 'undefined') { + req.sql.join('municipios', null, 'fk_municipio_id=municipios.pk_municipio_id') + .join('estados', null, 'municipios.fk_estado_id=estados.pk_estado_id') + .join('regioes', null, 'estados.fk_regiao_id=regioes.pk_regiao_id'); } - if (typeof params.min_year !== 'undefined') { - req.min_year = parseInt(params.min_year, 10); - req.paramCnt += 1; + if((typeof req.filter.state !== 'undefined' + || typeof req.dims.state !== 'undefined') + && (typeof req.filter.region === 'undefined' + && typeof req.dims.region === 'undefined')) { + req.sql.join('municipios', null, 'fk_municipio_id=municipios.pk_municipio_id') + .join('estados', null, 'municipios.fk_estado_id=estados.pk_estado_id'); } - if (typeof params.max_year !== 'undefined') { - req.max_year = parseInt(params.max_year, 10); - req.paramCnt += 1; + if((typeof req.filter.city !== 'undefined' + || typeof req.dims.city !== 'undefined') + && (typeof req.filter.state === 'undefined' + && typeof req.dims.state === 'undefined') + && (typeof req.filter.region === 'undefined' + && typeof req.dims.region === 'undefined')) { + req.sql.join('municipios', null, 'fk_municipio_id=municipios.pk_municipio_id'); } - if (typeof params.education_level_id !== 'undefined') { - req.education_level_id = parseInt(params.education_level_id, 10); - req.paramCnt += 1; + if(typeof req.dims.school !== 'undefined') { + req.sql.join('escolas', null, 'fk_escola_id=escolas.pk_escola_id'); } - next(); -}); + // Dimensions (add fields) -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate !== 'undefined' && params.aggregate === 'region') { - log.debug('Using enrollments query for regions'); - const q = squel.select().from('mat_regioes') - .field('name') - .field('SUM(total)', 'total') - .field('ano_censo', 'year'); + if(typeof req.dims.education_level_id !== 'undefined') { + req.sql.field('desc_etapa', 'education_level') + .group('desc_etapa') + .order('desc_etapa'); + } - filter(req, q); + if(typeof req.dims.region !== 'undefined') { + req.sql.field('regioes.nome', 'region_name') + .group('regioes.nome') + .order('regioes.nome'); + } - if (typeof req.id !== 'undefined') { - q.where('pk_regiao_id=?', req.id); - } - req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam(); + if(typeof req.dims.state !== 'undefined') { + req.sql.field('estados.nome', 'state_name') + .group('estados.nome') + .order('estados.nome'); } - next(); -}); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate !== 'undefined' && params.aggregate === 'state') { - log.debug('Using enrollments query for states'); - const q = squel.select().from('mat_estados') - .field('name') - .field('SUM(total)', 'total') - .field('ano_censo', 'year'); + if(typeof req.dims.city !== 'undefined') { + req.sql.field('municipios.nome', 'city_name') + .group('municipios.nome') + .order('municipios.nome'); + } - filter(req, q); + if(typeof req.dims.school !== 'undefined') { + req.sql.field('escolas.nome_entidade', 'school_name') + .group('escolas.nome_entidade') + .order('escolas.nome_entidade'); + } - if (typeof req.id !== 'undefined') { - q.where('pk_estado_id=?', req.id); - } - req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam(); + if(typeof req.dims.adm_dependency_id !== 'undefined') { + req.sql.field('dependencia_adms.nome', 'adm_dependency_name') + .group('dependencia_adms.nome') + .order('dependencia_adms.nome'); } - next(); -}); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate !== 'undefined' && params.aggregate === 'city') { - log.debug('Using enrollments query for cities'); - const q = squel.select().from('mat_municipios') - .field('name') - .field('SUM(total)', 'total') - .field('ano_censo', 'year'); + if(typeof req.dims.location_id !== 'undefined') { + req.sql.field('turmas.id_localizacao', 'location') + .group('turmas.id_localizacao') + .order('turmas.id_localizacao'); + } - filter(req, q); + if(typeof req.dims.region === 'undefined' + && typeof req.dims.state === 'undefined' + && typeof req.dims.city === 'undefined') { + req.sql.field("'Brasil'", 'name'); + } - if (typeof req.id !== 'undefined') { - q.where('pk_municipio_id=?', req.id); - } - req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam(); + // Filter (add where) + + if (typeof req.filter.min_year !== 'undefined') { + req.sql.where('turmas.ano_censo>=?', parseInt(req.filter.min_year, 10)); } - next(); -}); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate !== 'undefined' && params.aggregate === 'school') { - log.debug('Using enrollments query for schools'); - const q = squel.select().from('mat_escolas') - .field('name') - .field('SUM(total)', 'total') - .field('ano_censo', 'year'); + if (typeof req.filter.max_year !== 'undefined') { + req.sql.where('turmas.ano_censo<=?', parseInt(req.filter.max_year, 10)); + } - filter(req, q); + if (typeof req.filter.adm_dependency_id !== 'undefined') { + req.sql.where('pk_dependencia_adm_id=?', parseInt(req.filter.adm_dependency_id, 10)); + } - if (typeof req.id !== 'undefined') { - q.where('pk_escola_id=?', req.id); - } - req.sql = q.group('name').group('ano_censo').order('ano_censo').toParam(); + if (typeof req.filter.location_id !== 'undefined') { + req.sql.where('turmas.id_localizacao=?', parseInt(req.filter.location_id, 10)); + } + + if (typeof req.filter.education_level_id !== 'undefined') { + req.sql.where('pk_etapa_ensino_id=?', parseInt(req.filter.education_level_id, 10)); + } + + if (typeof req.filter.region !== 'undefined') { + req.sql.where('pk_regiao_id=?', parseInt(req.filter.region, 10)); } - next(); -}); -enrollmentApp.use('/', (req, res, next) => { - const params = req.query; - if (typeof params.aggregate === 'undefined') { - log.debug('Using enrollments query for the whole country'); - const q = squel.select().from('turmas').field("'Brasil'", 'name') - .field('COALESCE(SUM(num_matriculas),0)', 'total') - .field('ano_censo', 'year'); + if (typeof req.filter.state !== 'undefined') { + req.sql.where('pk_estado_id=?', parseInt(req.filter.state, 10)); + } - filter(req, q); + if (typeof req.filter.city !== 'undefined') { + req.sql.where('turmas.fk_municipio_id=?', parseInt(req.filter.city, 10)); + } - req.sql = q.group('ano_censo').order('ano_censo').toParam(); + if (typeof req.filter.school !== 'undefined') { + req.sql.where('turmas.fk_escola_id=?', parseInt(req.filter.school, 10)); } + log.debug(req.sql.toParam()); next(); }); enrollmentApp.get('/', (req, res, next) => { - log.debug(`Request parameters: ${req}`); + req.sql.field('COALESCE(SUM(num_matriculas), 0)', 'total') + .field('turmas.ano_censo', 'year') + .from('turmas') + .group('turmas.ano_censo') + .order('turmas.ano_censo'); next(); -}, query, response('enrollments')); - -// enrollmentApp.get('/', (req, res, next) => { -// log.debug(`Request parameters: ${req}`); -// if (typeof req.sqlQuery === 'undefined') { -// // Should only happen if there is a bug in the chaining of the -// // '/enrollments' route, since when no +aggregate+ parameter is given, -// // it defaults to use the query for the whole country. -// log.error('BUG -- No SQL query was found to be executed!'); -// next('Internal error, request could not be satisfied at this moment. Please, ' -// + 'try again later'); -// } else { -// log.debug('SQL query: ${ req.sqlQuery }?'); -// log.debug(req.sqlQuery); -// dbQuery(req.sqlQuery).then((result) => { -// req.result = result; -// return response(req, res); -// }, (error) => { -// log.error(`[${req.originalUrl}] SQL query error: ${error}`); -// next('Internal error, request could not be satisfied at this moment. Please, ' -// + 'try again later'); -// }); -// } -// }); +}, query, response('test')); module.exports = enrollmentApp;