diff --git a/src/Controllers/CalculatorController.ts b/src/Controllers/CalculatorController.ts index 51a0f43f142915352c4bbe53300d8aad1394805f..3550ab2fb5b9b9dc796f27298370014a8cd42cf7 100644 --- a/src/Controllers/CalculatorController.ts +++ b/src/Controllers/CalculatorController.ts @@ -5,13 +5,16 @@ 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: AgeGroupJSON[] = req.body; + const { groups } = req.body; + const { extraData } = req.body; try { - const EnergyReq: CalculatorResponse = CalculatorService.calculateEnergeticRequirement(groups); + const EnergyReq: + CalculatorResponse = CalculatorService.calculateEnergeticRequirement(groups, extraData); return res.status(200).send(EnergyReq); } catch (error) { const e = error as Error; diff --git a/src/DTOs/AdultPALDTO.ts b/src/DTOs/AdultPALDTO.ts new file mode 100644 index 0000000000000000000000000000000000000000..239593338f3066ec6c1baed1c0e89056590f9176 --- /dev/null +++ b/src/DTOs/AdultPALDTO.ts @@ -0,0 +1,10 @@ +type AdultPAL = { + porcentajeUrbano: number; + NAFActivosUrbano: number; + NAFLivianaUrbano: number; + porcentajeRural: number; + NAFActivosRural: number; + NAFLivianaRural: number; +}; + +export default AdultPAL; diff --git a/src/DTOs/ExtraDataDTO.ts b/src/DTOs/ExtraDataDTO.ts new file mode 100644 index 0000000000000000000000000000000000000000..c78835dd1e6139986ae5cafe13d76172597c9ea3 --- /dev/null +++ b/src/DTOs/ExtraDataDTO.ts @@ -0,0 +1,12 @@ +import AdultPAL from './AdultPALDTO'; +import IndividualMaternity from './IndividualMaternityDTO'; +import MinorPAL from './MinorPALDTO'; +import PopulationMaternity from './PopulationMaternityDTO'; + +type ExtraData = { + prevalenciaAFMenores: MinorPAL | undefined; + prevalenciaAFAdultos: AdultPAL | undefined; + datosEmbarazoLactancia: IndividualMaternity | PopulationMaternity | undefined; +}; + +export default ExtraData; diff --git a/src/DTOs/IndividualMaternityDTO.ts b/src/DTOs/IndividualMaternityDTO.ts new file mode 100644 index 0000000000000000000000000000000000000000..d6a07a631f9ba6373cf6ddbe602b97cdca9cc5ae --- /dev/null +++ b/src/DTOs/IndividualMaternityDTO.ts @@ -0,0 +1,7 @@ +type IndividualMaternity = { + mujeresEmbarazadas: number; + mujeresLactando: number; + mujeresEmbarazadasLactando: number; +}; + +export default IndividualMaternity; diff --git a/src/DTOs/MinorPALDTO.ts b/src/DTOs/MinorPALDTO.ts new file mode 100644 index 0000000000000000000000000000000000000000..dfb3ff83bc030aeb671be4bf6dd123a1964fe997 --- /dev/null +++ b/src/DTOs/MinorPALDTO.ts @@ -0,0 +1,7 @@ +type MinorPAL = { + NAFLiviano: number; + NAFModerado: number; + NAFIntenso: number; +}; + +export default MinorPAL; diff --git a/src/DTOs/PopulationMaternityDTO.ts b/src/DTOs/PopulationMaternityDTO.ts new file mode 100644 index 0000000000000000000000000000000000000000..8345efe4804470533d979ee2e758c9377f5dfcfd --- /dev/null +++ b/src/DTOs/PopulationMaternityDTO.ts @@ -0,0 +1,6 @@ +type PopulationMaternity = { + tasaNatalidadPais: number; + poblacionPais: number; +}; + +export default PopulationMaternity; diff --git a/src/Services/CalculatorService.ts b/src/Services/CalculatorService.ts index e0f628e668aebe230f458e17a2d1f16af01fefc6..66c82cebebdacc6da3054e0a10374058e01b61bc 100644 --- a/src/Services/CalculatorService.ts +++ b/src/Services/CalculatorService.ts @@ -4,15 +4,17 @@ import ParserService from './ParserService'; import AgeGroup from '../DTOs/AgeGroupDTO'; import CalculatorResponse from '../DTOs/CalculatorResponseDTO'; import AgeGroupJSON from '../DTOs/AgeGroupJSON'; +import extraData from '../DTOs/ExtraDataDTO'; -const calculateEnergeticRequirement = (data: AgeGroupJSON[]): CalculatorResponse => { +// eslint-disable-next-line max-len +const calculateEnergeticRequirement = (groups: AgeGroupJSON[], data: extraData): CalculatorResponse => { const parametros = new Map<number[], AgeGroup>(); - const ageGroups = ParserService.parseGroups(data); + const ageGroups = ParserService.parseGroups(groups); ageGroups.forEach((group: AgeGroup) => { parametros.set(ParameterService.getEquationValues(group.edad, group.sexo), group); }); - const res: CalculatorResponse = ERCalculator.calculateER(parametros); + const res: CalculatorResponse = ERCalculator.calculateER(parametros, data); return res; }; diff --git a/src/Services/ERCalculator.ts b/src/Services/ERCalculator.ts index 6be7f98ec3b015fe103f03619f1a23943b2b7c81..037caffc5ed8eeef92bd44f90ca497ca6e1875d8 100644 --- a/src/Services/ERCalculator.ts +++ b/src/Services/ERCalculator.ts @@ -2,9 +2,26 @@ import AgeGroup from '../DTOs/AgeGroupDTO'; import EnergeticRequirement from '../DTOs/EnergeticRequirementDTO'; import CalculatorResponse from '../DTOs/CalculatorResponseDTO'; import GroupEnergeticRequirement from '../DTOs/GroupEnergeticRequirementDTO'; +import extraData from '../DTOs/ExtraDataDTO'; +import prevalenciaAFMenores from '../DTOs/MinorPALDTO'; import ParserService from './ParserService'; import AgeBracket from '../Enum/AgeBracket'; +const calculateGET = (group: AgeGroup, params: number[], preval: prevalenciaAFMenores): number => { + const getModerado: number = params[0] + + (params[1] * group.pesoMediano) + - params[2] * (group.pesoMediano * group.pesoMediano); + + const getLiviano: number = getModerado - (getModerado * params[4]) / 100; + const getIntenso: number = getModerado + (getModerado * params[5]) / 100; + + const ret: number = (getLiviano * preval.NAFLiviano) / 100 + + (getModerado * preval.NAFModerado) / 100 + + (getIntenso * preval.NAFIntenso) / 100; + + return ret; +}; + const calculateLessThanAYear = (group: AgeGroup, params: number[]): GroupEnergeticRequirement => { const requirement = params[0] + (params[1] * group.pesoMediano) + params[2]; @@ -32,7 +49,28 @@ const calculateOneToFiveYears = (group: AgeGroup, params: number[]): GroupEnerge return groupRequirement; }; -const calculateER = (groupParameters: Map<number[], AgeGroup>): CalculatorResponse => { +// eslint-disable-next-line max-len +const calculateSixToSeventeenYears = (group: AgeGroup, params: number[], data: extraData): GroupEnergeticRequirement => { + let get: number; + if (typeof (data.prevalenciaAFMenores) === 'undefined') { + throw new Error('Data missing'); + } else { + get = calculateGET(group, params, data.prevalenciaAFMenores); + } + + const requirement = get + params[3]; + + const groupRequirement: GroupEnergeticRequirement = { + grupoEtario: ParserService.unparseGroup(group), + requerimientoEnergeticoPerCapita: requirement, + requerimientoEnergeticoTotal: requirement * group.cantidad, + }; + + return groupRequirement; +}; + +// eslint-disable-next-line max-len +const calculateER = (groupParameters: Map<number[], AgeGroup>, data: extraData): CalculatorResponse => { let totalOfPeople = 0; let totalRequirement = 0; @@ -65,6 +103,21 @@ const calculateER = (groupParameters: Map<number[], AgeGroup>): CalculatorRespon groupRequirement = calculateOneToFiveYears(group, params); break; } + case AgeBracket.a6: + case AgeBracket.a7: + case AgeBracket.a8: + case AgeBracket.a9: + case AgeBracket.a10: + case AgeBracket.a11: + case AgeBracket.a12: + case AgeBracket.a13: + case AgeBracket.a14: + case AgeBracket.a15: + case AgeBracket.a16: + case AgeBracket.a17: { + groupRequirement = calculateSixToSeventeenYears(group, params, data); + break; + } default: { throw new Error('Parsing error, attribute edad does not respect format'); } diff --git a/src/Services/ParameterService.ts b/src/Services/ParameterService.ts index efcfd53dd83e1f25b7965ff38599a39a5e1ec1a2..fffa9493e622c757b1389cd76b1c5953fa2ab712 100644 --- a/src/Services/ParameterService.ts +++ b/src/Services/ParameterService.ts @@ -178,121 +178,121 @@ const getEquationValues = (ageBracket: AgeBracket, sex: Sex): number[] => { } case (AgeBracket.a6): { if (sex === Sex.Masculino) { - // 310.2 + (63.3*MP) - 0.263*MP^2 + 11 - res = [310.2, 63.3, -0.263, 12]; + // 310.2 + (63.3*MP) - 0.263*MP^2 + 11, -15%/+15% para GET liviano/intenso + res = [310.2, 63.3, -0.263, 12, 15, 15]; } else if (sex === Sex.Femenino) { - // 263.4 + (65.3*MP) - 0.454*MP^2 + 10 - res = [263.4, 65.3, -0.454, 13]; + // 263.4 + (65.3*MP) - 0.454*MP^2 + 10, -15%/+15% para GET liviano/intenso + res = [263.4, 65.3, -0.454, 13, 15, 15]; } break; } case (AgeBracket.a7): { if (sex === Sex.Masculino) { - // 310.2 + (63.3*MP) - 0.263*MP^2 + 14 - res = [310.2, 63.3, -0.263, 14]; + // 310.2 + (63.3*MP) - 0.263*MP^2 + 14, -15%/+15% para GET liviano/intenso + res = [310.2, 63.3, -0.263, 14, 15, 15]; } else if (sex === Sex.Femenino) { - // 263.4 + (65.3*MP) - 0.454*MP^2 + 17 - res = [263.4, 65.3, -0.454, 17]; + // 263.4 + (65.3*MP) - 0.454*MP^2 + 17, -15%/+15% para GET liviano/intenso + res = [263.4, 65.3, -0.454, 17, 15, 15]; } break; } case (AgeBracket.a8): { if (sex === Sex.Masculino) { - // 310.2 + (63.3*MP) - 0.263*MP^2 + 16 - res = [310.2, 63.3, -0.263, 16]; + // 310.2 + (63.3*MP) - 0.263*MP^2 + 16, -15%/+15% para GET liviano/intenso + res = [310.2, 63.3, -0.263, 16, 15, 15]; } else if (sex === Sex.Femenino) { - // 263.4 + (65.3*MP) - 0.454*MP^2 + 20 - res = [263.4, 65.3, -0.454, 20]; + // 263.4 + (65.3*MP) - 0.454*MP^2 + 20, -15%/+15% para GET liviano/intenso + res = [263.4, 65.3, -0.454, 20, 15, 15]; } break; } case (AgeBracket.a9): { if (sex === Sex.Masculino) { - // 310.2 + (63.3*MP) - 0.263*MP^2 + 19 - res = [310.2, 63.3, -0.263, 19]; + // 310.2 + (63.3*MP) - 0.263*MP^2 + 19, -15%/+15% para GET liviano/intenso + res = [310.2, 63.3, -0.263, 19, 15, 15]; } else if (sex === Sex.Femenino) { - // 263.4 + (65.3*MP) - 0.454*MP^2 + 23 - res = [263.4, 65.3, -0.454, 23]; + // 263.4 + (65.3*MP) - 0.454*MP^2 + 23, -15%/+15% para GET liviano/intenso + res = [263.4, 65.3, -0.454, 23, 15, 15]; } break; } case (AgeBracket.a10): { if (sex === Sex.Masculino) { - // 310.2 + (63.3*MP) - 0.263*MP^2 + 22 - res = [310.2, 63.3, -0.263, 22]; + // 310.2 + (63.3*MP) - 0.263*MP^2 + 22, -15%/+15% para GET liviano/intenso + res = [310.2, 63.3, -0.263, 22, 15, 15]; } else if (sex === Sex.Femenino) { - // 263.4 + (65.3*MP) - 0.454*MP^2 + 25 - res = [263.4, 65.3, -0.454, 25]; + // 263.4 + (65.3*MP) - 0.454*MP^2 + 25, -15%/+15% para GET liviano/intenso + res = [263.4, 65.3, -0.454, 25, 15, 15]; } break; } case (AgeBracket.a11): { if (sex === Sex.Masculino) { - // 310.2 + (63.3*MP) - 0.263*MP^2 + 25 - res = [310.2, 63.3, -0.263, 25]; + // 310.2 + (63.3*MP) - 0.263*MP^2 + 25, -15%/+15% para GET liviano/intenso + res = [310.2, 63.3, -0.263, 25, 15, 15]; } else if (sex === Sex.Femenino) { - // 263.4 + (65.3*MP) - 0.454*MP^2 + 25 - res = [263.4, 65.3, -0.454, 25]; + // 263.4 + (65.3*MP) - 0.454*MP^2 + 25, -15%/+15% para GET liviano/intenso + res = [263.4, 65.3, -0.454, 25, 15, 15]; } break; } case (AgeBracket.a12): { if (sex === Sex.Masculino) { - // 310.2 + (63.3*MP) - 0.263*MP^2 + 29 - res = [310.2, 63.3, -0.263, 29]; + // 310.2 + (63.3*MP) - 0.263*MP^2 + 29, -15%/+15% para GET liviano/intenso + res = [310.2, 63.3, -0.263, 29, 15, 15]; } else if (sex === Sex.Femenino) { - // 263.4 + (65.3*MP) - 0.454*MP^2 + 26 - res = [263.4, 65.3, -0.454, 26]; + // 263.4 + (65.3*MP) - 0.454*MP^2 + 26, -15%/+15% para GET liviano/intenso + res = [263.4, 65.3, -0.454, 26, 15, 15]; } break; } case (AgeBracket.a13): { if (sex === Sex.Masculino) { - // 310.2 + (63.3*MP) - 0.263*MP^2 + 33 - res = [310.2, 63.3, -0.263, 33]; + // 310.2 + (63.3*MP) - 0.263*MP^2 + 33, -15%/+15% para GET liviano/intenso + res = [310.2, 63.3, -0.263, 33, 15, 15]; } else if (sex === Sex.Femenino) { - // 263.4 + (65.3*MP) - 0.454*MP^2 + 24 - res = [263.4, 65.3, -0.454, 24]; + // 263.4 + (65.3*MP) - 0.454*MP^2 + 24, -15%/+15% para GET liviano/intenso + res = [263.4, 65.3, -0.454, 24, 15, 15]; } break; } case (AgeBracket.a14): { if (sex === Sex.Masculino) { - // 310.2 + (63.3*MP) - 0.263*MP^2 + 33 - res = [310.2, 63.3, -0.263, 33]; + // 310.2 + (63.3*MP) - 0.263*MP^2 + 33, -15%/+15% para GET liviano/intenso + res = [310.2, 63.3, -0.263, 33, 15, 15]; } else if (sex === Sex.Femenino) { - // 263.4 + (65.3*MP) - 0.454*MP^2 + 19 - res = [263.4, 65.3, -0.454, 19]; + // 263.4 + (65.3*MP) - 0.454*MP^2 + 19, -15%/+15% para GET liviano/intenso + res = [263.4, 65.3, -0.454, 19, 15, 15]; } break; } case (AgeBracket.a15): { if (sex === Sex.Masculino) { - // 310.2 + (63.3*MP) - 0.263*MP^2 + 31 - res = [310.2, 63.3, -0.263, 31]; + // 310.2 + (63.3*MP) - 0.263*MP^2 + 31, -15%/+15% para GET liviano/intenso + res = [310.2, 63.3, -0.263, 31, 15, 15]; } else if (sex === Sex.Femenino) { - // 263.4 + (65.3*MP) - 0.454*MP^2 + 13 - res = [263.4, 65.3, -0.454, 13]; + // 263.4 + (65.3*MP) - 0.454*MP^2 + 13, -15%/+15% para GET liviano/intenso + res = [263.4, 65.3, -0.454, 13, 15, 15]; } break; } case (AgeBracket.a16): { if (sex === Sex.Masculino) { - // 310.2 + (63.3*MP) - 0.263*MP^2 + 24 - res = [310.2, 63.3, -0.263, 24]; + // 310.2 + (63.3*MP) - 0.263*MP^2 + 24, -15%/+15% para GET liviano/intenso + res = [310.2, 63.3, -0.263, 24, 15, 15]; } else if (sex === Sex.Femenino) { - // 263.4 + (65.3*MP) - 0.454*MP^2 + 5 - res = [263.4, 65.3, -0.454, 5]; + // 263.4 + (65.3*MP) - 0.454*MP^2 + 5, -15%/+15% para GET liviano/intenso + res = [263.4, 65.3, -0.454, 5, 15, 15]; } break; } case (AgeBracket.a17): { if (sex === Sex.Masculino) { - // 310.2 + (63.3*MP) - 0.263*MP^2 + 14 - res = [310.2, 63.3, -0.263, 14]; + // 310.2 + (63.3*MP) - 0.263*MP^2 + 14, -15%/+15% para GET liviano/intenso + res = [310.2, 63.3, -0.263, 14, 15, 15]; } else if (sex === Sex.Femenino) { - // 263.4 + (65.3*MP) - 0.454*MP^2 + 0 - res = [263.4, 65.3, -0.454, 0]; + // 263.4 + (65.3*MP) - 0.454*MP^2 + 0, -15%/+15% para GET liviano/intenso + res = [263.4, 65.3, -0.454, 0, 15, 15]; } break; }