import { HttpErrorResponse } from '@angular/common/http'; import { Component, inject, input, output, signal } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { MatButtonModule } from '@angular/material/button'; import { MatIconModule } from '@angular/material/icon'; import { Api } from '../api/api'; import { getConceptMap } from '../api/fn/ingest/get-concept-map'; import { saveConceptMap } from '../api/fn/ingest/save-concept-map'; /** One translation: a site's code onto a platform code of one target system. */ interface Row { target: string; code: string; platform: string; } /** A site's codes in platform terms (a FHIR ConceptMap), edited as a table: a new * laboratory spelling is one more row. The API refuses a code no platform field allows. */ @Component({ selector: 'app-concept-map-editor', imports: [FormsModule, MatButtonModule, MatIconModule], template: `

Códigos del sitio · {{ id() }}

@for (row of rows(); track $index) { }
Sistema destino Código del sitio Código de la plataforma
{{ message() }}
`, styles: ` h3 { font-size: 0.95rem; font-weight: 500; margin: 16px 0 8px; } input, select { width: 100%; box-sizing: border-box; font: inherit; padding: 4px; } .actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 8px; } `, }) export class ConceptMapEditor { private readonly api = inject(Api); readonly id = input.required(); readonly saved = output(); protected readonly rows = signal([]); protected readonly targets = signal([]); protected readonly message = signal(''); private source = 'urn:isssteleon:site'; async ngOnInit(): Promise { try { const doc = (await this.api.invoke(getConceptMap, { map_id: this.id() })) as Record< string, any >; const groups = (doc['group'] ?? []) as any[]; this.source = groups[0]?.source ?? this.source; this.targets.set([...new Set(groups.map((g) => g.target))]); this.rows.set( groups.flatMap((g) => (g.element ?? []).map((e: any) => ({ target: g.target, code: e.code, platform: e.target?.[0]?.code ?? '', })), ), ); } catch (e) { this.message.set( e instanceof HttpErrorResponse && e.status === 404 ? 'Nuevo: agregue filas.' : String(e), ); } } protected short(url: string): string { return url.split('/').pop() ?? url; } protected add(): void { this.rows.update((r) => [...r, { target: this.targets()[0] ?? '', code: '', platform: '' }]); } protected remove(i: number): void { this.rows.update((r) => r.filter((_, j) => j !== i)); } /** The rows as a ConceptMap: one group per target system. */ protected async save(): Promise { const groups = new Map(); for (const r of this.rows().filter((r) => r.target && r.code && r.platform)) { groups.set(r.target, [...(groups.get(r.target) ?? []), r]); } const doc = { resourceType: 'ConceptMap', status: 'active', group: [...groups].map(([target, rows]) => ({ source: this.source, target, element: rows.map((r) => ({ code: r.code, target: [{ code: r.platform, equivalence: 'equivalent' }], })), })), }; try { const { version } = await this.api.invoke(saveConceptMap, { map_id: this.id(), body: doc }); this.message.set(`Versión ${version} guardada.`); this.saved.emit(version); } catch (e) { const detail = e instanceof HttpErrorResponse ? e.error?.detail : e; this.message.set(Array.isArray(detail) ? detail.join('; ') : String(detail)); } } }