Bug 20581: API provide status_alias embed
[koha.git] / Koha / REST / V1 / Illrequests.pm
blob94ff8ce18f58d7f568f1d6e934dc0f2fb79fddfb
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
8 # version.
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;
22 use Koha::Libraries;
23 use Koha::Patrons;
24 use Koha::Libraries;
25 use Koha::DateUtils qw( format_sqldatetime );
27 =head1 NAME
29 Koha::REST::V1::Illrequests
31 =head2 Operations
33 =head3 list
35 Return a list of ILL requests, after applying filters.
37 =cut
39 sub list {
40 my $c = shift->openapi->valid_input or return;
42 my $args = $c->req->params->to_hash // {};
43 my $output = [];
44 my @format_dates = ( 'placed', 'updated' );
46 # Create a hash where all keys are embedded values
47 # Enables easy checking
48 my %embed;
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};
55 # Get all requests
56 my @requests = Koha::Illrequests->as_list;
58 # Identify patrons & branches that
59 # we're going to need and get them
60 my $to_fetch = {
61 patrons => {},
62 branches => {},
63 capabilities => {}
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
72 my $patron_arr = [];
73 if ($embed{patron}) {
74 my @patron_ids = keys %{$to_fetch->{patrons}};
75 if (scalar @patron_ids > 0) {
76 my $where = {
77 borrowernumber => { -in => \@patron_ids }
79 $patron_arr = Koha::Patrons->search($where)->unblessed;
83 # Fetch the branches we need
84 my $branch_arr = [];
85 if ($embed{library}) {
86 my @branchcodes = keys %{$to_fetch->{branches}};
87 if (scalar @branchcodes > 0) {
88 my $where = {
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
108 my @output = ();
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(
117 $to_push->{$field},
118 undef,
119 undef,
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}
132 last;
135 foreach my $b(@{$branch_arr}) {
136 if ($b->{branchcode} eq $req->branchcode) {
137 $to_push->{library} = $b;
138 last;
141 if ($embed{metadata}) {
142 my $metadata = Koha::Illrequestattributes->search(
143 { illrequest_id => $req->illrequest_id },
144 { columns => [qw/type value/] }
145 )->unblessed;
146 my $meta_hash = {};
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 );