Bug 27205: Check valid pickup location on POST /holds
[koha.git] / 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         unless (
261             !defined $pickup_library_id
262             or $hold->is_pickup_location_valid(
263                 { library_id => $pickup_library_id }
264             )
265           )
266         {
267             return $c->render(
268                 status  => 400,
269                 openapi => {
270                     error => 'The supplied pickup location is not valid'
271                 }
272             );
273         }
274
275         $pickup_library_id = $hold->branchcode
276             unless defined $pickup_library_id;
277         my $priority          = $body->{priority} // $hold->priority;
278         # suspended_until can also be set to undef
279         my $suspended_until   = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
280
281         my $params = {
282             reserve_id    => $hold_id,
283             branchcode    => $pickup_library_id,
284             rank          => $priority,
285             suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
286             itemnumber    => $hold->itemnumber
287         };
288
289         C4::Reserves::ModReserve($params);
290         $hold->discard_changes; # refresh
291
292         return $c->render(
293             status  => 200,
294             openapi => $hold->to_api
295         );
296     }
297     catch {
298         $c->unhandled_exception($_);
299     };
300 }
301
302 =head3 delete
303
304 Method that handles deleting a Koha::Hold object
305
306 =cut
307
308 sub delete {
309     my $c = shift->openapi->valid_input or return;
310
311     my $hold_id = $c->validation->param('hold_id');
312     my $hold    = Koha::Holds->find($hold_id);
313
314     unless ($hold) {
315         return $c->render( status => 404, openapi => { error => "Hold not found." } );
316     }
317
318     return try {
319         $hold->cancel;
320
321         return $c->render(
322             status  => 204,
323             openapi => q{}
324         );
325     }
326     catch {
327         $c->unhandled_exception($_);
328     };
329 }
330
331 =head3 suspend
332
333 Method that handles suspending a hold
334
335 =cut
336
337 sub suspend {
338     my $c = shift->openapi->valid_input or return;
339
340     my $hold_id  = $c->validation->param('hold_id');
341     my $hold     = Koha::Holds->find($hold_id);
342     my $body     = $c->req->json;
343     my $end_date = ($body) ? $body->{end_date} : undef;
344
345     unless ($hold) {
346         return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
347     }
348
349     return try {
350         my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
351         $hold->suspend_hold($date);
352         $hold->discard_changes;
353         $c->res->headers->location( $c->req->url->to_string );
354         my $suspend_end_date;
355         if ($hold->suspend_until) {
356             $suspend_end_date = output_pref({
357                 dt         => dt_from_string( $hold->suspend_until ),
358                 dateformat => 'rfc3339',
359                 dateonly   => 1
360                 }
361             );
362         }
363         return $c->render(
364             status  => 201,
365             openapi => {
366                 end_date => $suspend_end_date
367             }
368         );
369     }
370     catch {
371         if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
372             return $c->render( status => 400, openapi => { error => "$_" } );
373         }
374
375         $c->unhandled_exception($_);
376     };
377 }
378
379 =head3 resume
380
381 Method that handles resuming a hold
382
383 =cut
384
385 sub resume {
386     my $c = shift->openapi->valid_input or return;
387
388     my $hold_id = $c->validation->param('hold_id');
389     my $hold    = Koha::Holds->find($hold_id);
390     my $body    = $c->req->json;
391
392     unless ($hold) {
393         return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
394     }
395
396     return try {
397         $hold->resume;
398         return $c->render( status => 204, openapi => {} );
399     }
400     catch {
401         $c->unhandled_exception($_);
402     };
403 }
404
405 =head3 update_priority
406
407 Method that handles modifying a Koha::Hold object
408
409 =cut
410
411 sub update_priority {
412     my $c = shift->openapi->valid_input or return;
413
414     my $hold_id = $c->validation->param('hold_id');
415     my $hold = Koha::Holds->find($hold_id);
416
417     unless ($hold) {
418         return $c->render(
419             status  => 404,
420             openapi => { error => "Hold not found" }
421         );
422     }
423
424     return try {
425         my $priority = $c->req->json;
426         C4::Reserves::_FixPriority(
427             {
428                 reserve_id => $hold_id,
429                 rank       => $priority
430             }
431         );
432
433         return $c->render( status => 200, openapi => $priority );
434     }
435     catch {
436         $c->unhandled_exception($_);
437     };
438 }
439
440 =head3 pickup_locations
441
442 Method that returns the possible pickup_locations for a given hold
443 used for building the dropdown selector
444
445 =cut
446
447 sub pickup_locations {
448     my $c = shift->openapi->valid_input or return;
449
450     my $hold_id = $c->validation->param('hold_id');
451     my $hold = Koha::Holds->find( $hold_id, { prefetch => [ 'patron' ] } );
452
453     unless ($hold) {
454         return $c->render(
455             status  => 404,
456             openapi => { error => "Hold not found" }
457         );
458     }
459
460     return try {
461         my @pickup_locations =
462             $hold->itemnumber
463           ? @{ $hold->item->pickup_locations( { patron => $hold->patron } )->as_list() }
464           : @{ $hold->biblio->pickup_locations( { patron => $hold->patron } )->as_list() };
465
466         @pickup_locations = map { $_->to_api } @pickup_locations;
467
468         return $c->render(
469             status  => 200,
470             openapi => \@pickup_locations
471         );
472     }
473     catch {
474         $c->unhandled_exception($_);
475     };
476 }
477
478 1;