]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/booking/booking_resource_validator.directive.ts
LP1816475: Fix circular dependency warning
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / booking / booking_resource_validator.directive.ts
1 import {Directive, forwardRef, Injectable} from '@angular/core';
2 import {NG_ASYNC_VALIDATORS, AsyncValidator, FormControl} from '@angular/forms';
3 import {of} from 'rxjs';
4 import {switchMap, catchError} from 'rxjs/operators';
5 import {PcrudService} from '@eg/core/pcrud.service';
6
7 @Injectable({providedIn: 'root'})
8 export class BookingResourceBarcodeValidator implements AsyncValidator {
9     constructor(
10         private pcrud: PcrudService) {
11     }
12
13     validate = (control: FormControl) => {
14         return this.pcrud.search('brsrc',
15             {'barcode' : control.value},
16             {'limit': 1}).pipe(
17                 switchMap(() => of(null)),
18                 catchError((err) => {
19                     return of({ resourceBarcode: 'No resource found with that barcode' });
20                 }));
21     }
22 }
23
24 @Directive({
25     selector: '[egValidBookingResourceBarcode]',
26     providers: [{
27         provide: NG_ASYNC_VALIDATORS,
28         useExisting: forwardRef(() => BookingResourceBarcodeValidator),
29         multi: true
30     }]
31 })
32
33 export class BookingResourceBarcodeValidatorDirective {
34     constructor(
35         private validator: BookingResourceBarcodeValidator
36     ) { }
37
38     validate = (control: FormControl) => {
39         this.validator.validate(control);
40     }
41 }
42