From 770e263b870377e03100d5ec232fa98e6d5153f2 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 23 Dec 2021 11:47:19 -0300 Subject: [PATCH] Bug 29765: Adjust DELETE /patrons/:patron_id controller This patch adjusts the only current place using the Koha::Patron->safe_to_delete method to the new return value. There should be no behavior change To test: 1. Apply the previous patches 2. Run: $ kshell k$ prove t/db_dependent/api/v1/patrons.t => FAIL: Tests fail, because of the change, the controller is broken 3. Apply this patch 4. Repeat 2 => SUCCESS: Tests pass! No test tweak required, no behavior change 5. Sign off :-D Signed-off-by: David Nind Signed-off-by: Jonathan Druart Signed-off-by: Fridolin Somers --- Koha/REST/V1/Patrons.pm | 81 +++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 39 deletions(-) diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm index 122b99a2ce..469d9536b4 100644 --- a/Koha/REST/V1/Patrons.pm +++ b/Koha/REST/V1/Patrons.pm @@ -333,46 +333,49 @@ sub delete { my $safe_to_delete = $patron->safe_to_delete; - if ( $safe_to_delete eq 'ok' ) { - $patron->_result->result_source->schema->txn_do( - sub { - $patron->move_to_deleted; - $patron->delete; - - return $c->render( - status => 204, - openapi => q{} - ); - } - ); - } - elsif ( $safe_to_delete eq 'has_checkouts' ) { - return $c->render( - status => 409, - openapi => { error => 'Pending checkouts prevent deletion' } - ); - } - elsif ( $safe_to_delete eq 'has_debt' ) { - return $c->render( - status => 409, - openapi => { error => 'Pending debts prevent deletion' } - ); - } - elsif ( $safe_to_delete eq 'has_guarantees' ) { - return $c->render( - status => 409, - openapi => { error => 'Patron is a guarantor and it prevents deletion' } - ); - } - elsif ( $safe_to_delete eq 'is_anonymous_patron' ) { - return $c->render( - status => 403, - openapi => { error => 'Anonymous patron cannot be deleted' } - ); - } - else { - Koha::Exceptions::Exception->throw( "Koha::Patron->safe_to_delete returned an unexpected value: $safe_to_delete" ); + if ( !$safe_to_delete ) { + # Pick the first error, if any + my ( $error ) = grep { $_->type eq 'error' } @{ $safe_to_delete->messages }; + unless ( $error ) { + Koha::Exceptions::Exception->throw('Koha::Patron->safe_to_delete returned false but carried no error message'); + } + + if ( $error->message eq 'has_checkouts' ) { + return $c->render( + status => 409, + openapi => { error => 'Pending checkouts prevent deletion' } + ); + } elsif ( $error->message eq 'has_debt' ) { + return $c->render( + status => 409, + openapi => { error => 'Pending debts prevent deletion' } + ); + } elsif ( $error->message eq 'has_guarantees' ) { + return $c->render( + status => 409, + openapi => { error => 'Patron is a guarantor and it prevents deletion' } + ); + } elsif ( $error->message eq 'is_anonymous_patron' ) { + return $c->render( + status => 403, + openapi => { error => 'Anonymous patron cannot be deleted' } + ); + } else { + Koha::Exceptions::Exception->throw( 'Koha::Patron->safe_to_delete carried an unexpected message: ' . $error->message ); + } } + + return $patron->_result->result_source->schema->txn_do( + sub { + $patron->move_to_deleted; + $patron->delete; + + return $c->render( + status => 204, + openapi => q{} + ); + } + ); } catch { if ( blessed $_ && $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) { return $c->render( -- 2.39.5