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