Bug 22521: DBRev 18.12.00.055
[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 Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Illrequests;
23 use Koha::Illrequestattributes;
24 use Koha::Libraries;
25 use Koha::Patrons;
26 use Koha::Libraries;
27 use Koha::DateUtils qw( format_sqldatetime );
28
29 =head1 NAME
30
31 Koha::REST::V1::Illrequests
32
33 =head2 Operations
34
35 =head3 list
36
37 Return a list of ILL requests, after applying filters.
38
39 =cut
40
41 sub list {
42     my $c = shift->openapi->valid_input or return;
43
44     my $args = $c->req->params->to_hash // {};
45     my $output = [];
46     my @format_dates = ( 'placed', 'updated', 'completed' );
47
48     # Create a hash where all keys are embedded values
49     # Enables easy checking
50     my %embed;
51     my $args_arr = (ref $args->{embed} eq 'ARRAY') ? $args->{embed} : [ $args->{embed} ];
52     if (defined $args->{embed}) {
53         %embed = map { $_ => 1 }  @{$args_arr};
54         delete $args->{embed};
55     }
56
57     # Get all requests
58     # If necessary, only get those from a specified patron
59     my @requests;
60     if ($args->{borrowernumber}) {
61         @requests = Koha::Illrequests->search(
62             { borrowernumber => $args->{borrowernumber} }
63         );
64     } else {
65         @requests = Koha::Illrequests->as_list;
66     }
67
68     # Identify patrons & branches that
69     # we're going to need and get them
70     my $to_fetch = {
71         patrons      => {},
72         branches     => {},
73         capabilities => {}
74     };
75     foreach my $req(@requests) {
76         $to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron};
77         $to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library};
78         $to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities};
79     }
80
81     # Fetch the patrons we need
82     my $patron_arr = [];
83     if ($embed{patron}) {
84         my @patron_ids = keys %{$to_fetch->{patrons}};
85         if (scalar @patron_ids > 0) {
86             my $where = {
87                 borrowernumber => { -in => \@patron_ids }
88             };
89             $patron_arr = Koha::Patrons->search($where)->unblessed;
90         }
91     }
92
93     # Fetch the branches we need
94     my $branch_arr = [];
95     if ($embed{library}) {
96         my @branchcodes = keys %{$to_fetch->{branches}};
97         if (scalar @branchcodes > 0) {
98             my $where = {
99                 branchcode => { -in => \@branchcodes }
100             };
101             $branch_arr = Koha::Libraries->search($where)->unblessed;
102         }
103     }
104
105     # Fetch the capabilities we need
106     if ($embed{capabilities}) {
107         my @backends = keys %{$to_fetch->{capabilities}};
108         if (scalar @backends > 0) {
109             foreach my $bc(@backends) {
110                 my $backend = Koha::Illrequest->new->load_backend($bc);
111                 $to_fetch->{$bc} = $backend->capabilities;
112             }
113         }
114     }
115
116     # Now we've got all associated users and branches,
117     # we can augment the request objects
118     my @output = ();
119     foreach my $req(@requests) {
120         my $to_push = $req->unblessed;
121         $to_push->{id_prefix} = $req->id_prefix;
122         # Create new "formatted" columns for each date column
123         # that needs formatting
124         foreach my $field(@format_dates) {
125             if (defined $to_push->{$field}) {
126                 $to_push->{$field . "_formatted"} = format_sqldatetime(
127                     $to_push->{$field},
128                     undef,
129                     undef,
130                     1
131                 );
132             }
133         }
134
135         foreach my $p(@{$patron_arr}) {
136             if ($p->{borrowernumber} == $req->borrowernumber) {
137                 $to_push->{patron} = {
138                     patron_id => $p->{borrowernumber},
139                     firstname      => $p->{firstname},
140                     surname        => $p->{surname},
141                     cardnumber     => $p->{cardnumber}
142                 };
143                 last;
144             }
145         }
146         foreach my $b(@{$branch_arr}) {
147             if ($b->{branchcode} eq $req->branchcode) {
148                 $to_push->{library} = $b;
149                 last;
150             }
151         }
152         if ($embed{metadata}) {
153             my $metadata = Koha::Illrequestattributes->search(
154                 { illrequest_id => $req->illrequest_id },
155                 { columns => [qw/type value/] }
156             )->unblessed;
157             my $meta_hash = {};
158             foreach my $meta(@{$metadata}) {
159                 $meta_hash->{$meta->{type}} = $meta->{value};
160             }
161             $to_push->{metadata} = $meta_hash;
162         }
163         if ($embed{capabilities}) {
164             $to_push->{capabilities} = $to_fetch->{$req->backend};
165         }
166         if ($embed{comments}) {
167             $to_push->{comments} = $req->illcomments->count;
168         }
169         if ($embed{status_alias}) {
170             $to_push->{status_alias} = $req->statusalias;
171         }
172         if ($embed{requested_partners}) {
173             $to_push->{requested_partners} = $req->requested_partners;
174         }
175         push @output, $to_push;
176     }
177
178     return $c->render( status => 200, openapi => \@output );
179 }
180
181 1;