Skip to content
Snippets Groups Projects
Commit 1dc23454 authored by Vytor Calixto's avatar Vytor Calixto :space_invader:
Browse files

Fix SQL of enrollments to new schema

SQL is now compatible with DB schema in simcaqdb3
parent 8b78356e
No related branches found
No related tags found
1 merge request!116Release v1.0.0
Pipeline #
...@@ -71,7 +71,7 @@ enrollmentApp.use('/', parseParams('filter', [ ...@@ -71,7 +71,7 @@ enrollmentApp.use('/', parseParams('filter', [
// Do the joins // Do the joins
if(typeof req.filter.adm_dependency !== 'undefined' if(typeof req.filter.adm_dependency !== 'undefined'
|| typeof req.dims.adm_dependency !== 'undefined') { || typeof req.dims.adm_dependency !== 'undefined') {
req.sql.join('dependencia_adms', null, 'fk_dependencia_adm_id=dependencia_adms.pk_dependencia_adm_id'); req.sql.join('dependencia_adm', null, 'fk_dependencia_adm_id=dependencia_adms.pk_dependencia_adm_id');
} }
if(typeof req.filter.education_level !== 'undefined' if(typeof req.filter.education_level !== 'undefined'
...@@ -81,30 +81,25 @@ enrollmentApp.use('/', parseParams('filter', [ ...@@ -81,30 +81,25 @@ enrollmentApp.use('/', parseParams('filter', [
if(typeof req.filter.region !== 'undefined' if(typeof req.filter.region !== 'undefined'
|| typeof req.dims.region !== 'undefined') { || typeof req.dims.region !== 'undefined') {
req.sql.join('municipios', null, 'fk_municipio_id=municipios.pk_municipio_id') req.sql.join('regiao', null, 'fk_regiao_id=regiao.pk_regiao_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 req.filter.state !== 'undefined' if(typeof req.filter.state !== 'undefined'
|| typeof req.dims.state !== 'undefined') || typeof req.dims.state !== 'undefined') {
&& (typeof req.filter.region === 'undefined' req.sql.join('estado', null, 'fk_estado_id=estado.pk_estado_id');
&& 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 req.filter.city !== 'undefined' if(typeof req.filter.city !== 'undefined'
|| typeof req.dims.city !== 'undefined') || typeof req.dims.city !== 'undefined') {
&& (typeof req.filter.state === 'undefined' req.sql.join('municipio', null, 'fk_municipio_id=municipio.pk_cod_ibge');
&& 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 req.dims.school !== 'undefined') { if(typeof req.dims.school !== 'undefined') {
req.sql.join('escolas', null, 'fk_escola_id=escolas.pk_escola_id'); req.sql.join('escola', null, 'fk_escola_id=escola.pk_escola_id');
}
if(typeof req.dims.location !== 'undefined') {
req.sql.join('localizacao', null, 'id_localizacao=localizacao.pk_localizacao_id')
} }
// Dimensions (add fields) // Dimensions (add fields)
...@@ -116,39 +111,39 @@ enrollmentApp.use('/', parseParams('filter', [ ...@@ -116,39 +111,39 @@ enrollmentApp.use('/', parseParams('filter', [
} }
if(typeof req.dims.region !== 'undefined') { if(typeof req.dims.region !== 'undefined') {
req.sql.field('regioes.nome', 'region_name') req.sql.field('regiao.nome', 'region_name')
.group('regioes.nome') .group('regiao.nome')
.order('regioes.nome'); .order('regiao.nome');
} }
if(typeof req.dims.state !== 'undefined') { if(typeof req.dims.state !== 'undefined') {
req.sql.field('estados.nome', 'state_name') req.sql.field('estado.nome', 'state_name')
.group('estados.nome') .group('estado.nome')
.order('estados.nome'); .order('estado.nome');
} }
if(typeof req.dims.city !== 'undefined') { if(typeof req.dims.city !== 'undefined') {
req.sql.field('municipios.nome', 'city_name') req.sql.field('municipio.nome', 'city_name')
.group('municipios.nome') .group('municipio.nome')
.order('municipios.nome'); .order('municipio.nome');
} }
if(typeof req.dims.school !== 'undefined') { if(typeof req.dims.school !== 'undefined') {
req.sql.field('escolas.nome_entidade', 'school_name') req.sql.field('escola.nome_entidade', 'school_name')
.group('escolas.nome_entidade') .group('escola.nome_entidade')
.order('escolas.nome_entidade'); .order('escola.nome_entidade');
} }
if(typeof req.dims.adm_dependency !== 'undefined') { if(typeof req.dims.adm_dependency !== 'undefined') {
req.sql.field('dependencia_adms.nome', 'adm_dependency_name') req.sql.field('dependencia_adm.nome', 'adm_dependency_name')
.group('dependencia_adms.nome') .group('dependencia_adm.nome')
.order('dependencia_adms.nome'); .order('dependencia_adm.nome');
} }
if(typeof req.dims.location !== 'undefined') { if(typeof req.dims.location !== 'undefined') {
req.sql.field('turmas.id_localizacao', 'location_name') req.sql.field('localizacao.descricao', 'location_name')
.group('turmas.id_localizacao') .group('localizacao.descricao')
.order('turmas.id_localizacao'); .order('localizacao.descricao');
} }
if(typeof req.dims.region === 'undefined' if(typeof req.dims.region === 'undefined'
...@@ -161,11 +156,11 @@ enrollmentApp.use('/', parseParams('filter', [ ...@@ -161,11 +156,11 @@ enrollmentApp.use('/', parseParams('filter', [
// Filter (add where) // Filter (add where)
if (typeof req.filter.min_year !== 'undefined') { if (typeof req.filter.min_year !== 'undefined') {
req.sql.where('turmas.ano_censo>=?', parseInt(req.filter.min_year, 10)); req.sql.where('turma.ano_censo>=?', parseInt(req.filter.min_year, 10));
} }
if (typeof req.filter.max_year !== 'undefined') { if (typeof req.filter.max_year !== 'undefined') {
req.sql.where('turmas.ano_censo<=?', parseInt(req.filter.max_year, 10)); req.sql.where('turma.ano_censo<=?', parseInt(req.filter.max_year, 10));
} }
if (typeof req.filter.adm_dependency !== 'undefined') { if (typeof req.filter.adm_dependency !== 'undefined') {
...@@ -173,7 +168,7 @@ enrollmentApp.use('/', parseParams('filter', [ ...@@ -173,7 +168,7 @@ enrollmentApp.use('/', parseParams('filter', [
} }
if (typeof req.filter.location !== 'undefined') { if (typeof req.filter.location !== 'undefined') {
req.sql.where('turmas.id_localizacao=?', parseInt(req.filter.location, 10)); req.sql.where('turma.id_localizacao=?', parseInt(req.filter.location, 10));
} }
if (typeof req.filter.education_level !== 'undefined') { if (typeof req.filter.education_level !== 'undefined') {
...@@ -189,11 +184,11 @@ enrollmentApp.use('/', parseParams('filter', [ ...@@ -189,11 +184,11 @@ enrollmentApp.use('/', parseParams('filter', [
} }
if (typeof req.filter.city !== 'undefined') { if (typeof req.filter.city !== 'undefined') {
req.sql.where('turmas.fk_municipio_id=?', parseInt(req.filter.city, 10)); req.sql.where('turma.fk_municipio_id=?', parseInt(req.filter.city, 10));
} }
if (typeof req.filter.school !== 'undefined') { if (typeof req.filter.school !== 'undefined') {
req.sql.where('turmas.fk_escola_id=?', parseInt(req.filter.school, 10)); req.sql.where('turma.fk_escola_id=?', parseInt(req.filter.school, 10));
} }
log.debug(req.sql.toParam()); log.debug(req.sql.toParam());
next(); next();
...@@ -201,24 +196,11 @@ enrollmentApp.use('/', parseParams('filter', [ ...@@ -201,24 +196,11 @@ enrollmentApp.use('/', parseParams('filter', [
enrollmentApp.get('/', (req, res, next) => { enrollmentApp.get('/', (req, res, next) => {
req.sql.field('COALESCE(SUM(num_matriculas), 0)', 'total') req.sql.field('COALESCE(SUM(num_matriculas), 0)', 'total')
.field('turmas.ano_censo', 'year') .field('turma.ano_censo', 'year')
.from('turmas') .from('turma')
.group('turmas.ano_censo') .group('turma.ano_censo')
.order('turmas.ano_censo'); .order('turma.ano_censo');
next();
}, query, (req, res, next) => {
// 'Sanitize' result
let r = req.result;
r.forEach((data) => {
if(req.dims.location) {
if(data.location_name === 1) {
data.location_name = 'Urbana';
} else {
data.location_name = 'Rural';
}
}
});
next(); next();
}, response('enrollment')); }, query, response('enrollment'));
module.exports = enrollmentApp; module.exports = enrollmentApp;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment