Bug 25662: Make the route for holds restpect maxreserves
authorTomas Cohen Arazi <tomascohen@theke.io>
Tue, 23 Jun 2020 13:33:14 +0000 (10:33 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 25 Aug 2020 13:07:27 +0000 (15:07 +0200)
This patch fixes the behaviour for the POST /holds route. It assumed
maxreserves was checked in CanItemBeReserved which is not the case.

Tests are added to check for this behaviour.

To test:
1. Apply the regression tests
2. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/holds.t
=> FAIL: Tests fail!
3. Apply this patch
4. Repeat 2
=> SUCCESS: Tests pass!
5. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Koha/REST/V1/Holds.pm

index 87d7c05..09b7b7e 100644 (file)
@@ -127,11 +127,23 @@ sub add {
             );
         }
 
+        my $patron = Koha::Patrons->find( $patron_id );
+        unless ($patron) {
+            return $c->render(
+                status  => 400,
+                openapi => { error => 'patron_id not found' }
+            );
+        }
+
         my $can_place_hold
             = $item_id
             ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
             : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
 
+        if ( $patron->holds->count + 1 > C4::Context->preference('maxreserves') ) {
+            $can_place_hold->{status} = 'tooManyReserves';
+        }
+
         my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
 
         unless ($can_override || $can_place_hold->{status} eq 'OK' ) {