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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Mojo::Base 'Mojolicious::Controller';
23 use Koha::Illrequests;
24 use Koha::Illrequestattributes;
28 use Koha::DateUtils qw( format_sqldatetime );
32 Koha::REST::V1::Illrequests
38 Return a list of ILL requests, after applying filters.
43 my $c = shift->openapi->valid_input or return;
45 my $args = $c->req->params->to_hash // {};
47 my @format_dates = ( 'placed', 'updated', 'completed' );
49 # Create a hash where all keys are embedded values
50 # Enables easy checking
52 my $args_arr = (ref $args->{embed} eq 'ARRAY') ? $args->{embed} : [ $args->{embed} ];
53 if (defined $args->{embed}) {
54 %embed = map { $_ => 1 } @{$args_arr};
55 delete $args->{embed};
58 # Get the pipe-separated string of hidden ILL statuses
59 my $hidden_statuses_string = C4::Context->preference('ILLHiddenRequestStatuses') // q{};
61 my $hidden_statuses = [ split /\|/, $hidden_statuses_string ];
64 # If necessary, only get those from a specified patron
65 my @requests = Koha::Illrequests->search({
69 status => { 'not in' => $hidden_statuses },
70 status_alias => [ -or =>
71 { 'not in' => $hidden_statuses },
77 $args->{borrowernumber}
78 ? ( borrowernumber => $args->{borrowernumber} )
82 my $fetch_backends = {};
83 foreach my $request (@requests) {
84 $fetch_backends->{ $request->backend } ||=
85 Koha::Illrequest->new->load_backend( $request->backend );
88 # Pre-load the backend object to avoid useless backend lookup/loads
89 @requests = map { $_->_backend( $fetch_backends->{ $_->backend } ); $_ } @requests;
91 # Identify patrons & branches that
92 # we're going to need and get them
98 foreach my $req (@requests) {
99 $to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron};
100 $to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library};
101 $to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities};
104 # Fetch the patrons we need
106 if ($embed{patron}) {
107 my @patron_ids = keys %{$to_fetch->{patrons}};
108 if (scalar @patron_ids > 0) {
110 borrowernumber => { -in => \@patron_ids }
112 $patron_arr = Koha::Patrons->search($where)->unblessed;
116 # Fetch the branches we need
118 if ($embed{library}) {
119 my @branchcodes = keys %{$to_fetch->{branches}};
120 if (scalar @branchcodes > 0) {
122 branchcode => { -in => \@branchcodes }
124 $branch_arr = Koha::Libraries->search($where)->unblessed;
128 # Fetch the capabilities we need
129 if ($embed{capabilities}) {
130 my @backends = keys %{$to_fetch->{capabilities}};
131 if (scalar @backends > 0) {
132 foreach my $bc(@backends) {
133 $to_fetch->{$bc} = $fetch_backends->{$bc}->capabilities;
138 # Now we've got all associated users and branches,
139 # we can augment the request objects
141 foreach my $req(@requests) {
142 my $to_push = $req->unblessed;
143 $to_push->{id_prefix} = $req->id_prefix;
144 # Create new "formatted" columns for each date column
145 # that needs formatting
146 foreach my $field(@format_dates) {
147 if (defined $to_push->{$field}) {
148 $to_push->{$field . "_formatted"} = format_sqldatetime(
157 foreach my $p(@{$patron_arr}) {
158 if ($p->{borrowernumber} == $req->borrowernumber) {
159 $to_push->{patron} = {
160 patron_id => $p->{borrowernumber},
161 firstname => $p->{firstname},
162 surname => $p->{surname},
163 cardnumber => $p->{cardnumber}
168 foreach my $b(@{$branch_arr}) {
169 if ($b->{branchcode} eq $req->branchcode) {
170 $to_push->{library} = $b;
174 if ($embed{metadata}) {
175 my $metadata = Koha::Illrequestattributes->search(
176 { illrequest_id => $req->illrequest_id },
177 { columns => [qw/type value/] }
180 foreach my $meta(@{$metadata}) {
181 $meta_hash->{$meta->{type}} = $meta->{value};
183 $to_push->{metadata} = $meta_hash;
185 if ($embed{capabilities}) {
186 $to_push->{capabilities} = $to_fetch->{$req->backend};
188 if ($embed{comments}) {
189 $to_push->{comments} = $req->illcomments->count;
191 if ($embed{status_alias}) {
192 $to_push->{status_alias} = $req->statusalias;
194 if ($embed{requested_partners}) {
195 $to_push->{requested_partners} = $req->requested_partners;
197 push @output, $to_push;
200 return $c->render( status => 200, openapi => \@output );