Merged PR 487: #1405 bugfix search customer via customer card number (scan)

#1405 bugfix search customer via customer card number (scan)
This commit is contained in:
Nino Righi
2021-02-12 09:52:26 +00:00
committed by Lorenz Hilpert
2 changed files with 76 additions and 1 deletions

View File

@@ -52,6 +52,10 @@ export class CrmCustomerService {
});
}
getCustomersByCustomerCardNumber(queryString: string): Observable<PagedResult<CustomerInfoDTO>> {
return this.customerService.CustomerGetCustomerByBonuscard(!!queryString ? queryString : undefined);
}
getCustomer(customerId: number, eagerLoading?: number): Observable<Result<CustomerDTO>> {
return this.customerService.CustomerGetCustomer({ customerId, eagerLoading });
}

View File

@@ -330,10 +330,81 @@ export abstract class CustomerSearch implements OnInit, OnDestroy {
)
.subscribe((result) => {
this.setQuery(result.data);
this.search();
// this.search();
this.searchCustomerCard();
});
}
searchCustomerCard(
options: { isNewSearch: boolean; take?: number } = {
isNewSearch: true,
take: 10,
}
): void {
if (!this.shouldFetchNewProducts(options)) {
return; // early exit because no new products need to be fetched
}
if (this.searchState !== 'fetching') {
this.searchState$.next('fetching');
this.searchResult$
.pipe(
take(1),
switchMap(() => {
return this.customerSearch.getCustomersByCustomerCardNumber(this.queryFilter.query).pipe(
takeUntil(this.destroy$),
catchError((err: HttpErrorResponse) => {
return of<PagedResult<CustomerInfoDTO>>({
result: [],
error: true,
hits: 0,
message: err.message,
});
}),
tap((result) => {
if (result.error) {
this.searchState$.next('error');
} else if (result.hits === 0) {
this.searchState$.next('empty');
} else {
this.searchState$.next('result');
}
})
);
})
)
.subscribe((r) => {
const hits = r.hits || r.result.length;
this.searchResult$.next(this.removeLoadingProducts(this.searchResult));
if (options.isNewSearch) {
this.searchResult$.next({
...r,
hits,
result: r.result.map((a) => ({ ...a, loaded: true })),
});
if (this.searchState === 'result') {
if (hits === 1) {
this.navigateToDetails(r.result[0].id);
} else {
this.navigateToResults();
}
}
} else {
this.searchResult$.next({
...r,
hits,
result: [...this.searchResult$.value.result, ...r.result.map((a) => ({ ...a, loaded: true }))],
});
}
if (hits > 0) {
this.filterActive$.next(false);
}
});
}
}
navigateToDetails(customerId: number) {
this.router.navigate(['customer', customerId]);
}