Bug 23710: (follow-up) Add tests for new features in Koha::REST::V!::Holds::add and...
[koha.git] / Koha / REST / V1 / Holds.pm
blob00faf2ffed6a50b6fb91ffc3488a4d22301172e3
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
8 # version.
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.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Controller';
22 use C4::Biblio;
23 use C4::Reserves;
25 use Koha::Items;
26 use Koha::Patrons;
27 use Koha::Holds;
28 use Koha::DateUtils;
30 use Try::Tiny;
32 =head1 API
34 =head2 Class methods
36 =head3 list
38 Mehtod that handles listing Koha::Hold objects
40 =cut
42 sub list {
43 my $c = shift->openapi->valid_input or return;
45 return try {
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 );
50 catch {
51 if ( blessed $_ && $_->isa('Koha::Exceptions') ) {
52 return $c->render(
53 status => 500,
54 openapi => { error => "$_" }
57 else {
58 return $c->render(
59 status => 500,
60 openapi => { error => "Something went wrong, check Koha logs for details." }
66 =head3 add
68 Method that handles adding a new Koha::Hold object
70 =cut
72 sub add {
73 my $c = shift->openapi->valid_input or return;
75 return try {
76 my $body = $c->validation->param('body');
78 my $biblio;
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) {
90 return $c->render(
91 status => 400,
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 } )
100 ->count > 0 )
102 return $c->render(
103 status => 400,
104 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
107 else {
108 $biblio = Koha::Biblios->find($biblio_id);
111 elsif ($item_id) {
112 my $item = Koha::Items->find($item_id);
114 unless ($item) {
115 return $c->render(
116 status => 404,
117 openapi => { error => "item_id not found." }
120 else {
121 $biblio = $item->biblio;
124 elsif ($biblio_id) {
125 $biblio = Koha::Biblios->find($biblio_id);
127 else {
128 return $c->render(
129 status => 400,
130 openapi => { error => "At least one of biblio_id, item_id should be given" }
134 unless ($biblio) {
135 return $c->render(
136 status => 400,
137 openapi => "Biblio not found."
141 my $can_place_hold
142 = $item_id
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' ) {
149 return $c->render(
150 status => 403,
151 openapi =>
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(
164 $pickup_library_id,
165 $patron_id,
166 $biblio_id,
167 undef, # $bibitems param is unused
168 $priority,
169 $hold_date,
170 $expiration_date,
171 $notes,
172 $biblio->title,
173 $item_id,
174 undef, # TODO: Why not?
175 $item_type
178 unless ($hold_id) {
179 return $c->render(
180 status => 500,
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) );
189 catch {
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} ) {
195 $c->render(
196 status => 404,
197 openapi => $Koha::REST::V1::Holds::to_api_mapping->{$broken_fk} . ' not found.'
200 else {
201 return $c->render(
202 status => 500,
203 openapi => { error => "Uncaught exception: $_" }
207 else {
208 return $c->render(
209 status => 500,
210 openapi => { error => "$_" }
214 else {
215 return $c->render(
216 status => 500,
217 openapi => { error => "Something went wrong. check the logs." }
223 =head3 edit
225 Method that handles modifying a Koha::Hold object
227 =cut
229 sub edit {
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 );
235 unless ($hold) {
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'));
250 my $params = {
251 reserve_id => $hold_id,
252 branchcode => $pickup_library_id,
253 rank => $priority,
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 ) );
264 =head3 delete
266 Method that handles deleting a Koha::Hold object
268 =cut
270 sub delete {
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);
276 unless ($hold) {
277 return $c->render( status => 404, openapi => { error => "Hold not found." } );
280 $hold->cancel;
282 return $c->render( status => 200, openapi => {} );
285 =head3 suspend
287 Method that handles suspending a hold
289 =cut
291 sub suspend {
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;
299 unless ($hold) {
300 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
303 return try {
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 );
308 return $c->render(
309 status => 201,
310 openapi => {
311 end_date => output_pref(
312 { dt => dt_from_string( $hold->suspend_until ),
313 dateformat => 'rfc3339',
314 dateonly => 1
320 catch {
321 if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::CannotSuspendFound') ) {
322 return $c->render( status => 400, openapi => { error => "$_" } );
324 else {
325 return $c->render(
326 status => 500,
327 openapi => { error => "Something went wrong. check the logs." }
333 =head3 resume
335 Method that handles resuming a hold
337 =cut
339 sub resume {
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;
346 unless ($hold) {
347 return $c->render( status => 404, openapi => { error => 'Hold not found.' } );
350 return try {
351 $hold->resume;
352 return $c->render( status => 204, openapi => {} );
354 catch {
355 return $c->render(
356 status => 500,
357 openapi => { error => "Something went wrong. check the logs." }
362 =head3 update_priority
364 Method that handles modifying a Koha::Hold object
366 =cut
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);
374 unless ($hold) {
375 return $c->render(
376 status => 404,
377 openapi => { error => "Hold not found" }
381 return try {
382 my $priority = $c->req->json;
383 C4::Reserves::_FixPriority(
385 reserve_id => $hold_id,
386 rank => $priority
390 return $c->render( status => 200, openapi => $priority );
392 catch {
393 return $c->render(
394 status => 500,
395 openapi => { error => "Something went wrong. check the logs." }
400 =head2 Internal methods
402 =head3 _to_api
404 Helper function that maps unblessed Koha::Hold objects into REST api
405 attribute names.
407 =cut
409 sub _to_api {
410 my $hold = shift;
412 # Rename attributes
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 )
418 # key != undef
419 $hold->{ $mapped_column } = delete $hold->{ $column };
421 elsif ( exists $hold->{ $column }
422 && !defined $mapped_column )
424 # key == undef
425 delete $hold->{ $column };
429 return $hold;
432 =head3 _to_model
434 Helper function that maps REST api objects into Koha::Hold
435 attribute names.
437 =cut
439 sub _to_model {
440 my $hold = shift;
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 )
447 # key => !undef
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' });
490 return $hold;
493 =head2 Global variables
495 =head3 $to_api_mapping
497 =cut
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',
509 found => 'status',
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
522 =cut
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',
532 status => 'found',
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',