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