/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { plainToClass, Transform, Type } from 'class-transformer' import { GENDER_HEADER, HEADER_TO_BIOIMPEDANCE_IMPORTATION, VALID_HEADERS_TO_BIOIMPEDANCE } from '../utils/bioimpedance' import { parseISO } from '../utils/date' import { parse } from '../utils/object' import { isBlank } from '../utils/string' import { Anthropometry } from './anthropometry.model' import { BaseModel } from './base.model' import { Bioimpedance } from './bioimpedance.model' import { Patient } from './patient.model' import { SmartfitUnit } from './smartfit_unit.model' type VALID_HEADERS = keyof typeof VALID_HEADERS_TO_BIOIMPEDANCE export class BioimpedanceImportation extends BaseModel { id: string external_id: string @Transform(value => value?.replace(/\D+/g, ''), { toClassOnly: true }) document_number?: string name: string email?: string file_row: { [key in VALID_HEADERS]: string } file_path?: string anthropometry_id?: string @Transform(value => parseISO(value), { toClassOnly: true }) created_at: Date done_at: Date created_by: string @Transform(value => parseISO(value), { toClassOnly: true }) updated_at: Date updated_by: string users?: Array smartfit_unit_id?: number smartfit_teacher_id?: string @Type(() => SmartfitUnit) smartfit_unit: SmartfitUnit toAnthropometry(patient: Patient): Anthropometry { const row = parse(this.file_row, VALID_HEADERS_TO_BIOIMPEDANCE, BioimpedanceImportation.transformCsvValues) const bioimpedance = plainToClass(Bioimpedance, row, { excludeExtraneousValues: true }) return new Anthropometry({ patient_id: patient.id, height: Number(row.height.replace(/,/g, '.')), weight: Number(row.weight.replace(/,/g, '.')), done_at: this.done_at, user_id: this.created_by, body_mass_index: bioimpedance.bmi, bioimpedance: bioimpedance, }) } static toParsedValue( row: any, filePath: string, userId: string, users: Array, smartfitUnitId: number, ): BioimpedanceImportation { const parsed = parse({ ...row }, HEADER_TO_BIOIMPEDANCE_IMPORTATION, BioimpedanceImportation.transformCsvValues) return plainToClass(BioimpedanceImportation, { file_row: { ...row }, file_path: filePath, created_by: userId, smartfit_unit_id: smartfitUnitId, users, ...parsed, }) } get gender(): 'M' | 'F' { return this.file_row[GENDER_HEADER] as 'M' | 'F' } private static transformCsvValues = (value: any) => (isBlank(value) || value.trim() === '-' ? null : value) }