integration layer; merge work on indicators from forks with existing ingestion pipeline
This commit is contained in:
@@ -0,0 +1,191 @@
|
||||
@let f = flow();
|
||||
@let s = summary();
|
||||
<div class="mapper" cdkDropListGroup>
|
||||
<section class="mapper__columns" aria-labelledby="source-columns-title">
|
||||
<header class="mapper__header">
|
||||
<h3 id="source-columns-title">Columnas de origen</h3>
|
||||
<mat-button-toggle-group
|
||||
hideSingleSelectionIndicator
|
||||
[value]="onlyUnusedColumns()"
|
||||
(change)="onlyUnusedColumns.set($event.value)"
|
||||
aria-label="Filtrar columnas de origen"
|
||||
>
|
||||
<mat-button-toggle [value]="false">Todas ({{ f.sourceColumns().length }})</mat-button-toggle>
|
||||
<mat-button-toggle [value]="true">Sin usar ({{ s.unused }})</mat-button-toggle>
|
||||
</mat-button-toggle-group>
|
||||
</header>
|
||||
|
||||
<div class="tray" cdkDropList [cdkDropListData]="'tray'" [cdkDropListEnterPredicate]="acceptsColumns" (cdkDropListDropped)="onDropOnTray($event)">
|
||||
@for (column of columns(); track column.name) {
|
||||
<div class="column-chip" [class.column-chip--used]="!!mappedTo(column.name)" cdkDrag [cdkDragData]="column.name">
|
||||
<mat-icon class="column-chip__type" [matTooltip]="column.inferred_type | typeLabel" aria-hidden="true">{{
|
||||
column.inferred_type | typeIcon
|
||||
}}</mat-icon>
|
||||
<span class="column-chip__text">
|
||||
<span class="column-chip__name">{{ column.name }}</span>
|
||||
<span class="column-chip__sample" [title]="samples().get(column.name) ?? ''">{{ samples().get(column.name) || '(vacío)' }}</span>
|
||||
</span>
|
||||
@if (mappedTo(column.name); as field) {
|
||||
<span class="column-chip__target" [title]="'Asignada a ' + field">→ {{ field }}</span>
|
||||
}
|
||||
<mat-icon class="column-chip__grip" aria-hidden="true">drag_indicator</mat-icon>
|
||||
<div *cdkDragPreview class="drag-preview">
|
||||
<mat-icon aria-hidden="true">{{ column.inferred_type | typeIcon }}</mat-icon>{{ column.name }}
|
||||
</div>
|
||||
</div>
|
||||
} @empty {
|
||||
<p class="tray__empty">{{ onlyUnusedColumns() ? 'Todas las columnas están asignadas.' : 'La fuente no tiene columnas.' }}</p>
|
||||
}
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="mapper__fields" aria-labelledby="target-fields-title">
|
||||
<header class="mapper__header">
|
||||
<h3 id="target-fields-title">Campos de {{ f.target.table }}</h3>
|
||||
<span class="mapper__summary" aria-live="polite">
|
||||
{{ s.covered }}/{{ s.total }} asignados
|
||||
@if (s.missing > 0) {
|
||||
· <strong class="mapper__missing">{{ s.missing }} obligatorio(s) sin asignar</strong>
|
||||
}
|
||||
</span>
|
||||
</header>
|
||||
|
||||
<div class="mapper__toolbar">
|
||||
<mat-form-field appearance="outline" subscriptSizing="dynamic" class="mapper__search">
|
||||
<mat-icon matPrefix aria-hidden="true">search</mat-icon>
|
||||
<mat-label>Buscar campo</mat-label>
|
||||
<input matInput (input)="onSearch($event)" />
|
||||
</mat-form-field>
|
||||
<mat-button-toggle-group
|
||||
hideSingleSelectionIndicator
|
||||
[value]="fieldFilter()"
|
||||
(change)="fieldFilter.set($event.value)"
|
||||
aria-label="Filtrar campos de destino"
|
||||
>
|
||||
<mat-button-toggle value="all">Todos</mat-button-toggle>
|
||||
<mat-button-toggle value="required">Obligatorios</mat-button-toggle>
|
||||
<mat-button-toggle value="unassigned">Sin asignar</mat-button-toggle>
|
||||
</mat-button-toggle-group>
|
||||
<span class="mapper__spacer"></span>
|
||||
@if (s.suggestions > 0) {
|
||||
<button mat-stroked-button type="button" (click)="confirmAll()" matTooltip="Aceptar las asignaciones automáticas marcadas con %">
|
||||
<mat-icon aria-hidden="true">done_all</mat-icon>Confirmar {{ s.suggestions }}
|
||||
</button>
|
||||
}
|
||||
<button mat-stroked-button type="button" (click)="autoMap()" matTooltip="Asignar por nombre lo que siga libre">
|
||||
<mat-icon aria-hidden="true">auto_fix_high</mat-icon>Auto-mapear
|
||||
</button>
|
||||
<button mat-button type="button" (click)="f.clearMapping()">
|
||||
<mat-icon aria-hidden="true">layers_clear</mat-icon>Limpiar
|
||||
</button>
|
||||
</div>
|
||||
@if (lastAutoMap() !== null) {
|
||||
<p class="mapper__note" role="status">
|
||||
{{ lastAutoMap() === 0 ? 'No se encontraron más coincidencias por nombre.' : 'Se asignaron ' + lastAutoMap() + ' campo(s).' }}
|
||||
</p>
|
||||
}
|
||||
|
||||
<div class="fields" role="list">
|
||||
@for (field of fields(); track field.name) {
|
||||
@let mapping = f.mapping.byField().get(field.name);
|
||||
@let defaultValue = f.defaults()[field.name];
|
||||
<div
|
||||
class="field"
|
||||
role="listitem"
|
||||
[class.field--missing]="field.required && !mapping && !defaultValue"
|
||||
[class.field--mapped]="!!mapping || !!defaultValue"
|
||||
>
|
||||
<div class="field__meta">
|
||||
<mat-icon class="field__type" [matTooltip]="field.type | typeLabel" aria-hidden="true">{{ field.type | typeIcon }}</mat-icon>
|
||||
<span class="field__name">{{ field.name }}</span>
|
||||
@if (field.required) {
|
||||
<span class="field__required" matTooltip="Obligatorio">*</span>
|
||||
}
|
||||
@if (errorsFor(field.name); as errors) {
|
||||
<span class="field__errors" [matTooltip]="errors + ' fila(s) no pasan la validación en este campo'">
|
||||
<mat-icon aria-hidden="true">report</mat-icon>{{ errors }}
|
||||
</span>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="slot"
|
||||
cdkDropList
|
||||
[cdkDropListData]="field.name"
|
||||
[cdkDropListEnterPredicate]="acceptsColumns"
|
||||
(cdkDropListDropped)="onDropOnField($event)"
|
||||
>
|
||||
@if (mapping) {
|
||||
<div class="slot__chip" [class.slot__chip--suggested]="mapping.confidence !== undefined" cdkDrag [cdkDragData]="mapping.source">
|
||||
<mat-icon aria-hidden="true">drag_indicator</mat-icon>
|
||||
<span class="slot__source" [title]="mapping.source">{{ mapping.source }}</span>
|
||||
@if (mapping.confidence !== undefined) {
|
||||
<span class="slot__confidence" [matTooltip]="'Sugerencia automática (' + mapping.reason + ')'">{{
|
||||
confidenceLabel(mapping.confidence)
|
||||
}}</span>
|
||||
<button mat-icon-button type="button" class="slot__button" matTooltip="Confirmar" [attr.aria-label]="'Confirmar ' + field.name" (click)="f.confirmSuggestion(field.name)">
|
||||
<mat-icon aria-hidden="true">check</mat-icon>
|
||||
</button>
|
||||
}
|
||||
<button mat-icon-button type="button" class="slot__button" matTooltip="Quitar" [attr.aria-label]="'Quitar asignación de ' + field.name" (click)="f.unmapField(field.name)">
|
||||
<mat-icon aria-hidden="true">close</mat-icon>
|
||||
</button>
|
||||
<div *cdkDragPreview class="drag-preview"><mat-icon aria-hidden="true">view_column</mat-icon>{{ mapping.source }}</div>
|
||||
</div>
|
||||
} @else {
|
||||
<mat-form-field appearance="outline" subscriptSizing="dynamic" class="slot__select">
|
||||
<mat-select placeholder="Arrastra o elige una columna" [value]="null" (selectionChange)="pick(field.name, $event.value)" [attr.aria-label]="'Columna para ' + field.name">
|
||||
@for (column of f.sourceColumns(); track column.name) {
|
||||
<mat-option [value]="column.name">
|
||||
{{ column.name }}
|
||||
@if (mappedTo(column.name); as other) {
|
||||
<span class="slot__option-note">(ahora en {{ other }})</span>
|
||||
}
|
||||
</mat-option>
|
||||
}
|
||||
</mat-select>
|
||||
</mat-form-field>
|
||||
}
|
||||
</div>
|
||||
|
||||
<div class="field__default">
|
||||
@if (editingDefault() === field.name) {
|
||||
<mat-form-field appearance="outline" subscriptSizing="dynamic" class="field__default-input">
|
||||
<mat-label>Valor por defecto</mat-label>
|
||||
<input
|
||||
matInput
|
||||
appAutofocus
|
||||
#defaultInput
|
||||
[value]="defaultValue ?? ''"
|
||||
(keydown.enter)="saveDefault(field.name, defaultInput.value)"
|
||||
(keydown.escape)="editingDefault.set(null)"
|
||||
(blur)="saveDefault(field.name, defaultInput.value)"
|
||||
/>
|
||||
</mat-form-field>
|
||||
} @else if (defaultValue) {
|
||||
<mat-chip-set>
|
||||
<mat-chip (removed)="f.setDefault(field.name, null)" (click)="editingDefault.set(field.name)" matTooltip="Se usa cuando la celda está vacía o no hay columna asignada">
|
||||
= {{ defaultValue }}
|
||||
<button matChipRemove [attr.aria-label]="'Quitar valor por defecto de ' + field.name"><mat-icon>cancel</mat-icon></button>
|
||||
</mat-chip>
|
||||
</mat-chip-set>
|
||||
} @else {
|
||||
<button
|
||||
mat-icon-button
|
||||
type="button"
|
||||
class="field__default-button"
|
||||
matTooltip="Valor por defecto"
|
||||
[attr.aria-label]="'Valor por defecto para ' + field.name"
|
||||
(click)="editingDefault.set(field.name)"
|
||||
>
|
||||
<mat-icon aria-hidden="true">edit_note</mat-icon>
|
||||
</button>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
} @empty {
|
||||
<p class="fields__empty">Ningún campo coincide con el filtro.</p>
|
||||
}
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
@@ -0,0 +1,340 @@
|
||||
:host {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.mapper {
|
||||
align-items: start;
|
||||
display: grid;
|
||||
gap: var(--space-4);
|
||||
grid-template-columns: minmax(260px, 340px) 1fr;
|
||||
}
|
||||
|
||||
.mapper__header {
|
||||
align-items: center;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: var(--space-2);
|
||||
justify-content: space-between;
|
||||
margin-bottom: var(--space-2);
|
||||
|
||||
h3 {
|
||||
font: var(--mat-sys-title-small);
|
||||
margin: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.mapper__summary {
|
||||
color: var(--mat-sys-on-surface-variant);
|
||||
font: var(--mat-sys-body-small);
|
||||
}
|
||||
|
||||
.mapper__missing {
|
||||
color: var(--mat-sys-error);
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.mapper__columns {
|
||||
position: sticky;
|
||||
top: 0;
|
||||
}
|
||||
|
||||
.tray {
|
||||
background: var(--mat-sys-surface-container-low);
|
||||
border: 1px solid var(--mat-sys-outline-variant);
|
||||
border-radius: var(--mat-sys-corner-medium);
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: var(--space-1);
|
||||
max-height: 60vh;
|
||||
min-height: 96px;
|
||||
overflow: auto;
|
||||
padding: var(--space-2);
|
||||
}
|
||||
|
||||
.tray.cdk-drop-list-dragging {
|
||||
outline: 2px dashed var(--mat-sys-outline);
|
||||
}
|
||||
|
||||
.tray__empty,
|
||||
.fields__empty {
|
||||
color: var(--mat-sys-on-surface-variant);
|
||||
font: var(--mat-sys-body-small);
|
||||
margin: var(--space-2);
|
||||
}
|
||||
|
||||
.column-chip {
|
||||
align-items: center;
|
||||
background: var(--mat-sys-surface);
|
||||
border: 1px solid var(--mat-sys-outline-variant);
|
||||
border-radius: var(--mat-sys-corner-small);
|
||||
cursor: grab;
|
||||
display: flex;
|
||||
gap: var(--space-2);
|
||||
padding: 6px var(--space-2);
|
||||
|
||||
&:hover {
|
||||
border-color: var(--mat-sys-primary);
|
||||
}
|
||||
}
|
||||
|
||||
.column-chip--used {
|
||||
opacity: 0.62;
|
||||
}
|
||||
|
||||
.column-chip__type {
|
||||
color: var(--mat-sys-primary);
|
||||
flex: none;
|
||||
font-size: 18px;
|
||||
height: 18px;
|
||||
width: 18px;
|
||||
}
|
||||
|
||||
.column-chip__text {
|
||||
display: flex;
|
||||
flex: 1;
|
||||
flex-direction: column;
|
||||
line-height: 1.25;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.column-chip__name {
|
||||
font: var(--mat-sys-label-large);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.column-chip__sample {
|
||||
color: var(--mat-sys-on-surface-variant);
|
||||
font: var(--mat-sys-body-small);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.column-chip__target {
|
||||
color: var(--mat-sys-primary);
|
||||
flex: none;
|
||||
font: var(--mat-sys-label-small);
|
||||
max-width: 45%;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.column-chip__grip {
|
||||
color: var(--mat-sys-outline);
|
||||
flex: none;
|
||||
font-size: 18px;
|
||||
height: 18px;
|
||||
width: 18px;
|
||||
}
|
||||
|
||||
.mapper__toolbar {
|
||||
align-items: center;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: var(--space-2);
|
||||
margin-bottom: var(--space-2);
|
||||
}
|
||||
|
||||
.mapper__search {
|
||||
width: 220px;
|
||||
}
|
||||
|
||||
.mapper__spacer {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.mapper__note {
|
||||
color: var(--mat-sys-on-surface-variant);
|
||||
font: var(--mat-sys-body-small);
|
||||
margin: 0 0 var(--space-2);
|
||||
}
|
||||
|
||||
.fields {
|
||||
border: 1px solid var(--mat-sys-outline-variant);
|
||||
border-radius: var(--mat-sys-corner-medium);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.field {
|
||||
align-items: center;
|
||||
border-bottom: 1px solid var(--mat-sys-outline-variant);
|
||||
display: grid;
|
||||
gap: var(--space-3);
|
||||
grid-template-columns: minmax(180px, 1fr) minmax(220px, 1.2fr) minmax(48px, 0.7fr);
|
||||
min-height: 56px;
|
||||
padding: var(--space-1) var(--space-3);
|
||||
|
||||
&:last-child {
|
||||
border-bottom: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.field--missing {
|
||||
background: color-mix(in srgb, var(--mat-sys-error-container) 35%, transparent);
|
||||
}
|
||||
|
||||
.field__meta {
|
||||
align-items: center;
|
||||
display: flex;
|
||||
gap: var(--space-2);
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.field__type {
|
||||
color: var(--mat-sys-on-surface-variant);
|
||||
flex: none;
|
||||
font-size: 18px;
|
||||
height: 18px;
|
||||
width: 18px;
|
||||
}
|
||||
|
||||
.field__name {
|
||||
font: var(--mat-sys-body-medium);
|
||||
font-family: 'Roboto Mono', ui-monospace, monospace;
|
||||
font-size: 13px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.field__required {
|
||||
color: var(--mat-sys-error);
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.field__errors {
|
||||
align-items: center;
|
||||
background: #fff1d6;
|
||||
border-radius: 999px;
|
||||
color: #7a4b00;
|
||||
display: inline-flex;
|
||||
font: var(--mat-sys-label-small);
|
||||
gap: 2px;
|
||||
padding: 1px 6px 1px 3px;
|
||||
|
||||
mat-icon {
|
||||
font-size: 14px;
|
||||
height: 14px;
|
||||
width: 14px;
|
||||
}
|
||||
}
|
||||
|
||||
.slot {
|
||||
border: 1.5px dashed transparent;
|
||||
border-radius: var(--mat-sys-corner-small);
|
||||
min-height: 44px;
|
||||
padding: 2px;
|
||||
transition: border-color 0.12s ease, background-color 0.12s ease;
|
||||
}
|
||||
|
||||
.slot.cdk-drop-list-receiving {
|
||||
border-color: var(--mat-sys-outline);
|
||||
}
|
||||
|
||||
.slot.cdk-drop-list-dragging {
|
||||
background: var(--mat-sys-primary-container);
|
||||
border-color: var(--mat-sys-primary);
|
||||
}
|
||||
|
||||
.slot ::ng-deep .cdk-drag-placeholder {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.slot__select {
|
||||
width: 100%;
|
||||
--mat-form-field-container-height: 40px;
|
||||
--mat-form-field-container-vertical-padding: 8px;
|
||||
}
|
||||
|
||||
.slot__option-note {
|
||||
color: var(--mat-sys-on-surface-variant);
|
||||
font-size: 12px;
|
||||
margin-left: 4px;
|
||||
}
|
||||
|
||||
.slot__chip {
|
||||
align-items: center;
|
||||
background: var(--mat-sys-secondary-container);
|
||||
border-radius: var(--mat-sys-corner-small);
|
||||
color: var(--mat-sys-on-secondary-container);
|
||||
cursor: grab;
|
||||
display: flex;
|
||||
gap: var(--space-1);
|
||||
min-height: 40px;
|
||||
padding: 0 var(--space-1) 0 var(--space-2);
|
||||
|
||||
> mat-icon {
|
||||
font-size: 18px;
|
||||
height: 18px;
|
||||
opacity: 0.6;
|
||||
width: 18px;
|
||||
}
|
||||
}
|
||||
|
||||
.slot__chip--suggested {
|
||||
background: var(--mat-sys-surface-container-high);
|
||||
border: 1px dashed var(--mat-sys-primary);
|
||||
color: var(--mat-sys-on-surface);
|
||||
}
|
||||
|
||||
.slot__source {
|
||||
flex: 1;
|
||||
font: var(--mat-sys-label-large);
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.slot__confidence {
|
||||
background: var(--mat-sys-primary);
|
||||
border-radius: 999px;
|
||||
color: var(--mat-sys-on-primary);
|
||||
font: var(--mat-sys-label-small);
|
||||
padding: 1px 6px;
|
||||
}
|
||||
|
||||
.slot__button {
|
||||
--mat-icon-button-state-layer-size: 28px;
|
||||
height: 28px;
|
||||
padding: 2px;
|
||||
width: 28px;
|
||||
|
||||
mat-icon {
|
||||
font-size: 18px;
|
||||
height: 18px;
|
||||
width: 18px;
|
||||
}
|
||||
}
|
||||
|
||||
.field__default {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.field__default-input {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.field__default-button {
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.field:hover .field__default-button {
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
@media (max-width: 1100px) {
|
||||
.mapper {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
.mapper__columns {
|
||||
position: static;
|
||||
}
|
||||
.tray {
|
||||
max-height: 260px;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { CdkDragDrop } from '@angular/cdk/drag-drop';
|
||||
import { ImportFlow } from '../../domain/import-flow';
|
||||
import { FakeConnection, FakeTable, NO_GATEWAY, PACIENTE } from '../../testing/fakes';
|
||||
import { ColumnMapper } from './column-mapper';
|
||||
|
||||
describe('ColumnMapper', () => {
|
||||
function setup() {
|
||||
const flow = new ImportFlow(new FakeTable(new FakeConnection()), PACIENTE);
|
||||
flow.load(NO_GATEWAY).subscribe();
|
||||
const fixture = TestBed.createComponent(ColumnMapper);
|
||||
fixture.componentRef.setInput('flow', flow);
|
||||
fixture.detectChanges();
|
||||
return { flow, fixture, mapper: fixture.componentInstance };
|
||||
}
|
||||
|
||||
const drop = (column: string, target: string) =>
|
||||
({ item: { data: column }, container: { data: target }, previousContainer: { data: 'tray' } }) as unknown as CdkDragDrop<string>;
|
||||
|
||||
it('renders a slot per destination field, required first, with the missing one flagged', () => {
|
||||
const { fixture } = setup();
|
||||
const rows = Array.from(fixture.nativeElement.querySelectorAll('.field')) as HTMLElement[];
|
||||
expect(rows.map((row) => row.querySelector('.field__name')?.textContent?.trim())).toEqual(['pac_id_hospital', 'pac_sexo', 'pac_parentesco']);
|
||||
expect(rows[0].classList).toContain('field--missing');
|
||||
});
|
||||
|
||||
it('dropping a column on a field maps it; dropping it back on the tray unmaps it', () => {
|
||||
const { flow, mapper } = setup();
|
||||
mapper.onDropOnField(drop('Clave', 'pac_id_hospital'));
|
||||
expect(flow.mapping.toRecord()).toEqual({ Sexo: 'pac_sexo', Clave: 'pac_id_hospital' });
|
||||
expect(flow.status()).toBe('ready');
|
||||
|
||||
mapper.onDropOnTray({ item: { data: 'Clave' }, container: {}, previousContainer: { other: true } } as unknown as CdkDragDrop<string>);
|
||||
expect(flow.mapping.toRecord()).toEqual({ Sexo: 'pac_sexo' });
|
||||
});
|
||||
|
||||
it('the select is an alternative to dragging, and a default value covers a required field', () => {
|
||||
const { flow, mapper } = setup();
|
||||
mapper.pick('pac_sexo', null);
|
||||
expect(flow.mapping.size()).toBe(0);
|
||||
mapper.saveDefault('pac_id_hospital', ' H-DEFAULT ');
|
||||
expect(flow.defaults()).toEqual({ pac_id_hospital: 'H-DEFAULT' });
|
||||
expect(flow.missingRequired()).toEqual([]);
|
||||
});
|
||||
|
||||
it('filters fields and columns', () => {
|
||||
const { mapper } = setup();
|
||||
mapper.fieldFilter.set('unassigned');
|
||||
expect(mapper.fields().map((f) => f.name)).toEqual(['pac_id_hospital', 'pac_parentesco']);
|
||||
mapper.onlyUnusedColumns.set(true);
|
||||
expect(mapper.columns().map((c) => c.name)).toEqual(['Clave']);
|
||||
expect(mapper.samples().get('Sexo')).toBe('F · X');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,152 @@
|
||||
import { CdkDrag, CdkDragDrop, DragDropModule } from '@angular/cdk/drag-drop';
|
||||
import { Component, computed, input, signal } from '@angular/core';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
import { MatButtonToggleModule } from '@angular/material/button-toggle';
|
||||
import { MatChipsModule } from '@angular/material/chips';
|
||||
import { MatFormFieldModule } from '@angular/material/form-field';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatInputModule } from '@angular/material/input';
|
||||
import { MatSelectModule } from '@angular/material/select';
|
||||
import { MatTooltipModule } from '@angular/material/tooltip';
|
||||
import { SourceColumn, TargetField } from '../../api/api.types';
|
||||
import { ImportFlow } from '../../domain/import-flow';
|
||||
import { AutofocusDirective } from '../shared/autofocus.directive';
|
||||
import { TypeIconPipe, TypeLabelPipe } from '../shared/type-icon.pipe';
|
||||
|
||||
type FieldFilter = 'all' | 'required' | 'unassigned';
|
||||
|
||||
/**
|
||||
* Drag-and-drop column mapping for one flow: source columns on the left,
|
||||
* destination fields (drop slots) on the right. Every slot also takes a
|
||||
* pick from a select (keyboard/accessibility path) and an optional default
|
||||
* value, so a field the source lacks can still be filled.
|
||||
*
|
||||
* Presentation only: every edit goes through `ImportFlow`, which owns the
|
||||
* mapping state.
|
||||
*/
|
||||
@Component({
|
||||
selector: 'app-column-mapper',
|
||||
imports: [
|
||||
AutofocusDirective,
|
||||
DragDropModule,
|
||||
MatButtonModule,
|
||||
MatButtonToggleModule,
|
||||
MatChipsModule,
|
||||
MatFormFieldModule,
|
||||
MatIconModule,
|
||||
MatInputModule,
|
||||
MatSelectModule,
|
||||
MatTooltipModule,
|
||||
TypeIconPipe,
|
||||
TypeLabelPipe,
|
||||
],
|
||||
templateUrl: './column-mapper.html',
|
||||
styleUrl: './column-mapper.scss',
|
||||
})
|
||||
export class ColumnMapper {
|
||||
readonly flow = input.required<ImportFlow>();
|
||||
|
||||
readonly fieldFilter = signal<FieldFilter>('all');
|
||||
readonly fieldSearch = signal('');
|
||||
readonly onlyUnusedColumns = signal(false);
|
||||
readonly editingDefault = signal<string | null>(null);
|
||||
readonly lastAutoMap = signal<number | null>(null);
|
||||
|
||||
readonly acceptsColumns = (drag: CdkDrag) => typeof drag.data === 'string';
|
||||
|
||||
readonly fields = computed<TargetField[]>(() => {
|
||||
const flow = this.flow();
|
||||
const mapped = flow.mapping.byField();
|
||||
const defaults = flow.defaults();
|
||||
const term = this.fieldSearch().trim().toLowerCase();
|
||||
return flow.target.fields
|
||||
.filter((field) => !term || field.name.toLowerCase().includes(term))
|
||||
.filter((field) => {
|
||||
switch (this.fieldFilter()) {
|
||||
case 'required':
|
||||
return field.required;
|
||||
case 'unassigned':
|
||||
return !mapped.has(field.name) && !defaults[field.name];
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
})
|
||||
.sort((a, b) => Number(b.required) - Number(a.required));
|
||||
});
|
||||
|
||||
readonly columns = computed<SourceColumn[]>(() => {
|
||||
const flow = this.flow();
|
||||
return this.onlyUnusedColumns() ? flow.unmappedColumns() : flow.sourceColumns();
|
||||
});
|
||||
|
||||
/** Up to three distinct non-empty sample values per source column, for the tray. */
|
||||
readonly samples = computed(() => {
|
||||
const rows = this.flow().preview()?.sample_rows ?? [];
|
||||
const samples = new Map<string, string>();
|
||||
for (const column of this.flow().sourceColumns()) {
|
||||
const values = new Set<string>();
|
||||
for (const row of rows) {
|
||||
const value = row[column.name];
|
||||
if (value !== null && value !== undefined && String(value).trim() !== '') values.add(String(value));
|
||||
if (values.size === 3) break;
|
||||
}
|
||||
samples.set(column.name, [...values].join(' · '));
|
||||
}
|
||||
return samples;
|
||||
});
|
||||
|
||||
readonly summary = computed(() => {
|
||||
const flow = this.flow();
|
||||
const covered = flow.target.fields.filter((f) => flow.mapping.byField().has(f.name) || flow.defaults()[f.name]).length;
|
||||
return {
|
||||
covered,
|
||||
total: flow.target.fields.length,
|
||||
missing: flow.missingRequired().length,
|
||||
suggestions: flow.pendingSuggestions(),
|
||||
unused: flow.unmappedColumns().length,
|
||||
};
|
||||
});
|
||||
|
||||
onSearch(event: Event): void {
|
||||
this.fieldSearch.set((event.target as HTMLInputElement).value);
|
||||
}
|
||||
|
||||
onDropOnField(event: CdkDragDrop<string>): void {
|
||||
this.flow().map(event.item.data as string, event.container.data);
|
||||
}
|
||||
|
||||
onDropOnTray(event: CdkDragDrop<string>): void {
|
||||
if (event.previousContainer !== event.container) this.flow().unmapSource(event.item.data as string);
|
||||
}
|
||||
|
||||
pick(field: string, source: string | null): void {
|
||||
if (source) this.flow().map(source, field);
|
||||
else this.flow().unmapField(field);
|
||||
}
|
||||
|
||||
autoMap(): void {
|
||||
this.lastAutoMap.set(this.flow().autoMap());
|
||||
}
|
||||
|
||||
confirmAll(): void {
|
||||
const flow = this.flow();
|
||||
flow.mapping.items().forEach((m) => m.confidence !== undefined && flow.confirmSuggestion(m.field));
|
||||
}
|
||||
|
||||
saveDefault(field: string, value: string): void {
|
||||
this.flow().setDefault(field, value.trim() || null);
|
||||
this.editingDefault.set(null);
|
||||
}
|
||||
|
||||
errorsFor(field: string): number {
|
||||
return this.flow().quality().get(field)?.errors ?? 0;
|
||||
}
|
||||
|
||||
mappedTo(column: string): string | undefined {
|
||||
return this.flow().mapping.bySource().get(column)?.field;
|
||||
}
|
||||
|
||||
confidenceLabel(confidence: number): string {
|
||||
return `${Math.round(confidence * 100)}%`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user