Files
WebApp/src/app/fhir.ts
T
alberto 7c98566579 T17: sweep of the WebApp after the feature-parity work
Removed the legacy indicator layout, unused chart helpers, styles and
exports, the card-actions component, fetch-all and explanatory comments;
one pager shared by the four worklists, shared filter-row styles, one
FHIR name and CURP reader, downloads through export/, the concept-map
editor on Material controls.
2026-12-01 06:48:57 -06:00

89 lines
3.2 KiB
TypeScript

import { HttpClient } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';
import type { Bundle, CodeableConcept, Extension, FhirResource, Patient } from 'fhir/r4';
import { firstValueFrom } from 'rxjs';
import { ApiConfiguration } from './api/api-configuration';
export const CURP = 'https://isssteleon.org/fhir/sid/curp';
export const EXT = 'https://isssteleon.org/fhir/StructureDefinition/';
export type Params = Record<string, string | number | readonly (string | number)[] | undefined>;
@Injectable({ providedIn: 'root' })
export class Fhir {
private readonly http = inject(HttpClient);
private readonly root = inject(ApiConfiguration).rootUrl;
read<T extends FhirResource>(domain: string, type: T['resourceType'], id: string): Promise<T> {
return firstValueFrom(this.http.get<T>(`${this.root}/${domain}/fhir/${type}/${id}`));
}
search(domain: string, type: string, params: Params = {}): Promise<Bundle> {
const query = Object.fromEntries(
Object.entries(params).filter(([, v]) => v !== undefined),
) as Record<string, string | number | readonly (string | number)[]>;
return firstValueFrom(
this.http.get<Bundle>(`${this.root}/${domain}/fhir/${type}`, { params: query }),
);
}
follow(domain: string, bundle: Bundle, relation: 'next' | 'previous'): Promise<Bundle> | null {
const link = bundle.link?.find((l) => l.relation === relation)?.url;
if (!link) return null;
const url = new URL(link);
const type = url.pathname.split('/').pop();
return firstValueFrom(
this.http.get<Bundle>(`${this.root}/${domain}/fhir/${type}${url.search}`),
);
}
async all(domain: string, type: string, params: Params = {}): Promise<Bundle[]> {
const pages = [await this.search(domain, type, params)];
for (let next = this.follow(domain, pages[0], 'next'); next;) {
const page = await next;
pages.push(page);
next = this.follow(domain, page, 'next');
}
return pages;
}
}
export function resources<T extends FhirResource>(
bundles: Bundle | Bundle[],
type: T['resourceType'],
mode?: 'match' | 'include',
): T[] {
return [bundles]
.flat()
.flatMap((b) => b.entry ?? [])
.filter((e) => e.resource?.resourceType === type && (!mode || e.search?.mode === mode))
.map((e) => e.resource as T);
}
export function ext(
element: { extension?: Extension[] } | undefined,
name: string,
): Extension | undefined {
return element?.extension?.find((e) => e.url === EXT + name);
}
export function code(concept: CodeableConcept | undefined): string | undefined {
return concept?.coding?.[0]?.code;
}
export function label(concept: CodeableConcept | undefined): string {
return concept?.text ?? concept?.coding?.[0]?.display ?? concept?.coding?.[0]?.code ?? '';
}
export function patientName(patient: Patient | undefined): string | undefined {
return patient?.name?.[0]?.text;
}
export function patientCurp(patient: Patient | undefined): string | undefined {
return patient?.identifier?.find((i) => i.system === CURP)?.value;
}
export function extensions(element: { extension?: Extension[] }, name: string): Extension[] {
return (element.extension ?? []).filter((e) => e.url === EXT + name);
}