Bug 21063: Add "Columns settings" for ILL
[koha.git] / Koha / REST / V1 / Illrequests.pm
index 0807eb1..f8385a7 100644 (file)
@@ -20,66 +20,151 @@ use Modern::Perl;
 use Mojo::Base 'Mojolicious::Controller';
 
 use Koha::Illrequests;
-use Koha::Library;
+use Koha::Illrequestattributes;
+use Koha::Libraries;
+use Koha::Patrons;
+use Koha::Libraries;
+use Koha::DateUtils qw( format_sqldatetime );
+
+=head1 NAME
+
+Koha::REST::V1::Illrequests
+
+=head2 Operations
+
+=head3 list
+
+Return a list of ILL requests, after applying filters.
+
+=cut
 
 sub list {
-    my ($c, $args, $cb) = @_;
+    my $c = shift->openapi->valid_input or return;
 
-    my $filter;
-    $args //= {};
+    my $args = $c->req->params->to_hash // {};
     my $output = [];
+    my @format_dates = ( 'placed', 'updated', 'completed' );
 
     # Create a hash where all keys are embedded values
     # Enables easy checking
     my %embed;
+    my $args_arr = (ref $args->{embed} eq 'ARRAY') ? $args->{embed} : [ $args->{embed} ];
     if (defined $args->{embed}) {
-        %embed = map { $_ => 1 }  @{$args->{embed}};
+        %embed = map { $_ => 1 }  @{$args_arr};
         delete $args->{embed};
     }
 
-    for my $filter_param ( keys %$args ) {
-        my @values = split(/,/, $args->{$filter_param});
-        $filter->{$filter_param} = \@values;
+    # Get all requests
+    my @requests = Koha::Illrequests->as_list;
+
+    # Identify patrons & branches that
+    # we're going to need and get them
+    my $to_fetch = {
+        patrons      => {},
+        branches     => {},
+        capabilities => {}
+    };
+    foreach my $req(@requests) {
+        $to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron};
+        $to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library};
+        $to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities};
+    }
+
+    # Fetch the patrons we need
+    my $patron_arr = [];
+    if ($embed{patron}) {
+        my @patron_ids = keys %{$to_fetch->{patrons}};
+        if (scalar @patron_ids > 0) {
+            my $where = {
+                borrowernumber => { -in => \@patron_ids }
+            };
+            $patron_arr = Koha::Patrons->search($where)->unblessed;
+        }
     }
 
-    my $requests = Koha::Illrequests->search($filter);
-
-    while (my $request = $requests->next) {
-        my $unblessed = $request->unblessed;
-        # Add the request's id_prefix
-        $unblessed->{id_prefix} = $request->id_prefix;
-        # Augment the request response with patron details
-        # if appropriate
-        if (defined $embed{patron}) {
-            my $patron = $request->patron;
-            $unblessed->{patron} = {
-                firstname  => $patron->firstname,
-                surname    => $patron->surname,
-                cardnumber => $patron->cardnumber
+    # Fetch the branches we need
+    my $branch_arr = [];
+    if ($embed{library}) {
+        my @branchcodes = keys %{$to_fetch->{branches}};
+        if (scalar @branchcodes > 0) {
+            my $where = {
+                branchcode => { -in => \@branchcodes }
             };
+            $branch_arr = Koha::Libraries->search($where)->unblessed;
         }
-        # Augment the request response with metadata details
-        # if appropriate
-        if (defined $embed{metadata}) {
-            $unblessed->{metadata} = $request->metadata;
+    }
+
+    # Fetch the capabilities we need
+    if ($embed{capabilities}) {
+        my @backends = keys %{$to_fetch->{capabilities}};
+        if (scalar @backends > 0) {
+            foreach my $bc(@backends) {
+                my $backend = Koha::Illrequest->new->load_backend($bc);
+                $to_fetch->{$bc} = $backend->capabilities;
+            }
+        }
+    }
+
+    # Now we've got all associated users and branches,
+    # we can augment the request objects
+    my @output = ();
+    foreach my $req(@requests) {
+        my $to_push = $req->unblessed;
+        $to_push->{id_prefix} = $req->id_prefix;
+        # Create new "formatted" columns for each date column
+        # that needs formatting
+        foreach my $field(@format_dates) {
+            if (defined $to_push->{$field}) {
+                $to_push->{$field . "_formatted"} = format_sqldatetime(
+                    $to_push->{$field},
+                    undef,
+                    undef,
+                    1
+                );
+            }
         }
-        # Augment the request response with status details
-        # if appropriate
-        if (defined $embed{capabilities}) {
-            $unblessed->{capabilities} = $request->capabilities;
+
+        foreach my $p(@{$patron_arr}) {
+            if ($p->{borrowernumber} == $req->borrowernumber) {
+                $to_push->{patron} = {
+                    patron_id => $p->{borrowernumber},
+                    firstname      => $p->{firstname},
+                    surname        => $p->{surname},
+                    cardnumber     => $p->{cardnumber}
+                };
+                last;
+            }
         }
-        # Augment the request response with branch details
-        # if appropriate
-        if (defined $embed{branch}) {
-            $unblessed->{branch} = Koha::Libraries->find(
-                $request->branchcode
+        foreach my $b(@{$branch_arr}) {
+            if ($b->{branchcode} eq $req->branchcode) {
+                $to_push->{library} = $b;
+                last;
+            }
+        }
+        if ($embed{metadata}) {
+            my $metadata = Koha::Illrequestattributes->search(
+                { illrequest_id => $req->illrequest_id },
+                { columns => [qw/type value/] }
             )->unblessed;
+            my $meta_hash = {};
+            foreach my $meta(@{$metadata}) {
+                $meta_hash->{$meta->{type}} = $meta->{value};
+            }
+            $to_push->{metadata} = $meta_hash;
+        }
+        if ($embed{capabilities}) {
+            $to_push->{capabilities} = $to_fetch->{$req->backend};
+        }
+        if ($embed{comments}) {
+            $to_push->{comments} = $req->illcomments->count;
         }
-        push @{$output}, $unblessed
+        if ($embed{status_alias}) {
+            $to_push->{status_alias} = $req->statusalias;
+        }
+        push @output, $to_push;
     }
 
-    return $c->$cb( $output, 200 );
-
+    return $c->render( status => 200, openapi => \@output );
 }
 
 1;