Bug 27205: Improve logic readability in conditional
[srvgit] / Koha / REST / V1 / Holds.pm
1 package Koha::REST::V1::Holds;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use C4::Biblio;
23 use C4::Reserves;
24
25 use Koha::Items;
26 use Koha::Patrons;
27 use Koha::Holds;
28 use Koha::DateUtils;
29
30 use List::MoreUtils qw(any);
31 use Try::Tiny;
32
33 =head1 API
34
35 =head2 Methods
36
37 =head3 list
38
39 Mehtod that handles listing Koha::Hold objects
40
41 =cut
42
43 sub list {
44     my $c = shift->openapi->valid_input or return;
45
46     return try {
47         my $holds_set = Koha::Holds->new;
48         my $holds     = $c->objects->search( $holds_set );
49         return $c->render( status => 200, openapi => $holds );
50     }
51     catch {
52         $c->unhandled_exception($_);
53     };
54 }
55
56 =head3 add
57
58 Method that handles adding a new Koha::Hold object
59
60 =cut
61
62 sub add {
63     my $c = shift->openapi->valid_input or return;
64
65     return try {
66         my $body = $c->validation->param('body');
67
68         my $biblio;
69         my $item;
70
71         my $biblio_id         = $body->{biblio_id};
72         my $pickup_library_id = $body->{pickup_library_id};
73         my $item_id           = $body->{item_id};
74         my $patron_id         = $body->{patron_id};
75         my $item_type         = $body->{item_type};
76         my $expiration_date   = $body->{expiration_date};
77         my $notes             = $body->{notes};
78         my $hold_date         = $body->{hold_date};
79         my $non_priority      = $body->{non_priority};
80
81         if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
82             return $c->render(
83                 status  => 400,
84                 openapi => { error => "Hold date in future not allowed" }
85             );
86         }
87
88         if ( $item_id and $biblio_id ) {
89
90             # check they are consistent
91             unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
92                 ->count > 0 )
93             {
94                 return $c->render(
95                     status  => 400,
96                     openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
97                 );
98             }
99             else {
100                 $biblio = Koha::Biblios->find($biblio_id);
101             }
102         }
103         elsif ($item_id) {
104             $item = Koha::Items->find($item_id);
105
106             unless ($item) {
107                 return $c->render(
108                     status  => 404,
109                     openapi => { error => "item_id not found." }
110                 );
111             }
112             else {
113                 $biblio = $item->biblio;
114             }
115         }
116         elsif ($biblio_id) {
117             $biblio = Koha::Biblios->find($biblio_id);
118         }
119         else {
120             return $c->render(
121                 status  => 400,
122                 openapi => { error => "At least one of biblio_id, item_id should be given" }
123             );
124         }
125
126         unless ($biblio) {
127             return $c->render(
128                 status  => 400,
129                 openapi => "Biblio not found."
130             );
131         }
132
133         my $patron = Koha::Patrons->find( $patron_id );
134         unless ($patron) {
135             return $c->render(
136                 status  => 400,
137                 openapi => { error => 'patron_id not found' }
138             );
139         }
140
141         # Validate pickup location
142         my $valid_pickup_location;
143         if ($item) {    # item-level hold
144             $valid_pickup_location =
145               any { $_->branchcode eq $pickup_library_id }
146             $item->pickup_locations(
147                 { patron => $patron } );
148         }
149         else {
150             $valid_pickup_location =
151               any { $_->branchcode eq $pickup_library_id }
152             $biblio->pickup_locations(
153                 { patron => $patron } );
154         }
155
156         return $c->render(
157             status  => 400,
158             openapi => {
159                 error => 'The supplied pickup location is not valid'
160             }
161         ) unless $valid_pickup_location;
162
163         my $can_place_hold
164             = $item_id
165             ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
166             : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
167
168         if ( $patron->holds->count + 1 > C4::Context->preference('maxreserves') ) {
169             $can_place_hold->{status} = 'tooManyReserves';
170         }
171
172         my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
173
174         unless ($can_override || $can_place_hold->{status} eq 'OK' ) {
175             return $c->render(
176                 status => 403,
177                 openapi =>
178                     { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
179             );
180         }
181
182         my $priority = C4::Reserves::CalculatePriority($biblio_id);
183
184         # AddReserve expects date to be in syspref format
185         if ($expiration_date) {
186             $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
187         }
188
189         my $hold_id = C4::Reserves::AddReserve(
190             {
191                 branchcode       => $pickup_library_id,
192                 borrowernumber   => $patron_id,
193                 biblionumber     => $biblio_id,
194                 priority         => $priority,
195                 reservation_date => $hold_date,
196                 expiration_date  => $expiration_date,
197                 notes            => $notes,
198                 title            => $biblio->title,
199                 itemnumber       => $item_id,
200                 found            => undef,                # TODO: Why not?
201                 itemtype         => $item_type,
202                 non_priority     => $non_priority,
203             }
204         );
205
206         unless ($hold_id) {
207             return $c->render(
208                 status  => 500,
209                 openapi => 'Error placing the hold. See Koha logs for details.'
210             );
211         }
212
213         my $hold = Koha::Holds->find($hold_id);
214
215         return $c->render(
216             status  => 201,
217             openapi => $hold->to_api
218         );
219     }
220     catch {
221         if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
222             if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
223                 my $broken_fk = $_->broken_fk;
224
225                 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
226                     $c->render(
227                         status  => 404,
228                         openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
229                     );
230                 }
231             }
232         }
233
234         $c->unhandled_exception($_);
235     };
236 }
237
238 =head3 edit
239
240 Method that handles modifying a Koha::Hold object
241
242 =cut
243
244 sub edit {
245     my $c = shift->openapi->valid_input or return;
246
247     return try {
248         my $hold_id = $c->validation->param('hold_id');
249         my $hold = Koha::Holds->find( $hold_id );
250
251         unless ($hold) {
252             return $c->render( status  => 404,
253                             openapi => {error => "Hold not found"} );
254         }
255
256         my $body = $c->req->json;
257
258         my $pickup_library_id = $body->{pickup_library_id};
259
260         if (
261             defined $pickup_library_id
262             and not $hold->is_pickup_location_valid({ library_id => $pickup_library_id })
263           )
264         {
265             return $c->render(
266                 status  => 400,
267                 openapi => {
268                     error => 'The supplied pickup location is not valid'
269                 }
270             );
271         }
272
273         $pickup_library_id    //= $hold->branchcode;
274         my $priority          = $body->{priority} // $hold->priority;
275         # suspended_until can also be set to undef
276         my $suspended_until   = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
277
278         my $params = {
279             reserve_id    => $hold_id,
280             branchcode    => $pickup_library_id,
281             rank          => $priority,
282             suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
283             itemnumber    => $hold->itemnumber
284         };
285
286         C4::Reserves::ModReserve($params);
287         $hold->discard_changes; # refresh
288
289         return $c->render(
290             status  => 200,
291             openapi => $hold->to_api
292         );
293     }
294     catch {
295         $c->unhandled_exception($_);
296     };
297 }
298
299 =head3 delete
300
301 Method that handles deleting a Koha::Hold object
302
303 =cut
304
305 sub delete {
306     my $c = shift->openapi->valid_input or return;
307
308     my $hold_id = $c->validation->param('hold_id');
309     my $hold    = Koha::Holds->find($hold_id);
310
311     unless ($hold) {
312         return $c->render( status => 404, openapi => { error => "Hold not found." } );
313     }
314
315     return try {
316         $hold->cancel;
317
318         return $c->render(
319             status  => 204,
320             openapi => q{}
321         );
322     }
323     catch {
324         $c->unhandled_exception($_);
325     };
326 }
327
328 =head3 suspend
329
330 Method that handles suspending a hold
331
332 =cut
333
334 sub suspend {
335     my $c = shift->openapi->valid_input or return;
336
337     my $hold_id  = $c->validation->param('hold_id');
338     my $hold     = Koha::Holds->find($hold_id);
339     my $body     = $c->req->json;
340     my $end_date = ($body) ? $body->{end_date} : undef;
341
342     unless ($hold) {
343         return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
344     }
345
346     return try {
347         my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
348         $hold->suspend_hold($date);
349         $hold->discard_changes;
350         $c->res->headers->location( $c->req->url->to_string );
351         my $suspend_end_date;
352         if ($hold->suspend_until) {
353             $suspend_end_date = output_pref({
354                 dt         => dt_from_string( $hold->suspend_until ),
355                 dateformat => 'rfc3339',
356                 dateonly   => 1
357                 }
358             );
359         }
360         return $c->render(
361             status  => 201,
362             openapi => {
363                 end_date => $suspend_end_date
364             }
365         );
366     }
367     catch {
368         if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
369             return $c->render( status => 400, openapi => { error => "$_" } );
370         }
371
372         $c->unhandled_exception($_);
373     };
374 }
375
376 =head3 resume
377
378 Method that handles resuming a hold
379
380 =cut
381
382 sub resume {
383     my $c = shift->openapi->valid_input or return;
384
385     my $hold_id = $c->validation->param('hold_id');
386     my $hold    = Koha::Holds->find($hold_id);
387     my $body    = $c->req->json;
388
389     unless ($hold) {
390         return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
391     }
392
393     return try {
394         $hold->resume;
395         return $c->render( status => 204, openapi => {} );
396     }
397     catch {
398         $c->unhandled_exception($_);
399     };
400 }
401
402 =head3 update_priority
403
404 Method that handles modifying a Koha::Hold object
405
406 =cut
407
408 sub update_priority {
409     my $c = shift->openapi->valid_input or return;
410
411     my $hold_id = $c->validation->param('hold_id');
412     my $hold = Koha::Holds->find($hold_id);
413
414     unless ($hold) {
415         return $c->render(
416             status  => 404,
417             openapi => { error => "Hold not found" }
418         );
419     }
420
421     return try {
422         my $priority = $c->req->json;
423         C4::Reserves::_FixPriority(
424             {
425                 reserve_id => $hold_id,
426                 rank       => $priority
427             }
428         );
429
430         return $c->render( status => 200, openapi => $priority );
431     }
432     catch {
433         $c->unhandled_exception($_);
434     };
435 }
436
437 =head3 pickup_locations
438
439 Method that returns the possible pickup_locations for a given hold
440 used for building the dropdown selector
441
442 =cut
443
444 sub pickup_locations {
445     my $c = shift->openapi->valid_input or return;
446
447     my $hold_id = $c->validation->param('hold_id');
448     my $hold = Koha::Holds->find( $hold_id, { prefetch => [ 'patron' ] } );
449
450     unless ($hold) {
451         return $c->render(
452             status  => 404,
453             openapi => { error => "Hold not found" }
454         );
455     }
456
457     return try {
458         my @pickup_locations =
459             $hold->itemnumber
460           ? @{ $hold->item->pickup_locations( { patron => $hold->patron } )->as_list() }
461           : @{ $hold->biblio->pickup_locations( { patron => $hold->patron } )->as_list() };
462
463         @pickup_locations = map { $_->to_api } @pickup_locations;
464
465         return $c->render(
466             status  => 200,
467             openapi => \@pickup_locations
468         );
469     }
470     catch {
471         $c->unhandled_exception($_);
472     };
473 }
474
475 1;