Files
WebApp/src/app/gobernanza/bundles.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

152 lines
5.2 KiB
TypeScript

import { download } from '../export';
import { HttpErrorResponse } from '@angular/common/http';
import { Component, inject, input, model, output, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatSelectModule } from '@angular/material/select';
import { Api } from '../api/api';
import { exportModels, type ExportModels$Params } from '../api/fn/models/export-models';
import { importModels } from '../api/fn/models/import-models';
import type { Domain } from '../api/models/domain';
import { errorText } from './run-monitor';
export async function verifiedImport<T>(
file: Blob,
expected: string,
upload: (sha256: string) => Promise<T>,
): Promise<T> {
const digest = await crypto.subtle.digest('SHA-256', await file.arrayBuffer());
const sha256 = Array.from(new Uint8Array(digest), (byte) =>
byte.toString(16).padStart(2, '0'),
).join('');
if (expected.trim().toLowerCase() !== sha256) {
throw new Error('El SHA-256 del archivo no coincide con el esperado.');
}
return upload(sha256);
}
export async function exportErrorText(error: unknown): Promise<string> {
if (!(error instanceof HttpErrorResponse && error.error instanceof Blob)) return errorText(error);
const text = await error.error.text();
try {
const detail = JSON.parse(text).detail;
return typeof detail === 'string' ? detail : text;
} catch {
return text;
}
}
@Component({
selector: 'app-governance-bundles',
imports: [FormsModule, MatButtonModule, MatFormFieldModule, MatInputModule, MatSelectModule],
template: `
<section class="card" aria-labelledby="bundles-heading">
<h2 id="bundles-heading">Bundles</h2>
<form (ngSubmit)="export()">
<mat-form-field appearance="outline">
<mat-label>Versiones</mat-label>
<mat-select name="versions" [(ngModel)]="selection">
<mat-option value="champion">En producción</mat-option>
<mat-option value="latest">Últimas</mat-option>
<mat-option value="all">Todas</mat-option>
</mat-select>
</mat-form-field>
<button mat-stroked-button type="submit" [disabled]="exporting()">Exportar bundle</button>
</form>
@if (exportedHash()) {
<p>
SHA-256 <code>{{ exportedHash() }}</code>
</p>
}
<form (ngSubmit)="import(file)">
<input
#file
type="file"
accept=".zip"
required
hidden
aria-label="Archivo ZIP"
(change)="zipFiles.set(file.files)"
/>
<button mat-stroked-button type="button" (click)="file.click()">Archivo ZIP</button>
@for (file of zipFiles(); track $index) {
<span>{{ file.name }}</span>
}
<mat-form-field appearance="outline">
<mat-label>SHA-256 esperado</mat-label>
<input matInput name="hash" [(ngModel)]="hash" required />
</mat-form-field>
<button
mat-stroked-button
type="submit"
[disabled]="importing() || disabled() || !zipFiles()?.length"
>
Importar bundle
</button>
</form>
@if (message()) {
<p role="alert">{{ message() }}</p>
}
</section>
`,
})
export class Bundles {
private readonly api = inject(Api);
readonly domain = input.required<Domain>();
readonly disabled = input(false);
readonly started = output<string>();
protected readonly selection = signal<NonNullable<ExportModels$Params['versions']>>('champion');
protected readonly exportedHash = signal('');
protected readonly zipFiles = signal<FileList | null>(null);
protected readonly hash = signal('');
protected readonly message = signal('');
protected readonly exporting = signal(false);
readonly importing = model(false);
protected async export(): Promise<void> {
this.exporting.set(true);
this.message.set('');
this.exportedHash.set('');
const domain = this.domain();
try {
const response = await this.api.invoke$Response(exportModels, {
domain,
versions: this.selection(),
});
const sha256 = response.headers.get('X-Bundle-Sha256') ?? '';
this.exportedHash.set(sha256);
download(response.body, `${domain}-models-${sha256.slice(0, 12)}.zip`);
} catch (error) {
this.message.set(await exportErrorText(error));
} finally {
this.exporting.set(false);
}
}
protected async import(input: HTMLInputElement): Promise<void> {
const file = input.files?.[0];
if (!file || this.importing() || this.disabled()) return;
this.importing.set(true);
this.message.set('');
const domain = this.domain();
try {
const { run_id } = await verifiedImport(file, this.hash(), (sha256) =>
this.api.invoke(importModels, {
domain,
sha256,
body: { file: file as unknown as string },
}),
);
input.value = '';
this.zipFiles.set(null);
if (domain === this.domain()) this.started.emit(run_id);
} catch (error) {
this.message.set(errorText(error));
} finally {
this.importing.set(false);
}
}
}