From 0e23a877694cff38e48422e26d4bdb8047c0226a Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 20 Dec 2021 12:05:59 -0300 Subject: [PATCH] Bug 29018: Make DELETE /patrons/:patron_id check things When the route was implemented, the checks were overlooked. This patch adds checks for: - Guarantees - Debts - Current checkouts Any of those will block deletion, as it should. To test: 1. Apply the regression tests patch 2. Run: $ kshell k$ prove t/db_dependent/api/v1/patrons.t => FAIL: Tests fail, the route misses checks 3. Apply this patch 4. Repeat 2 => SUCCESS: Tests pass! The three conditions prevent deletion! 5. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Nind Signed-off-by: Jonathan Druart Signed-off-by: Fridolin Somers Signed-off-by: Kyle M Hall --- Koha/REST/V1/Patrons.pm | 37 +++++++++++++++++++++++++++---- api/v1/swagger/paths/patrons.yaml | 8 +++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm index 80b64ee963..5b82447c8e 100644 --- a/Koha/REST/V1/Patrons.pm +++ b/Koha/REST/V1/Patrons.pm @@ -330,10 +330,39 @@ sub delete { return try { - $patron->delete; - return $c->render( - status => 204, - openapi => q{} + if ( $patron->checkouts->count > 0 ) { + return $c->render( + status => 409, + openapi => { error => 'Pending checkouts prevent deletion' } + ); + } + + my $account = $patron->account; + + if ( $account->outstanding_debits->total_outstanding > 0 ) { + return $c->render( + status => 409, + openapi => { error => 'Pending debts prevent deletion' } + ); + } + + if ( $patron->guarantee_relationships->count > 0 ) { + return $c->render( + status => 409, + openapi => { error => 'Patron is a guarantor and it prevents deletion' } + ); + } + + $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') ) { diff --git a/api/v1/swagger/paths/patrons.yaml b/api/v1/swagger/paths/patrons.yaml index 2341df8dba..b825bb8c9a 100644 --- a/api/v1/swagger/paths/patrons.yaml +++ b/api/v1/swagger/paths/patrons.yaml @@ -546,6 +546,14 @@ description: Patron not found schema: $ref: ../definitions.yaml#/error + "409": + description: Conflict + schema: + $ref: ../definitions.yaml#/error + "500": + description: Internal server error + schema: + $ref: ../definitions.yaml#/error x-koha-authorization: permissions: borrowers: delete_borrowers -- 2.39.5