From b1dc91f36dc8df0511536a71c0032ee2f3310670 Mon Sep 17 00:00:00 2001
From: Agustin <agusr1999@gmail.com>
Date: Wed, 22 Sep 2021 19:06:41 -0300
Subject: [PATCH] Minor fixes, swagger update

Calculations work but the actual values returned haven't been checked
---
 src/Controllers/CalculatorController.ts |   5 +-
 src/DTOs/AgeGroupJSON.ts                |   6 +-
 src/DTOs/MinorPALDTO.ts                 |   2 +-
 src/Services/ERCalculator.ts            |   4 +-
 src/Services/ParameterService.ts        |   2 +-
 src/Services/ParserService.ts           |   8 +-
 swagger.yaml                            | 101 +++++++++++++++++++++---
 7 files changed, 104 insertions(+), 24 deletions(-)

diff --git a/src/Controllers/CalculatorController.ts b/src/Controllers/CalculatorController.ts
index 3550ab2..dbdf560 100644
--- a/src/Controllers/CalculatorController.ts
+++ b/src/Controllers/CalculatorController.ts
@@ -1,17 +1,14 @@
 import {
   Handler, Request, Response, Router,
 } from 'express';
-import AgeGroupJSON from '../DTOs/AgeGroupJSON';
 import CalculatorService from '../Services/CalculatorService';
 import CalculatorResponse from '../DTOs/CalculatorResponseDTO';
 import logger from '../Logger/logger';
-import ExtraData from '../DTOs/ExtraDataDTO';
 
 const router = Router();
 
 const getREP: Handler = async (req: Request, res: Response) => {
-  const { groups } = req.body;
-  const { extraData } = req.body;
+  const { groups, extraData } = req.body;
   try {
     const EnergyReq:
     CalculatorResponse = CalculatorService.calculateEnergeticRequirement(groups, extraData);
diff --git a/src/DTOs/AgeGroupJSON.ts b/src/DTOs/AgeGroupJSON.ts
index 0a795b4..6459407 100644
--- a/src/DTOs/AgeGroupJSON.ts
+++ b/src/DTOs/AgeGroupJSON.ts
@@ -1,8 +1,8 @@
 type AgeGroupJSON = {
-  age: string;
+  age: string; // TODO: check enum
   sex: string;
-  medianWeight: string;
-  population: string;
+  medianWeight: number; // TODO: check if it can be number again
+  population: number; // TODO: check if it can be number again
 };
 
 export default AgeGroupJSON;
diff --git a/src/DTOs/MinorPALDTO.ts b/src/DTOs/MinorPALDTO.ts
index 9633fca..75b39bd 100644
--- a/src/DTOs/MinorPALDTO.ts
+++ b/src/DTOs/MinorPALDTO.ts
@@ -1,5 +1,5 @@
 type MinorPAL = {
-  lowPalPrevalence: number;
+  lowPALPrevalence: number;
   moderatePALPrevalence: number;
   intensePALPrevalence: number;
 };
diff --git a/src/Services/ERCalculator.ts b/src/Services/ERCalculator.ts
index b75ec53..3a3513a 100644
--- a/src/Services/ERCalculator.ts
+++ b/src/Services/ERCalculator.ts
@@ -28,7 +28,7 @@ const calculateTEE = (group: AgeGroup, params: number[], preval: MinorPAL): numb
   const teeLow: number = teeModerate - (teeModerate * params[4]) / 100;
   const teeIntense: number = teeModerate + (teeModerate * params[5]) / 100;
 
-  const ret: number = (teeLow * preval.lowPalPrevalence) / 100
+  const ret: number = (teeLow * preval.lowPALPrevalence) / 100
   + (teeModerate * preval.moderatePALPrevalence) / 100
   + (teeIntense * preval.intensePALPrevalence) / 100;
 
@@ -41,7 +41,7 @@ const calculateBMR = (group: AgeGroup, params: number[]): number => {
   return ret;
 };
 
-// PAL (Physical activity level) = NAF (Nivel de Actividad Fisica)
+// PAL (Physical Activity Level) = NAF (Nivel de Actividad Fisica)
 const calculatePAL = (params: number[], popData: AdultPAL): number => {
   const ruralPAL: number = (popData.activeRuralPAL * params[2]) / 100
   + (popData.lowRuralPAL * params[3]) / 100;
diff --git a/src/Services/ParameterService.ts b/src/Services/ParameterService.ts
index bd847ce..30f6085 100644
--- a/src/Services/ParameterService.ts
+++ b/src/Services/ParameterService.ts
@@ -328,7 +328,7 @@ const getEquationValues = (ageBracket: AgeBracket, sex: Sex): number[] => {
     }
 
     default: {
-      throw new Error('Parsing error, attribute edad does not respect format');
+      throw new Error(`Parsing error, attribute edad does not respect format ${ageBracket}`);
     }
   }
   return res;
diff --git a/src/Services/ParserService.ts b/src/Services/ParserService.ts
index 1da2a27..dc7131b 100644
--- a/src/Services/ParserService.ts
+++ b/src/Services/ParserService.ts
@@ -169,8 +169,8 @@ const parseGroups = (groups: AgeGroupJSON[]): AgeGroup[] => {
     const group: AgeGroup = {
       age: ageGroup.age as AgeBracket,
       sex: ageGroup.sex as Sex,
-      medianWeight: parseFloat(ageGroup.medianWeight),
-      population: parseFloat(ageGroup.population),
+      medianWeight: ageGroup.medianWeight,
+      population: ageGroup.population,
     };
     retGroups.push(group);
   });
@@ -181,8 +181,8 @@ const unparseGroup = (group: AgeGroup): AgeGroupJSON => {
   const retGroup: AgeGroupJSON = {
     age: group.age as string,
     sex: group.sex as string,
-    medianWeight: String(group.medianWeight),
-    population: String(group.population),
+    medianWeight: group.medianWeight,
+    population: group.population,
   };
   return retGroup;
 };
diff --git a/swagger.yaml b/swagger.yaml
index a531d9d..1adc477 100644
--- a/swagger.yaml
+++ b/swagger.yaml
@@ -46,19 +46,21 @@ paths:
         - BearerAuth: []
 
   /repCalculator:
-      post:
-        tags:
-          - Calculation
+    post:
+      tags:
+        - Calculation
       summary: Given population data calculates its energetic requirement
       requestBody:
         content: 
           application/json:
             schema:
-              $ref: '#/components/schemas/AgeGroupJSON'
+              $ref: '#/components/schemas/ReqCalculation'
         required: true
       responses:
         '200':
           description: Ok.
+        '400':
+          description: Bad request.
       security:
         - BearerAuth: []
 components:
@@ -107,11 +109,92 @@ components:
           type: string
     AgeGroupJSON:
       properties:
-        edad:
+        age:
           type: string
-        sexo:
+        sex:
           type: string
-        pesoMediano:
+        medianWeight:
           type: string
-        cantidad:
-          type: string
\ No newline at end of file
+        population:
+          type: string
+      required:
+        - age
+        - sex
+        - medianWeight
+        - population
+    MinorPAL:
+      properties:
+        lowPALPrevalence:
+          type: number
+        moderatePALPrevalence:
+          type: number
+        intensePALPrevalence:
+          type: number
+      required:
+        - lowPALPrevalence
+        - moderatePALPrevalence
+        - intensePALPrevalence
+    AdultPAL:
+      properties:
+        urbanPercentage:
+          type: number
+        activeUrbanPAL:
+          type: number
+        lowUrbanPAL:
+          type: number
+        ruralPercentage:
+          type: number
+        activeRuralPAL:
+          type: number
+        lowRuralPAL:
+          type: number
+      required:
+        - urbanPercentage
+        - activeUrbanPAL
+        - lowUrbanPAL
+        - ruralPercentage
+        - activeRuralPAL
+        - lowRuralPAL
+    IndividualMaternity:
+      properties:
+        pregnantWomen:
+          type: number
+        lactatingWomen:
+          type: number
+      required:
+        - pregnantWomen
+        - lactatingWomen
+    PopulationMaternity:
+      properties:
+        countryBirthRate:
+          type: number
+        countryPopulation:
+          type: number
+      required:
+        - countryBirthRate
+        - countryPopulation
+    ExtraData:
+      properties:
+        minorPAL:
+          $ref: '#/components/schemas/MinorPAL'
+        adultPAL:
+          $ref: '#/components/schemas/AdultPAL'
+        maternity18to29:
+          oneOf:
+            - $ref: '#/components/schemas/IndividualMaternity'
+            - $ref: '#/components/schemas/PopulationMaternity'
+        maternity30to59:
+          oneOf:
+            - $ref: '#/components/schemas/IndividualMaternity'
+            - $ref: '#/components/schemas/PopulationMaternity'
+    ReqCalculation:
+      properties:
+        groups:
+          type: array
+          items:
+            $ref: '#/components/schemas/AgeGroupJSON'
+        extraData:
+          $ref: '#/components/schemas/ExtraData'
+      required:
+        - groups
+        - extraData
\ No newline at end of file
-- 
GitLab