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);
187 return $c->render( status
=> 201, openapi
=> _to_api
($hold->TO_JSON) );
190 if ( blessed
$_ and $_->isa('Koha::Exceptions') ) {
191 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
192 my $broken_fk = $_->broken_fk;
194 if ( grep { $_ eq $broken_fk } keys %{$Koha::REST
::V1
::Holds
::to_api_mapping
} ) {
197 openapi
=> $Koha::REST
::V1
::Holds
::to_api_mapping
->{$broken_fk} . ' not found.'
203 openapi
=> { error
=> "Uncaught exception: $_" }
210 openapi
=> { error
=> "$_" }
217 openapi
=> { error
=> "Something went wrong. check the logs." }
225 Method that handles modifying a Koha::Hold object
230 my $c = shift->openapi->valid_input or return;
232 my $hold_id = $c->validation->param('hold_id');
233 my $hold = Koha
::Holds
->find( $hold_id );
236 return $c->render( status
=> 404,
237 openapi
=> {error
=> "Hold not found"} );
240 my $body = $c->req->json;
242 my $pickup_library_id = $body->{pickup_library_id
};
243 my $priority = $body->{priority
};
244 my $suspended_until = $body->{suspended_until
};
246 if ($suspended_until) {
247 $suspended_until = output_pref
(dt_from_string
($suspended_until, 'rfc3339'));
251 reserve_id
=> $hold_id,
252 branchcode
=> $pickup_library_id,
254 suspend_until
=> $suspended_until,
255 itemnumber
=> $hold->itemnumber
258 C4
::Reserves
::ModReserve
($params);
259 $hold->discard_changes; # refresh
261 return $c->render( status
=> 200, openapi
=> _to_api
( $hold->TO_JSON ) );
266 Method that handles deleting a Koha::Hold object
271 my $c = shift->openapi->valid_input or return;
273 my $hold_id = $c->validation->param('hold_id');
274 my $hold = Koha
::Holds
->find($hold_id);
277 return $c->render( status
=> 404, openapi
=> { error
=> "Hold not found." } );
282 return $c->render( status
=> 200, openapi
=> {} );
287 Method that handles suspending a hold
292 my $c = shift->openapi->valid_input or return;
294 my $hold_id = $c->validation->param('hold_id');
295 my $hold = Koha
::Holds
->find($hold_id);
296 my $body = $c->req->json;
297 my $end_date = ($body) ?
$body->{end_date
} : undef;
300 return $c->render( status
=> 404, openapi
=> { error
=> 'Hold not found.' } );
304 my $date = ($end_date) ? dt_from_string
( $end_date, 'rfc3339' ) : undef;
305 $hold->suspend_hold($date);
306 $hold->discard_changes;
307 $c->res->headers->location( $c->req->url->to_string );
311 end_date
=> output_pref
(
312 { dt
=> dt_from_string
( $hold->suspend_until ),
313 dateformat
=> 'rfc3339',
321 if ( blessed
$_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
322 return $c->render( status
=> 400, openapi
=> { error
=> "$_" } );
327 openapi
=> { error
=> "Something went wrong. check the logs." }
335 Method that handles resuming a hold
340 my $c = shift->openapi->valid_input or return;
342 my $hold_id = $c->validation->param('hold_id');
343 my $hold = Koha
::Holds
->find($hold_id);
344 my $body = $c->req->json;
347 return $c->render( status
=> 404, openapi
=> { error
=> 'Hold not found.' } );
352 return $c->render( status
=> 204, openapi
=> {} );
357 openapi
=> { error
=> "Something went wrong. check the logs." }
362 =head3 update_priority
364 Method that handles modifying a Koha::Hold object
368 sub update_priority
{
369 my $c = shift->openapi->valid_input or return;
371 my $hold_id = $c->validation->param('hold_id');
372 my $hold = Koha
::Holds
->find($hold_id);
377 openapi
=> { error
=> "Hold not found" }
382 my $priority = $c->req->json;
383 C4
::Reserves
::_FixPriority
(
385 reserve_id
=> $hold_id,
390 return $c->render( status
=> 200, openapi
=> $priority );
395 openapi
=> { error
=> "Something went wrong. check the logs." }
400 =head2 Internal methods
404 Helper function that maps unblessed Koha::Hold objects into REST api
413 foreach my $column ( keys %{ $Koha::REST
::V1
::Holds
::to_api_mapping
} ) {
414 my $mapped_column = $Koha::REST
::V1
::Holds
::to_api_mapping
->{$column};
415 if ( exists $hold->{ $column }
416 && defined $mapped_column )
419 $hold->{ $mapped_column } = delete $hold->{ $column };
421 elsif ( exists $hold->{ $column }
422 && !defined $mapped_column )
425 delete $hold->{ $column };
434 Helper function that maps REST api objects into Koha::Hold
442 foreach my $attribute ( keys %{ $Koha::REST
::V1
::Holds
::to_model_mapping
} ) {
443 my $mapped_attribute = $Koha::REST
::V1
::Holds
::to_model_mapping
->{$attribute};
444 if ( exists $hold->{ $attribute }
445 && defined $mapped_attribute )
448 $hold->{ $mapped_attribute } = delete $hold->{ $attribute };
450 elsif ( exists $hold->{ $attribute }
451 && !defined $mapped_attribute )
453 # key => undef / to be deleted
454 delete $hold->{ $attribute };
458 if ( exists $hold->{lowestPriority
} ) {
459 $hold->{lowestPriority
} = ($hold->{lowestPriority
}) ?
1 : 0;
462 if ( exists $hold->{suspend
} ) {
463 $hold->{suspend
} = ($hold->{suspend
}) ?
1 : 0;
466 if ( exists $hold->{reservedate
} ) {
467 $hold->{reservedate
} = output_pref
({ str
=> $hold->{reservedate
}, dateformat
=> 'sql' });
470 if ( exists $hold->{cancellationdate
} ) {
471 $hold->{cancellationdate
} = output_pref
({ str
=> $hold->{cancellationdate
}, dateformat
=> 'sql' });
474 if ( exists $hold->{timestamp
} ) {
475 $hold->{timestamp
} = output_pref
({ str
=> $hold->{timestamp
}, dateformat
=> 'sql' });
478 if ( exists $hold->{waitingdate
} ) {
479 $hold->{waitingdate
} = output_pref
({ str
=> $hold->{waitingdate
}, dateformat
=> 'sql' });
482 if ( exists $hold->{expirationdate
} ) {
483 $hold->{expirationdate
} = output_pref
({ str
=> $hold->{expirationdate
}, dateformat
=> 'sql' });
486 if ( exists $hold->{suspend_until
} ) {
487 $hold->{suspend_until
} = output_pref
({ str
=> $hold->{suspend_until
}, dateformat
=> 'sql' });
493 =head2 Global variables
495 =head3 $to_api_mapping
499 our $to_api_mapping = {
500 reserve_id
=> 'hold_id',
501 borrowernumber
=> 'patron_id',
502 reservedate
=> 'hold_date',
503 biblionumber
=> 'biblio_id',
504 branchcode
=> 'pickup_library_id',
505 notificationdate
=> undef,
506 reminderdate
=> undef,
507 cancellationdate
=> 'cancelation_date',
508 reservenotes
=> 'notes',
510 itemnumber
=> 'item_id',
511 waitingdate
=> 'waiting_date',
512 expirationdate
=> 'expiration_date',
513 lowestPriority
=> 'lowest_priority',
514 suspend
=> 'suspended',
515 suspend_until
=> 'suspended_until',
516 itemtype
=> 'item_type',
517 item_level_hold
=> 'item_level',
520 =head3 $to_model_mapping
524 our $to_model_mapping = {
525 hold_id
=> 'reserve_id',
526 patron_id
=> 'borrowernumber',
527 hold_date
=> 'reservedate',
528 biblio_id
=> 'biblionumber',
529 pickup_library_id
=> 'branchcode',
530 cancelation_date
=> 'cancellationdate',
531 notes
=> 'reservenotes',
533 item_id
=> 'itemnumber',
534 waiting_date
=> 'waitingdate',
535 expiration_date
=> 'expirationdate',
536 lowest_priority
=> 'lowestPriority',
537 suspended
=> 'suspend',
538 suspended_until
=> 'suspend_until',
539 item_type
=> 'itemtype',
540 item_level
=> 'item_level_hold',