From 6cac716bc28d0d096e43b6fd63f44f91328ba739 Mon Sep 17 00:00:00 2001
From: Pietro Polinari Cavassin <pietro.polinari@gmail.com>
Date: Thu, 19 May 2022 10:15:52 -0300
Subject: [PATCH] [wip] fix negative percentages and string format

---
 src/libs/routes/classroomCount.js | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/libs/routes/classroomCount.js b/src/libs/routes/classroomCount.js
index c258ba03..27ff6e86 100644
--- a/src/libs/routes/classroomCount.js
+++ b/src/libs/routes/classroomCount.js
@@ -1023,19 +1023,24 @@ classroomCountApp.post('/', rqf.parse(), (req, res, next) => {
         for (let value of teacherByFormation) {
             sum += value;
         }
+        teacherByFormation[1] += teacherByFormation[0]
+        teacherByFormation[0] = 0
+
         let diff = 1 - sum;
-        // Se for menor/maior que 100 soma/subtrai na P2
+        
+        // Se soma de porcentagens for menor/maior que 100, faz correção
         if (Math.abs(diff) > 0.0001) {
-            teacherByFormation[1] += diff;
+            // Garante que a porcentagem corrigida não ficará negativa
+            let indDiff = 1;
+            while (teacherByFormation[indDiff] + diff < 0) indDiff++;
+            teacherByFormation[indDiff] += diff;
         }
-        teacherByFormation[1] += teacherByFormation[0]
-        teacherByFormation[0] = 0
 
         // Cria vetor de porcentagens de carreira dos professores
         req.teacherFormation.forEach((formation, i) => {
             lastCity.percentage_teacher_career.push({
                 formation_level_id: formation.idFormationLevel,
-                percentage: (teacherByFormation[i]*100).toFixed(2),
+                percentage: Number((teacherByFormation[i]*100).toFixed(2)),
             })
         });
 
-- 
GitLab