LP#1879983: make fetch_{staged,arrived,delivered} return holds
authorGalen Charlton <gmc@equinoxinitiative.org>
Mon, 15 Jun 2020 15:41:11 +0000 (11:41 -0400)
committerGalen Charlton <gmc@equinoxinitiative.org>
Mon, 15 Jun 2020 15:41:11 +0000 (11:41 -0400)
This allows the corresponding tabs in the staff user interface
to display the list of items, allowing for better error-checking
by staff.

In the case of fetch_delivered, a heuristic is used: holds that
have been marked fulfilled at the appointment's pickup library
within 90 seconds of the appointment's delivered timestamp
are included.

Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/src/perlmods/lib/OpenILS/Application/Curbside.pm

index 6a9df9b..d3b76fd 100644 (file)
@@ -143,6 +143,44 @@ __PACKAGE__->register_method(
     }
 );
 
+sub _flesh_and_emit_slots {
+    my ($conn, $e, $slots) = @_;
+
+    for my $s (@$slots) {
+        my $start_time;
+        my $end_time;
+        if ($s->delivered) {
+            $start_time = DateTime::Format::ISO8601->new->parse_datetime(clean_ISO8601($s->delivered));
+            $end_time = $start_time->clone->add(seconds => 90); # 90 seconds is arbitrary
+        }
+        my $holds = $e->search_action_hold_request([{
+            usr => $s->patron->id,
+            current_shelf_lib => $s->org,
+            pickup_lib => $s->org,
+            shelf_time => {'!=' => undef},
+            cancel_time => undef,
+            ($s->delivered) ?
+                (
+                    '-and' => [ { fulfillment_time => {'>=' => $start_time->strftime('%FT%T%z') } },
+                                { fulfillment_time => {'<=' => $end_time->strftime('%FT%T%z') } } ],
+                ) :
+                (fulfillment_time => undef),
+        },{
+            flesh => 1, flesh_fields => {ahr => ['current_copy']},
+        }]);
+
+        my $rhrr_list = $e->search_reporter_hold_request_record(
+            {id => [ map { $_->id } @$holds ]}
+        );
+
+        my %bib_data = map {
+            ($_->id => $e->retrieve_metabib_wide_display_entry( $_->bib_record))
+        } @$rhrr_list;
+
+        $conn->respond({slot_id => $s->id, slot => $s, holds => $holds, bib_data_by_hold => \%bib_data});
+    }
+}
+
 sub fetch_delivered { # returns appointments delivered TODAY
     my ($self, $conn, $authtoken, $org, $limit, $offset) = @_;
 
@@ -166,7 +204,8 @@ sub fetch_delivered { # returns appointments delivered TODAY
         order_by => { acsp => {delivered => {direction => 'desc'}} }
     }]);
 
-    $conn->respond($_) for @$slots;
+    _flesh_and_emit_slots($conn, $e, $slots);
+
     return undef;
 }
 __PACKAGE__->register_method(
@@ -241,18 +280,8 @@ sub fetch_arrived {
         order_by => { acsp => 'arrival' }
     }]);
 
-    for my $s (@$slots) {
-        my $holds = $e->search_action_hold_request({
-            usr => $s->patron->id,
-            current_shelf_lib => $s->org,
-            pickup_lib => $s->org,
-            shelf_time => {'!=' => undef},
-            cancel_time => undef,
-            fulfillment_time => undef
-        });
 
-        $conn->respond({slot_id => $s->id, slot => $s, holds => $holds});
-    }
+    _flesh_and_emit_slots($conn, $e, $slots);
 
     return undef;
 }
@@ -328,18 +357,7 @@ sub fetch_staged {
         order_by => { acsp => 'slot' }
     }]);
 
-    for my $s (@$slots) {
-        my $holds = $e->search_action_hold_request({
-            usr => $s->patron->id,
-            current_shelf_lib => $s->org,
-            pickup_lib => $s->org,
-            shelf_time => {'!=' => undef},
-            cancel_time => undef,
-            fulfillment_time => undef
-        });
-
-        $conn->respond({slot_id => $s->id, slot => $s, holds => $holds});
-    }
+    _flesh_and_emit_slots($conn, $e, $slots);
 
     return undef;
 }
@@ -423,28 +441,7 @@ sub fetch_to_be_staged {
         order_by => { acsp => 'slot' }
     }]);
 
-    for my $s (@$slots) {
-        my $holds = $e->search_action_hold_request([{
-            usr => $s->patron->id,
-            current_shelf_lib => $s->org,
-            pickup_lib => $s->org,
-            shelf_time => {'!=' => undef},
-            cancel_time => undef,
-            fulfillment_time => undef
-        },{
-            flesh => 1, flesh_fields => {ahr => ['current_copy']},
-        }]);
-
-        my $rhrr_list = $e->search_reporter_hold_request_record(
-            {id => [ map { $_->id } @$holds ]}
-        );
-
-        my %bib_data = map {
-            ($_->id => $e->retrieve_metabib_wide_display_entry( $_->bib_record))
-        } @$rhrr_list;
-
-        $conn->respond({slot_id => $s->id, slot => $s, holds => $holds, bib_data_by_hold => \%bib_data});
-    }
+    _flesh_and_emit_slots($conn, $e, $slots);
 
     return undef;
 }