Bug 24321: Clean /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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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         if ( blessed $_ && $_->isa('Koha::Exceptions') ) {
52             return $c->render(
53                 status  => 500,
54                 openapi => { error => "$_" }
55             );
56         }
57         else {
58             return $c->render(
59                 status  => 500,
60                 openapi => { error => "Something went wrong, check Koha logs for details." }
61             );
62         }
63     };
64 }
65
66 =head3 add
67
68 Method that handles adding a new Koha::Hold object
69
70 =cut
71
72 sub add {
73     my $c = shift->openapi->valid_input or return;
74
75     return try {
76         my $body = $c->validation->param('body');
77
78         my $biblio;
79
80         my $biblio_id         = $body->{biblio_id};
81         my $pickup_library_id = $body->{pickup_library_id};
82         my $item_id           = $body->{item_id};
83         my $patron_id         = $body->{patron_id};
84         my $item_type         = $body->{item_type};
85         my $expiration_date   = $body->{expiration_date};
86         my $notes             = $body->{notes};
87         my $hold_date         = $body->{hold_date};
88
89         if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
90             return $c->render(
91                 status  => 400,
92                 openapi => { error => "Hold date in future not allowed" }
93             );
94         }
95
96         if ( $item_id and $biblio_id ) {
97
98             # check they are consistent
99             unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
100                 ->count > 0 )
101             {
102                 return $c->render(
103                     status  => 400,
104                     openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
105                 );
106             }
107             else {
108                 $biblio = Koha::Biblios->find($biblio_id);
109             }
110         }
111         elsif ($item_id) {
112             my $item = Koha::Items->find($item_id);
113
114             unless ($item) {
115                 return $c->render(
116                     status  => 404,
117                     openapi => { error => "item_id not found." }
118                 );
119             }
120             else {
121                 $biblio = $item->biblio;
122             }
123         }
124         elsif ($biblio_id) {
125             $biblio = Koha::Biblios->find($biblio_id);
126         }
127         else {
128             return $c->render(
129                 status  => 400,
130                 openapi => { error => "At least one of biblio_id, item_id should be given" }
131             );
132         }
133
134         unless ($biblio) {
135             return $c->render(
136                 status  => 400,
137                 openapi => "Biblio not found."
138             );
139         }
140
141         my $can_place_hold
142             = $item_id
143             ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
144             : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
145
146         my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
147
148         unless ($can_override || $can_place_hold->{status} eq 'OK' ) {
149             return $c->render(
150                 status => 403,
151                 openapi =>
152                     { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
153             );
154         }
155
156         my $priority = C4::Reserves::CalculatePriority($biblio_id);
157
158         # AddReserve expects date to be in syspref format
159         if ($expiration_date) {
160             $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
161         }
162
163         my $hold_id = C4::Reserves::AddReserve(
164             $pickup_library_id,
165             $patron_id,
166             $biblio_id,
167             undef,    # $bibitems param is unused
168             $priority,
169             $hold_date,
170             $expiration_date,
171             $notes,
172             $biblio->title,
173             $item_id,
174             undef,    # TODO: Why not?
175             $item_type
176         );
177
178         unless ($hold_id) {
179             return $c->render(
180                 status  => 500,
181                 openapi => 'Error placing the hold. See Koha logs for details.'
182             );
183         }
184
185         my $hold = Koha::Holds->find($hold_id);
186
187         return $c->render(
188             status  => 201,
189             openapi => $hold->to_api
190         );
191     }
192     catch {
193         if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
194             if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
195                 my $broken_fk = $_->broken_fk;
196
197                 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
198                     $c->render(
199                         status  => 404,
200                         openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
201                     );
202                 }
203                 else {
204                     return $c->render(
205                         status  => 500,
206                         openapi => { error => "Uncaught exception: $_" }
207                     );
208                 }
209             }
210             else {
211                 return $c->render(
212                     status  => 500,
213                     openapi => { error => "$_" }
214                 );
215             }
216         }
217         else {
218             return $c->render(
219                 status  => 500,
220                 openapi => { error => "Something went wrong. check the logs." }
221             );
222         }
223     };
224 }
225
226 =head3 edit
227
228 Method that handles modifying a Koha::Hold object
229
230 =cut
231
232 sub edit {
233     my $c = shift->openapi->valid_input or return;
234
235     my $hold_id = $c->validation->param('hold_id');
236     my $hold = Koha::Holds->find( $hold_id );
237
238     unless ($hold) {
239         return $c->render( status  => 404,
240                            openapi => {error => "Hold not found"} );
241     }
242
243     my $body = $c->req->json;
244
245     my $pickup_library_id = $body->{pickup_library_id};
246     my $priority          = $body->{priority};
247     my $suspended_until   = $body->{suspended_until};
248
249     if ($suspended_until) {
250         $suspended_until = output_pref(dt_from_string($suspended_until, 'rfc3339'));
251     }
252
253     my $params = {
254         reserve_id    => $hold_id,
255         branchcode    => $pickup_library_id,
256         rank          => $priority,
257         suspend_until => $suspended_until,
258         itemnumber    => $hold->itemnumber
259     };
260
261     C4::Reserves::ModReserve($params);
262     $hold->discard_changes; # refresh
263
264     return $c->render(
265         status  => 200,
266         openapi => $hold->to_api
267     );
268 }
269
270 =head3 delete
271
272 Method that handles deleting a Koha::Hold object
273
274 =cut
275
276 sub delete {
277     my $c = shift->openapi->valid_input or return;
278
279     my $hold_id = $c->validation->param('hold_id');
280     my $hold    = Koha::Holds->find($hold_id);
281
282     unless ($hold) {
283         return $c->render( status => 404, openapi => { error => "Hold not found." } );
284     }
285
286     $hold->cancel;
287
288     return $c->render( status => 200, openapi => {} );
289 }
290
291 =head3 suspend
292
293 Method that handles suspending a hold
294
295 =cut
296
297 sub suspend {
298     my $c = shift->openapi->valid_input or return;
299
300     my $hold_id  = $c->validation->param('hold_id');
301     my $hold     = Koha::Holds->find($hold_id);
302     my $body     = $c->req->json;
303     my $end_date = ($body) ? $body->{end_date} : undef;
304
305     unless ($hold) {
306         return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
307     }
308
309     return try {
310         my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
311         $hold->suspend_hold($date);
312         $hold->discard_changes;
313         $c->res->headers->location( $c->req->url->to_string );
314         return $c->render(
315             status  => 201,
316             openapi => {
317                 end_date => output_pref(
318                     {   dt         => dt_from_string( $hold->suspend_until ),
319                         dateformat => 'rfc3339',
320                         dateonly   => 1
321                     }
322                 )
323             }
324         );
325     }
326     catch {
327         if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
328             return $c->render( status => 400, openapi => { error => "$_" } );
329         }
330         else {
331             return $c->render(
332                 status  => 500,
333                 openapi => { error => "Something went wrong. check the logs." }
334             );
335         }
336     };
337 }
338
339 =head3 resume
340
341 Method that handles resuming a hold
342
343 =cut
344
345 sub resume {
346     my $c = shift->openapi->valid_input or return;
347
348     my $hold_id = $c->validation->param('hold_id');
349     my $hold    = Koha::Holds->find($hold_id);
350     my $body    = $c->req->json;
351
352     unless ($hold) {
353         return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
354     }
355
356     return try {
357         $hold->resume;
358         return $c->render( status => 204, openapi => {} );
359     }
360     catch {
361         return $c->render(
362             status  => 500,
363             openapi => { error => "Something went wrong. check the logs." }
364         );
365     };
366 }
367
368 =head3 update_priority
369
370 Method that handles modifying a Koha::Hold object
371
372 =cut
373
374 sub update_priority {
375     my $c = shift->openapi->valid_input or return;
376
377     my $hold_id = $c->validation->param('hold_id');
378     my $hold = Koha::Holds->find($hold_id);
379
380     unless ($hold) {
381         return $c->render(
382             status  => 404,
383             openapi => { error => "Hold not found" }
384         );
385     }
386
387     return try {
388         my $priority = $c->req->json;
389         C4::Reserves::_FixPriority(
390             {
391                 reserve_id => $hold_id,
392                 rank       => $priority
393             }
394         );
395
396         return $c->render( status => 200, openapi => $priority );
397     }
398     catch {
399         return $c->render(
400             status  => 500,
401             openapi => { error => "Something went wrong. check the logs." }
402         );
403     };
404 }
405
406 1;