import {Injectable} from '@angular/core'; import { HttpErrorResponse, HttpEvent, HttpHandler, HttpHeaders, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http'; import {Observable} from 'rxjs'; import {catchError, finalize, map} from 'rxjs/operators'; import {JsonTransformerUtil} from './json-transformer.util'; import {ActiveRequests} from '../app/services/active-requests.service'; import {UserContextHolder} from '../app/services/user-context.service'; import {PatientContextService} from '../app/services/patient-context.service'; import {ErrorService} from '../app/services/error.service'; @Injectable() export class HttpRequestInterceptor implements HttpInterceptor { constructor( private activeRequests: ActiveRequests, private patientContextService: PatientContextService, private userContextHolder: UserContextHolder, private errorService: ErrorService ) { } intercept(request: HttpRequest, next: HttpHandler): Observable> { let headers = this.addHeader(request.headers, 'X-NSP-GM-Platform', 'Web'); const paths = request.url.split('/'); if (paths[paths.length - 1] !== 'usercontext') { headers = this.addHeader(headers, 'X-NSP-GM-Chosen-Role', this.userContextHolder.currentRole?.educationType); headers = this.addHeader(headers, 'X-NSP-GM-User-Identifier', `${this.userContextHolder.uniqueIdentifier}`); headers = this.addHeader(headers, 'X-NSP-GM-Organization-Sor', this.userContextHolder.currentOrganization?.sorId); headers = this.addHeader(headers, 'X-NSP-GM-Institution-Owner-Sor', this.userContextHolder.currentOrganization?.institutionOwnerSorId); headers = this.addHeader(headers, 'X-NSP-GM-Patient-Identifier', this.patientContextService.getCurrentCpr()); } headers = this.addHeader(headers, 'X-NSP-GM-Episode-Of-Care-Identifier', this.patientContextService.getCurrentEpisodeOfCare()); if (this.patientContextService.getCurrentXBreakTheGlassReason()) { headers = this.addHeader(headers, 'X-NSP-GM-Break-The-Glass-Reason', this.patientContextService.getCurrentXBreakTheGlassReason()); } const requestWithHeaders = request.clone({headers}); if (this.isBlockingRequest(request)) { this.activeRequests._registerBlockingRequest(); } return next.handle(requestWithHeaders).pipe( map((event: HttpEvent) => { if (event instanceof HttpResponse) { const replaced = JsonTransformerUtil.transformDates(event.body); return event.clone({body: replaced}); } return event; }), catchError((error: HttpErrorResponse) => { this.errorService.redirectIfRequested(error); return this.errorService.handleHttpErrorResponse(error); }), finalize(() => { if (this.isBlockingRequest(request)) { this.activeRequests._unregisterBlockingRequest(); } }) ); } private addHeader(headers: HttpHeaders, header: string, value?: string): HttpHeaders { if (value) { return (headers || new HttpHeaders()).append(header, value); } return (headers || new HttpHeaders()); } private isBlockingRequest(request: HttpRequest): boolean { return !(request.url.includes('pdf') || request.url.includes('sorlookup')); } }