diff --git a/Koha/REST/V1/Patrons.pm b/Koha/REST/V1/Patrons.pm index 5eba4bdee3..abefe227a5 100644 --- a/Koha/REST/V1/Patrons.pm +++ b/Koha/REST/V1/Patrons.pm @@ -315,10 +315,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.json b/api/v1/swagger/paths/patrons.json index 3e4e7739a1..6eab8b0109 100644 --- a/api/v1/swagger/paths/patrons.json +++ b/api/v1/swagger/paths/patrons.json @@ -695,6 +695,18 @@ "schema": { "$ref": "../definitions.json#/error" } + }, + "409": { + "description": "Conflict in updating resource", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "500": { + "description": "Internal server error", + "schema": { + "$ref": "../definitions.json#/error" + } } }, "x-koha-authorization": {