import { Injectable } from '@nestjs/common' import { isNil } from 'lodash' import { BioimpedanceService } from '../../../services/bioimpedance/bioimpedance.service' import { CloudWatchService } from '../../../services/cloudwatch' import { isEmail } from '../../../utils/string' import { NotFoundError } from '../../../errors' import { Patient } from '../../../models/patient.model' import { AnthropometriesFilter } from '../../../repositories' import { PatientAnthropomentry } from './models' @Injectable() export class Bioimpedance { constructor( private readonly bioimpedanceService: BioimpedanceService, private readonly cloudWatch: CloudWatchService, ) {} async getAllAnthropometries(login: string, filter: AnthropometriesFilter): Promise { const anthropometryCached = await this.getCache(login, filter) return anthropometryCached } private async getCache(login: string, filter: AnthropometriesFilter): Promise { const anthropometries = await this.bioimpedanceService.getPatientAnthropometriesCache( Patient.toInstance(isEmail(login) ? { email: login } : { document_number: login }), filter, ) if (!isNil(anthropometries)) await this.putCacheHitLog(!isNil(anthropometries), login) if (isNil(anthropometries)) throw new NotFoundError(`Patient not found.`) return anthropometries.map(row => PatientAnthropomentry.toInstance(row)) } private async putCacheHitLog(hit: boolean, login: string) { await this.cloudWatch.putLog(JSON.stringify({ hit, login }), 'api-patients-bio-cache') } }