44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import { HttpClient, httpResource } from '@angular/common/http';
|
|
import { inject, Injectable, Signal } from '@angular/core';
|
|
import { Observable } from 'rxjs';
|
|
import { catchError } from 'rxjs/operators';
|
|
|
|
import { ErrorLoggerService } from '../core/error-logger.service';
|
|
import { Voucher } from '../core/voucher';
|
|
import { ExecuteResponse, ImportRequest, MappingUpdate, MappingEntry, PreviewResponse } from './sales-import';
|
|
|
|
const url = '/api/sales-import';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class SalesImportService {
|
|
private http = inject(HttpClient);
|
|
private log = inject(ErrorLoggerService);
|
|
|
|
voucher(id: Signal<string | null>) {
|
|
return httpResource<Voucher>(() => {
|
|
const id_ = id();
|
|
return id_ === null ? undefined : `${url}/voucher/${id_}`;
|
|
});
|
|
}
|
|
|
|
preview(request: ImportRequest): Observable<PreviewResponse> {
|
|
return this.http
|
|
.post<PreviewResponse>(`${url}/preview`, request)
|
|
.pipe(catchError(this.log.handleError('SalesImportService', 'preview'))) as Observable<PreviewResponse>;
|
|
}
|
|
|
|
execute(request: ImportRequest): Observable<ExecuteResponse> {
|
|
return this.http
|
|
.post<ExecuteResponse>(`${url}/execute`, request)
|
|
.pipe(catchError(this.log.handleError('SalesImportService', 'execute'))) as Observable<ExecuteResponse>;
|
|
}
|
|
|
|
saveMapping(mapping: MappingUpdate): Observable<MappingEntry[]> {
|
|
return this.http
|
|
.put<MappingEntry[]>(`${url}/mapping`, mapping)
|
|
.pipe(catchError(this.log.handleError('SalesImportService', 'saveMapping'))) as Observable<MappingEntry[]>;
|
|
}
|
|
}
|