import {Injectable} from '@angular/core'; import {Observable, ReplaySubject} from 'rxjs'; import {SessionStorageService} from './session-storage.service'; @Injectable({ providedIn: 'root' }) export class PatientContextService { private currentCpr: string | undefined; private currentEpisodeOfCare: string | undefined; private currentXBreakTheGlassReason: string | undefined; private currentHasPrivatelyMarkedData: boolean | undefined; private currentPatientExists: boolean | undefined; private readonly innerCpr: ReplaySubject = new ReplaySubject(); private readonly innerEpisodeOfCare: ReplaySubject = new ReplaySubject(); private readonly innerXBreakTheGlassReason: ReplaySubject = new ReplaySubject(); private readonly innerHasPrivatelyMarkedData: ReplaySubject = new ReplaySubject(); private readonly innerPatientExists: ReplaySubject = new ReplaySubject(); constructor(private sessionStorageService: SessionStorageService) { } public loadPatientContextFromSessionStorage(): void { const {cpr, xBreakTheGlassReason} = this.sessionStorageService.getPatientContext(); this.setCpr(cpr); this.setXBreakTheGlassReason(xBreakTheGlassReason); } public setCpr(cpr?: string): void { if (this.currentCpr !== cpr) { this.currentCpr = cpr; this.sessionStorageService.setPatientContextCpr(cpr); this.innerCpr.next(cpr); // Clear patient context on new patient this.setEpisodeOfCare(undefined); this.setXBreakTheGlassReason(undefined); this.setHasPrivatelyMarkedData(undefined); this.setPatientExists(undefined); } } public setEpisodeOfCare(episodeOfCareIdentifier?: string): void { if (episodeOfCareIdentifier) { this.currentEpisodeOfCare = episodeOfCareIdentifier; this.innerEpisodeOfCare.next(episodeOfCareIdentifier); // publish every time it is set. } } public setXBreakTheGlassReason(xBreakTheGlassReason?: string): void { if (this.currentXBreakTheGlassReason !== xBreakTheGlassReason) { this.currentXBreakTheGlassReason = xBreakTheGlassReason; this.sessionStorageService.setPatientContextXBreakTheGlassReason(xBreakTheGlassReason); this.innerXBreakTheGlassReason.next(xBreakTheGlassReason); } } public setHasPrivatelyMarkedData(value?: boolean): void { if (this.currentHasPrivatelyMarkedData !== value) { this.currentHasPrivatelyMarkedData = value; this.innerHasPrivatelyMarkedData.next(value); } } public setPatientExists(exists?: boolean): void { if (this.currentPatientExists !== exists) { this.currentPatientExists = exists; this.innerPatientExists.next(exists); } } public getCurrentCpr(): string | undefined { return this.currentCpr; } public getCurrentEpisodeOfCare(): string | undefined { return this.currentEpisodeOfCare; } public getCurrentXBreakTheGlassReason(): string | undefined { return this.currentXBreakTheGlassReason; } public getCurrentHasPrivatelyMarkedData(): boolean | undefined { return this.currentHasPrivatelyMarkedData; } public getCurrentPatientExists(): boolean | undefined { return this.currentPatientExists; } public isAllowedToAccessData(): boolean { return !this.currentHasPrivatelyMarkedData || !!this.currentXBreakTheGlassReason; } get cpr$(): Observable { return this.innerCpr; } get episodeOfCare$(): Observable { return this.innerEpisodeOfCare; } get xBreakTheGlassReason$(): Observable { return this.innerXBreakTheGlassReason; } get hasPrivatelyMarkedData$(): Observable { return this.innerHasPrivatelyMarkedData; } get patientExists$(): Observable { return this.innerPatientExists; } }