Skip to content
Snippets Groups Projects
Commit 493421c8 authored by Agustin's avatar Agustin
Browse files

Data sent to calculator is now checked, returning errors when it should

This hasn't been correctly verified yet
parent d77b8b95
No related branches found
No related tags found
No related merge requests found
This diff is collapsed.
......@@ -21,6 +21,14 @@ const isIndividualMaternity = (obj: IndividualMaternity | PopulationMaternity):
// TEE (Total Energetic Expenditure) = GET (Gasto Energetico Total)
const calculateTEE = (group: AgeGroup, params: number[], preval: MinorPAL): number => {
const prevalCheck: number = preval.intensePALPrevalence
+ preval.lowPALPrevalence
+ preval.moderatePALPrevalence;
if (prevalCheck !== 100 || preval.intensePALPrevalence < 0
|| preval.moderatePALPrevalence < 0 || preval.lowPALPrevalence < 0) {
throw new Error('Minor PAL data does not respect format');
}
const teeModerate: number = params[0]
+ (params[1] * group.medianWeight)
+ params[2] * (group.medianWeight * group.medianWeight);
......@@ -43,6 +51,16 @@ const calculateBMR = (group: AgeGroup, params: number[]): number => {
// PAL (Physical Activity Level) = NAF (Nivel de Actividad Fisica)
const calculatePAL = (params: number[], popData: AdultPAL): number => {
const popCheck: number = popData.ruralPercentage + popData.urbanPercentage;
const urbanPALCheck: number = popData.activeUrbanPAL + popData.lowUrbanPAL;
const ruralPALCheck: number = popData.activeRuralPAL + popData.lowRuralPAL;
if (popCheck !== 100 || urbanPALCheck !== 100 || ruralPALCheck !== 100
|| popData.ruralPercentage < 0 || popData.urbanPercentage < 0
|| popData.activeUrbanPAL < 0 || popData.lowUrbanPAL < 0
|| popData.activeRuralPAL < 0 || popData.lowRuralPAL < 0) {
throw new Error('Adult PAL data does not respect format');
}
const ruralPAL: number = (popData.activeRuralPAL * params[2]) / 100
+ (popData.lowRuralPAL * params[3]) / 100;
const urbanPAL: number = (popData.activeUrbanPAL * params[4]) / 100
......@@ -55,6 +73,11 @@ const calculatePAL = (params: number[], popData: AdultPAL): number => {
// eslint-disable-next-line max-len
const calculateERWomenIndividual = (group: AgeGroup, params: number[], popData: IndividualMaternity, req: number): number => {
const popCheck: number = popData.lactatingWomen + popData.pregnantWomen;
if (popCheck > group.population || popData.lactatingWomen < 0 || popData.pregnantWomen < 0) {
throw new Error('Individual Maternity does not respect format');
}
const percentPregnantWomen = (popData.pregnantWomen * 100) / group.population;
const percentLactatingWomen = (popData.lactatingWomen * 100) / group.population;
......@@ -67,6 +90,13 @@ const calculateERWomenIndividual = (group: AgeGroup, params: number[], popData:
// eslint-disable-next-line max-len
const calculateERWomenPopulation = (params: number[], popData: PopulationMaternity, req: number): number => {
if (popData.countryWomenInAgeGroup > popData.countryPopulation
|| typeof (popData.countryBirthRate) !== 'number' || popData.countryBirthRate < 0
|| typeof (popData.countryWomenInAgeGroup) !== 'number' || popData.countryWomenInAgeGroup < 0
|| typeof (popData.countryPopulation) !== 'number' || popData.countryPopulation < 0) {
throw new Error('Population Maternity does not respect format');
}
const annualBirths = (popData.countryBirthRate * popData.countryPopulation) / 1000;
const percentPregnantWomen = (annualBirths * 75) / popData.countryWomenInAgeGroup;
......
......@@ -326,7 +326,7 @@ const getEquationValues = (ageBracket: AgeBracket, sex: Sex): number[] => {
}
default: {
throw new Error(`Parsing error, attribute edad does not respect format ${ageBracket}`);
throw new Error('Parsing error, attribute edad does not respect format');
}
}
return res;
......
......@@ -3,6 +3,45 @@ import AgeGroupJSON from '../DTOs/AgeGroupJSON';
import AgeBracket from '../Enum/AgeBracket';
import Sex from '../Enum/Sex';
const checkAgeGroup = (group: AgeGroupJSON): boolean => (
typeof (group.age) !== 'string'
|| typeof (group.sex) !== 'string'
|| typeof (group.medianWeight) !== 'number'
|| typeof (group.population) !== 'number'
|| group.medianWeight <= 0
|| group.population <= 0
);
const parseGroups = (groups: AgeGroupJSON[]): AgeGroup[] => {
const retGroups: AgeGroup[] = [];
groups.forEach((ageGroup: AgeGroupJSON) => {
if (checkAgeGroup(ageGroup)) {
throw new Error('Age group data does not meet specification');
} else {
const group: AgeGroup = {
age: ageGroup.age as AgeBracket,
sex: ageGroup.sex as Sex,
medianWeight: ageGroup.medianWeight,
population: ageGroup.population,
};
retGroups.push(group);
}
});
return retGroups;
};
const unparseGroup = (group: AgeGroup): AgeGroupJSON => {
const retGroup: AgeGroupJSON = {
age: group.age as string,
sex: group.sex as string,
medianWeight: group.medianWeight,
population: group.population,
};
return retGroup;
};
export default { parseGroups, unparseGroup };
// const parseSex = (sexo: string): Sex => {
// let sex: Sex = sexo;
// switch (sexo) {
......@@ -158,29 +197,3 @@ import Sex from '../Enum/Sex';
// }
// return age;
// };
const parseGroups = (groups: AgeGroupJSON[]): AgeGroup[] => {
const retGroups: AgeGroup[] = [];
groups.forEach((ageGroup: AgeGroupJSON) => {
const group: AgeGroup = {
age: ageGroup.age as AgeBracket,
sex: ageGroup.sex as Sex,
medianWeight: ageGroup.medianWeight,
population: ageGroup.population,
};
retGroups.push(group);
});
return retGroups;
};
const unparseGroup = (group: AgeGroup): AgeGroupJSON => {
const retGroup: AgeGroupJSON = {
age: group.age as string,
sex: group.sex as string,
medianWeight: group.medianWeight,
population: group.population,
};
return retGroup;
};
export default { parseGroups, unparseGroup };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment