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