Bug 21460: (follow-up) Fix bugs found in QA
[koha.git] / Koha / REST / V1 / Illrequests.pm
blob907dc5ce80ad1c746094a34effa91448a0e37755
1 package Koha::REST::V1::Illrequests;
3 # This file is part of Koha.
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.
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.
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.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Controller';
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 );
29 =head1 NAME
31 Koha::REST::V1::Illrequests
33 =head2 Operations
35 =head3 list
37 Return a list of ILL requests, after applying filters.
39 =cut
41 sub list {
42 my $c = shift->openapi->valid_input or return;
44 my $args = $c->req->params->to_hash // {};
45 my $output = [];
46 my @format_dates = ( 'placed', 'updated', 'completed' );
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};
57 # Get all requests
58 # If necessary, only get those from a specified patron
59 my @requests = Koha::Illrequests->search({
60 $args->{borrowernumber}
61 ? ( borrowernumber => $args->{borrowernumber} )
62 : ()
63 })->as_list;
65 # Identify patrons & branches that
66 # we're going to need and get them
67 my $to_fetch = {
68 patrons => {},
69 branches => {},
70 capabilities => {}
72 foreach my $req(@requests) {
73 $to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron};
74 $to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library};
75 $to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities};
78 # Fetch the patrons we need
79 my $patron_arr = [];
80 if ($embed{patron}) {
81 my @patron_ids = keys %{$to_fetch->{patrons}};
82 if (scalar @patron_ids > 0) {
83 my $where = {
84 borrowernumber => { -in => \@patron_ids }
86 $patron_arr = Koha::Patrons->search($where)->unblessed;
90 # Fetch the branches we need
91 my $branch_arr = [];
92 if ($embed{library}) {
93 my @branchcodes = keys %{$to_fetch->{branches}};
94 if (scalar @branchcodes > 0) {
95 my $where = {
96 branchcode => { -in => \@branchcodes }
98 $branch_arr = Koha::Libraries->search($where)->unblessed;
102 # Fetch the capabilities we need
103 if ($embed{capabilities}) {
104 my @backends = keys %{$to_fetch->{capabilities}};
105 if (scalar @backends > 0) {
106 foreach my $bc(@backends) {
107 my $backend = Koha::Illrequest->new->load_backend($bc);
108 $to_fetch->{$bc} = $backend->capabilities;
113 # Now we've got all associated users and branches,
114 # we can augment the request objects
115 my @output = ();
116 foreach my $req(@requests) {
117 my $to_push = $req->unblessed;
118 $to_push->{id_prefix} = $req->id_prefix;
119 # Create new "formatted" columns for each date column
120 # that needs formatting
121 foreach my $field(@format_dates) {
122 if (defined $to_push->{$field}) {
123 $to_push->{$field . "_formatted"} = format_sqldatetime(
124 $to_push->{$field},
125 undef,
126 undef,
132 foreach my $p(@{$patron_arr}) {
133 if ($p->{borrowernumber} == $req->borrowernumber) {
134 $to_push->{patron} = {
135 patron_id => $p->{borrowernumber},
136 firstname => $p->{firstname},
137 surname => $p->{surname},
138 cardnumber => $p->{cardnumber}
140 last;
143 foreach my $b(@{$branch_arr}) {
144 if ($b->{branchcode} eq $req->branchcode) {
145 $to_push->{library} = $b;
146 last;
149 if ($embed{metadata}) {
150 my $metadata = Koha::Illrequestattributes->search(
151 { illrequest_id => $req->illrequest_id },
152 { columns => [qw/type value/] }
153 )->unblessed;
154 my $meta_hash = {};
155 foreach my $meta(@{$metadata}) {
156 $meta_hash->{$meta->{type}} = $meta->{value};
158 $to_push->{metadata} = $meta_hash;
160 if ($embed{capabilities}) {
161 $to_push->{capabilities} = $to_fetch->{$req->backend};
163 if ($embed{comments}) {
164 $to_push->{comments} = $req->illcomments->count;
166 if ($embed{status_alias}) {
167 $to_push->{status_alias} = $req->statusalias;
169 if ($embed{requested_partners}) {
170 $to_push->{requested_partners} = $req->requested_partners;
172 push @output, $to_push;
175 return $c->render( status => 200, openapi => \@output );