View | Details | Raw Unified | Return to bug 29741
Collapse All | Expand All

(-)a/Koha/REST/V1/Patrons.pm (-19 / +28 lines)
Lines 20-25 use Modern::Perl; Link Here
20
use Mojo::Base 'Mojolicious::Controller';
20
use Mojo::Base 'Mojolicious::Controller';
21
21
22
use Koha::Database;
22
use Koha::Database;
23
use Koha::Exceptions;
23
use Koha::Patrons;
24
use Koha::Patrons;
24
25
25
use Scalar::Util qw( blessed );
26
use Scalar::Util qw( blessed );
Lines 330-369 sub delete { Link Here
330
331
331
    return try {
332
    return try {
332
333
333
        if ( $patron->checkouts->count > 0 ) {
334
        my $safe_to_delete = $patron->safe_to_delete;
335
336
        if ( $safe_to_delete eq 'ok' ) {
337
            $patron->_result->result_source->schema->txn_do(
338
                sub {
339
                    $patron->move_to_deleted;
340
                    $patron->delete;
341
342
                    return $c->render(
343
                        status  => 204,
344
                        openapi => q{}
345
                    );
346
                }
347
            );
348
        }
349
        elsif ( $safe_to_delete eq 'has_checkouts' ) {
334
            return $c->render(
350
            return $c->render(
335
                status  => 409,
351
                status  => 409,
336
                openapi => { error => 'Pending checkouts prevent deletion' }
352
                openapi => { error => 'Pending checkouts prevent deletion' }
337
            );
353
            );
338
        }
354
        }
339
355
        elsif ( $safe_to_delete eq 'has_debt' ) {
340
        my $account = $patron->account;
341
342
        if ( $account->outstanding_debits->total_outstanding > 0 ) {
343
            return $c->render(
356
            return $c->render(
344
                status  => 409,
357
                status  => 409,
345
                openapi => { error => 'Pending debts prevent deletion' }
358
                openapi => { error => 'Pending debts prevent deletion' }
346
            );
359
            );
347
        }
360
        }
348
361
        elsif ( $safe_to_delete eq 'has_guarantees' ) {
349
        if ( $patron->guarantee_relationships->count > 0 ) {
350
            return $c->render(
362
            return $c->render(
351
                status  => 409,
363
                status  => 409,
352
                openapi => { error => 'Patron is a guarantor and it prevents deletion' }
364
                openapi => { error => 'Patron is a guarantor and it prevents deletion' }
353
            );
365
            );
354
        }
366
        }
355
367
        elsif ( $safe_to_delete eq 'is_anonymous_patron' ) {
356
        $patron->_result->result_source->schema->txn_do(
368
            return $c->render(
357
            sub {
369
                status  => 403,
358
                $patron->move_to_deleted;
370
                openapi => { error => 'Anonymous patron cannot be deleted' }
359
                $patron->delete;
371
            );
360
372
        }
361
                return $c->render(
373
        else {
362
                    status  => 204,
374
            Koha::Exceptions::Exception->throw( "Koha::Patron->safe_to_delete returned an unexpected value: $safe_to_delete" );
363
                    openapi => q{}
375
        }
364
                );
365
            }
366
        );
367
    } catch {
376
    } catch {
368
        if ( blessed $_ && $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) {
377
        if ( blessed $_ && $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) {
369
            return $c->render(
378
            return $c->render(
(-)a/t/db_dependent/api/v1/patrons.t (-2 / +1 lines)
Lines 665-670 subtest 'delete() tests' => sub { Link Here
665
        $t->delete_ok("//$userid:$password@/api/v1/patrons/" . $patron->borrowernumber)
665
        $t->delete_ok("//$userid:$password@/api/v1/patrons/" . $patron->borrowernumber)
666
          ->status_is(403, 'Anonymous patron cannot be deleted')
666
          ->status_is(403, 'Anonymous patron cannot be deleted')
667
          ->json_is( { error => 'Anonymous patron cannot be deleted' } );
667
          ->json_is( { error => 'Anonymous patron cannot be deleted' } );
668
        t::lib::Mocks::mock_preference('AnonymousPatron', 0); # back to default
668
669
669
        t::lib::Mocks::mock_preference( 'borrowerRelationship', 'parent' );
670
        t::lib::Mocks::mock_preference( 'borrowerRelationship', 'parent' );
670
671
Lines 700-706 subtest 'delete() tests' => sub { Link Here
700
        # Remove guarantee
701
        # Remove guarantee
701
        $patron->guarantee_relationships->delete;
702
        $patron->guarantee_relationships->delete;
702
703
703
        t::lib::Mocks::mock_preference('AnonymousPatron', 0); # back to default
704
        $t->delete_ok("//$userid:$password@/api/v1/patrons/" . $patron->borrowernumber)
704
        $t->delete_ok("//$userid:$password@/api/v1/patrons/" . $patron->borrowernumber)
705
          ->status_is(204, 'SWAGGER3.2.4')
705
          ->status_is(204, 'SWAGGER3.2.4')
706
          ->content_is('', 'SWAGGER3.3.4');
706
          ->content_is('', 'SWAGGER3.3.4');
707
- 

Return to bug 29741