114 lines
3.8 KiB
TypeScript
114 lines
3.8 KiB
TypeScript
import { DatePipe } from '@angular/common';
|
|
import { Component, DestroyRef, OnInit, inject, signal } from '@angular/core';
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
|
|
import { FormControl, ReactiveFormsModule } from '@angular/forms';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatInputModule } from '@angular/material/input';
|
|
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
|
import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar';
|
|
import { MatTableModule } from '@angular/material/table';
|
|
import { MatTooltipModule } from '@angular/material/tooltip';
|
|
import { apiErrorMessage } from '../../../connect/api/api.types';
|
|
import { RiskBatchesApi } from '../../api/risk-batches.api';
|
|
import { PendingRiskBatch } from '../../api/risk-batches.types';
|
|
|
|
const DOMAIN = 'cacu';
|
|
|
|
/**
|
|
* Risk batches a model produced that nobody has confirmed or voided yet (ADR-MD-008): invisible to patient
|
|
* records until someone decides, with a reason. Only CaCu has a model estimating risks today.
|
|
*/
|
|
@Component({
|
|
selector: 'app-risk-batches-panel',
|
|
imports: [
|
|
DatePipe,
|
|
MatButtonModule,
|
|
MatFormFieldModule,
|
|
MatIconModule,
|
|
MatInputModule,
|
|
MatProgressBarModule,
|
|
MatSnackBarModule,
|
|
MatTableModule,
|
|
MatTooltipModule,
|
|
ReactiveFormsModule,
|
|
],
|
|
templateUrl: './risk-batches-panel.html',
|
|
styleUrl: './risk-batches-panel.scss',
|
|
})
|
|
export class RiskBatchesPanel implements OnInit {
|
|
private readonly api = inject(RiskBatchesApi);
|
|
private readonly snackBar = inject(MatSnackBar);
|
|
private readonly destroyRef = inject(DestroyRef);
|
|
|
|
readonly batches = signal<PendingRiskBatch[]>([]);
|
|
readonly loading = signal(false);
|
|
readonly loadError = signal<string | null>(null);
|
|
readonly deciding = signal<number | null>(null);
|
|
|
|
readonly displayedColumns = ['modelo', 'evaluado_en', 'n_evaluaciones', 'motivo', 'acciones'];
|
|
private readonly reasons = new Map<number, FormControl<string>>();
|
|
|
|
ngOnInit(): void {
|
|
this.reload();
|
|
}
|
|
|
|
reload(): void {
|
|
this.loading.set(true);
|
|
this.loadError.set(null);
|
|
this.api
|
|
.listPending(DOMAIN)
|
|
.pipe(takeUntilDestroyed(this.destroyRef))
|
|
.subscribe({
|
|
next: (batches) => {
|
|
this.loading.set(false);
|
|
this.batches.set(batches);
|
|
},
|
|
error: (error) => {
|
|
this.loading.set(false);
|
|
this.loadError.set(apiErrorMessage(error, 'No se pudieron cargar los lotes de riesgo.'));
|
|
},
|
|
});
|
|
}
|
|
|
|
reasonFor(batch: PendingRiskBatch): FormControl<string> {
|
|
let control = this.reasons.get(batch.lote_id);
|
|
if (!control) {
|
|
control = new FormControl('', { nonNullable: true });
|
|
this.reasons.set(batch.lote_id, control);
|
|
}
|
|
return control;
|
|
}
|
|
|
|
confirm(batch: PendingRiskBatch): void {
|
|
this.decide(batch, (reason) => this.api.confirm(DOMAIN, batch.lote_id, reason), 'confirmado');
|
|
}
|
|
|
|
void(batch: PendingRiskBatch): void {
|
|
this.decide(batch, (reason) => this.api.void(DOMAIN, batch.lote_id, reason), 'anulado');
|
|
}
|
|
|
|
private decide(
|
|
batch: PendingRiskBatch,
|
|
request: (reason: string) => ReturnType<RiskBatchesApi['confirm']>,
|
|
verb: string,
|
|
): void {
|
|
const reason = this.reasonFor(batch).value.trim();
|
|
if (!reason) return;
|
|
this.deciding.set(batch.lote_id);
|
|
request(reason).subscribe({
|
|
next: () => {
|
|
this.deciding.set(null);
|
|
this.reasons.delete(batch.lote_id);
|
|
this.snackBar.open(`El lote ${batch.lote_id} quedó ${verb}.`, 'OK', { duration: 3000 });
|
|
this.reload();
|
|
},
|
|
error: (error) => {
|
|
this.deciding.set(null);
|
|
this.snackBar.open(apiErrorMessage(error, 'La operación no se pudo completar.'), 'OK', { duration: 5000 });
|
|
},
|
|
});
|
|
}
|
|
}
|