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 = $body->{hold_date};
89 if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
92 openapi => { error => "Hold date in future not allowed" }
96 if ( $item_id and $biblio_id ) {
98 # check they are consistent
99 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
104 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
108 $biblio = Koha::Biblios->find($biblio_id);
112 my $item = Koha::Items->find($item_id);
117 openapi => { error => "item_id not found." }
121 $biblio = $item->biblio;
125 $biblio = Koha::Biblios->find($biblio_id);
130 openapi => { error => "At least one of biblio_id, item_id should be given" }
137 openapi => "Biblio not found."
143 ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
144 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
146 my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
148 unless ($can_override || $can_place_hold->{status} eq 'OK' ) {
152 { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
156 my $priority = C4::Reserves::CalculatePriority($biblio_id);
158 # AddReserve expects date to be in syspref format
159 if ($expiration_date) {
160 $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
163 my $hold_id = C4::Reserves::AddReserve(
167 undef, # $bibitems param is unused
174 undef, # TODO: Why not?
181 openapi => 'Error placing the hold. See Koha logs for details.'
185 my $hold = Koha::Holds->find($hold_id);
189 openapi => $hold->to_api
193 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
194 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
195 my $broken_fk = $_->broken_fk;
197 if ( grep { $_ eq $broken_fk } keys %{$Koha::REST::V1::Holds::to_api_mapping} ) {
200 openapi => $Koha::REST::V1::Holds::to_api_mapping->{$broken_fk} . ' not found.'
206 openapi => { error => "Uncaught exception: $_" }
213 openapi => { error => "$_" }
220 openapi => { error => "Something went wrong. check the logs." }
228 Method that handles modifying a Koha::Hold object
233 my $c = shift->openapi->valid_input or return;
235 my $hold_id = $c->validation->param('hold_id');
236 my $hold = Koha::Holds->find( $hold_id );
239 return $c->render( status => 404,
240 openapi => {error => "Hold not found"} );
243 my $body = $c->req->json;
245 my $pickup_library_id = $body->{pickup_library_id};
246 my $priority = $body->{priority};
247 my $suspended_until = $body->{suspended_until};
249 if ($suspended_until) {
250 $suspended_until = output_pref(dt_from_string($suspended_until, 'rfc3339'));
254 reserve_id => $hold_id,
255 branchcode => $pickup_library_id,
257 suspend_until => $suspended_until,
258 itemnumber => $hold->itemnumber
261 C4::Reserves::ModReserve($params);
262 $hold->discard_changes; # refresh
266 openapi => $hold->to_api
272 Method that handles deleting a Koha::Hold object
277 my $c = shift->openapi->valid_input or return;
279 my $hold_id = $c->validation->param('hold_id');
280 my $hold = Koha::Holds->find($hold_id);
283 return $c->render( status => 404, openapi => { error => "Hold not found." } );
288 return $c->render( status => 200, openapi => {} );
293 Method that handles suspending a hold
298 my $c = shift->openapi->valid_input or return;
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;
306 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
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 );
317 end_date => output_pref(
318 { dt => dt_from_string( $hold->suspend_until ),
319 dateformat => 'rfc3339',
327 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
328 return $c->render( status => 400, openapi => { error => "$_" } );
333 openapi => { error => "Something went wrong. check the logs." }
341 Method that handles resuming a hold
346 my $c = shift->openapi->valid_input or return;
348 my $hold_id = $c->validation->param('hold_id');
349 my $hold = Koha::Holds->find($hold_id);
350 my $body = $c->req->json;
353 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
358 return $c->render( status => 204, openapi => {} );
363 openapi => { error => "Something went wrong. check the logs." }
368 =head3 update_priority
370 Method that handles modifying a Koha::Hold object
374 sub update_priority {
375 my $c = shift->openapi->valid_input or return;
377 my $hold_id = $c->validation->param('hold_id');
378 my $hold = Koha::Holds->find($hold_id);
383 openapi => { error => "Hold not found" }
388 my $priority = $c->req->json;
389 C4::Reserves::_FixPriority(
391 reserve_id => $hold_id,
396 return $c->render( status => 200, openapi => $priority );
401 openapi => { error => "Something went wrong. check the logs." }
406 =head2 Internal methods
410 Helper function that maps unblessed Koha::Hold objects into REST api
419 foreach my $column ( keys %{ $Koha::REST::V1::Holds::to_api_mapping } ) {
420 my $mapped_column = $Koha::REST::V1::Holds::to_api_mapping->{$column};
421 if ( exists $hold->{ $column }
422 && defined $mapped_column )
425 $hold->{ $mapped_column } = delete $hold->{ $column };
427 elsif ( exists $hold->{ $column }
428 && !defined $mapped_column )
431 delete $hold->{ $column };
440 Helper function that maps REST api objects into Koha::Hold
448 foreach my $attribute ( keys %{ $Koha::REST::V1::Holds::to_model_mapping } ) {
449 my $mapped_attribute = $Koha::REST::V1::Holds::to_model_mapping->{$attribute};
450 if ( exists $hold->{ $attribute }
451 && defined $mapped_attribute )
454 $hold->{ $mapped_attribute } = delete $hold->{ $attribute };
456 elsif ( exists $hold->{ $attribute }
457 && !defined $mapped_attribute )
459 # key => undef / to be deleted
460 delete $hold->{ $attribute };
464 if ( exists $hold->{lowestPriority} ) {
465 $hold->{lowestPriority} = ($hold->{lowestPriority}) ? 1 : 0;
468 if ( exists $hold->{suspend} ) {
469 $hold->{suspend} = ($hold->{suspend}) ? 1 : 0;
472 if ( exists $hold->{reservedate} ) {
473 $hold->{reservedate} = output_pref({ str => $hold->{reservedate}, dateformat => 'sql' });
476 if ( exists $hold->{cancellationdate} ) {
477 $hold->{cancellationdate} = output_pref({ str => $hold->{cancellationdate}, dateformat => 'sql' });
480 if ( exists $hold->{timestamp} ) {
481 $hold->{timestamp} = output_pref({ str => $hold->{timestamp}, dateformat => 'sql' });
484 if ( exists $hold->{waitingdate} ) {
485 $hold->{waitingdate} = output_pref({ str => $hold->{waitingdate}, dateformat => 'sql' });
488 if ( exists $hold->{expirationdate} ) {
489 $hold->{expirationdate} = output_pref({ str => $hold->{expirationdate}, dateformat => 'sql' });
492 if ( exists $hold->{suspend_until} ) {
493 $hold->{suspend_until} = output_pref({ str => $hold->{suspend_until}, dateformat => 'sql' });
499 =head2 Global variables
501 =head3 $to_api_mapping
505 our $to_api_mapping = {
506 reserve_id => 'hold_id',
507 borrowernumber => 'patron_id',
508 reservedate => 'hold_date',
509 biblionumber => 'biblio_id',
510 branchcode => 'pickup_library_id',
511 notificationdate => undef,
512 reminderdate => undef,
513 cancellationdate => 'cancelation_date',
514 reservenotes => 'notes',
516 itemnumber => 'item_id',
517 waitingdate => 'waiting_date',
518 expirationdate => 'expiration_date',
519 lowestPriority => 'lowest_priority',
520 suspend => 'suspended',
521 suspend_until => 'suspended_until',
522 itemtype => 'item_type',
523 item_level_hold => 'item_level',
526 =head3 $to_model_mapping
530 our $to_model_mapping = {
531 hold_id => 'reserve_id',
532 patron_id => 'borrowernumber',
533 hold_date => 'reservedate',
534 biblio_id => 'biblionumber',
535 pickup_library_id => 'branchcode',
536 cancelation_date => 'cancellationdate',
537 notes => 'reservenotes',
539 item_id => 'itemnumber',
540 waiting_date => 'waitingdate',
541 expiration_date => 'expirationdate',
542 lowest_priority => 'lowestPriority',
543 suspended => 'suspend',
544 suspended_until => 'suspend_until',
545 item_type => 'itemtype',
546 item_level => 'item_level_hold',