From 2a51e27ecc5e487c1c1d4a5205a4d2f0e25f3a1f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Leon=20A=2E=20Okida=20Gon=C3=A7alves?= <laog19@inf.ufpr.br>
Date: Tue, 28 Mar 2023 09:48:11 -0300
Subject: [PATCH] Add shift converter

---
 src/libs/convert/shift.js               | 30 +++++++++++++++++++++++++
 src/libs/middlewares/id2str.js          |  7 ++++--
 src/libs/routes_v2/simcaqFirstReport.js | 13 ++++++++---
 3 files changed, 45 insertions(+), 5 deletions(-)
 create mode 100644 src/libs/convert/shift.js

diff --git a/src/libs/convert/shift.js b/src/libs/convert/shift.js
new file mode 100644
index 00000000..effe0a52
--- /dev/null
+++ b/src/libs/convert/shift.js
@@ -0,0 +1,30 @@
+/*
+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/>.
+*/
+
+module.exports = function shift(id) {
+    switch(id) {
+        case 1:
+            return 'Parcial';
+        case 2:
+            return 'Integral';
+        default:
+            return 'Não especificado';
+    }
+}
\ No newline at end of file
diff --git a/src/libs/middlewares/id2str.js b/src/libs/middlewares/id2str.js
index bd5c83c0..b59f6463 100644
--- a/src/libs/middlewares/id2str.js
+++ b/src/libs/middlewares/id2str.js
@@ -95,6 +95,7 @@ const enrollmentSituation = require(`${libs}/convert/enrollmentSituation`);
 const diffLocation = require(`${libs}/convert/diffLocation`);
 const peePorCategoria = require(`${libs}/convert/peePorCategoria`);
 const pee = require(`${libs}/convert/booleanVariable`);
+const shift = require(`${libs}/convert/shift`);
 
 const ids = {
     gender_id: gender,
@@ -181,7 +182,8 @@ const ids = {
     enrollment_situation: enrollmentSituation,
     diff_location_id: diffLocation,
     pee_por_categoria: peePorCategoria,
-    pee_id: pee
+    pee_id: pee,
+    shift_id: shift
 };
 
 function transform(removeId=false) {
@@ -295,5 +297,6 @@ module.exports = {
     enrollmentSituation,
     diffLocation,
     peePorCategoria,
-    pee
+    pee,
+    shift
 };
diff --git a/src/libs/routes_v2/simcaqFirstReport.js b/src/libs/routes_v2/simcaqFirstReport.js
index b3bca02b..7f99fbd6 100644
--- a/src/libs/routes_v2/simcaqFirstReport.js
+++ b/src/libs/routes_v2/simcaqFirstReport.js
@@ -40,6 +40,8 @@ const cache = require('apicache').options({ debug: config.debug, statusCodes: {i
 
 let rqf = new ReqQueryFields();
 
+const id2str = require(`${libs}/middlewares/id2str`);
+
 simcaqFirstReportApp.use(cache('15 day'));
 
 rqf.addField({
@@ -110,7 +112,7 @@ rqf.addField({
         field: 'etapa'
     }
 }).addValue({
-    name: 'shift',
+    name: 'shift_id',
     table: 'simcaq_relatorio_1',
     tableField: 'turno',
     where: {
@@ -123,7 +125,7 @@ rqf.addField({
 simcaqFirstReportApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
     req.sql.from('simcaq_relatorio_1')
         .field('simcaq_relatorio_1.etapa', 'education_level_short_id')
-        .field('simcaq_relatorio_1.turno', 'shift')
+        .field('simcaq_relatorio_1.turno', 'shift_id')
         .field('simcaq_relatorio_1.localizacao_id', 'location')
         .field('SUM(simcaq_relatorio_1.num_matriculas)', 'num_enrollments')
         .field('SUM(simcaq_relatorio_1.num_escolas)', 'num_schools')
@@ -133,6 +135,11 @@ simcaqFirstReportApp.get('/', rqf.parse(), rqf.build(), (req, res, next) => {
         .group('simcaq_relatorio_1.turno')
         .group('simcaq_relatorio_1.localizacao_id');
     next();
-}, query, response('simcaqFirstReport'));
+}, query, (req, res, next) => {
+    req.result.forEach((result) => {
+        result.shift = id2str.shift(result.shift_id);
+    });
+    next();
+}, response('simcaqFirstReport'));
 
 module.exports = simcaqFirstReportApp;
-- 
GitLab