From e758ce8f3ff3bafd815f5021f94f8ca9a7d189ee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Leon=20A=2E=20Okida=20Gon=C3=A7alves?= <laog19@inf.ufpr.br>
Date: Tue, 18 Apr 2023 10:04:09 -0300
Subject: [PATCH] Add enrollment projection route

---
 src/libs/routes_v2/api.js                     |   3 +
 .../routes_v2/simcaqEnrollmentProjection.js   | 143 ++++++++++++++++++
 2 files changed, 146 insertions(+)
 create mode 100644 src/libs/routes_v2/simcaqEnrollmentProjection.js

diff --git a/src/libs/routes_v2/api.js b/src/libs/routes_v2/api.js
index f63dd586..25c829f9 100644
--- a/src/libs/routes_v2/api.js
+++ b/src/libs/routes_v2/api.js
@@ -154,6 +154,8 @@ const simcaqResult = require(`${libs}/routes_v2/simcaqResult`);
 
 const simcaqNumberOfTeachers = require(`${libs}/routes_v2/simcaqNumberOfTeachers`);
 
+const simcaqEnrollmentProjection = require(`${libs}/routes_v2/simcaqEnrollmentProjection`);
+
 api.get('/', (req, res) => {
     res.json({ msg: 'SimCAQ API v2 is running' });
 });
@@ -222,5 +224,6 @@ api.use('/simcaq_number_of_employees', simcaqNumberOfEmployees);
 api.use('/simcaq_new_classes', simcaqNewClasses);
 api.use('/simcaq_result', simcaqResult);
 api.use('/simcaq_number_of_teachers', simcaqNumberOfTeachers);
+api.use('/simcaq_enrollment_projection', simcaqEnrollmentProjection);
 
 module.exports = api;
diff --git a/src/libs/routes_v2/simcaqEnrollmentProjection.js b/src/libs/routes_v2/simcaqEnrollmentProjection.js
new file mode 100644
index 00000000..cb472b4e
--- /dev/null
+++ b/src/libs/routes_v2/simcaqEnrollmentProjection.js
@@ -0,0 +1,143 @@
+/*
+Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of simcaq-node.
+
+simcaq-node is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+simcaq-node is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with simcaq-node.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
+const express = require('express');
+
+const simcaqEnrollmentProjectionApp = 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 reqBody = require(`${libs}/middlewares/reqBody`);
+
+const config = require(`${libs}/config`); 
+
+const cache = require('apicache').options({ debug: config.debug, statusCodes: {include: [200]} }).middleware;
+
+let rqf = new ReqQueryFields();
+
+const id2str = require(`${libs}/middlewares/id2str`);
+
+simcaqEnrollmentProjectionApp.use(cache('15 day'));
+
+rqf.addField({
+    name: 'filter',
+    field: false,
+    where: true
+}).addField({
+    name: 'dims',
+    field: true,
+    where: false
+}).addValue({
+    name: 'year',
+    table: 'simcaq_projecao_de_matricula',
+    tableField: 'ano_censo',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'ano_censo'
+    }
+}).addValue({
+    name: 'city',
+    table: 'municipio',
+    tableField: ['nome', 'id'],
+    resultField: ['city_name', 'city_id'],
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'municipio_id',
+        foreignTable: 'simcaq_projecao_de_matricula'
+    }
+}, 'dims').addValue({
+    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: 'simcaq_projecao_de_matricula'
+    }
+}, 'dims').addValue({
+    name: 'school',
+    table: 'escola_agregada',
+    tableField: 'id',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'id'
+    },
+    join: {
+        primary: 'id',
+        foreign: 'escola_id',
+        foreignTable: 'simcaq_projecao_de_matricula'
+    }
+}, 'dims').addValue({
+    name: 'education_level_short_id',
+    table: 'simcaq_projecao_de_matricula',
+    tableField: 'etapa',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'etapa'
+    }
+}).addValue({
+    name: 'adm_dependency_public_id',
+    table: 'simcaq_projecao_de_matricula',
+    tableField: 'rede',
+    where: {
+        relation: '=',
+        type: 'integer',
+        field: 'rede'
+    }
+});
+
+simcaqEnrollmentProjectionApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
+    req.sql.from('simcaq_projecao_de_matricula')
+        .field('simcaq_projecao_de_matricula.ano_censo', 'year')
+        .field('simcaq_projecao_de_matricula.etapa', 'education_level_short_id')
+        .field('simcaq_projecao_de_matricula.rede', 'adm_dependency_public_id')
+        .field('SUM(simcaq_projecao_de_matricula.total_matriculas)', 'total_enrollments')
+        .field('SUM(simcaq_projecao_de_matricula.matriculas_diurno)', 'daytime_enrollments')
+        .field('SUM(simcaq_projecao_de_matricula.matriculas_noturno)', 'nighttime_enrollments')
+        .field('SUM(simcaq_projecao_de_matricula.total_tempo_integral)', 'fulltime_enrollments')
+        .group('simcaq_projecao_de_matricula.ano_censo')
+        .group('simcaq_projecao_de_matricula.etapa')
+        .group('simcaq_projecao_de_matricula.rede');
+    next();
+}, query, id2str.transform(), response('enrollmentProjection'));
+
+module.exports = simcaqEnrollmentProjectionApp;
-- 
GitLab