49 lines
1.9 KiB
TypeScript
49 lines
1.9 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { Injectable, inject } from '@angular/core';
|
|
import { Observable, shareReplay } from 'rxjs';
|
|
import {
|
|
BatchResult,
|
|
ConflictStrategy,
|
|
DerivationCatalog,
|
|
ImportSourcePayload,
|
|
PreviewResponse,
|
|
Row,
|
|
ValidationResult,
|
|
} from './api.types';
|
|
|
|
/**
|
|
* POST /imports/validate (one source, writes nothing), POST /imports/commit
|
|
* (a batch, one transaction), the rows a source would write
|
|
* (POST /imports/mapped-preview), the columns documents add to a source
|
|
* (POST /imports/sources/document/columns) and the operations derived
|
|
* fields may use (GET /imports/derivations, fetched once).
|
|
*/
|
|
@Injectable({ providedIn: 'root' })
|
|
export class ImportsApi {
|
|
private readonly http = inject(HttpClient);
|
|
private catalog?: Observable<DerivationCatalog>;
|
|
|
|
derivations(): Observable<DerivationCatalog> {
|
|
this.catalog ??= this.http.get<DerivationCatalog>('/imports/derivations').pipe(shareReplay(1));
|
|
return this.catalog;
|
|
}
|
|
|
|
/** A source with document columns: its own columns plus every field and caption its documents print. */
|
|
documentColumns(source: ImportSourcePayload): Observable<PreviewResponse> {
|
|
return this.http.post<PreviewResponse>('/imports/sources/document/columns', { source });
|
|
}
|
|
|
|
mappedPreview(source: ImportSourcePayload, limit = 20): Observable<{ rows: Row[] }> {
|
|
return this.http.post<{ rows: Row[] }>('/imports/mapped-preview', { source, limit });
|
|
}
|
|
|
|
validate(source: ImportSourcePayload): Observable<ValidationResult> {
|
|
return this.http.post<ValidationResult>('/imports/validate', { source });
|
|
}
|
|
|
|
/** `mergeDuplicates`: rows of different sources with the same natural key become one record. */
|
|
commit(sources: ImportSourcePayload[], onConflict: ConflictStrategy, mergeDuplicates = false): Observable<BatchResult> {
|
|
return this.http.post<BatchResult>('/imports/commit', { sources, on_conflict: onConflict, merge_duplicates: mergeDuplicates });
|
|
}
|
|
}
|