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';
7 @Injectable({providedIn: 'root'})
8 export class BookingResourceBarcodeValidator implements AsyncValidator {
10 private pcrud: PcrudService) {
13 validate = (control: FormControl) => {
14 return this.pcrud.search('brsrc',
15 {'barcode' : control.value},
17 switchMap(() => of(null)),
18 catchError((err: unknown) => {
19 return of({ resourceBarcode: 'No resource found with that barcode' });
25 selector: '[egValidBookingResourceBarcode]',
27 provide: NG_ASYNC_VALIDATORS,
28 useExisting: forwardRef(() => BookingResourceBarcodeValidator),
33 export class BookingResourceBarcodeValidatorDirective {
35 private validator: BookingResourceBarcodeValidator
38 validate = (control: FormControl) => {
39 this.validator.validate(control);