Bug 20600: (follow-up) Fix critic error
[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 $output = [];
44     my @format_dates = ( 'placed', 'updated' );
45
46     # Create a hash where all keys are embedded values
47     # Enables easy checking
48     my %embed;
49     my $args_arr = (ref $args->{embed} eq 'ARRAY') ? $args->{embed} : [ $args->{embed} ];
50     if (defined $args->{embed}) {
51         %embed = map { $_ => 1 }  @{$args_arr};
52         delete $args->{embed};
53     }
54
55     # Get all requests
56     my @requests = Koha::Illrequests->as_list;
57
58     # Identify patrons & branches that
59     # we're going to need and get them
60     my $to_fetch = {
61         patrons      => {},
62         branches     => {},
63         capabilities => {}
64     };
65     foreach my $req(@requests) {
66         $to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron};
67         $to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library};
68         $to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities};
69     }
70
71     # Fetch the patrons we need
72     my $patron_arr = [];
73     if ($embed{patron}) {
74         my @patron_ids = keys %{$to_fetch->{patrons}};
75         if (scalar @patron_ids > 0) {
76             my $where = {
77                 borrowernumber => { -in => \@patron_ids }
78             };
79             $patron_arr = Koha::Patrons->search($where)->unblessed;
80         }
81     }
82
83     # Fetch the branches we need
84     my $branch_arr = [];
85     if ($embed{library}) {
86         my @branchcodes = keys %{$to_fetch->{branches}};
87         if (scalar @branchcodes > 0) {
88             my $where = {
89                 branchcode => { -in => \@branchcodes }
90             };
91             $branch_arr = Koha::Libraries->search($where)->unblessed;
92         }
93     }
94
95     # Fetch the capabilities we need
96     if ($embed{capabilities}) {
97         my @backends = keys %{$to_fetch->{capabilities}};
98         if (scalar @backends > 0) {
99             foreach my $bc(@backends) {
100                 my $backend = Koha::Illrequest->new->load_backend($bc);
101                 $to_fetch->{$bc} = $backend->capabilities;
102             }
103         }
104     }
105
106     # Now we've got all associated users and branches,
107     # we can augment the request objects
108     my @output = ();
109     foreach my $req(@requests) {
110         my $to_push = $req->unblessed;
111         $to_push->{id_prefix} = $req->id_prefix;
112         # Create new "formatted" columns for each date column
113         # that needs formatting
114         foreach my $field(@format_dates) {
115             if (defined $to_push->{$field}) {
116                 $to_push->{$field . "_formatted"} = format_sqldatetime(
117                     $to_push->{$field},
118                     undef,
119                     undef,
120                     1
121                 );
122             }
123         }
124
125         foreach my $p(@{$patron_arr}) {
126             if ($p->{borrowernumber} == $req->borrowernumber) {
127                 $to_push->{patron} = {
128                     firstname  => $p->{firstname},
129                     surname    => $p->{surname},
130                     cardnumber => $p->{cardnumber}
131                 };
132                 last;
133             }
134         }
135         foreach my $b(@{$branch_arr}) {
136             if ($b->{branchcode} eq $req->branchcode) {
137                 $to_push->{library} = $b;
138                 last;
139             }
140         }
141         if ($embed{metadata}) {
142             my $metadata = Koha::Illrequestattributes->search(
143                 { illrequest_id => $req->illrequest_id },
144                 { columns => [qw/type value/] }
145             )->unblessed;
146             my $meta_hash = {};
147             foreach my $meta(@{$metadata}) {
148                 $meta_hash->{$meta->{type}} = $meta->{value};
149             }
150             $to_push->{metadata} = $meta_hash;
151         }
152         if ($embed{capabilities}) {
153             $to_push->{capabilities} = $to_fetch->{$req->backend};
154         }
155         if ($embed{comments}) {
156             $to_push->{comments} = $req->illcomments->count;
157         }
158         push @output, $to_push;
159     }
160
161     return $c->render( status => 200, openapi => \@output );
162 }
163
164 1;