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
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.
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::Illrequests;
23 use Koha::Illrequestattributes;
27 use Koha::DateUtils qw( format_sqldatetime );
31 Koha::REST::V1::Illrequests
37 Return a list of ILL requests, after applying filters.
42 my $c = shift->openapi->valid_input or return;
44 my $args = $c->req->params->to_hash // {};
46 my @format_dates = ( 'placed', 'updated', 'completed' );
48 # Create a hash where all keys are embedded values
49 # Enables easy checking
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};
58 # If necessary, only get those from a specified patron
60 if ($args->{borrowernumber}) {
61 @requests = Koha::Illrequests->search(
62 { borrowernumber => $args->{borrowernumber} }
65 @requests = Koha::Illrequests->as_list;
68 # Identify patrons & branches that
69 # we're going to need and get them
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};
81 # Fetch the patrons we need
84 my @patron_ids = keys %{$to_fetch->{patrons}};
85 if (scalar @patron_ids > 0) {
87 borrowernumber => { -in => \@patron_ids }
89 $patron_arr = Koha::Patrons->search($where)->unblessed;
93 # Fetch the branches we need
95 if ($embed{library}) {
96 my @branchcodes = keys %{$to_fetch->{branches}};
97 if (scalar @branchcodes > 0) {
99 branchcode => { -in => \@branchcodes }
101 $branch_arr = Koha::Libraries->search($where)->unblessed;
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;
116 # Now we've got all associated users and branches,
117 # we can augment the request objects
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(
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}
146 foreach my $b(@{$branch_arr}) {
147 if ($b->{branchcode} eq $req->branchcode) {
148 $to_push->{library} = $b;
152 if ($embed{metadata}) {
153 my $metadata = Koha::Illrequestattributes->search(
154 { illrequest_id => $req->illrequest_id },
155 { columns => [qw/type value/] }
158 foreach my $meta(@{$metadata}) {
159 $meta_hash->{$meta->{type}} = $meta->{value};
161 $to_push->{metadata} = $meta_hash;
163 if ($embed{capabilities}) {
164 $to_push->{capabilities} = $to_fetch->{$req->backend};
166 if ($embed{comments}) {
167 $to_push->{comments} = $req->illcomments->count;
169 if ($embed{status_alias}) {
170 $to_push->{status_alias} = $req->statusalias;
172 if ($embed{requested_partners}) {
173 $to_push->{requested_partners} = $req->requested_partners;
175 push @output, $to_push;
178 return $c->render( status => 200, openapi => \@output );