Bug 25032: Make existing controllers use unhandled_exception
[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
78         if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
79             return $c->render(
80                 status  => 400,
81                 openapi => { error => "Hold date in future not allowed" }
82             );
83         }
84
85         if ( $item_id and $biblio_id ) {
86
87             # check they are consistent
88             unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
89                 ->count > 0 )
90             {
91                 return $c->render(
92                     status  => 400,
93                     openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
94                 );
95             }
96             else {
97                 $biblio = Koha::Biblios->find($biblio_id);
98             }
99         }
100         elsif ($item_id) {
101             my $item = Koha::Items->find($item_id);
102
103             unless ($item) {
104                 return $c->render(
105                     status  => 404,
106                     openapi => { error => "item_id not found." }
107                 );
108             }
109             else {
110                 $biblio = $item->biblio;
111             }
112         }
113         elsif ($biblio_id) {
114             $biblio = Koha::Biblios->find($biblio_id);
115         }
116         else {
117             return $c->render(
118                 status  => 400,
119                 openapi => { error => "At least one of biblio_id, item_id should be given" }
120             );
121         }
122
123         unless ($biblio) {
124             return $c->render(
125                 status  => 400,
126                 openapi => "Biblio not found."
127             );
128         }
129
130         my $can_place_hold
131             = $item_id
132             ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
133             : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
134
135         my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
136
137         unless ($can_override || $can_place_hold->{status} eq 'OK' ) {
138             return $c->render(
139                 status => 403,
140                 openapi =>
141                     { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
142             );
143         }
144
145         my $priority = C4::Reserves::CalculatePriority($biblio_id);
146
147         # AddReserve expects date to be in syspref format
148         if ($expiration_date) {
149             $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
150         }
151
152         my $hold_id = C4::Reserves::AddReserve(
153             {
154                 branchcode       => $pickup_library_id,
155                 borrowernumber   => $patron_id,
156                 biblionumber     => $biblio_id,
157                 priority         => $priority,
158                 reservation_date => $hold_date,
159                 expiration_date  => $expiration_date,
160                 notes            => $notes,
161                 title            => $biblio->title,
162                 itemnumber       => $item_id,
163                 found            => undef,                # TODO: Why not?
164                 itemtype         => $item_type,
165             }
166         );
167
168         unless ($hold_id) {
169             return $c->render(
170                 status  => 500,
171                 openapi => 'Error placing the hold. See Koha logs for details.'
172             );
173         }
174
175         my $hold = Koha::Holds->find($hold_id);
176
177         return $c->render(
178             status  => 201,
179             openapi => $hold->to_api
180         );
181     }
182     catch {
183         if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
184             if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
185                 my $broken_fk = $_->broken_fk;
186
187                 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
188                     $c->render(
189                         status  => 404,
190                         openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
191                     );
192                 }
193             }
194         }
195
196         $c->unhandled_exception($_);
197     };
198 }
199
200 =head3 edit
201
202 Method that handles modifying a Koha::Hold object
203
204 =cut
205
206 sub edit {
207     my $c = shift->openapi->valid_input or return;
208
209     return try {
210         my $hold_id = $c->validation->param('hold_id');
211         my $hold = Koha::Holds->find( $hold_id );
212
213         unless ($hold) {
214             return $c->render( status  => 404,
215                             openapi => {error => "Hold not found"} );
216         }
217
218         my $body = $c->req->json;
219
220         my $pickup_library_id = $body->{pickup_library_id} // $hold->branchcode;
221         my $priority          = $body->{priority} // $hold->priority;
222         # suspended_until can also be set to undef
223         my $suspended_until   = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
224
225         my $params = {
226             reserve_id    => $hold_id,
227             branchcode    => $pickup_library_id,
228             rank          => $priority,
229             suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
230             itemnumber    => $hold->itemnumber
231         };
232
233         C4::Reserves::ModReserve($params);
234         $hold->discard_changes; # refresh
235
236         return $c->render(
237             status  => 200,
238             openapi => $hold->to_api
239         );
240     }
241     catch {
242         $c->unhandled_exception($_);
243     };
244 }
245
246 =head3 delete
247
248 Method that handles deleting a Koha::Hold object
249
250 =cut
251
252 sub delete {
253     my $c = shift->openapi->valid_input or return;
254
255     my $hold_id = $c->validation->param('hold_id');
256     my $hold    = Koha::Holds->find($hold_id);
257
258     unless ($hold) {
259         return $c->render( status => 404, openapi => { error => "Hold not found." } );
260     }
261
262     return try {
263         $hold->cancel;
264
265         return $c->render( status => 200, openapi => {} );
266     }
267     catch {
268         $c->unhandled_exception($_);
269     };
270 }
271
272 =head3 suspend
273
274 Method that handles suspending a hold
275
276 =cut
277
278 sub suspend {
279     my $c = shift->openapi->valid_input or return;
280
281     my $hold_id  = $c->validation->param('hold_id');
282     my $hold     = Koha::Holds->find($hold_id);
283     my $body     = $c->req->json;
284     my $end_date = ($body) ? $body->{end_date} : undef;
285
286     unless ($hold) {
287         return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
288     }
289
290     return try {
291         my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
292         $hold->suspend_hold($date);
293         $hold->discard_changes;
294         $c->res->headers->location( $c->req->url->to_string );
295         my $suspend_end_date;
296         if ($hold->suspend_until) {
297             $suspend_end_date = output_pref({
298                 dt         => dt_from_string( $hold->suspend_until ),
299                 dateformat => 'rfc3339',
300                 dateonly   => 1
301                 }
302             );
303         }
304         return $c->render(
305             status  => 201,
306             openapi => {
307                 end_date => $suspend_end_date
308             }
309         );
310     }
311     catch {
312         if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
313             return $c->render( status => 400, openapi => { error => "$_" } );
314         }
315
316         $c->unhandled_exception($_);
317     };
318 }
319
320 =head3 resume
321
322 Method that handles resuming a hold
323
324 =cut
325
326 sub resume {
327     my $c = shift->openapi->valid_input or return;
328
329     my $hold_id = $c->validation->param('hold_id');
330     my $hold    = Koha::Holds->find($hold_id);
331     my $body    = $c->req->json;
332
333     unless ($hold) {
334         return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
335     }
336
337     return try {
338         $hold->resume;
339         return $c->render( status => 204, openapi => {} );
340     }
341     catch {
342         $c->unhandled_exception($_);
343     };
344 }
345
346 =head3 update_priority
347
348 Method that handles modifying a Koha::Hold object
349
350 =cut
351
352 sub update_priority {
353     my $c = shift->openapi->valid_input or return;
354
355     my $hold_id = $c->validation->param('hold_id');
356     my $hold = Koha::Holds->find($hold_id);
357
358     unless ($hold) {
359         return $c->render(
360             status  => 404,
361             openapi => { error => "Hold not found" }
362         );
363     }
364
365     return try {
366         my $priority = $c->req->json;
367         C4::Reserves::_FixPriority(
368             {
369                 reserve_id => $hold_id,
370                 rank       => $priority
371             }
372         );
373
374         return $c->render( status => 200, openapi => $priority );
375     }
376     catch {
377         $c->unhandled_exception($_);
378     };
379 }
380
381 1;