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