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 # Create new "formatted" columns for each date column
60 # that needs formatting
61 foreach $req(@requests) {
62 foreach my $field(@format_dates) {
63 if (defined $req->{$field}) {
64 $req->{$field . "_formatted"} = format_sqldatetime(
75 # Identify patrons & branches that
76 # we're going to need and get them
82 foreach my $req(@requests) {
83 $to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron};
84 $to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library};
85 $to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities};
88 # Fetch the patrons we need
91 my @patron_ids = keys %{$to_fetch->{patrons}};
92 if (scalar @patron_ids > 0) {
94 borrowernumber => { -in => \@patron_ids }
96 $patron_arr = Koha::Patrons->search($where)->unblessed;
100 # Fetch the branches we need
102 if ($embed{library}) {
103 my @branchcodes = keys %{$to_fetch->{branches}};
104 if (scalar @branchcodes > 0) {
106 branchcode => { -in => \@branchcodes }
108 $branch_arr = Koha::Libraries->search($where)->unblessed;
112 # Fetch the capabilities we need
113 if ($embed{capabilities}) {
114 my @backends = keys %{$to_fetch->{capabilities}};
115 if (scalar @backends > 0) {
116 foreach my $bc(@backends) {
117 my $backend = Koha::Illrequest->new->load_backend($bc);
118 $to_fetch->{$bc} = $backend->capabilities;
123 # Now we've got all associated users and branches,
124 # we can augment the request objects
126 foreach my $req(@requests) {
127 my $to_push = $req->unblessed;
128 $to_push->{id_prefix} = $req->id_prefix;
129 foreach my $p(@{$patron_arr}) {
130 if ($p->{borrowernumber} == $req->borrowernumber) {
131 $to_push->{patron} = {
132 firstname => $p->{firstname},
133 surname => $p->{surname},
134 cardnumber => $p->{cardnumber}
139 foreach my $b(@{$branch_arr}) {
140 if ($b->{branchcode} eq $req->branchcode) {
141 $to_push->{library} = $b;
145 if ($embed{metadata}) {
146 my $metadata = Koha::Illrequestattributes->search(
147 { illrequest_id => $req->illrequest_id },
148 { columns => [qw/type value/] }
151 foreach my $meta(@{$metadata}) {
152 $meta_hash->{$meta->{type}} = $meta->{value};
154 $to_push->{metadata} = $meta_hash;
156 if ($embed{capabilities}) {
157 $to_push->{capabilities} = $to_fetch->{$req->backend};
159 if ($embed{comments}) {
160 $to_push->{comments} = $req->illcomments->count;
162 push @output, $to_push;
165 return $c->render( status => 200, openapi => \@output );