Bug 20600: Add filtering of ILL requests in list
[koha.git] / Koha / REST / V1 / Illrequests.pm
1 package Koha::REST::V1::Illrequests;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Mojo::Base 'Mojolicious::Controller';
19
20 use Koha::Illrequests;
21 use Koha::Illrequestattributes;
22 use Koha::Libraries;
23 use Koha::Patrons;
24 use Koha::Libraries;
25 use Koha::DateUtils qw( format_sqldatetime );
26
27 =head1 NAME
28
29 Koha::REST::V1::Illrequests
30
31 =head2 Operations
32
33 =head3 list
34
35 Return a list of ILL requests, after applying filters.
36
37 =cut
38
39 sub list {
40     my $c = shift->openapi->valid_input or return;
41
42     my $args = $c->req->params->to_hash // {};
43     my $filter;
44     my $output = [];
45     my @format_dates = ( 'placed', 'updated' );
46
47     # Create a hash where all keys are embedded values
48     # Enables easy checking
49     my %embed;
50     my $args_arr = (ref $args->{embed} eq 'ARRAY') ? $args->{embed} : [ $args->{embed} ];
51     if (defined $args->{embed}) {
52         %embed = map { $_ => 1 }  @{$args_arr};
53         delete $args->{embed};
54     }
55
56     # Get all requests
57     my @requests = Koha::Illrequests->as_list;
58
59     # Create new "formatted" columns for each date column
60     # that needs formatting
61     foreach(@requests) {
62         foreach my $field(@format_dates) {
63             if (defined $_->{$field}) {
64                 $_->{$field . "_formatted"} = format_sqldatetime(
65                     $_->{$field},
66                     undef,
67                     undef,
68                     1
69                 );
70             }
71         }
72
73     }
74
75     # Identify patrons & branches that
76     # we're going to need and get them
77     my $to_fetch = {
78         patrons      => {},
79         branches     => {},
80         capabilities => {}
81     };
82     foreach my $req(@requests) {
83         $to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron};
84         $to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library};
85         $to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities};
86     }
87
88     # Fetch the patrons we need
89     my $patron_arr = [];
90     if ($embed{patron}) {
91         my @patron_ids = keys %{$to_fetch->{patrons}};
92         if (scalar @patron_ids > 0) {
93             my $where = {
94                 borrowernumber => { -in => \@patron_ids }
95             };
96             $patron_arr = Koha::Patrons->search($where)->unblessed;
97         }
98     }
99
100     # Fetch the branches we need
101     my $branch_arr = [];
102     if ($embed{library}) {
103         my @branchcodes = keys %{$to_fetch->{branches}};
104         if (scalar @branchcodes > 0) {
105             my $where = {
106                 branchcode => { -in => \@branchcodes }
107             };
108             $branch_arr = Koha::Libraries->search($where)->unblessed;
109         }
110     }
111
112     # Fetch the capabilities we need
113     if ($embed{capabilities}) {
114         my @backends = keys %{$to_fetch->{capabilities}};
115         if (scalar @backends > 0) {
116             foreach my $bc(@backends) {
117                 my $backend = Koha::Illrequest->new->load_backend($bc);
118                 $to_fetch->{$bc} = $backend->capabilities;
119             }
120         }
121     }
122
123     # Now we've got all associated users and branches,
124     # we can augment the request objects
125     my @output = ();
126     foreach my $req(@requests) {
127         my $to_push = $req->unblessed;
128         $to_push->{id_prefix} = $req->id_prefix;
129         foreach my $p(@{$patron_arr}) {
130             if ($p->{borrowernumber} == $req->borrowernumber) {
131                 $to_push->{patron} = {
132                     firstname  => $p->{firstname},
133                     surname    => $p->{surname},
134                     cardnumber => $p->{cardnumber}
135                 };
136                 last;
137             }
138         }
139         foreach my $b(@{$branch_arr}) {
140             if ($b->{branchcode} eq $req->branchcode) {
141                 $to_push->{library} = $b;
142                 last;
143             }
144         }
145         if ($embed{metadata}) {
146             my $metadata = Koha::Illrequestattributes->search(
147                 { illrequest_id => $req->illrequest_id },
148                 { columns => [qw/type value/] }
149             )->unblessed;
150             my $meta_hash = {};
151             foreach my $meta(@{$metadata}) {
152                 $meta_hash->{$meta->{type}} = $meta->{value};
153             }
154             $to_push->{metadata} = $meta_hash;
155         }
156         if ($embed{capabilities}) {
157             $to_push->{capabilities} = $to_fetch->{$req->backend};
158         }
159         if ($embed{comments}) {
160             $to_push->{comments} = $req->illcomments->count;
161         }
162         push @output, $to_push;
163     }
164
165     return $c->render( status => 200, openapi => \@output );
166 }
167
168 1;