Add Search with CustomerCard (query for customerNumber before querying))

This commit is contained in:
Sebastian
2020-09-03 10:54:41 +02:00
parent cc69a0a24e
commit 39de228eff
2 changed files with 61 additions and 13 deletions

View File

@@ -117,7 +117,7 @@ export class ShelfSearchInputComponent
this.triggerSearch({ type: 'scan', value: barcode });
}
triggerSearch(
async triggerSearch(
{
type,
value,
@@ -136,12 +136,17 @@ export class ShelfSearchInputComponent
if (this.isAutocompleteSelected()) {
searchQuery = this.selectedItem$.value.query;
this.shelfSearchService.search(searchQuery, { bypassValidation });
await this.shelfSearchService.search(searchQuery, { bypassValidation });
this.updateSearchbarValue(searchQuery);
} else if (type === 'search') {
this.shelfSearchService.search(value, { bypassValidation });
await this.shelfSearchService.search(value, {
bypassValidation,
allowCustomerCardSearch: true,
});
} else if (type === 'scan') {
this.shelfSearchService.searchWithBarcode(value);
await this.shelfSearchService.searchWithBarcode(value, {
allowCustomerCardSearch: true,
});
}
this.setUpNavigation();

View File

@@ -2,14 +2,14 @@ import { Injectable } from '@angular/core';
import { Select } from '@ngxs/store';
import { BranchSelectors } from 'apps/sales/src/app/core/store/selectors/branch.selector';
import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { switchMap, map, take, catchError } from 'rxjs/operators';
import { CollectingShelfService } from 'apps/sales/src/app/core/services/collecting-shelf.service';
import {
ListResponseArgsOfOrderItemListItemDTO,
AutocompleteTokenDTO,
ResponseArgsOfIEnumerableOfAutocompleteDTO,
} from '@swagger/oms/lib';
import { SearchStateFacade } from '@shelf-store';
import { CustomerService } from '@sales/core-services';
@Injectable({ providedIn: 'root' })
export class ShelfSearchFacadeService {
@@ -18,20 +18,25 @@ export class ShelfSearchFacadeService {
>;
constructor(
private collectingShelfService: CollectingShelfService,
private searchStateFacade: SearchStateFacade
private readonly customerService: CustomerService,
private readonly collectingShelfService: CollectingShelfService,
private readonly searchStateFacade: SearchStateFacade
) {}
search(
async search(
queryString: string,
options: {
bypassValidation: boolean;
allowCustomerCardSearch?: boolean;
} = {
bypassValidation: false,
allowCustomerCardSearch: true,
}
) {
const searchQuery = queryString.trim();
const { bypassValidation } = options;
const { bypassValidation, allowCustomerCardSearch } = options;
const searchQuery = await this.getSearchQuery(queryString, {
allowCustomerCardSearch,
});
if (!bypassValidation && !this.isValidSearchQuery(searchQuery)) {
return;
@@ -41,8 +46,16 @@ export class ShelfSearchFacadeService {
return this.requestSearch(true);
}
searchWithBarcode(barcode: string) {
const searchQuery = this.getBarcodeSearchQuery(barcode);
async searchWithBarcode(
barcode: string,
options: { allowCustomerCardSearch: boolean } = {
allowCustomerCardSearch: true,
}
) {
const searchQuery = await this.getSearchQuery(
this.getBarcodeSearchQuery(barcode),
{ allowCustomerCardSearch: options.allowCustomerCardSearch }
);
if (!this.isValidSearchQuery(searchQuery)) {
return;
@@ -76,6 +89,17 @@ export class ShelfSearchFacadeService {
return this.searchStateFacade.fetchResult({ isNewSearch });
}
private getSearchQuery(
queryString: string,
options: { allowCustomerCardSearch: boolean }
): Promise<string> {
if (options.allowCustomerCardSearch && this.isCustomerCard(queryString)) {
return this.getCustomerNumber(queryString);
}
return Promise.resolve(queryString.trim());
}
private generateAutocompleteToken(params: {
queryString: string;
take?: number;
@@ -109,4 +133,23 @@ export class ShelfSearchFacadeService {
private getBarcodeSearchQuery(barcode: string) {
return barcode.replace('ORD:', '').trim();
}
private isCustomerCard(queryString: string): boolean {
const regExp = /^(?=.*\d)(?=.*[A-Z])[A-Z0-9]{8,12}$/;
return regExp.test(queryString);
}
private getCustomerNumber(customerCardNumber: string): Promise<string> {
return this.customerService
.searchCustomer(customerCardNumber)
.pipe(
map((response) => response.customers),
map((customers) =>
customers[0] ? customers[0].customerNumber : customerCardNumber
),
catchError(() => customerCardNumber),
take(1)
)
.toPromise();
}
}