1 package Koha::REST::V1::Holds;
3 # This file is part of Koha.
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
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.
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.
20 use Mojo::Base 'Mojolicious::Controller';
38 Mehtod that handles listing Koha::Hold objects
43 my $c = shift->openapi->valid_input or return;
46 my $holds_set = Koha::Holds->new;
47 my $holds = $c->objects->search( $holds_set, \&_to_model, \&_to_api );
48 return $c->render( status => 200, openapi => $holds );
51 if ( blessed $_ && $_->isa('Koha::Exceptions') ) {
54 openapi => { error => "$_" }
60 openapi => { error => "Something went wrong, check Koha logs for details." }
68 Method that handles adding a new Koha::Hold object
73 my $c = shift->openapi->valid_input or return;
76 my $body = $c->validation->param('body');
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};
88 if ( $item_id and $biblio_id ) {
90 # check they are consistent
91 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
96 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
100 $biblio = Koha::Biblios->find($biblio_id);
104 my $item = Koha::Items->find($item_id);
109 openapi => { error => "item_id not found." }
113 $biblio = $item->biblio;
117 $biblio = Koha::Biblios->find($biblio_id);
122 openapi => { error => "At least one of biblio_id, item_id should be given" }
129 openapi => "Biblio not found."
135 ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
136 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
138 unless ( $can_place_hold->{status} eq 'OK' ) {
142 { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
146 my $priority = C4::Reserves::CalculatePriority($biblio_id);
148 # AddReserve expects date to be in syspref format
149 if ($expiration_date) {
150 $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
153 my $hold_id = C4::Reserves::AddReserve(
157 undef, # $bibitems param is unused
159 undef, # hold date, we don't allow it currently
164 undef, # TODO: Why not?
171 openapi => 'Error placing the hold. See Koha logs for details.'
175 my $hold = Koha::Holds->find($hold_id);
177 return $c->render( status => 201, openapi => _to_api($hold->TO_JSON) );
180 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
181 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
182 my $broken_fk = $_->broken_fk;
184 if ( grep { $_ eq $broken_fk } keys %{$Koha::REST::V1::Holds::to_api_mapping} ) {
187 openapi => $Koha::REST::V1::Holds::to_api_mapping->{$broken_fk} . ' not found.'
193 openapi => { error => "Uncaught exception: $_" }
200 openapi => { error => "$_" }
207 openapi => { error => "Something went wrong. check the logs." }
215 Method that handles modifying a Koha::Hold object
220 my $c = shift->openapi->valid_input or return;
222 my $hold_id = $c->validation->param('hold_id');
223 my $hold = Koha::Holds->find( $hold_id );
226 return $c->render( status => 404,
227 openapi => {error => "Hold not found"} );
230 my $body = $c->req->json;
232 my $pickup_library_id = $body->{pickup_library_id};
233 my $priority = $body->{priority};
234 my $suspended_until = $body->{suspended_until};
236 if ($suspended_until) {
237 $suspended_until = output_pref(dt_from_string($suspended_until, 'rfc3339'));
241 reserve_id => $hold_id,
242 branchcode => $pickup_library_id,
244 suspend_until => $suspended_until,
245 itemnumber => $hold->itemnumber
248 C4::Reserves::ModReserve($params);
249 $hold->discard_changes; # refresh
251 return $c->render( status => 200, openapi => _to_api( $hold->TO_JSON ) );
256 Method that handles deleting a Koha::Hold object
261 my $c = shift->openapi->valid_input or return;
263 my $hold_id = $c->validation->param('hold_id');
264 my $hold = Koha::Holds->find($hold_id);
267 return $c->render( status => 404, openapi => { error => "Hold not found." } );
272 return $c->render( status => 200, openapi => {} );
277 Method that handles suspending a hold
282 my $c = shift->openapi->valid_input or return;
284 my $hold_id = $c->validation->param('hold_id');
285 my $hold = Koha::Holds->find($hold_id);
286 my $body = $c->req->json;
287 my $end_date = ($body) ? $body->{end_date} : undef;
290 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
294 my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
295 $hold->suspend_hold($date);
296 $hold->discard_changes;
297 $c->res->headers->location( $c->req->url->to_string );
301 end_date => output_pref(
302 { dt => dt_from_string( $hold->suspend_until ),
303 dateformat => 'rfc3339',
311 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
312 return $c->render( status => 400, openapi => { error => "$_" } );
317 openapi => { error => "Something went wrong. check the logs." }
325 Method that handles resuming a hold
330 my $c = shift->openapi->valid_input or return;
332 my $hold_id = $c->validation->param('hold_id');
333 my $hold = Koha::Holds->find($hold_id);
334 my $body = $c->req->json;
337 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
342 return $c->render( status => 204, openapi => {} );
347 openapi => { error => "Something went wrong. check the logs." }
352 =head3 update_priority
354 Method that handles modifying a Koha::Hold object
358 sub update_priority {
359 my $c = shift->openapi->valid_input or return;
361 my $hold_id = $c->validation->param('hold_id');
362 my $hold = Koha::Holds->find($hold_id);
367 openapi => { error => "Hold not found" }
372 my $priority = $c->req->json;
373 C4::Reserves::_FixPriority(
375 reserve_id => $hold_id,
380 return $c->render( status => 200, openapi => $priority );
385 openapi => { error => "Something went wrong. check the logs." }
390 =head2 Internal methods
394 Helper function that maps unblessed Koha::Hold objects into REST api
403 foreach my $column ( keys %{ $Koha::REST::V1::Holds::to_api_mapping } ) {
404 my $mapped_column = $Koha::REST::V1::Holds::to_api_mapping->{$column};
405 if ( exists $hold->{ $column }
406 && defined $mapped_column )
409 $hold->{ $mapped_column } = delete $hold->{ $column };
411 elsif ( exists $hold->{ $column }
412 && !defined $mapped_column )
415 delete $hold->{ $column };
424 Helper function that maps REST api objects into Koha::Hold
432 foreach my $attribute ( keys %{ $Koha::REST::V1::Holds::to_model_mapping } ) {
433 my $mapped_attribute = $Koha::REST::V1::Holds::to_model_mapping->{$attribute};
434 if ( exists $hold->{ $attribute }
435 && defined $mapped_attribute )
438 $hold->{ $mapped_attribute } = delete $hold->{ $attribute };
440 elsif ( exists $hold->{ $attribute }
441 && !defined $mapped_attribute )
443 # key => undef / to be deleted
444 delete $hold->{ $attribute };
448 if ( exists $hold->{lowestPriority} ) {
449 $hold->{lowestPriority} = ($hold->{lowestPriority}) ? 1 : 0;
452 if ( exists $hold->{suspend} ) {
453 $hold->{suspend} = ($hold->{suspend}) ? 1 : 0;
456 if ( exists $hold->{reservedate} ) {
457 $hold->{reservedate} = output_pref({ str => $hold->{reservedate}, dateformat => 'sql' });
460 if ( exists $hold->{cancellationdate} ) {
461 $hold->{cancellationdate} = output_pref({ str => $hold->{cancellationdate}, dateformat => 'sql' });
464 if ( exists $hold->{timestamp} ) {
465 $hold->{timestamp} = output_pref({ str => $hold->{timestamp}, dateformat => 'sql' });
468 if ( exists $hold->{waitingdate} ) {
469 $hold->{waitingdate} = output_pref({ str => $hold->{waitingdate}, dateformat => 'sql' });
472 if ( exists $hold->{expirationdate} ) {
473 $hold->{expirationdate} = output_pref({ str => $hold->{expirationdate}, dateformat => 'sql' });
476 if ( exists $hold->{suspend_until} ) {
477 $hold->{suspend_until} = output_pref({ str => $hold->{suspend_until}, dateformat => 'sql' });
483 =head2 Global variables
485 =head3 $to_api_mapping
489 our $to_api_mapping = {
490 reserve_id => 'hold_id',
491 borrowernumber => 'patron_id',
492 reservedate => 'hold_date',
493 biblionumber => 'biblio_id',
494 branchcode => 'pickup_library_id',
495 notificationdate => undef,
496 reminderdate => undef,
497 cancellationdate => 'cancelation_date',
498 reservenotes => 'notes',
500 itemnumber => 'item_id',
501 waitingdate => 'waiting_date',
502 expirationdate => 'expiration_date',
503 lowestPriority => 'lowest_priority',
504 suspend => 'suspended',
505 suspend_until => 'suspended_until',
506 itemtype => 'item_type',
507 item_level_hold => 'item_level',
510 =head3 $to_model_mapping
514 our $to_model_mapping = {
515 hold_id => 'reserve_id',
516 patron_id => 'borrowernumber',
517 hold_date => 'reservedate',
518 biblio_id => 'biblionumber',
519 pickup_library_id => 'branchcode',
520 cancelation_date => 'cancellationdate',
521 notes => 'reservenotes',
523 item_id => 'itemnumber',
524 waiting_date => 'waitingdate',
525 expiration_date => 'expirationdate',
526 lowest_priority => 'lowestPriority',
527 suspended => 'suspend',
528 suspended_until => 'suspend_until',
529 item_type => 'itemtype',
530 item_level => 'item_level_hold',