Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const express = require('express');
const classroomCountApp = express.Router();
const libs = `${process.cwd()}/libs`;
const squel = require('squel');
const query = require(`${libs}/middlewares/query`).query;
const response = require(`${libs}/middlewares/response`);
const ReqQueryFields = require(`${libs}/middlewares/reqQueryFields`);
const id2str = require(`${libs}/middlewares/id2str`);
const addMissing = require(`${libs}/middlewares/addMissing`);
const config = require(`${libs}/config`);
const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware;
let rqf = new ReqQueryFields();
rqf.addField({
name: 'filter',
field: false,
where: true
}).addField({
name: 'dims',
field: true,
where: false
}).addValueToField({
name: 'city',
table: 'municipio',
tableField: 'nome',
resultField: 'city_name',
where: {
relation: '=',
type: 'integer',
field: 'municipio_id',
},
join: {
primary: 'id',
foreign: 'municipio_id',
}
}, 'filter').addValueToField({
name: 'city',
table: 'municipio',
tableField: ['nome', 'id'],
resultField: ['city_name', 'city_id'],
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: 'id',
foreign: 'municipio_id',
name: 'state',
table: 'estado',
tableField: 'nome',
resultField: 'state_name',
where: {
relation: '=',
type: 'integer',
field: 'estado_id',
},
join: {
primary: 'id',
foreign: 'estado_id',
}, 'filter').addValueToField({
name: 'state',
table: 'estado',
tableField: ['nome', 'id'],
resultField: ['state_name', 'state_id'],
where: {
relation: '=',
type: 'integer',
field: 'id',
},
join: {
primary: 'id',
foreign: 'estado_id',
foreignTable: '@'
}
}, 'dims').addValue({
name: 'region',
table: 'regiao',
tableField: 'nome',
resultField: 'region_name',
where: {
relation: '=',
type: 'integer',
field: 'id'
},
join: {
primary: 'id',
foreign: 'regiao_id',
}
}).addValue({
name: 'min_year',
tableField: 'ano_censo',
resultField: 'year',
where: {
relation: '>=',
type: 'integer',
field: 'ano_censo'
}
}).addValue({
name: 'max_year',
tableField: 'ano_censo',
resultField: 'year',
where: {
relation: '<=',
type: 'integer',
field: 'ano_censo'
}
}).addValue({
name: 'period',
tableField: 'turma_turno_id',
resultField: 'period_id',
where: {
relation: '=',
type: 'integer',
field: 'turma_turno_id'
}
}).addValue({
name: 'school_year',
tableField: 'serie_ano_id',
resultField: 'school_year_id',
where: {
relation: '=',
type: 'integer',
field: 'serie_ano_id'
}
}).addValue({
name: 'location',
tableField: 'localizacao_id',
resultField: 'location_id',
where: {
relation: '=',
type: 'integer',
field: 'localizacao_id'
}
}).addValue({
name: 'education_level_short',
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
tableField: 'etapa_resumida',
resultField: 'education_level_short_id',
where: {
relation: '=',
type: 'integer',
field: 'etapa_resumida'
}
});
classroomCountApp.post('/', rqf.parse(), (req, res, next) => {
req.dims.state = true;
req.dims.city = true;
req.dims.period = true;
req.dims.education_level_short = true;
req.dims.location = true;
req.sql.field('COUNT(*)', 'total')
.field("'Brasil'", 'name')
.field('matricula.ano_censo', 'year')
.from('matricula')
.group('matricula.ano_censo')
.order('matricula.ano_censo')
.where('matricula.tipo<=3');
next();
}, rqf.build(), query, id2str.transform(), (req, res, next) => {
req.enrollment = req.result;
req.resetSql();
next();
}, rqf.parse(), (req, res, next) => {
req.dims.state = true;
req.dims.city = true;
req.dims.location = true;
req.sql.field('SUM(escola.num_salas)', 'total')
.field("'Brasil'", 'name')
.field('escola.ano_censo', 'year')
.from('escola')
.group('escola.ano_censo')
.order('escola.ano_censo')
.where('escola.situacao_de_funcionamento = 1 AND escola.local_func_predio_escolar = 1');
next();
}, rqf.build(), query, id2str.transform(), (req, res, next) => {
let classSize = JSON.parse(req.body.class_size) || null;
let integralTime = JSON.parse(req.body.integral_time) || null;
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
req.classroom = req.result;
let classroom = [];
req.classroom.forEach((c) => {
let cityObjects = req.classroom.filter((value) => {
return (c.city_id == value.city_id && c.year == value.year);
});
let obj = {
state_id: c.state_id,
state_name: c.state_name,
city_id: c.city_id,
city_name: c.city_name,
year: c.year,
locations: []
};
cityObjects.forEach((co) => {
obj.locations.push({
location_id: co.location_id,
location_name: co.location_name,
total_classroom: co.total
});
});
if(classroom.findIndex((el) => {return el.city_id === obj.city_id}) === -1) {
classroom.push(obj);
}
});
let result = [];
classroom.forEach((c) => {
function filterUrbanEnrollment(e) {
return (e.city_id == c.city_id && e.year == c.year && e.location_id == 1);
}
function filterRuralEnrollment(e) {
return (e.city_id == c.city_id && e.year == c.year && e.location_id == 2);
}
let urbanEnrollments = req.enrollment.filter(filterUrbanEnrollment);
let ruralEnrollments = req.enrollment.filter(filterRuralEnrollment);
let r = c;
// r.locations[0].enrollments = urbanEnrollments;
// r.locations[1].enrollments = ruralEnrollments;
result.push(r);
});
console.log('FEITO');
// console.log('SALAS');
// console.log(classroom[0]);
// console.log('FILTRO');
// console.log(classEnrollments);
req.result = result;
next();
}, response('classroom_count'));
module.exports = classroomCountApp;