@@ -, +, @@ - Guarantees - Debts - Current checkouts $ kshell k$ prove t/db_dependent/api/v1/patrons.t --- Koha/REST/V1/Patrons.pm | 37 +++++++++++++++++++++++++++---- api/v1/swagger/paths/patrons.yaml | 8 +++++++ 2 files changed, 41 insertions(+), 4 deletions(-) --- a/Koha/REST/V1/Patrons.pm +++ a/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') ) { --- a/api/v1/swagger/paths/patrons.yaml +++ a/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 --