LP1816475: Refactoring create grid
authorJane Sandberg <sandbej@linnbenton.edu>
Mon, 8 Apr 2019 19:40:35 +0000 (12:40 -0700)
committerJane Sandberg <sandbej@linnbenton.edu>
Wed, 17 Apr 2019 20:41:53 +0000 (13:41 -0700)
Signed-off-by: Jane Sandberg <sandbej@linnbenton.edu>
Open-ILS/src/eg2/src/app/staff/booking/create-reservation.component.ts

index ee24c2b..7623665 100644 (file)
@@ -336,7 +336,6 @@ export class CreateReservationComponent implements OnInit, AfterViewInit {
 
     fetchData () {
         this.resources = [];
-        let select = {};
         let where = {};
 
         if (this.resourceId) {
@@ -347,34 +346,22 @@ export class CreateReservationComponent implements OnInit, AfterViewInit {
             return;
         }
 
-        if (this.multiday) {
-            this.dateRangeLimiters.forEach((drl) => { select = {
-                'curr_rsrcs': {'end_time': {'>' : Moment.tz([drl.fromDate.year, drl.fromDate.month - 1, drl.fromDate.day],
-                    this.format.wsOrgTimezone).startOf('day').toISOString()},
-                'start_time': { '<': Moment.tz([drl.toDate.year, drl.toDate.month - 1, drl.toDate.day],
-                    this.format.wsOrgTimezone).endOf('day').toISOString() }}};
-            });
-        } else { select = { 'curr_rsrcs': {
-            'end_time': {'>' : Moment.tz([this.idealDate.getFullYear(), this.idealDate.getMonth(), this.idealDate.getDate()],
-                this.format.wsOrgTimezone).startOf('day').toISOString()},
-            'start_time': { '<': Moment.tz([this.idealDate.getFullYear(), this.idealDate.getMonth(), this.idealDate.getDate()],
-                this.format.wsOrgTimezone).add(1, 'days').endOf('day').toISOString() }}};
-        }
         if (this.selectedAttributes.length) {
             where['id'] = {'in': {'from': 'bram', 'select': {'bram': ['resource']}, 'where': {'value':  this.selectedAttributes.filter((a) => (a !== null))}}};
         }
         this.scheduleSource.data = [];
         this.pcrud.search('brsrc', where, {
             order_by: 'barcode ASC',
-            flesh: 2,
-            flesh_fields: {'brsrc': ['curr_rsrcs', 'attr_maps'], 'bresv': ['usr']},
-            select: select
+            flesh: 1,
+            flesh_fields: {'brsrc': ['attr_maps']},
         }).subscribe(
             r => {
                 this.resources.push(r);
 
                 let startTime = Moment();
                 let endTime = Moment();
+                let reservations = [];
+
                 if (this.multiday) {
                    this.dateRangeLimiters.forEach((drl) => {
                        startTime = Moment.tz([drl.fromDate.year, drl.fromDate.month - 1, drl.fromDate.day],
@@ -400,34 +387,44 @@ export class CreateReservationComponent implements OnInit, AfterViewInit {
                             this.format.wsOrgTimezone);
                     });
                 }
-                const currentTime = startTime;
-                while (currentTime < endTime) {
-                    let idx: number;
-                    let existingRow: number;
-                    if (this.multiday) {
-                        existingRow = this.scheduleSource.data.findIndex(
-                            (row) => row['time'] === this.format.transform({value: currentTime, datatype: 'timestamp'}));
-                        idx = (existingRow > -1) ? existingRow :
-                            (this.scheduleSource.data.push(
-                                {'time': this.format.transform({value: currentTime, datatype: 'timestamp'})}) - 1);
-                    } else {
-                        existingRow = this.scheduleSource.data.findIndex((row) => row['time'] === currentTime.format('LT')) ;
-                        idx = (existingRow > -1) ? existingRow : (this.scheduleSource.data.push({'time': currentTime.format('LT')}) - 1);
-                    }
-                    r.curr_rsrcs().forEach((reservation) => {
-                        if ((Moment(reservation.start_time(), this.format.wsOrgTimezone) <
-                            (currentTime.clone().add(this.granularity, 'minutes'))) &&
-                            (Moment(reservation.end_time(), this.format.wsOrgTimezone) > currentTime)) {
-                            if (!this.scheduleSource.data[idx][r.barcode()]) { this.scheduleSource.data[idx][r.barcode()] = []; }
-                            this.scheduleSource.data[idx][r.barcode()].push(
-                                {'patronLabel': reservation.usr().usrname(), 'patronId': reservation.usr().id()});
+                this.pcrud.search('bresv', {
+                    '-or': {'target_resource': r.id(), 'current_resource': r.id()},
+                    'end_time': {'>': startTime.toISOString()},
+                    'start_time': {'<': endTime.toISOString()},
+                    'return_time': null,
+                    'cancel_time': null },
+                    {'flesh': 1, 'flesh_fields': {'bresv': ['usr']}})
+                .subscribe((res) => {reservations.push(res);},
+                    (err) => {console.warn(err);},
+                    () =>  {
+                    const currentTime = startTime;
+                    while (currentTime < endTime) {
+                        let idx: number;
+                        let existingRow: number;
+                        if (this.multiday) {
+                            existingRow = this.scheduleSource.data.findIndex(
+                                (row) => row['time'] === this.format.transform({value: currentTime, datatype: 'timestamp'}));
+                            idx = (existingRow > -1) ? existingRow :
+                                (this.scheduleSource.data.push(
+                                    {'time': this.format.transform({value: currentTime, datatype: 'timestamp'})}) - 1);
+                        } else {
+                            existingRow = this.scheduleSource.data.findIndex((row) => row['time'] === currentTime.format('LT')) ;
+                            idx = (existingRow > -1) ? existingRow : (this.scheduleSource.data.push({'time': currentTime.format('LT')}) - 1);
                         }
-                    });
-                    currentTime.add(this.granularity, 'minutes');
-                }
+                        reservations.forEach((reservation) => {
+                            if ((Moment.tz(reservation.start_time(), this.format.wsOrgTimezone) <
+                                (currentTime.clone().add(this.granularity, 'minutes'))) &&
+                                (Moment.tz(reservation.end_time(), this.format.wsOrgTimezone) > currentTime)) {
+                                if (!this.scheduleSource.data[idx][r.barcode()]) { this.scheduleSource.data[idx][r.barcode()] = []; }
+                                this.scheduleSource.data[idx][r.barcode()].push(
+                                    {'patronLabel': reservation.usr().usrname(), 'patronId': reservation.usr().id()});
+                            }
+                        });
+                        currentTime.add(this.granularity, 'minutes');
+                    }
+                });
             });
     }
 
 }
 
-