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};
87 my $hold_date = C4::Context->preference( 'AllowHoldDateInFuture' )?$body->{hold_date}:undef;
89 if ( $item_id and $biblio_id ) {
91 # check they are consistent
92 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
97 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
101 $biblio = Koha::Biblios->find($biblio_id);
105 my $item = Koha::Items->find($item_id);
110 openapi => { error => "item_id not found." }
114 $biblio = $item->biblio;
118 $biblio = Koha::Biblios->find($biblio_id);
123 openapi => { error => "At least one of biblio_id, item_id should be given" }
130 openapi => "Biblio not found."
136 ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
137 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
139 my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
141 unless ($can_override || $can_place_hold->{status} eq 'OK' ) {
145 { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
149 my $priority = C4::Reserves::CalculatePriority($biblio_id);
151 # AddReserve expects date to be in syspref format
152 if ($expiration_date) {
153 $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
156 my $hold_id = C4::Reserves::AddReserve(
160 undef, # $bibitems param is unused
167 undef, # TODO: Why not?
174 openapi => 'Error placing the hold. See Koha logs for details.'
178 my $hold = Koha::Holds->find($hold_id);
180 return $c->render( status => 201, openapi => _to_api($hold->TO_JSON) );
183 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
184 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
185 my $broken_fk = $_->broken_fk;
187 if ( grep { $_ eq $broken_fk } keys %{$Koha::REST::V1::Holds::to_api_mapping} ) {
190 openapi => $Koha::REST::V1::Holds::to_api_mapping->{$broken_fk} . ' not found.'
196 openapi => { error => "Uncaught exception: $_" }
203 openapi => { error => "$_" }
210 openapi => { error => "Something went wrong. check the logs." }
218 Method that handles modifying a Koha::Hold object
223 my $c = shift->openapi->valid_input or return;
225 my $hold_id = $c->validation->param('hold_id');
226 my $hold = Koha::Holds->find( $hold_id );
229 return $c->render( status => 404,
230 openapi => {error => "Hold not found"} );
233 my $body = $c->req->json;
235 my $pickup_library_id = $body->{pickup_library_id};
236 my $priority = $body->{priority};
237 my $suspended_until = $body->{suspended_until};
239 if ($suspended_until) {
240 $suspended_until = output_pref(dt_from_string($suspended_until, 'rfc3339'));
244 reserve_id => $hold_id,
245 branchcode => $pickup_library_id,
247 suspend_until => $suspended_until,
248 itemnumber => $hold->itemnumber
251 C4::Reserves::ModReserve($params);
252 $hold->discard_changes; # refresh
254 return $c->render( status => 200, openapi => _to_api( $hold->TO_JSON ) );
259 Method that handles deleting a Koha::Hold object
264 my $c = shift->openapi->valid_input or return;
266 my $hold_id = $c->validation->param('hold_id');
267 my $hold = Koha::Holds->find($hold_id);
270 return $c->render( status => 404, openapi => { error => "Hold not found." } );
275 return $c->render( status => 200, openapi => {} );
280 Method that handles suspending a hold
285 my $c = shift->openapi->valid_input or return;
287 my $hold_id = $c->validation->param('hold_id');
288 my $hold = Koha::Holds->find($hold_id);
289 my $body = $c->req->json;
290 my $end_date = ($body) ? $body->{end_date} : undef;
293 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
297 my $date = ($end_date) ? dt_from_string( $end_date, 'rfc3339' ) : undef;
298 $hold->suspend_hold($date);
299 $hold->discard_changes;
300 $c->res->headers->location( $c->req->url->to_string );
304 end_date => output_pref(
305 { dt => dt_from_string( $hold->suspend_until ),
306 dateformat => 'rfc3339',
314 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
315 return $c->render( status => 400, openapi => { error => "$_" } );
320 openapi => { error => "Something went wrong. check the logs." }
328 Method that handles resuming a hold
333 my $c = shift->openapi->valid_input or return;
335 my $hold_id = $c->validation->param('hold_id');
336 my $hold = Koha::Holds->find($hold_id);
337 my $body = $c->req->json;
340 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
345 return $c->render( status => 204, openapi => {} );
350 openapi => { error => "Something went wrong. check the logs." }
355 =head3 update_priority
357 Method that handles modifying a Koha::Hold object
361 sub update_priority {
362 my $c = shift->openapi->valid_input or return;
364 my $hold_id = $c->validation->param('hold_id');
365 my $hold = Koha::Holds->find($hold_id);
370 openapi => { error => "Hold not found" }
375 my $priority = $c->req->json;
376 C4::Reserves::_FixPriority(
378 reserve_id => $hold_id,
383 return $c->render( status => 200, openapi => $priority );
388 openapi => { error => "Something went wrong. check the logs." }
393 =head2 Internal methods
397 Helper function that maps unblessed Koha::Hold objects into REST api
406 foreach my $column ( keys %{ $Koha::REST::V1::Holds::to_api_mapping } ) {
407 my $mapped_column = $Koha::REST::V1::Holds::to_api_mapping->{$column};
408 if ( exists $hold->{ $column }
409 && defined $mapped_column )
412 $hold->{ $mapped_column } = delete $hold->{ $column };
414 elsif ( exists $hold->{ $column }
415 && !defined $mapped_column )
418 delete $hold->{ $column };
427 Helper function that maps REST api objects into Koha::Hold
435 foreach my $attribute ( keys %{ $Koha::REST::V1::Holds::to_model_mapping } ) {
436 my $mapped_attribute = $Koha::REST::V1::Holds::to_model_mapping->{$attribute};
437 if ( exists $hold->{ $attribute }
438 && defined $mapped_attribute )
441 $hold->{ $mapped_attribute } = delete $hold->{ $attribute };
443 elsif ( exists $hold->{ $attribute }
444 && !defined $mapped_attribute )
446 # key => undef / to be deleted
447 delete $hold->{ $attribute };
451 if ( exists $hold->{lowestPriority} ) {
452 $hold->{lowestPriority} = ($hold->{lowestPriority}) ? 1 : 0;
455 if ( exists $hold->{suspend} ) {
456 $hold->{suspend} = ($hold->{suspend}) ? 1 : 0;
459 if ( exists $hold->{reservedate} ) {
460 $hold->{reservedate} = output_pref({ str => $hold->{reservedate}, dateformat => 'sql' });
463 if ( exists $hold->{cancellationdate} ) {
464 $hold->{cancellationdate} = output_pref({ str => $hold->{cancellationdate}, dateformat => 'sql' });
467 if ( exists $hold->{timestamp} ) {
468 $hold->{timestamp} = output_pref({ str => $hold->{timestamp}, dateformat => 'sql' });
471 if ( exists $hold->{waitingdate} ) {
472 $hold->{waitingdate} = output_pref({ str => $hold->{waitingdate}, dateformat => 'sql' });
475 if ( exists $hold->{expirationdate} ) {
476 $hold->{expirationdate} = output_pref({ str => $hold->{expirationdate}, dateformat => 'sql' });
479 if ( exists $hold->{suspend_until} ) {
480 $hold->{suspend_until} = output_pref({ str => $hold->{suspend_until}, dateformat => 'sql' });
486 =head2 Global variables
488 =head3 $to_api_mapping
492 our $to_api_mapping = {
493 reserve_id => 'hold_id',
494 borrowernumber => 'patron_id',
495 reservedate => 'hold_date',
496 biblionumber => 'biblio_id',
497 branchcode => 'pickup_library_id',
498 notificationdate => undef,
499 reminderdate => undef,
500 cancellationdate => 'cancelation_date',
501 reservenotes => 'notes',
503 itemnumber => 'item_id',
504 waitingdate => 'waiting_date',
505 expirationdate => 'expiration_date',
506 lowestPriority => 'lowest_priority',
507 suspend => 'suspended',
508 suspend_until => 'suspended_until',
509 itemtype => 'item_type',
510 item_level_hold => 'item_level',
513 =head3 $to_model_mapping
517 our $to_model_mapping = {
518 hold_id => 'reserve_id',
519 patron_id => 'borrowernumber',
520 hold_date => 'reservedate',
521 biblio_id => 'biblionumber',
522 pickup_library_id => 'branchcode',
523 cancelation_date => 'cancellationdate',
524 notes => 'reservenotes',
526 item_id => 'itemnumber',
527 waiting_date => 'waitingdate',
528 expiration_date => 'expirationdate',
529 lowest_priority => 'lowestPriority',
530 suspended => 'suspend',
531 suspended_until => 'suspend_until',
532 item_type => 'itemtype',
533 item_level => 'item_level_hold',