Skip to content
Snippets Groups Projects
school.js 22.1 KiB
Newer Older
Vytor Calixto's avatar
Vytor Calixto committed
const express = require('express');

const schoolApp = express.Router();
Vytor Calixto's avatar
Vytor Calixto committed

const libs = `${process.cwd()}/libs`;

const squel = require('squel');

const query = require(`${libs}/middlewares/query`);

const response = require(`${libs}/middlewares/response`);

const id2str = require(`${libs}/middlewares/id2str`);

const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`);
Vytor Calixto's avatar
Vytor Calixto committed

const request = require(`request`);
const config = require(`${libs}/config`);

let rqf = new ReqQueryFields();
let rqfCount = new ReqQueryFields();
Fernando Erd's avatar
Fernando Erd committed
// Return location
Hamer Iboshi's avatar
Hamer Iboshi committed
schoolApp.get('/year_range', (req, res, next) => {
    req.sql.from('escola')
    .field('MIN(escola.ano_censo)', 'start_year')
    .field('MAX(escola.ano_censo)', 'end_year');
    next();
}, query, response('range'));

Vytor Calixto's avatar
Vytor Calixto committed
schoolApp.get('/years', (req, res, next) => {
    req.sql.from('escola').
    field('DISTINCT escola.ano_censo', 'year');
Vytor Calixto's avatar
Vytor Calixto committed
    next();
}, query, response('years'));

Fernando Erd's avatar
Fernando Erd committed
schoolApp.get('/location', (req, res, next) => {
    req.result = [
        {id: 1, name: 'Urbana'},
        {id: 2, name: 'Rural'}
    ];
    next();
}, response('location'));

schoolApp.get('/rural_location', (req, res, next) => {
    req.result = [
        {id: 1, name: "Urbana"},
        {id: 2, name: "Rural"},
        {id: 3, name: "Rural - Área de assentamento"},
        {id: 4, name: "Rural - Terra indígena"},
        {id: 5, name: "Rural - Área remanescente de quilombos"},
        {id: 6, name: "Rural - Unidade de uso sustentável"}
    ];
    next();
}, response('rural_location'));

Fernando Erd's avatar
Fernando Erd committed
schoolApp.get('/adm_dependency', (req, res, next) => {
    req.sql.from('dependencia_adm')
    .field('id')
    .field('nome', 'name')
    .where('id <= 4');
    next();
}, query, response('adm_dependency'));

schoolApp.get('/adm_dependency_detailed', (req, res, next) => {
    req.sql.from('dependencia_adm_priv')
Fernando Erd's avatar
Fernando Erd committed
    .field('id', 'id')
    .field('nome', 'name');
    next();
}, query, response('adm_dependency_detailed'));

schoolApp.get('/government_agreement', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('government_agreement'));

Fernando Erd's avatar
Fernando Erd committed
schoolApp.get('/agreement', (req, res, next) => {
    req.result = [
        {id: 1, name: 'Estadual'},
        {id: 2, name: 'Municipal'},
        {id: 3, name: 'Estadual e Municipal'}
    ];
    next();
}, response('agreement'));

schoolApp.get('/building_school', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('building_school'));

schoolApp.get('/informatics_lab', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('informatics_lab'));


schoolApp.get('/science_lab', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('/science_lab'));

schoolApp.get('/directors_room', (req, res, next) => {
    req.result = [
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('directors_room'));

schoolApp.get('/teacher_room', (req, res, next) => {
    req.result = [
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('teacher_room'));

Fernando Erd's avatar
Fernando Erd committed
schoolApp.get('/cook_room', (req, res, next) => {
    req.result = [
Fernando Erd's avatar
Fernando Erd committed
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('cook_room'));

schoolApp.get('/playground', (req, res, next) => {
Fernando Erd's avatar
Fernando Erd committed
    req.result = [
Fernando Erd's avatar
Fernando Erd committed
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('playground'));

schoolApp.get('/indor_sports_court', (req, res, next) => {
    req.result = [
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('indor_sports_court'));

schoolApp.get('/nusery', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('nusery'));

schoolApp.get('/special_attendence_room', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('special_attendence_room'));

schoolApp.get('/toilet_inside_building', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('toilet_inside_building'));

schoolApp.get('/denpendency_pne', (req, res, next) => {
    req.result = [
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('denpendency_pne'));

schoolApp.get('/restroom_pne', (req, res, next) => {
    req.result = [
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('restroom_pne'));

schoolApp.get('/broadband', (req, res, next) => {
    req.result = [
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('broadband'));

schoolApp.get('/energy', (req, res, next) => {
    req.result = [
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('energy'));

schoolApp.get('/water', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('water'));

schoolApp.get('/wastepipe', (req, res, next) => {
    req.result = [
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('wastepipe'));

schoolApp.get('/education_day_care_child', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('education_day_care_child'));

schoolApp.get('/education_preschool_child', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('education_preschool_child'));

schoolApp.get('/education_begin_elementary_school', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('education_begin_elementary_school'));

schoolApp.get('/education_end_elementary_school', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('education_end_elementary_school'));

schoolApp.get('/education_middle_school', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('education_middle_school'));

schoolApp.get('/education_professional', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('education_professional'));

schoolApp.get('/education_eja', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('education_eja'));
schoolApp.get('/library', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('library'));

schoolApp.get('/reading_room', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('reading_room'));

schoolApp.get('/library_reading_room', (req, res, next) => {
    req.result = [
        {id: null, name: 'Não Declarado'},
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('library_reading_room'));

rqf.addField({
    name: 'filter',
    field: false,
    where: true
}).addValue({
    name: 'id',
    table: 'escola',
    tableField: 'id',
    where: {
        relation: '=',
        type: 'integer',
Vytor Calixto's avatar
Vytor Calixto committed
}).addValue({
    name: 'city',
    table: 'municipio',
    tableField: 'nome',
    resultField: 'city_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'municipio_id',
        primary: 'id',
        foreign: 'municipio_id',
        foreignTable: 'escola'
    }
Vytor Calixto's avatar
Vytor Calixto committed
}).addValue({
    name: 'state',
    table: 'estado',
    tableField: 'nome',
    resultField: 'state_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'estado_id',
        primary: 'id',
        foreign: 'estado_id',
        foreignTable: 'escola'
    }
}).addValue({
    name: 'year',
    table: 'escola',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '=',
        type: 'integer',
        field: 'ano_censo',
        table: 'escola'
    }
Vytor Calixto's avatar
Vytor Calixto committed

rqfCount.addField({
    name: 'filter',
    field: false,
    where: true
}).addField({
    name: 'dims',
    field: true,
    where: false
}).addValue({
    name: 'id',
    table: 'escola',
    tableField: 'id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    }
Vytor Calixto's avatar
Vytor Calixto committed
}).addValueToField({
    name: 'city',
    table: 'municipio',
    tableField: ['nome', 'id'],
    resultField: ['city_name', 'city_id'],
    where: {
        relation: '=',
        type: 'integer',
        field: 'municipio_id',
        table: 'escola'
    },
    join: {
        primary: 'id',
        foreign: 'municipio_id',
        foreignTable: 'escola'
    }
}, 'dims').addValueToField({
    name: 'city',
    table: 'municipio',
    tableField: 'nome',
    resultField: 'city_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'municipio_id',
        table: 'escola'
    },
    join: {
        primary: 'id',
        foreign: 'municipio_id',
        foreignTable: 'escola'
    }
Vytor Calixto's avatar
Vytor Calixto committed
}, 'filter').addValue({
    name: 'state',
    table: 'estado',
    tableField: 'nome',
    resultField: 'state_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'estado_id',
        table: 'escola'
    },
    join: {
        primary: 'id',
        foreign: 'estado_id',
        foreignTable: 'escola'
    }
}).addValue({
    name: 'region',
    table: 'regiao',
    tableField: 'nome',
    resultField: 'region_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: 'id',
        foreign: 'regiao_id',
        foreignTable: 'escola'
    }
}).addValue({
    name: 'year',
    table: 'escola',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '=',
        type: 'integer',
        field: 'ano_censo',
        table: 'escola'
    }
}).addValue({
    name: 'adm_dependency',
    table: 'escola',
    tableField: 'dependencia_adm_id',
    resultField: 'adm_dependency_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'dependencia_adm_id'
    }
}).addValue({
    name: 'adm_dependency_detailed',
    table: 'escola',
    tableField: 'dependencia_adm_priv',
    resultField: 'adm_dependency_detailed_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'dependencia_adm_priv'
}).addValue({
    name: 'location',
    table: 'escola',
    tableField: 'cod_localizacao',
    resultField: 'location_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'cod_localizacao'
    }
}).addValue({
    name: 'rural_location',
    table: 'escola',
    tableField: 'localidade_area_rural',
    resultField: 'rural_location_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'localidade_area_rural'
    }
Fernando Erd's avatar
Fernando Erd committed
}).addValue({
Fernando Erd's avatar
Fernando Erd committed
    name: 'cook_room',
Fernando Erd's avatar
Fernando Erd committed
    table: 'escola',
    tableField: 'cozinha',
Fernando Erd's avatar
Fernando Erd committed
    where: {
        relation: '=',
        type: 'boolean',
        field: 'cozinha'
    }
}).addValue({
    name: 'government_agreement',
    table: 'escola',
    tableField: 'conveniada_pp',
    resultField: 'government_agreement_id',
        type: 'boolean',
}).addValue({
    name: 'informatics_lab',
    table: 'escola',
    tableField: 'lab_informatica',
    resultField: 'informatics_lab_id',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'lab_informatica'
    }
}).addValue({
    name: 'science_lab',
    table: 'escola',
    tableField: 'lab_ciencias',
    resultField: 'science_lab_id',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'lab_ciencias'
    }
}).addValue({
    name: 'special_attendence_room',
    table: 'escola',
    tableField: 'sala_atendimento_especial',
    resultField: 'special_attendence_room_id',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'sala_atendimento_especial'
    }
}).addValue({
    name: 'indor_sports_court',
    table: 'escola',
    tableField: 'quadra_esportes_coberta',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'quadra_esportes_coberta'
    }
}).addValue({
    name: 'education_eja',
    table: 'escola',
    tableField: 'ensino_eja',
    resultField: 'education_eja_id',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'ensino_eja'
    }
}).addValue({
    name: 'education_professional',
    table: 'escola',
    tableField: 'educacao_profissional',
    resultField: 'education_professional_id',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'educacao_profissional'
    }
}).addValue({
    name: 'education_middle_school',
    table: 'escola',
    tableField: 'reg_medio_medio',
    resultField: 'education_middle_school_id',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'reg_medio_medio'
    }
}).addValue({
    name: 'education_end_elementary_school',
    table: 'escola',
    tableField: 'reg_fund_af',
    resultField: 'education_end_elementary_school_id',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'reg_fund_af'
    }
}).addValue({
    name: 'education_begin_elementary_school',
    table: 'escola',
    tableField: 'reg_fund_ai',
    resultField: 'education_begin_elementary_school_id',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'reg_fund_ai'
    }
}).addValue({
    name: 'education_preschool_child',
    table: 'escola',
    tableField: 'reg_infantil_preescola',
    resultField: 'education_preschool_child_id',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'reg_infantil_preescola'
    }
}).addValue({
    name: 'education_day_care_child',
    table: 'escola',
    tableField: 'reg_infantil_creche',
    resultField: 'education_day_care_child_id',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'reg_infantil_creche'
    }
}).addValue({
    name: 'directors_room',
    table: 'escola',
    tableField: 'sala_diretoria',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'sala_diretoria'
    }
}).addValue({
    name: 'teacher_room',
    table: 'escola',
    tableField: 'sala_professor',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'sala_professor'
    }
}).addValue({
    name: 'playground',
    table: 'escola',
    tableField: 'parque_infantil',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'parque_infantil'
    }
}).addValue({
    name: 'nusery',
    table: 'escola',
    tableField: 'bercario',
    resultField: 'nusery_id',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'bercario'
    }
}).addValue({
    name: 'toilet_inside_building',
    table: 'escola',
    tableField: 'sanitario_dentro_predio',
    resultField: 'toilet_inside_building_id',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'sanitario_dentro_predio'
    }
    name: 'wastepipe',
    table: 'escola',
    tableField: 'esgoto_sanitario',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'esgoto_sanitario'
    }
}).addValue({
Fernando Erd's avatar
Fernando Erd committed
    name: 'water',
    table: 'escola',
    tableField: 'fornecimento_agua',
Fernando Erd's avatar
Fernando Erd committed
    resultField: 'water_id',
Fernando Erd's avatar
Fernando Erd committed
    where: {
        relation: '=',
        type: 'boolean',
        field: 'fornecimento_agua '
    }
}).addValue({
    name: 'energy',
    table: 'escola',
    tableField: 'fornecimento_energia',
Fernando Erd's avatar
Fernando Erd committed
    where: {
        relation: '=',
        type: 'boolean',
        field: 'fornecimento_energia '
    }
}).addValue({
    name: 'broadband',
    table: 'escola',
    tableField: 'internet_banda_larga',
Fernando Erd's avatar
Fernando Erd committed
    where: {
        relation: '=',
        type: 'boolean',
        field: 'internet_banda_larga '
    }
}).addValue({
    name: 'restroom_pne',
    table: 'escola',
    tableField: 'sanitario_pne',
Fernando Erd's avatar
Fernando Erd committed
    where: {
        relation: '=',
        type: 'boolean',
        field: 'sanitario_pne '
    }
}).addValue({
    name: 'denpendency_pne',
    table: 'escola',
    tableField: 'dependencias_pne',
Fernando Erd's avatar
Fernando Erd committed
    where: {
        relation: '=',
        type: 'boolean',
        field: 'dependencias_pne '
    }
Fernando Erd's avatar
Fernando Erd committed
}).addValue({
    name: 'agreement',
    table: 'escola',
    tableField: 'tipo_convenio_pp',
Fernando Erd's avatar
Fernando Erd committed
    resultField: 'agreement_id',
Fernando Erd's avatar
Fernando Erd committed
    where: {
        relation: '=',
        type: 'integer',
        field: 'tipo_convenio_pp'
    }
}).addValue({
    name: 'building_school',
    table: 'escola',
    tableField: 'local_func_predio_escolar',
    resultField: 'building_school_id',
Fernando Erd's avatar
Fernando Erd committed
    where: {
        relation: '=',
        type: 'boolean',
        field: 'local_func_predio_escolar'
    }
}).addValue({
    name: 'library',
    table: 'escola',
    tableField: 'biblioteca',
    resultField: 'library_id',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'biblioteca'
    }
}).addValue({
    name: 'reading_room',
    table: 'escola',
    tableField: 'sala_leitura',
    resultField: 'reading_room_id',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'sala_leitura'
    }
}).addValue({
    name: 'library_reading_room',
    table: 'escola',
    tableField: 'biblioteca_sala_leitura',
    resultField: 'library_reading_room_id',
    where: {
        relation: '=',
        type: 'boolean',
        condition: 'or',
        field: 'biblioteca_sala_leitura'
Hamer Iboshi's avatar
Hamer Iboshi committed
}).addValue({
    name: 'min_year',
    table: 'escola',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '>=',
        type: 'integer',
        field: 'ano_censo'
    }
}).addValue({
    name: 'max_year',
    table: 'escola',
    tableField: 'ano_censo',
    resultField: 'year',
    where: {
        relation: '<=',
        type: 'integer',
        field: 'ano_censo'
    }

// .addValue({ //Using multiple_where
//     name: 'library_reading_room',
//     table: 'escola',
//     tableField: ['biblioteca', 'sala_leitura', 'biblioteca_sala_leitura'],
//     resultField: 'library_or_reading_room',
//      where: {
//         relation: '=',
//         type: 'boolean',
//         condition: 'or',
//         field: ['biblioteca', 'sala_leitura', 'biblioteca_sala_leitura']
//      }

// SELECT COUNT(escola.id) AS "total", 'Brasil' AS "name", escola.ano_censo AS "year" FROM escola WHERE (escola.biblioteca = ? OR escola.sala_leitura = ? OR escola.biblioteca_sala_leitura) AND (escola.situacao_de_funcionamento = 1 AND escola.ensino_regular = 1) GROUP BY escola.ano_censo ORDER BY escola.ano_censo ASC
schoolApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
    if(typeof req.filter === 'undefined' || Object.keys(req.filter).length === 0) {
        res.status(400);
        next({
            status: 400,
            message: 'Wrong/No filter specified'
        });
    }
    req.sql.from('escola')
        .field('escola.id')
        .field('escola.ano_censo', 'year')
        .field('escola.nome_escola', 'name')
        .field('escola.estado_id', 'state_id')
        .field('escola.municipio_id', 'city_id');
Vytor Calixto's avatar
Vytor Calixto committed
    next();
}, query, response('school'));

schoolApp.get('/count', rqfCount.parse(), rqfCount.build(), (req, res, next) => {

    req.sql.from('escola')
        .field('COUNT(escola.id)', 'total')
        .field("'Brasil'", 'name')
        .field('escola.ano_censo', 'year')
        .group('escola.ano_censo')
        .order('escola.ano_censo')
        .where('escola.situacao_de_funcionamento = 1 AND (escola.ensino_regular = 1 OR escola.ensino_eja=1 or escola.educacao_profissional=1)');
Vytor Calixto's avatar
Vytor Calixto committed
}, query, id2str.transform(), response('school'));
schoolApp.get('/download', rqf.parse(), rqf.build(), (req, res, next) => {
    req.sql.from('escola')
    .field('*');
    // agenda.now('download database', {sql: req.sql.toString(), table: req.sql.tableFrom});
    let form = {
        query: req.sql.toString(),
        table: req.sql.tableFrom,
        name: req.sql.tableFrom
    };
    request.post(config.cdn.url + '/api/v1/file', {form}, (err, response, body) => {
        if(err) {
            log.error(err);
            return res.json({error: err});
        }
        console.log(body);
        res.json({msg: 'Wait for download email'});
    });
module.exports = schoolApp;