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.
18 use Mojo::Base 'Mojolicious::Controller';
20 use Koha::Illrequests;
21 use Koha::Illrequestattributes;
25 use Koha::DateUtils qw( format_sqldatetime );
29 Koha::REST::V1::Illrequests
35 Return a list of ILL requests, after applying filters.
40 my $c = shift->openapi->valid_input or return;
42 my $args = $c->req->params->to_hash // {};
45 my @format_dates = ( 'placed', 'updated' );
47 # Create a hash where all keys are embedded values
48 # Enables easy checking
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};
57 my @requests = Koha::Illrequests->as_list;
59 # Identify patrons & branches that
60 # we're going to need and get them
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};
72 # Fetch the patrons we need
75 my @patron_ids = keys %{$to_fetch->{patrons}};
76 if (scalar @patron_ids > 0) {
78 borrowernumber => { -in => \@patron_ids }
80 $patron_arr = Koha::Patrons->search($where)->unblessed;
84 # Fetch the branches we need
86 if ($embed{library}) {
87 my @branchcodes = keys %{$to_fetch->{branches}};
88 if (scalar @branchcodes > 0) {
90 branchcode => { -in => \@branchcodes }
92 $branch_arr = Koha::Libraries->search($where)->unblessed;
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;
107 # Now we've got all associated users and branches,
108 # we can augment the request objects
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(
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}
136 foreach my $b(@{$branch_arr}) {
137 if ($b->{branchcode} eq $req->branchcode) {
138 $to_push->{library} = $b;
142 if ($embed{metadata}) {
143 my $metadata = Koha::Illrequestattributes->search(
144 { illrequest_id => $req->illrequest_id },
145 { columns => [qw/type value/] }
148 foreach my $meta(@{$metadata}) {
149 $meta_hash->{$meta->{type}} = $meta->{value};
151 $to_push->{metadata} = $meta_hash;
153 if ($embed{capabilities}) {
154 $to_push->{capabilities} = $to_fetch->{$req->backend};
156 if ($embed{comments}) {
157 $to_push->{comments} = $req->illcomments->count;
159 push @output, $to_push;
162 return $c->render( status => 200, openapi => \@output );