import {Injectable} from '@angular/core'; import {ApiService} from './api.service'; import {HttpClient} from '@angular/common/http'; import {DataCard} from '@nspop/gm-web-facade-api'; import {BehaviorSubject, Observable} from 'rxjs'; import {map, tap} from 'rxjs/operators'; import {PatientContextService} from '../services/patient-context.service'; @Injectable({ providedIn: 'root', }) export class DataCardService extends ApiService { constructor(httpClient: HttpClient, private patientContextService: PatientContextService) { super('/data-card', httpClient); } // Inner subjects - for manually publishing updates to subscribers private innerDataCards: BehaviorSubject = new BehaviorSubject([]); private innerSelectedDataCard: BehaviorSubject = new BehaviorSubject(undefined); // Observables (from inner subjects) - subscribe to these to get updated info! public allDataCards$: Observable = this.innerDataCards.asObservable(); public selectedDataCard$: Observable = this.innerSelectedDataCard.asObservable(); public isDataCardClosed: Observable = this.selectedDataCard$.pipe(map(value => value?.documentStatus === 'CLOSED')); // Refresh methods - these are the ones that actually call the server and manually publishes new data public refresh(): Promise { return this.getAll().toPromise().then((dataCards: DataCard[]) => { this.innerDataCards.next(dataCards); }); } public refreshSelectedDataCard(episodeOfCareId?: string): void { if (!episodeOfCareId) { episodeOfCareId = this.patientContextService.getCurrentEpisodeOfCare(); } const DataCards: DataCard[] = this.innerDataCards.getValue(); this.innerSelectedDataCard.next(DataCards.find(card => card.episodeOfCareIdentifier === episodeOfCareId)); } // Overriding api.service methods public create(): Observable { return super.create(null); } public close(): Observable { return this.httpClient.put(this.url + '/close', {}); } public update(resource: DataCard, documentUniqueId: string, version: string): Observable { return super.update(resource, documentUniqueId, version).pipe( tap((dataCard: DataCard) => { this.innerSelectedDataCard.next(dataCard); }) ); } }