/* eslint-disable @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/ban-types */ import { plainToClass } from 'class-transformer' import { first, isDate, isEmpty, isNil, isNumber, isString } from 'lodash' import { format } from 'date-fns' import { Anthropometry } from '../anthropometry.model' import { Bioimpedance } from '../bioimpedance.model' import { parse } from '../../utils/number/parse' import { parse as objectParse } from '../../utils/object' import { BioimpedanceImportation } from '../bioimpedance_importation.model' import { Patient } from '../patient.model' import { RequiredArgumentError } from '../../errors' import { VALID_HEADERS_TO_BIOIMPEDANCE } from '../../utils/bioimpedance' import { generateUUID } from '../../utils/uuid' import { BaseVO } from './base.vo' export class CompressedBioimpedance extends BaseVO { USER_NAME: string USER_ID: string USER_HEIGHT: string USER_BIRTHDAY: string USER_GENDER: 'M' | 'F' USER_AGE: string DATETIMES: string WT: string WT_MIN: string WT_MAX: string TBW: string TBW_MIN: string TBW_MAX: string PROTEIN: string PROTEIN_MIN: string PROTEIN_MAX: string MINERAL: string MINERAL_MIN: string MINERAL_MAX: string BFM: string PBFM_MIN: string PBFM_MAX: string SMM: string SMM_MIN: string SMM_MAX: string BMI: string BMI_MIN: string BMI_MAX: string PBF: string PBF_MIN: string PBF_MAX: string LRA: string PILRA: string LLA: string PILLA: string LT: string PILT: string LRL: string PILRL: string LLL: string PILLL: string FRA: string PFRA: string FLA: string PFLA: string FT: string PFT: string FRL: string PFRL: string FLL: string PFLL: string FS: string TW: string WC: string FC: string MC: string BMR: string WHR: string WHR_MIN: string WHR_MAX: string VFL: string OBESITY: string OD_MIN: string OD_MAX: string IRA20: string ILA20: string IT20: string IRL20: string ILL20: string IRA100: string ILA100: string IT100: string IRL100: string ILL100: string EQUIP: string SICK_SN: string GROUP_SN: string BMR_MIN: string BMR_MAX: string ORDER_DATE: string toBioimpedanceImportation( patient: Partial, userIds: Array, smartfitUnitId: number, smartfitTeacherId?: string, ): BioimpedanceImportation { if (!patient) throw new RequiredArgumentError('patient') if (!userIds || isEmpty(userIds)) throw new RequiredArgumentError('userIds') if (!smartfitUnitId) throw new RequiredArgumentError('smartfitUnitId') const principalUserId = first(userIds) const data = { external_id: this.USER_ID || generateUUID(), document_number: patient.document_number || '', name: patient.name || '', email: patient.email, } const anthropometry = this.toAnthropometry(patient.id, principalUserId) const file_row = this.getParsedImportationAnthropometry(patient, anthropometry, data) return plainToClass(BioimpedanceImportation, { ...data, file_row, done_at: anthropometry.done_at, created_by: principalUserId, users: userIds, smartfit_unit_id: smartfitUnitId, smartfit_teacher_id: smartfitTeacherId, }) } toAnthropometry(patient_id: string, user_id: string): Anthropometry { const bioimpedance = this.toBioimpedance() return plainToClass(Anthropometry, { bioimpedance, patient_id, user_id, body_mass_index: bioimpedance.bmi, height: parse(this.USER_HEIGHT), weight: parse(this.WT), done_at: new Date().toISOString(), }) } toBioimpedance(): Bioimpedance { return plainToClass(Bioimpedance, { lower_limit_weight: this.WT_MIN, upper_limit_weight: this.WT_MAX, total_body_water: this.TBW, lower_limit_total_body_water: this.TBW_MIN, upper_limit_total_body_water: this.TBW_MAX, bfm: this.BFM, lower_limit_bfm: this.PBFM_MIN, upper_limit_bfm: this.PBFM_MAX, smm: this.SMM, lower_limit_smm: this.SMM_MIN, upper_limit_smm: this.SMM_MAX, bmi: this.BMI, lower_limit_bmi: this.BMI_MIN, upper_limit_bmi: this.BMI_MAX, pbf: this.PBF, lower_limit_pbf: this.PBF_MIN, upper_limit_pbf: this.PBF_MAX, ffm_right_arm: this.LRA, ffm_left_arm: this.LLA, ffm_percentage_right_arm: this.PILRA, ffm_percentage_left_arm: this.PILLA, ffm_trunk: this.LT, ffm_percentage_trunk: this.PILT, ffm_right_leg: this.LRL, ffm_left_leg: this.LLL, ffm_percentage_right_leg: this.PILRL, ffm_percentage_left_leg: this.PILLL, bfm_right_arm: this.FRA, bfm_left_arm: this.FLA, bfm_percentage_right_arm: this.PFRA, bfm_percentage_left_arm: this.PFLA, bfm_trunk: this.FT, bfm_percentage_trunk: this.PFT, bfm_right_leg: this.FRL, bfm_left_leg: this.FLL, bfm_percentage_right_leg: this.PFRL, bfm_percentage_left_leg: this.PFLL, score: this.FS, vfl: this.VFL, }) } private getParsedImportationAnthropometry( patient: Partial, anthropometry: Anthropometry, data: object, ): object { return objectParse( { ...data, ...anthropometry, ...anthropometry.bioimpedance, gender: patient?.genderInitial }, VALID_HEADERS_TO_BIOIMPEDANCE, this.transformerToCsv, true, ) } private transformerToCsv = (value: any) => { if (isNil(value)) return '-' if (isDate(value)) return format(value, 'yyyy.MM.dd. HH:mm:ss') if (isNumber(value)) return value.toString().replace('.', ',') if (isString(value)) return value.toString ? value.toString() : `${value}` return value } }