Skip to content
Snippets Groups Projects
Commit 7f48695f authored by Renzo Beux's avatar Renzo Beux
Browse files

feat: sheetParser now returns an ageGroup array

parent 2a7ff936
No related branches found
No related tags found
No related merge requests found
Pipeline #15853 passed
...@@ -4,13 +4,14 @@ import { ...@@ -4,13 +4,14 @@ import {
import { SheetParserResponse } from '../Models/SheetParserResponse'; import { SheetParserResponse } from '../Models/SheetParserResponse';
import SheetService from '../Services/SheetService'; import SheetService from '../Services/SheetService';
import logger from '../Logger/logger'; import logger from '../Logger/logger';
import AgeGroupJSON from '../DTOs/AgeGroupJSON';
const router = Router(); const router = Router();
const parseSheet: Handler = async (req: Request, res: Response) => { const parseSheet: Handler = async (req: Request, res: Response) => {
const sheet: Buffer = req.body; const sheet: Buffer = req.body;
try { try {
const parsedSheet: SheetParserResponse = SheetService.parseSheetService(sheet); const parsedSheet: AgeGroupJSON[] = SheetService.parseSheetService(sheet);
return res.status(200).send(parsedSheet); return res.status(200).send(parsedSheet);
} catch (error) { } catch (error) {
const e = error as Error; const e = error as Error;
......
enum AgeBracket { enum AgeBracket {
m0 = '0 meses', m0 = '0 meses',
m1 = '1 mes', m1 = '1 meses',
m2 = '2 meses', m2 = '2 meses',
m3 = '3 meses', m3 = '3 meses',
m4 = '4 meses', m4 = '4 meses',
......
import * as XLSX from 'xlsx'; import * as XLSX from 'xlsx';
import { SheetNames } from '../Config/Constants'; import { SheetNames } from '../Config/Constants';
import AgeGroupJSON from '../DTOs/AgeGroupJSON';
import Sex from '../Enum/Sex';
import { import {
SheetParserResponse, Menores, Mayores, MenoresSheet, MayoresSheet, SheetParserResponse, Menores, Mayores, MenoresSheet, MayoresSheet,
} from '../Models/SheetParserResponse'; } from '../Models/SheetParserResponse';
import ParameterService from './ParameterService';
/* PRIVATE FUNCTIONS */ /* PRIVATE FUNCTIONS */
// const ec = (r: number, c: number): string => XLSX.utils.encode_cell({ r, c }); // const ec = (r: number, c: number): string => XLSX.utils.encode_cell({ r, c });
...@@ -56,12 +59,30 @@ const parseBabies = (worksheet: XLSX.WorkSheet): Menores[] => { ...@@ -56,12 +59,30 @@ const parseBabies = (worksheet: XLSX.WorkSheet): Menores[] => {
}); });
return res; return res;
}; };
const getMedianFromArray = (arr: number[]): number => {
const arrSort = arr.sort((a, b) => a - b);
const len = arr.length;
const mid = Math.ceil(len / 2);
const median = len % 2 === 0 ? (arrSort[mid] + arrSort[mid - 1]) / 2 : arrSort[mid - 1];
return median;
};
const getLiteralGroup = (age: number): string => {
if (age >= 18 && age <= 29) {
return '18-29';
} if (age >= 30 && age <= 59) {
return '30-59';
} if (age >= 60) {
return '60+';
}
return `${age}`;
};
/* EXPORT FUNCTIONS */ /* EXPORT FUNCTIONS */
const parseSheetService = (data: Buffer): SheetParserResponse => { const parseSheetService = (data: Buffer): AgeGroupJSON[] => {
const workbook: XLSX.WorkBook = XLSX.read(data); const workbook: XLSX.WorkBook = XLSX.read(data);
let parsed: SheetParserResponse = null; const parsed: SheetParserResponse = null;
let hombresMenores: Menores[] = []; let hombresMenores: Menores[] = [];
let hombres: Mayores[] = []; let hombres: Mayores[] = [];
let mujeresMenores: Menores[] = []; let mujeresMenores: Menores[] = [];
...@@ -92,14 +113,109 @@ const parseSheetService = (data: Buffer): SheetParserResponse => { ...@@ -92,14 +113,109 @@ const parseSheetService = (data: Buffer): SheetParserResponse => {
throw new Error(`Sheet name ${name} is not part of the scheme `); throw new Error(`Sheet name ${name} is not part of the scheme `);
} }
}); });
parsed = {
hombresMenores, const res: AgeGroupJSON[] = [];
hombres,
mujeresMenores, let auxObj: {[key: string]: number[]} = {};
mujeres, // group HombresMenores
}; // Iterate on hombresMenores and put
// elemnts on auxObj
return parsed; hombresMenores.forEach((item) => {
if (item === null) throw new Error('Item is null');
const bridge: string = (item.edad).toString();
auxObj[bridge] = auxObj[bridge] ? auxObj[bridge] : [];
(auxObj[bridge]).push(item.peso);
});
// creates AgeGroup and insert into res
let auxObjKeys = Object.keys(auxObj);
auxObjKeys.forEach((key) => {
const toInsert: AgeGroupJSON = {
age: `${key} meses`,
sex: Sex.Male,
medianWeight: getMedianFromArray(auxObj[key]),
population: auxObj[key].length,
};
res.push(toInsert);
});
auxObj = {};
mujeresMenores.forEach((item) => {
if (item === null) throw new Error('Item is null');
const bridge: string = (item.edad).toString();
auxObj[bridge] = auxObj[bridge] ? auxObj[bridge] : [];
(auxObj[bridge]).push(item.peso);
});
// creates AgeGroup for mujeresMenores and insert into res
auxObjKeys = Object.keys(auxObj);
auxObjKeys.forEach((key) => {
const toInsert: AgeGroupJSON = {
age: `${key} meses`,
sex: Sex.Female,
medianWeight: getMedianFromArray(auxObj[key]),
population: auxObj[key].length,
};
res.push(toInsert);
});
auxObj = {};
hombres.forEach((item) => {
if (item === null) throw new Error('Item is null');
const bridge: string = getLiteralGroup(item.edad);
auxObj[bridge] = auxObj[bridge] ? auxObj[bridge] : [];
let peso;
if (!item.peso) {
if (!item.talla) { throw new Error('Talla and Peso not defined'); }
// ParameterService. TODO:
peso = 0;
} else {
peso = item.peso;
}
(auxObj[bridge]).push(peso);
});
// creates AgeGroup for hombres and insert into res
auxObjKeys = Object.keys(auxObj);
auxObjKeys.forEach((key) => {
const toInsert: AgeGroupJSON = {
age: `${key} años`,
sex: Sex.Male,
medianWeight: getMedianFromArray(auxObj[key]),
population: auxObj[key].length,
};
res.push(toInsert);
});
auxObj = {};
mujeres.forEach((item) => {
if (item === null) throw new Error('Item is null');
const bridge: string = getLiteralGroup(item.edad);
auxObj[bridge] = auxObj[bridge] ? auxObj[bridge] : [];
let peso;
if (!item.peso) {
if (!item.talla) { throw new Error('Talla and Peso not defined'); }
// ParameterService. TODO:
peso = 0;
} else {
peso = item.peso;
}
(auxObj[bridge]).push(peso);
});
// creates AgeGroup for hombres and insert into res
auxObjKeys = Object.keys(auxObj);
auxObjKeys.forEach((key) => {
const toInsert: AgeGroupJSON = {
age: `${key} años`,
sex: Sex.Female,
medianWeight: getMedianFromArray(auxObj[key]),
population: auxObj[key].length,
};
res.push(toInsert);
});
return res;
// TODO: depends on sheet layout what to do // TODO: depends on sheet layout what to do
}; };
......
No preview for this file type
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