Bug 22026: Removed 'use Modern::Perl;' from Koha::REST::classes
[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
26 =head1 NAME
27
28 Koha::REST::V1::Illrequests
29
30 =head2 Operations
31
32 =head3 list
33
34 Return a list of ILL requests, after applying filters.
35
36 =cut
37
38 sub list {
39     my $c = shift->openapi->valid_input or return;
40
41     my $args = $c->req->params->to_hash // {};
42
43     # Create a hash where all keys are embedded values
44     # Enables easy checking
45     my %embed;
46     my $args_arr = (ref $args->{embed} eq 'ARRAY') ? $args->{embed} : [ $args->{embed} ];
47     if (defined $args->{embed}) {
48         %embed = map { $_ => 1 }  @{$args_arr};
49         delete $args->{embed};
50     }
51
52     # Get all requests
53     my @requests = Koha::Illrequests->as_list;
54
55     # Identify patrons & branches that
56     # we're going to need and get them
57     my $to_fetch = {
58         patrons      => {},
59         branches     => {},
60         capabilities => {}
61     };
62     foreach my $req(@requests) {
63         $to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron};
64         $to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library};
65         $to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities};
66     }
67
68     # Fetch the patrons we need
69     my $patron_arr = [];
70     if ($embed{patron}) {
71         my @patron_ids = keys %{$to_fetch->{patrons}};
72         if (scalar @patron_ids > 0) {
73             my $where = {
74                 borrowernumber => { -in => \@patron_ids }
75             };
76             $patron_arr = Koha::Patrons->search($where)->unblessed;
77         }
78     }
79
80     # Fetch the branches we need
81     my $branch_arr = [];
82     if ($embed{library}) {
83         my @branchcodes = keys %{$to_fetch->{branches}};
84         if (scalar @branchcodes > 0) {
85             my $where = {
86                 branchcode => { -in => \@branchcodes }
87             };
88             $branch_arr = Koha::Libraries->search($where)->unblessed;
89         }
90     }
91
92     # Fetch the capabilities we need
93     if ($embed{capabilities}) {
94         my @backends = keys %{$to_fetch->{capabilities}};
95         if (scalar @backends > 0) {
96             foreach my $bc(@backends) {
97                 my $backend = Koha::Illrequest->new->load_backend($bc);
98                 $to_fetch->{$bc} = $backend->capabilities;
99             }
100         }
101     }
102
103     # Now we've got all associated users and branches,
104     # we can augment the request objects
105     my @output = ();
106     foreach my $req(@requests) {
107         my $to_push = $req->unblessed;
108         $to_push->{id_prefix} = $req->id_prefix;
109         foreach my $p(@{$patron_arr}) {
110             if ($p->{borrowernumber} == $req->borrowernumber) {
111                 $to_push->{patron} = {
112                     firstname  => $p->{firstname},
113                     surname    => $p->{surname},
114                     cardnumber => $p->{cardnumber}
115                 };
116                 last;
117             }
118         }
119         foreach my $b(@{$branch_arr}) {
120             if ($b->{branchcode} eq $req->branchcode) {
121                 $to_push->{library} = $b;
122                 last;
123             }
124         }
125         if ($embed{metadata}) {
126             my $metadata = Koha::Illrequestattributes->search(
127                 { illrequest_id => $req->illrequest_id },
128                 { columns => [qw/type value/] }
129             )->unblessed;
130             my $meta_hash = {};
131             foreach my $meta(@{$metadata}) {
132                 $meta_hash->{$meta->{type}} = $meta->{value};
133             }
134             $to_push->{metadata} = $meta_hash;
135         }
136         if ($embed{capabilities}) {
137             $to_push->{capabilities} = $to_fetch->{$req->backend};
138         }
139         if ($embed{comments}) {
140             $to_push->{comments} = $req->illcomments->count;
141         }
142         push @output, $to_push;
143     }
144
145     return $c->render( status => 200, openapi => \@output );
146 }
147
148 1;