import {Injectable} from '@angular/core'; import {Observable} from 'rxjs'; import {map} from 'rxjs/operators'; import {BodyMeasuresSection, IntegerValueWithUnitEnum, OverallAssessmentSection} from '@nspop/gm-web-facade-api'; import {DataCardService} from '../api/data-card.service'; export interface MeasurementMidwife { bodyMeasuresSection?: BodyMeasuresSection; overallAssessmentSection?: OverallAssessmentSection; calculatedWeight?: number | undefined; } @Injectable({ providedIn: 'root' }) export class MeasurementService { public midwifeMeasurements$: Observable = this.dataCardService.activeDataCard$.pipe( map((dataCard) => { const measurement: MeasurementMidwife = { bodyMeasuresSection: dataCard?.bodyMeasuresSection, overallAssessmentSection: dataCard?.overallAssessmentSection, calculatedWeight: this.calculateWeight(dataCard?.bodyMeasuresSection) }; return [measurement]; }) ); constructor(private dataCardService: DataCardService) { } public calculateWeight(bodyMeasuresSection?: BodyMeasuresSection): number | undefined { const bodyHeightMeters: number | undefined = this.calculateHeightMeters(bodyMeasuresSection?.bodyHeight); const bodyMassIndex: number | undefined = bodyMeasuresSection?.bodyMassIndex?.value; console.log(`measurementService calculateHeight() for bodyHeight: ${bodyHeightMeters} & bodyMassIndex ${bodyMassIndex}`); if (bodyHeightMeters && bodyMassIndex){ return +(bodyMassIndex * bodyHeightMeters * bodyHeightMeters).toFixed(2); } return undefined; } private calculateHeightMeters(bodyHeight: IntegerValueWithUnitEnum | undefined): number | undefined { if (!bodyHeight) { return undefined; } else if (bodyHeight.unit === 'cm' && bodyHeight.value) { return bodyHeight.value / 100; } return bodyHeight.value; } }