Skip to content
Snippets Groups Projects
school.js 17.4 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

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

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')
    .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('/library_or_reading_room', (req, res, next) => {
    req.result = [
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('library_or_reading_room'));
schoolApp.get('/informatics_lab', (req, res, next) => {
    req.result = [
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('informatics_lab'));


schoolApp.get('/science_lab', (req, res, next) => {
    req.result = [
        {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 = [
        {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 = [
        {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: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('nusery'));

schoolApp.get('/special_attendence_room', (req, res, next) => {
    req.result = [
        {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: 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('/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: 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: 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: 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: 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: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('education_middle_school'));

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

schoolApp.get('/education_eja', (req, res, next) => {
    req.result = [
        {id: 0, name: 'Não'},
        {id: 1, name: 'Sim'}
    ];
    next();
}, response('education_eja'));
rqf.addField({
    name: 'filter',
    field: false,
    where: true
}).addValue({
    name: 'id',
    table: 'escola',
    tableField: 'id',
    where: {
        relation: '=',
        type: 'integer',
    }
}).addValue({
    name: 'city',
    table: 'municipio',
    tableField: 'nome',
    resultField: 'city_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'municipio_id',
        primary: 'id',
        foreign: 'municipio_id',
        foreignTable: 'escola'
    }
}).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'
    }
}).addValue({
    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'
    }
}).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: 'dependencia_adm',
    tableField: 'nome',
    resultField: 'adm_dependency_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: 'id',
        foreign: 'dependencia_adm_id',
        foreignTable: 'escola'
    }
}).addValue({
    name: 'adm_dependency_detailed',
    table: 'dependencia_adm',
    tableField: 'nome',
    resultField: 'adm_dependency_detailed_name',
    where: {
        relation: '=',
        type: 'integer',
        field: 'id'
    },
    join: {
        primary: 'id',
        foreign: 'dependencia_adm_priv',
        foreignTable: 'escola'
    }
}).addValue({
    name: 'location',
    table: 'escola',
    tableField: 'cod_localizacao',
    resultField: 'location_id',
    where: {
        relation: '=',
        type: 'integer',
        field: 'cod_localizacao'
    }
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
    resultField: 'cook_room_name',
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_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'lab_informatica'
    }
}).addValue({
    name: 'science_lab',
    table: 'escola',
    tableField: 'lab_ciencias',
    resultField: 'informatics_lab_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'lab_ciencias'
    }
}).addValue({
    name: 'special_attendence_room',
    table: 'escola',
    tableField: 'sala_atendimento_especial',
    resultField: 'special_attendence_room_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'sala_atendimento_especial'
    }
}).addValue({
    name: 'indor_sports_court',
    table: 'escola',
    tableField: 'quadra_esportes_coberta',
    resultField: 'indor_sports_court_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'quadra_esportes_coberta'
    }
}).addValue({
    name: 'education_eja',
    table: 'escola',
    tableField: 'ensino_eja',
    resultField: 'education_eja_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'ensino_eja'
    }
}).addValue({
    name: 'education_professional',
    table: 'escola',
    tableField: 'educacao_profissional',
    resultField: 'education_professional_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'educacao_profissional'
    }
}).addValue({
    name: 'education_middle_school',
    table: 'escola',
    tableField: 'reg_medio_medio',
    resultField: 'education_middle_school_name',
    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_name',
    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_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'reg_fund_ai'
    }
}).addValue({
    name: 'education_preschool_child',
    table: 'escola',
    tableField: 'reg_infantil_preescola',
    resultField: 'education_preschool_child_name',
    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_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'reg_infantil_creche'
    }
}).addValue({
    name: 'directors_room',
    table: 'escola',
    tableField: 'sala_diretoria',
    resultField: 'directors_room_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'sala_diretoria'
    }
}).addValue({
    name: 'teacher_room',
    table: 'escola',
    tableField: 'sala_professor',
    resultField: 'teacher_room_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'sala_professor'
    }
}).addValue({
    name: 'playground',
    table: 'escola',
    tableField: 'parque_infantil',
    resultField: 'playground_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'parque_infantil'
    }
}).addValue({
    name: 'nusery',
    table: 'escola',
    tableField: 'bercario',
    resultField: 'nusery_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'bercario'
    }
}).addValue({
    name: 'toilet_inside_building',
    table: 'escola',
    tableField: 'sanitario_dentro_predio',
    resultField: 'toilet_inside_building_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'sanitario_dentro_predio'
    }
Fernando Erd's avatar
Fernando Erd committed
  }).addValue({
    name: 'wastepipe',
    table: 'escola',
    tableField: 'esgoto_sanitario',
    resultField: 'wastepipe_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'esgoto_sanitario'
    }
}).addValue({
Fernando Erd's avatar
Fernando Erd committed
    name: 'water',
    table: 'escola',
    tableField: 'fornecimento_agua',
    resultField: 'water_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'fornecimento_agua '
    }
}).addValue({
    name: 'energy',
    table: 'escola',
    tableField: 'fornecimento_energia',
    resultField: 'energy_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'fornecimento_energia '
    }
}).addValue({
    name: 'broadband',
    table: 'escola',
    tableField: 'internet_banda_larga',
    resultField: 'broadband_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'internet_banda_larga '
    }
}).addValue({
    name: 'restroom_pne',
    table: 'escola',
    tableField: 'sanitario_pne',
    resultField: 'restroom_pne_name',
    where: {
        relation: '=',
        type: 'boolean',
        field: 'sanitario_pne '
    }
}).addValue({
    name: 'denpendency_pne',
    table: 'escola',
    tableField: 'dependencias_pne',
    resultField: 'denpendency_pne_name',
    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_or_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) => {
    console.log(req.filter);
    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');
    next();
}, query, id2str.transform(true), response('school'));
module.exports = schoolApp;