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' );
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 my @requests = Koha::Illrequests->as_list;
60 # Identify patrons & branches that
61 # we're going to need and get them
67 foreach my $req(@requests) {
68 $to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron};
69 $to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library};
70 $to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities};
73 # Fetch the patrons we need
76 my @patron_ids = keys %{$to_fetch->{patrons}};
77 if (scalar @patron_ids > 0) {
79 borrowernumber => { -in => \@patron_ids }
81 $patron_arr = Koha::Patrons->search($where)->unblessed;
85 # Fetch the branches we need
87 if ($embed{library}) {
88 my @branchcodes = keys %{$to_fetch->{branches}};
89 if (scalar @branchcodes > 0) {
91 branchcode => { -in => \@branchcodes }
93 $branch_arr = Koha::Libraries->search($where)->unblessed;
97 # Fetch the capabilities we need
98 if ($embed{capabilities}) {
99 my @backends = keys %{$to_fetch->{capabilities}};
100 if (scalar @backends > 0) {
101 foreach my $bc(@backends) {
102 my $backend = Koha::Illrequest->new->load_backend($bc);
103 $to_fetch->{$bc} = $backend->capabilities;
108 # Now we've got all associated users and branches,
109 # we can augment the request objects
111 foreach my $req(@requests) {
112 my $to_push = $req->unblessed;
113 $to_push->{id_prefix} = $req->id_prefix;
114 # Create new "formatted" columns for each date column
115 # that needs formatting
116 foreach my $field(@format_dates) {
117 if (defined $to_push->{$field}) {
118 $to_push->{$field . "_formatted"} = format_sqldatetime(
127 foreach my $p(@{$patron_arr}) {
128 if ($p->{borrowernumber} == $req->borrowernumber) {
129 $to_push->{patron} = {
130 firstname => $p->{firstname},
131 surname => $p->{surname},
132 cardnumber => $p->{cardnumber}
137 foreach my $b(@{$branch_arr}) {
138 if ($b->{branchcode} eq $req->branchcode) {
139 $to_push->{library} = $b;
143 if ($embed{metadata}) {
144 my $metadata = Koha::Illrequestattributes->search(
145 { illrequest_id => $req->illrequest_id },
146 { columns => [qw/type value/] }
149 foreach my $meta(@{$metadata}) {
150 $meta_hash->{$meta->{type}} = $meta->{value};
152 $to_push->{metadata} = $meta_hash;
154 if ($embed{capabilities}) {
155 $to_push->{capabilities} = $to_fetch->{$req->backend};
157 if ($embed{comments}) {
158 $to_push->{comments} = $req->illcomments->count;
160 if ($embed{status_alias}) {
161 $to_push->{status_alias} = $req->statusalias;
163 push @output, $to_push;
166 return $c->render( status => 200, openapi => \@output );