From 607d93980d12f205ef798cfd1ab1fb7ae8f871c3 Mon Sep 17 00:00:00 2001 From: Jane Sandberg Date: Mon, 8 Apr 2019 08:54:25 -0700 Subject: [PATCH] LP1816475: Move reservation validations to their own service Signed-off-by: Jane Sandberg --- .../booking/create-reservation.component.html | 2 + .../staff/booking/create-reservation.component.ts | 2 + .../staff/booking/reservation-validate.service.ts | 62 ++++++++++++++++++++++ .../staff/booking/reservations-grid.component.html | 2 +- .../staff/booking/reservations-grid.component.ts | 20 ++----- 5 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 Open-ILS/src/eg2/src/app/staff/booking/reservation-validate.service.ts diff --git a/Open-ILS/src/eg2/src/app/staff/booking/create-reservation.component.html b/Open-ILS/src/eg2/src/app/staff/booking/create-reservation.component.html index 642022f5a7..ce0d0bca36 100644 --- a/Open-ILS/src/eg2/src/app/staff/booking/create-reservation.component.html +++ b/Open-ILS/src/eg2/src/app/staff/booking/create-reservation.component.html @@ -174,6 +174,8 @@ [minuteStep]="minuteStep()" [timezone]="pickupLibUsesDifferentTz ? pickupLibUsesDifferentTz : format.wsOrgTimezone" (onChangeAsIso)="record[field.name]($event)" + (onChangeAsMoment)="field.validatorError = reservationValidate[field.name](field.name, $event, record)" + [validatorError]="field.validatorError" [initialMoment]="defaultTimes[field.name]"> diff --git a/Open-ILS/src/eg2/src/app/staff/booking/create-reservation.component.ts b/Open-ILS/src/eg2/src/app/staff/booking/create-reservation.component.ts index bbaee0029b..ee24c2b334 100644 --- a/Open-ILS/src/eg2/src/app/staff/booking/create-reservation.component.ts +++ b/Open-ILS/src/eg2/src/app/staff/booking/create-reservation.component.ts @@ -19,6 +19,7 @@ import {PcrudService} from '@eg/core/pcrud.service'; import {ResourceTypeComboboxComponent} from './resource-type-combobox.component'; import {ServerStoreService} from '@eg/core/server-store.service'; import {ToastService} from '@eg/share/toast/toast.service'; +import {ReservationValidateService} from './reservation-validate.service' import * as Moment from 'moment-timezone'; @@ -91,6 +92,7 @@ export class CreateReservationComponent implements OnInit, AfterViewInit { private router: Router, private store: ServerStoreService, private toast: ToastService, + public reservationValidate: ReservationValidateService ) { this.resourceAvailabilityIcon = (row: any) => { let icon = {icon: 'event_busy', title: 'All resources are reserved at this time'}; diff --git a/Open-ILS/src/eg2/src/app/staff/booking/reservation-validate.service.ts b/Open-ILS/src/eg2/src/app/staff/booking/reservation-validate.service.ts new file mode 100644 index 0000000000..28534e36fc --- /dev/null +++ b/Open-ILS/src/eg2/src/app/staff/booking/reservation-validate.service.ts @@ -0,0 +1,62 @@ +import {Injectable} from '@angular/core'; +import {IdlObject} from '@eg/core/idl.service'; +import {PcrudService} from '@eg/core/pcrud.service'; +import * as Moment from 'moment-timezone'; + +@Injectable({providedIn: 'root'}) +export class ReservationValidateService { + + constructor( + private pcrud: PcrudService, + ) { + } + errorMessage = ''; + + private duringExistingReservation = (value: Moment, record: IdlObject) => { + if (record.target_resource()) { + this.pcrud.search('bresv', { + 'cancel_time': null, + 'return_time': null, + 'start_time': {'<': value.toISOString()}, + 'end_time': {'>': value.toISOString()}, + '-or': {'current_resource': record.target_resource(), 'target_resource': record.target_resource()}}) + .subscribe((foundOne) => {this.errorMessage = 'There is already a reservation for this resource at this time.'}); + } + } + + start_time = (fieldName: string, value: Moment, record: IdlObject) => { + this.errorMessage = ''; + this.duringExistingReservation(value, record); + if (record.target_resource_type() && record.target_resource()) { + this.pcrud.retrieve('brt', record.target_resource_type()) + .subscribe((brt) => { + if (brt.catalog_item()) { + this.pcrud.retrieve('brsrc', record.target_resource()) + .subscribe((brsrc) => { + this.pcrud.search('circ', { + 'checkin_time': 'null', + 'target_copy': {'barcode': brsrc.barcode()}, + 'due_date': {'>': value.toISOString()}}, + {'flesh': 1, 'flesh_fields': {'circ': ['target_copy']}}) + .subscribe(() => {this.errorMessage = 'Start time conflicts with an existing circulation';}); + }); + } + }); + } + if (Moment(value) < Moment()) { + this.errorMessage = 'Start time must be in the future'; + } + return this.errorMessage; + } + + end_time = (fieldName: string, value: Moment, record: IdlObject) => { + this.errorMessage = ''; + this.duringExistingReservation(value, record); + if (Moment(value) <= Moment(record.start_time())) { + return 'End time must be after start time'; + } + return ''; + } + +} + diff --git a/Open-ILS/src/eg2/src/app/staff/booking/reservations-grid.component.html b/Open-ILS/src/eg2/src/app/staff/booking/reservations-grid.component.html index 06015039e5..6c9a5dd737 100644 --- a/Open-ILS/src/eg2/src/app/staff/booking/reservations-grid.component.html +++ b/Open-ILS/src/eg2/src/app/staff/booking/reservations-grid.component.html @@ -42,7 +42,7 @@ idlClass="bresv" datetimeFields="start_time,end_time" hiddenFields="xact_finish,cancel_time,booking_interval" - [fieldOptions]="{start_time:{validator:startTimeShouldBeFuture},end_time:{validator:endTimeShouldBeAfterStartTime}}" + [fieldOptions]="{start_time:{validator:reservationValidate.start_time},end_time:{validator:reservationValidate.end_time}}" [readonlyFields]="listReadOnlyFields()"> void; listReadOnlyFields: () => string; - startTimeShouldBeFuture: (fieldName: string, value: Moment, record: IdlObject) => string; - endTimeShouldBeAfterStartTime: (fieldName: string, value: Moment, record: IdlObject) => string; - handleRowActivate: (row: IdlObject) => void; redirectToCreate: () => void; @@ -82,7 +80,8 @@ export class ReservationsGridComponent implements OnInit { private toast: ToastService, private net: NetService, private org: OrgService, - private patronService: PatronService + private patronService: PatronService, + public reservationValidate: ReservationValidateService ) { } @@ -248,19 +247,6 @@ export class ReservationsGridComponent implements OnInit { return list; }; - this.startTimeShouldBeFuture = (fieldName: string, value: Moment, record: IdlObject) => { - if (Moment(value) < Moment()) { - return 'Start time must be in the future'; - } - return ''; - }; - this.endTimeShouldBeAfterStartTime = (fieldName: string, value: Moment, record: IdlObject) => { - if (Moment(value) <= Moment(record.start_time())) { - return 'End time must be after start time'; - } - return ''; - }; - this.handleRowActivate = (row: IdlObject) => { if (this.status) { if ('returnReady' === this.status) { -- 2.11.0