98 lines
4.1 KiB
TypeScript
98 lines
4.1 KiB
TypeScript
import { DecimalPipe } from '@angular/common';
|
|
import { Component, OnInit, computed, inject, signal } from '@angular/core';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatCheckboxModule } from '@angular/material/checkbox';
|
|
import { MatDialogModule, MatDialogRef } from '@angular/material/dialog';
|
|
import { MatIconModule } from '@angular/material/icon';
|
|
import { MatProgressBarModule } from '@angular/material/progress-bar';
|
|
import { MatRadioModule } from '@angular/material/radio';
|
|
import { MatTableModule } from '@angular/material/table';
|
|
import { ConflictStrategy } from '../../api/api.types';
|
|
import { ImportFlow } from '../../domain/import-flow';
|
|
import { ConnectWorkspaceStore } from '../../state/connect-workspace.store';
|
|
import { FlowStatusChip } from '../shared/flow-status';
|
|
|
|
export type LoadDialogResult = 'explore' | undefined;
|
|
|
|
/**
|
|
* "Cargar a la base": validates every flow, shows what will be written,
|
|
* sets the batch options (conflict policy, merging a patient's data spread
|
|
* across sources) and loads the batch in one request and one transaction.
|
|
* Rows failing validation are skipped and reported, never written.
|
|
*/
|
|
@Component({
|
|
selector: 'app-load-dialog',
|
|
imports: [DecimalPipe, FlowStatusChip, MatButtonModule, MatCheckboxModule, MatDialogModule, MatIconModule, MatProgressBarModule, MatRadioModule, MatTableModule],
|
|
templateUrl: './load-dialog.html',
|
|
styleUrl: './load-dialog.scss',
|
|
})
|
|
export class LoadDialog implements OnInit {
|
|
readonly store = inject(ConnectWorkspaceStore);
|
|
private readonly dialogRef = inject<MatDialogRef<LoadDialog, LoadDialogResult>>(MatDialogRef);
|
|
|
|
readonly columns = ['flow', 'status', 'rows', 'valid', 'errors', 'outcome'];
|
|
readonly validating = signal(true);
|
|
/** Whether merging is even possible here; independent of the checkbox itself. */
|
|
readonly mergeAvailable = this.store.mergeAvailable;
|
|
/** With merging, rows incomplete on their own may be completed by another source. */
|
|
readonly canLoad = computed(() => this.totals().valid > 0 || (this.store.mergeDuplicates() && this.totals().rows > 0));
|
|
|
|
/** The flows the next batch writes; frozen once it starts so the results match what was sent. */
|
|
private readonly _frozenFlows = signal<ImportFlow[] | null>(null);
|
|
readonly flows = computed(() => this._frozenFlows() ?? this.store.submittableFlows());
|
|
readonly skipped = computed(() => this.store.flows().filter((flow) => !this.flows().includes(flow) && !flow.canSubmit));
|
|
|
|
readonly totals = computed(() =>
|
|
this.flows().reduce(
|
|
(acc, flow) => {
|
|
const validation = flow.validation();
|
|
acc.rows += validation?.total_rows ?? 0;
|
|
acc.valid += validation?.valid_count ?? 0;
|
|
acc.errors += validation?.error_count ?? 0;
|
|
return acc;
|
|
},
|
|
{ rows: 0, valid: 0, errors: 0 },
|
|
),
|
|
);
|
|
|
|
readonly batch = this.store.lastBatch;
|
|
readonly outcome = computed(() => {
|
|
const batch = this.batch();
|
|
if (!batch) return null;
|
|
return batch.results.reduce(
|
|
(acc, result) => ({
|
|
created: acc.created + result.created,
|
|
updated: acc.updated + result.updated,
|
|
rejected: acc.rejected + result.rejected,
|
|
merged: acc.merged + (result.merged ?? 0),
|
|
warnings: acc.warnings + (result.warnings?.length ?? 0),
|
|
}),
|
|
{ created: 0, updated: 0, rejected: 0, merged: 0, warnings: 0 },
|
|
);
|
|
});
|
|
|
|
async ngOnInit(): Promise<void> {
|
|
await Promise.all(this.flows().filter((flow) => !flow.validation()).map((flow) => this.store.validate(flow)));
|
|
this.validating.set(false);
|
|
}
|
|
|
|
setStrategy(strategy: ConflictStrategy): void {
|
|
this.store.setOnConflict(strategy);
|
|
}
|
|
|
|
/** Merging can add flows to the batch; validate them so their rows show up in the preview. */
|
|
toggleMerge(merge: boolean): void {
|
|
this.store.setMergeDuplicates(merge);
|
|
Promise.all(this.flows().filter((flow) => !flow.validation()).map((flow) => this.store.validate(flow)));
|
|
}
|
|
|
|
load(): void {
|
|
this._frozenFlows.set(this.store.submittableFlows());
|
|
this.store.commit().subscribe();
|
|
}
|
|
|
|
explore(): void {
|
|
this.dialogRef.close('explore');
|
|
}
|
|
}
|