Lines 20-25
use Modern::Perl;
Link Here
|
20 |
use Mojo::Base 'Mojolicious::Controller'; |
20 |
use Mojo::Base 'Mojolicious::Controller'; |
21 |
|
21 |
|
22 |
use Koha::Illrequests; |
22 |
use Koha::Illrequests; |
|
|
23 |
use Koha::Illrequestattributes; |
24 |
use Koha::Libraries; |
25 |
use Koha::Patrons; |
23 |
use Koha::Libraries; |
26 |
use Koha::Libraries; |
24 |
|
27 |
|
25 |
=head1 NAME |
28 |
=head1 NAME |
Lines 38-71
sub list {
Link Here
|
38 |
my $c = shift->openapi->valid_input or return; |
41 |
my $c = shift->openapi->valid_input or return; |
39 |
|
42 |
|
40 |
my $args = $c->req->params->to_hash // {}; |
43 |
my $args = $c->req->params->to_hash // {}; |
41 |
my $filter; |
|
|
42 |
my $output = []; |
43 |
|
44 |
|
44 |
# Create a hash where all keys are embedded values |
45 |
# Create a hash where all keys are embedded values |
45 |
# Enables easy checking |
46 |
# Enables easy checking |
46 |
my %embed; |
47 |
my %embed; |
|
|
48 |
my $args_arr = (ref $args->{embed} eq 'ARRAY') ? $args->{embed} : [ $args->{embed} ]; |
47 |
if (defined $args->{embed}) { |
49 |
if (defined $args->{embed}) { |
48 |
%embed = map { $_ => 1 } @{$args->{embed}}; |
50 |
%embed = map { $_ => 1 } @{$args_arr}; |
49 |
delete $args->{embed}; |
51 |
delete $args->{embed}; |
50 |
} |
52 |
} |
51 |
|
53 |
|
52 |
for my $filter_param ( keys %$args ) { |
54 |
my $requests = Koha::Illrequests->unblessed; |
53 |
my @values = split(/,/, $args->{$filter_param}); |
55 |
|
54 |
$filter->{$filter_param} = \@values; |
56 |
# Identify patrons & branches that |
|
|
57 |
# we're going to need and get them |
58 |
my $to_fetch = {}; |
59 |
$to_fetch->{patrons} = {} if $embed{patron}; |
60 |
$to_fetch->{branches} = {} if $embed{library}; |
61 |
$to_fetch->{capabilities} = {} if $embed{capabilities}; |
62 |
foreach my $req(@{$requests}) { |
63 |
$to_fetch->{patrons}->{$req->{borrowernumber}} = 1 if $embed{patron}; |
64 |
$to_fetch->{branches}->{$req->{branchcode}} = 1 if $embed{library}; |
65 |
$to_fetch->{capabilities}->{$req->{backend}} = 1 if $embed{capabilities}; |
55 |
} |
66 |
} |
56 |
|
67 |
|
57 |
my $requests = Koha::Illrequests->search($filter); |
68 |
# Fetch the patrons we need |
|
|
69 |
my $patron_arr = []; |
70 |
if ($embed{patron}) { |
71 |
my @patron_ids = keys %{$to_fetch->{patrons}}; |
72 |
if (scalar @patron_ids > 0) { |
73 |
my $where = { |
74 |
borrowernumber => { -in => \@patron_ids } |
75 |
}; |
76 |
$patron_arr = Koha::Patrons->search($where)->unblessed; |
77 |
} |
78 |
} |
58 |
|
79 |
|
59 |
if ( scalar (keys %embed) ) |
80 |
# Fetch the branches we need |
60 |
{ |
81 |
my $branch_arr = []; |
61 |
# Need to embed stuff |
82 |
if ($embed{library}) { |
62 |
my @results = map { $_->TO_JSON(\%embed) } $requests->as_list; |
83 |
my @branchcodes = keys %{$to_fetch->{branches}}; |
63 |
return $c->render( status => 200, openapi => \@results ); |
84 |
if (scalar @branchcodes > 0) { |
|
|
85 |
my $where = { |
86 |
branchcode => { -in => \@branchcodes } |
87 |
}; |
88 |
$branch_arr = Koha::Libraries->search($where)->unblessed; |
89 |
} |
64 |
} |
90 |
} |
65 |
else |
91 |
|
66 |
{ |
92 |
# Fetch the capabilities we need |
67 |
return $c->render( status => 200, openapi => $requests ); |
93 |
if ($embed{capabilities}) { |
|
|
94 |
my @backends = keys %{$to_fetch->{capabilities}}; |
95 |
if (scalar @backends > 0) { |
96 |
foreach my $bc(@backends) { |
97 |
my $backend = Koha::Illrequest->new->load_backend($bc); |
98 |
$to_fetch->{$bc} = $backend->capabilities; |
99 |
} |
100 |
} |
68 |
} |
101 |
} |
|
|
102 |
|
103 |
|
104 |
# Now we've got all associated users and branches, |
105 |
# we can augment the request objects |
106 |
foreach my $req(@{$requests}) { |
107 |
my $r = Koha::Illrequests->new->find($req->{illrequest_id}); |
108 |
$req->{id_prefix} = $r->id_prefix; |
109 |
foreach my $p(@{$patron_arr}) { |
110 |
if ($p->{borrowernumber} == $req->{borrowernumber}) { |
111 |
$req->{patron} = { |
112 |
firstname => $p->{firstname}, |
113 |
surname => $p->{surname}, |
114 |
cardnumber => $p->{cardnumber} |
115 |
}; |
116 |
last; |
117 |
} |
118 |
} |
119 |
foreach my $b(@{$branch_arr}) { |
120 |
if ($b->{branchcode} eq $req->{branchcode}) { |
121 |
$req->{library} = $b; |
122 |
last; |
123 |
} |
124 |
} |
125 |
if ($embed{metadata}) { |
126 |
my $metadata = Koha::Illrequestattributes->search( |
127 |
{ illrequest_id => $req->{illrequest_id} }, |
128 |
{ columns => [qw/type value/] } |
129 |
)->unblessed; |
130 |
my $meta_hash = {}; |
131 |
foreach my $meta(@{$metadata}) { |
132 |
$meta_hash->{$meta->{type}} = $meta->{value}; |
133 |
} |
134 |
$req->{metadata} = $meta_hash; |
135 |
} |
136 |
if ($embed{capabilities}) { |
137 |
$req->{capabilities} = $to_fetch->{$req->{backend}}; |
138 |
} |
139 |
} |
140 |
|
141 |
return $c->render( status => 200, openapi => $requests ); |
69 |
} |
142 |
} |
70 |
|
143 |
|
71 |
1; |
144 |
1; |