Bug 26988: Add API route to fetch hold pickup locations and use it in the holds table
[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} // $hold->branchcode;
235         my $priority          = $body->{priority} // $hold->priority;
236         # suspended_until can also be set to undef
237         my $suspended_until   = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
238
239         my $params = {
240             reserve_id    => $hold_id,
241             branchcode    => $pickup_library_id,
242             rank          => $priority,
243             suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
244             itemnumber    => $hold->itemnumber
245         };
246
247         C4::Reserves::ModReserve($params);
248         $hold->discard_changes; # refresh
249
250         return $c->render(
251             status  => 200,
252             openapi => $hold->to_api
253         );
254     }
255     catch {
256         $c->unhandled_exception($_);
257     };
258 }
259
260 =head3 delete
261
262 Method that handles deleting a Koha::Hold object
263
264 =cut
265
266 sub delete {
267     my $c = shift->openapi->valid_input or return;
268
269     my $hold_id = $c->validation->param('hold_id');
270     my $hold    = Koha::Holds->find($hold_id);
271
272     unless ($hold) {
273         return $c->render( status => 404, openapi => { error => "Hold not found." } );
274     }
275
276     return try {
277         $hold->cancel;
278
279         return $c->render(
280             status  => 204,
281             openapi => q{}
282         );
283     }
284     catch {
285         $c->unhandled_exception($_);
286     };
287 }
288
289 =head3 suspend
290
291 Method that handles suspending a hold
292
293 =cut
294
295 sub suspend {
296     my $c = shift->openapi->valid_input or return;
297
298     my $hold_id  = $c->validation->param('hold_id');
299     my $hold     = Koha::Holds->find($hold_id);
300     my $body     = $c->req->json;
301     my $end_date = ($body) ? $body->{end_date} : undef;
302
303     unless ($hold) {
304         return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
305     }
306
307     return try {
308         my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
309         $hold->suspend_hold($date);
310         $hold->discard_changes;
311         $c->res->headers->location( $c->req->url->to_string );
312         my $suspend_end_date;
313         if ($hold->suspend_until) {
314             $suspend_end_date = output_pref({
315                 dt         => dt_from_string( $hold->suspend_until ),
316                 dateformat => 'rfc3339',
317                 dateonly   => 1
318                 }
319             );
320         }
321         return $c->render(
322             status  => 201,
323             openapi => {
324                 end_date => $suspend_end_date
325             }
326         );
327     }
328     catch {
329         if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
330             return $c->render( status => 400, openapi => { error => "$_" } );
331         }
332
333         $c->unhandled_exception($_);
334     };
335 }
336
337 =head3 resume
338
339 Method that handles resuming a hold
340
341 =cut
342
343 sub resume {
344     my $c = shift->openapi->valid_input or return;
345
346     my $hold_id = $c->validation->param('hold_id');
347     my $hold    = Koha::Holds->find($hold_id);
348     my $body    = $c->req->json;
349
350     unless ($hold) {
351         return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
352     }
353
354     return try {
355         $hold->resume;
356         return $c->render( status => 204, openapi => {} );
357     }
358     catch {
359         $c->unhandled_exception($_);
360     };
361 }
362
363 =head3 update_priority
364
365 Method that handles modifying a Koha::Hold object
366
367 =cut
368
369 sub update_priority {
370     my $c = shift->openapi->valid_input or return;
371
372     my $hold_id = $c->validation->param('hold_id');
373     my $hold = Koha::Holds->find($hold_id);
374
375     unless ($hold) {
376         return $c->render(
377             status  => 404,
378             openapi => { error => "Hold not found" }
379         );
380     }
381
382     return try {
383         my $priority = $c->req->json;
384         C4::Reserves::_FixPriority(
385             {
386                 reserve_id => $hold_id,
387                 rank       => $priority
388             }
389         );
390
391         return $c->render( status => 200, openapi => $priority );
392     }
393     catch {
394         $c->unhandled_exception($_);
395     };
396 }
397
398 =head3 pickup_locations
399
400 Method that returns the possible pickup_locations for a given hold
401 used for building the dropdown selector
402
403 =cut
404
405 sub pickup_locations {
406     my $c = shift->openapi->valid_input or return;
407
408     my $hold_id = $c->validation->param('hold_id');
409     my $hold = Koha::Holds->find($hold_id);
410
411     unless ($hold) {
412         return $c->render(
413             status  => 404,
414             openapi => { error => "Hold not found" }
415         );
416     }
417
418     return try {
419         my $pickup_locations = $hold->itemnumber ?
420         $hold->item->pickup_locations({ patron => $hold->patron }) : $hold->biblio->pickup_locations({ patron => $hold->patron });
421         warn Data::Dumper::Dumper( $pickup_locations );
422
423         return $c->render( status => 200, openapi => $pickup_locations );
424     }
425     catch {
426         $c->unhandled_exception($_);
427     };
428 }
429
430 1;