4e08bb8c4b
(cherry picked from commit 88c1eb33be)
131 lines
4.7 KiB
TypeScript
131 lines
4.7 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 { MatButtonToggleModule } from '@angular/material/button-toggle';
|
|
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 { AuthService } from '../../../auth/auth.service';
|
|
import { apiErrorMessage } from '../../../connect/api/api.types';
|
|
import { RiskBatchesApi } from '../../api/risk-batches.api';
|
|
import { PendingRiskBatch } from '../../api/risk-batches.types';
|
|
|
|
/** The domains with a model that estimates risks: each has its own batches under `/<domain>/riesgos/lotes`. */
|
|
export const RISK_DOMAINS = [
|
|
{ id: 'cacu', label: 'Cáncer cervicouterino' },
|
|
{ id: 'cama', label: 'Cáncer de mama' },
|
|
];
|
|
|
|
/**
|
|
* 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. CaCu and CaMa have a model estimating risks.
|
|
*/
|
|
@Component({
|
|
selector: 'app-risk-batches-panel',
|
|
imports: [
|
|
DatePipe,
|
|
MatButtonModule,
|
|
MatButtonToggleModule,
|
|
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 auth = inject(AuthService);
|
|
private readonly snackBar = inject(MatSnackBar);
|
|
private readonly destroyRef = inject(DestroyRef);
|
|
|
|
/** Only the account's domains. */
|
|
readonly domains = RISK_DOMAINS.filter((d) => this.auth.puedeVer(d.id));
|
|
readonly domain = signal<string>(this.domains[0]?.id ?? RISK_DOMAINS[0].id);
|
|
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();
|
|
}
|
|
|
|
setDomain(domain: string): void {
|
|
this.domain.set(domain);
|
|
this.reload();
|
|
}
|
|
|
|
reload(): void {
|
|
this.loading.set(true);
|
|
this.loadError.set(null);
|
|
this.api
|
|
.listPending(this.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(this.domain(), batch.lote_id, reason), 'confirmado');
|
|
}
|
|
|
|
void(batch: PendingRiskBatch): void {
|
|
this.decide(batch, (reason) => this.api.void(this.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();
|
|
// a double click reaches here before the disabled button is drawn: one decision at a time
|
|
if (!reason || this.deciding() !== null) 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 });
|
|
},
|
|
});
|
|
}
|
|
}
|