import { Injectable } from '@nestjs/common' import { subMonths } from 'date-fns' import { first, isNil } from 'lodash' import { AnthropometryRepository } from '../../../repositories' import { BioimpedanceWithWeight } from '../models/bioimpedance-with-weight.model' @Injectable() export class BioimpedanceHelper { constructor(private readonly anthropometryRepository: AnthropometryRepository) {} async getLastBioimpedanceFromLast30Days(patientId: string): Promise { const anthropometries = await this.anthropometryRepository.findByPatientSince(patientId, subMonths(new Date(), 1)) const anthropometry = first(anthropometries.filter(anthropometry => !isNil(anthropometry.bioimpedance_id))) return !isNil(anthropometry) ? BioimpedanceWithWeight.toInstance({ ...anthropometry.bioimpedance, weight: anthropometry.weight, done_at: anthropometry.done_at, }) : null } async getBioimpedanceByAnthropometry(patientId: string): Promise { const anthropometry = await this.anthropometryRepository.getWithBioimpedance(patientId) return !isNil(anthropometry?.bioimpedance_id) ? BioimpedanceWithWeight.toInstance({ ...anthropometry.bioimpedance, weight: anthropometry.weight, done_at: anthropometry.done_at, }) : null } }