Skip to content

Commit

Permalink
feat: handle list of items on barcode search (TT-1679) (#8)
Browse files Browse the repository at this point in the history
Extends the regex to match more barcode variants and also handles the
API now returning a list of DigitizedItems for barcode search.

---------

Co-authored-by: Marius Levang <55030714+MariusLevang@users.noreply.github.com>
  • Loading branch information
fredrikmonsen and MariusLevang authored Aug 22, 2024
1 parent 006e337 commit 5cd86e3
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/app/services/production.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@ import {environment} from "../../environments/environment";
export class ProductionService {

private baseUrl = `${environment.baseHref}/api`
private almaBarcodeRegEx = new RegExp('^\\d{2}g\\d{6}$|^h\\d{2}\\w\\d{5}$|^h\\d{8}$');

constructor(
private http: HttpClient
) { }

searchItem(searchQuery: string): Observable<DigitizedItem | undefined> {
let httpRequest: Observable<DigitizedItem>;
if (searchQuery.match(this.almaBarcodeRegEx)) {
httpRequest = this.http.get<DigitizedItem>(`${this.baseUrl}/proddb/barcode/${searchQuery}`)
}
else {
httpRequest = this.http.get<DigitizedItem>(`${this.baseUrl}/proddb/${searchQuery}`)
let httpQuery: Observable<DigitizedItem>;

if (this.isValidBarcode(searchQuery)) {
httpQuery = this.http.get<DigitizedItem[]>(`${this.baseUrl}/proddb/barcode/${searchQuery}`).pipe(
mergeMap(items => items.map(item => new DigitizedItemBuilder(item).build()))
)
} else {
httpQuery = this.http.get<DigitizedItem>(`${this.baseUrl}/proddb/${searchQuery}`).pipe(
map(item => new DigitizedItemBuilder(item).build())
);
}
return httpRequest.pipe(
map(item => new DigitizedItemBuilder(item).build()),

return httpQuery.pipe(
mergeMap(item => {
if (item.id && (item.type === MaterialTypeEnum.NewspaperBundle || item.type === MaterialTypeEnum.PeriodicalBundle)) {
return this.getRelatedItems(item.id).pipe(
Expand Down Expand Up @@ -120,4 +123,11 @@ export class ProductionService {
return false;
}
}

private isValidBarcode(barcode: string): boolean {
const barcodeVariant1 = new RegExp('^\\d{2}[a-zA-Z]{1,2}\\d{5}$');
const barcodeVariant2 = new RegExp('^\\d{2}[a-zA-Z]\\d{6}$');
const barcodeVariant3 = new RegExp('^h\\d{2}[a-zA-Z]\\d{5}$|^h\\d{8}$')
return barcodeVariant1.test(barcode) || barcodeVariant2.test(barcode) || barcodeVariant3.test(barcode);
}
}

0 comments on commit 5cd86e3

Please sign in to comment.