Bug 25662: Make the route for holds restpect maxreserves
[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 $patron = Koha::Patrons->find( $patron_id );
131         unless ($patron) {
132             return $c->render(
133                 status  => 400,
134                 openapi => { error => 'patron_id not found' }
135             );
136         }
137
138         my $can_place_hold
139             = $item_id
140             ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
141             : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
142
143         if ( $patron->holds->count + 1 > C4::Context->preference('maxreserves') ) {
144             $can_place_hold->{status} = 'tooManyReserves';
145         }
146
147         my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
148
149         unless ($can_override || $can_place_hold->{status} eq 'OK' ) {
150             return $c->render(
151                 status => 403,
152                 openapi =>
153                     { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
154             );
155         }
156
157         my $priority = C4::Reserves::CalculatePriority($biblio_id);
158
159         # AddReserve expects date to be in syspref format
160         if ($expiration_date) {
161             $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
162         }
163
164         my $hold_id = C4::Reserves::AddReserve(
165             {
166                 branchcode       => $pickup_library_id,
167                 borrowernumber   => $patron_id,
168                 biblionumber     => $biblio_id,
169                 priority         => $priority,
170                 reservation_date => $hold_date,
171                 expiration_date  => $expiration_date,
172                 notes            => $notes,
173                 title            => $biblio->title,
174                 itemnumber       => $item_id,
175                 found            => undef,                # TODO: Why not?
176                 itemtype         => $item_type,
177             }
178         );
179
180         unless ($hold_id) {
181             return $c->render(
182                 status  => 500,
183                 openapi => 'Error placing the hold. See Koha logs for details.'
184             );
185         }
186
187         my $hold = Koha::Holds->find($hold_id);
188
189         return $c->render(
190             status  => 201,
191             openapi => $hold->to_api
192         );
193     }
194     catch {
195         if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
196             if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
197                 my $broken_fk = $_->broken_fk;
198
199                 if ( grep { $_ eq $broken_fk } keys %{Koha::Holds->new->to_api_mapping} ) {
200                     $c->render(
201                         status  => 404,
202                         openapi => Koha::Holds->new->to_api_mapping->{$broken_fk} . ' not found.'
203                     );
204                 }
205             }
206         }
207
208         $c->unhandled_exception($_);
209     };
210 }
211
212 =head3 edit
213
214 Method that handles modifying a Koha::Hold object
215
216 =cut
217
218 sub edit {
219     my $c = shift->openapi->valid_input or return;
220
221     return try {
222         my $hold_id = $c->validation->param('hold_id');
223         my $hold = Koha::Holds->find( $hold_id );
224
225         unless ($hold) {
226             return $c->render( status  => 404,
227                             openapi => {error => "Hold not found"} );
228         }
229
230         my $body = $c->req->json;
231
232         my $pickup_library_id = $body->{pickup_library_id} // $hold->branchcode;
233         my $priority          = $body->{priority} // $hold->priority;
234         # suspended_until can also be set to undef
235         my $suspended_until   = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
236
237         my $params = {
238             reserve_id    => $hold_id,
239             branchcode    => $pickup_library_id,
240             rank          => $priority,
241             suspend_until => $suspended_until ? output_pref(dt_from_string($suspended_until, 'rfc3339')) : '',
242             itemnumber    => $hold->itemnumber
243         };
244
245         C4::Reserves::ModReserve($params);
246         $hold->discard_changes; # refresh
247
248         return $c->render(
249             status  => 200,
250             openapi => $hold->to_api
251         );
252     }
253     catch {
254         $c->unhandled_exception($_);
255     };
256 }
257
258 =head3 delete
259
260 Method that handles deleting a Koha::Hold object
261
262 =cut
263
264 sub delete {
265     my $c = shift->openapi->valid_input or return;
266
267     my $hold_id = $c->validation->param('hold_id');
268     my $hold    = Koha::Holds->find($hold_id);
269
270     unless ($hold) {
271         return $c->render( status => 404, openapi => { error => "Hold not found." } );
272     }
273
274     return try {
275         $hold->cancel;
276
277         return $c->render(
278             status  => 204,
279             openapi => q{}
280         );
281     }
282     catch {
283         $c->unhandled_exception($_);
284     };
285 }
286
287 =head3 suspend
288
289 Method that handles suspending a hold
290
291 =cut
292
293 sub suspend {
294     my $c = shift->openapi->valid_input or return;
295
296     my $hold_id  = $c->validation->param('hold_id');
297     my $hold     = Koha::Holds->find($hold_id);
298     my $body     = $c->req->json;
299     my $end_date = ($body) ? $body->{end_date} : undef;
300
301     unless ($hold) {
302         return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
303     }
304
305     return try {
306         my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
307         $hold->suspend_hold($date);
308         $hold->discard_changes;
309         $c->res->headers->location( $c->req->url->to_string );
310         my $suspend_end_date;
311         if ($hold->suspend_until) {
312             $suspend_end_date = output_pref({
313                 dt         => dt_from_string( $hold->suspend_until ),
314                 dateformat => 'rfc3339',
315                 dateonly   => 1
316                 }
317             );
318         }
319         return $c->render(
320             status  => 201,
321             openapi => {
322                 end_date => $suspend_end_date
323             }
324         );
325     }
326     catch {
327         if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
328             return $c->render( status => 400, openapi => { error => "$_" } );
329         }
330
331         $c->unhandled_exception($_);
332     };
333 }
334
335 =head3 resume
336
337 Method that handles resuming a hold
338
339 =cut
340
341 sub resume {
342     my $c = shift->openapi->valid_input or return;
343
344     my $hold_id = $c->validation->param('hold_id');
345     my $hold    = Koha::Holds->find($hold_id);
346     my $body    = $c->req->json;
347
348     unless ($hold) {
349         return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
350     }
351
352     return try {
353         $hold->resume;
354         return $c->render( status => 204, openapi => {} );
355     }
356     catch {
357         $c->unhandled_exception($_);
358     };
359 }
360
361 =head3 update_priority
362
363 Method that handles modifying a Koha::Hold object
364
365 =cut
366
367 sub update_priority {
368     my $c = shift->openapi->valid_input or return;
369
370     my $hold_id = $c->validation->param('hold_id');
371     my $hold = Koha::Holds->find($hold_id);
372
373     unless ($hold) {
374         return $c->render(
375             status  => 404,
376             openapi => { error => "Hold not found" }
377         );
378     }
379
380     return try {
381         my $priority = $c->req->json;
382         C4::Reserves::_FixPriority(
383             {
384                 reserve_id => $hold_id,
385                 rank       => $priority
386             }
387         );
388
389         return $c->render( status => 200, openapi => $priority );
390     }
391     catch {
392         $c->unhandled_exception($_);
393     };
394 }
395
396 1;