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 // {};
44 my @format_dates = ( 'placed', 'updated' );
46 # Create a hash where all keys are embedded values
47 # Enables easy checking
49 my $args_arr = (ref $args->{embed
} eq 'ARRAY') ?
$args->{embed
} : [ $args->{embed
} ];
50 if (defined $args->{embed
}) {
51 %embed = map { $_ => 1 } @
{$args_arr};
52 delete $args->{embed
};
56 my @requests = Koha
::Illrequests
->as_list;
58 # Identify patrons & branches that
59 # we're going to need and get them
65 foreach my $req(@requests) {
66 $to_fetch->{patrons
}->{$req->borrowernumber} = 1 if $embed{patron
};
67 $to_fetch->{branches
}->{$req->branchcode} = 1 if $embed{library
};
68 $to_fetch->{capabilities
}->{$req->backend} = 1 if $embed{capabilities
};
71 # Fetch the patrons we need
74 my @patron_ids = keys %{$to_fetch->{patrons
}};
75 if (scalar @patron_ids > 0) {
77 borrowernumber
=> { -in => \
@patron_ids }
79 $patron_arr = Koha
::Patrons
->search($where)->unblessed;
83 # Fetch the branches we need
85 if ($embed{library
}) {
86 my @branchcodes = keys %{$to_fetch->{branches
}};
87 if (scalar @branchcodes > 0) {
89 branchcode
=> { -in => \
@branchcodes }
91 $branch_arr = Koha
::Libraries
->search($where)->unblessed;
95 # Fetch the capabilities we need
96 if ($embed{capabilities
}) {
97 my @backends = keys %{$to_fetch->{capabilities
}};
98 if (scalar @backends > 0) {
99 foreach my $bc(@backends) {
100 my $backend = Koha
::Illrequest
->new->load_backend($bc);
101 $to_fetch->{$bc} = $backend->capabilities;
106 # Now we've got all associated users and branches,
107 # we can augment the request objects
109 foreach my $req(@requests) {
110 my $to_push = $req->unblessed;
111 $to_push->{id_prefix
} = $req->id_prefix;
112 # Create new "formatted" columns for each date column
113 # that needs formatting
114 foreach my $field(@format_dates) {
115 if (defined $to_push->{$field}) {
116 $to_push->{$field . "_formatted"} = format_sqldatetime
(
125 foreach my $p(@
{$patron_arr}) {
126 if ($p->{borrowernumber
} == $req->borrowernumber) {
127 $to_push->{patron
} = {
128 firstname
=> $p->{firstname
},
129 surname
=> $p->{surname
},
130 cardnumber
=> $p->{cardnumber
}
135 foreach my $b(@
{$branch_arr}) {
136 if ($b->{branchcode
} eq $req->branchcode) {
137 $to_push->{library
} = $b;
141 if ($embed{metadata
}) {
142 my $metadata = Koha
::Illrequestattributes
->search(
143 { illrequest_id
=> $req->illrequest_id },
144 { columns
=> [qw
/type value/] }
147 foreach my $meta(@
{$metadata}) {
148 $meta_hash->{$meta->{type
}} = $meta->{value
};
150 $to_push->{metadata
} = $meta_hash;
152 if ($embed{capabilities
}) {
153 $to_push->{capabilities
} = $to_fetch->{$req->backend};
155 if ($embed{comments
}) {
156 $to_push->{comments
} = $req->illcomments->count;
158 if ($embed{status_alias
}) {
159 $to_push->{status_alias
} = $req->statusalias;
161 push @output, $to_push;
164 return $c->render( status
=> 200, openapi
=> \
@output );