Lines 35-203
Koha::REST::V1::Illrequests
Link Here
|
35 |
|
35 |
|
36 |
=head3 list |
36 |
=head3 list |
37 |
|
37 |
|
38 |
Return a list of ILL requests, after applying filters. |
38 |
Controller function that handles listing Koha::Illrequest objects |
39 |
|
39 |
|
40 |
=cut |
40 |
=cut |
41 |
|
41 |
|
42 |
sub list { |
42 |
sub list { |
43 |
my $c = shift->openapi->valid_input or return; |
43 |
my $c = shift->openapi->valid_input or return; |
44 |
|
44 |
|
45 |
my $args = $c->req->params->to_hash // {}; |
45 |
return try { |
46 |
my $output = []; |
|
|
47 |
my @format_dates = ( 'placed', 'updated', 'completed' ); |
48 |
|
46 |
|
49 |
# Create a hash where all keys are embedded values |
47 |
my $reqs = $c->objects->search(Koha::Illrequests->new->filter_by_visible); |
50 |
# Enables easy checking |
|
|
51 |
my %embed; |
52 |
my $args_arr = (ref $args->{embed} eq 'ARRAY') ? $args->{embed} : [ $args->{embed} ]; |
53 |
if (defined $args->{embed}) { |
54 |
%embed = map { $_ => 1 } @{$args_arr}; |
55 |
delete $args->{embed}; |
56 |
} |
57 |
|
48 |
|
58 |
# Get the pipe-separated string of hidden ILL statuses |
49 |
return $c->render( |
59 |
my $hidden_statuses_string = C4::Context->preference('ILLHiddenRequestStatuses') // q{}; |
50 |
status => 200, |
60 |
# Turn into arrayref |
51 |
openapi => $reqs, |
61 |
my $hidden_statuses = [ split /\|/, $hidden_statuses_string ]; |
52 |
); |
62 |
|
53 |
} catch { |
63 |
# Get all requests |
54 |
$c->unhandled_exception($_); |
64 |
# If necessary, only get those from a specified patron |
|
|
65 |
my @requests = Koha::Illrequests->search({ |
66 |
$hidden_statuses |
67 |
? ( |
68 |
-and => { |
69 |
status => { 'not in' => $hidden_statuses }, |
70 |
status_alias => [ -or => |
71 |
{ 'not in' => $hidden_statuses }, |
72 |
{ '=' => undef } |
73 |
] |
74 |
} |
75 |
) |
76 |
: (), |
77 |
$args->{borrowernumber} |
78 |
? ( borrowernumber => $args->{borrowernumber} ) |
79 |
: () |
80 |
})->as_list; |
81 |
|
82 |
my $fetch_backends = {}; |
83 |
foreach my $request (@requests) { |
84 |
$fetch_backends->{ $request->backend } ||= |
85 |
Koha::Illrequest->new->load_backend( $request->backend ); |
86 |
} |
87 |
|
88 |
# Pre-load the backend object to avoid useless backend lookup/loads |
89 |
@requests = map { $_->_backend( $fetch_backends->{ $_->backend } ); $_ } @requests; |
90 |
|
91 |
# Identify patrons & branches that |
92 |
# we're going to need and get them |
93 |
my $to_fetch = { |
94 |
patrons => {}, |
95 |
branches => {}, |
96 |
capabilities => {} |
97 |
}; |
55 |
}; |
98 |
foreach my $req (@requests) { |
|
|
99 |
$to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron}; |
100 |
$to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library}; |
101 |
$to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities}; |
102 |
} |
103 |
|
104 |
# Fetch the patrons we need |
105 |
my $patron_arr = []; |
106 |
if ($embed{patron}) { |
107 |
my @patron_ids = keys %{$to_fetch->{patrons}}; |
108 |
if (scalar @patron_ids > 0) { |
109 |
my $where = { |
110 |
borrowernumber => { -in => \@patron_ids } |
111 |
}; |
112 |
$patron_arr = Koha::Patrons->search($where)->unblessed; |
113 |
} |
114 |
} |
115 |
|
116 |
# Fetch the branches we need |
117 |
my $branch_arr = []; |
118 |
if ($embed{library}) { |
119 |
my @branchcodes = keys %{$to_fetch->{branches}}; |
120 |
if (scalar @branchcodes > 0) { |
121 |
my $where = { |
122 |
branchcode => { -in => \@branchcodes } |
123 |
}; |
124 |
$branch_arr = Koha::Libraries->search($where)->unblessed; |
125 |
} |
126 |
} |
127 |
|
128 |
# Fetch the capabilities we need |
129 |
if ($embed{capabilities}) { |
130 |
my @backends = keys %{$to_fetch->{capabilities}}; |
131 |
if (scalar @backends > 0) { |
132 |
foreach my $bc(@backends) { |
133 |
$to_fetch->{$bc} = $fetch_backends->{$bc}->capabilities; |
134 |
} |
135 |
} |
136 |
} |
137 |
|
138 |
# Now we've got all associated users and branches, |
139 |
# we can augment the request objects |
140 |
my @output = (); |
141 |
foreach my $req(@requests) { |
142 |
my $to_push = $req->unblessed; |
143 |
$to_push->{id_prefix} = $req->id_prefix; |
144 |
# Create new "formatted" columns for each date column |
145 |
# that needs formatting |
146 |
foreach my $field(@format_dates) { |
147 |
if (defined $to_push->{$field}) { |
148 |
$to_push->{$field . "_formatted"} = format_sqldatetime( |
149 |
$to_push->{$field}, |
150 |
undef, |
151 |
undef, |
152 |
1 |
153 |
); |
154 |
} |
155 |
} |
156 |
|
157 |
foreach my $p(@{$patron_arr}) { |
158 |
if ($p->{borrowernumber} == $req->borrowernumber) { |
159 |
$to_push->{patron} = { |
160 |
patron_id => $p->{borrowernumber}, |
161 |
firstname => $p->{firstname}, |
162 |
surname => $p->{surname}, |
163 |
cardnumber => $p->{cardnumber} |
164 |
}; |
165 |
last; |
166 |
} |
167 |
} |
168 |
foreach my $b(@{$branch_arr}) { |
169 |
if ($b->{branchcode} eq $req->branchcode) { |
170 |
$to_push->{library} = $b; |
171 |
last; |
172 |
} |
173 |
} |
174 |
if ($embed{metadata}) { |
175 |
my $metadata = Koha::Illrequestattributes->search( |
176 |
{ illrequest_id => $req->illrequest_id }, |
177 |
{ columns => [qw/type value/] } |
178 |
)->unblessed; |
179 |
my $meta_hash = {}; |
180 |
foreach my $meta(@{$metadata}) { |
181 |
$meta_hash->{$meta->{type}} = $meta->{value}; |
182 |
} |
183 |
$to_push->{metadata} = $meta_hash; |
184 |
} |
185 |
if ($embed{capabilities}) { |
186 |
$to_push->{capabilities} = $to_fetch->{$req->backend}; |
187 |
} |
188 |
if ($embed{comments}) { |
189 |
$to_push->{comments} = $req->illcomments->count; |
190 |
} |
191 |
if ($embed{status_alias}) { |
192 |
$to_push->{status_alias} = $req->statusalias; |
193 |
} |
194 |
if ($embed{requested_partners}) { |
195 |
$to_push->{requested_partners} = $req->requested_partners; |
196 |
} |
197 |
push @output, $to_push; |
198 |
} |
199 |
|
200 |
return $c->render( status => 200, openapi => \@output ); |
201 |
} |
56 |
} |
202 |
|
57 |
|
203 |
1; |
58 |
1; |