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

(-)a/Koha/REST/V1/Patrons.pm (-28 / +19 lines)
Lines 304-343 Controller function that handles deleting a Koha::Patron object Link Here
304
sub delete {
304
sub delete {
305
    my $c = shift->openapi->valid_input or return;
305
    my $c = shift->openapi->valid_input or return;
306
306
307
    my $patron;
307
    my $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
308
309
    unless ( $patron ) {
310
        return $c->render(
311
            status  => 404,
312
            openapi => { error => "Patron not found" }
313
        );
314
    }
308
315
309
    return try {
316
    return try {
310
        $patron = Koha::Patrons->find( $c->validation->param('patron_id') );
311
317
312
        # check if loans, reservations, debarrment, etc. before deletion!
318
        $patron->delete;
313
        try {
319
        return $c->render(
314
            $patron->delete;
320
            status  => 204,
315
            return $c->render(
321
            openapi => q{}
316
                status  => 204,
322
        );
317
                openapi => q{}
323
    } catch {
318
            );
324
        if ( blessed $_ && $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) {
319
        } catch {
320
            if ( $_->isa('Koha::Exceptions::Patron::FailedDeleteAnonymousPatron') ) {
321
                return $c->render(
322
                    status  => 403,
323
                    openapi => { error => "Anonymous patron cannot be deleted" }
324
                );
325
            }
326
            else {
327
                $c->unhandled_exception($_);
328
            }
329
        };
330
    }
331
    catch {
332
        unless ($patron) {
333
            return $c->render(
325
            return $c->render(
334
                status  => 404,
326
                status  => 403,
335
                openapi => { error => "Patron not found" }
327
                openapi => { error => "Anonymous patron cannot be deleted" }
336
            );
328
            );
337
        }
329
        }
338
        else {
330
339
            $c->unhandled_exception($_);
331
        $c->unhandled_exception($_);
340
        }
341
    };
332
    };
342
}
333
}
343
334
(-)a/t/db_dependent/api/v1/patrons.t (-2 / +7 lines)
Lines 405-411 subtest 'delete() tests' => sub { Link Here
405
    $schema->storage->txn_rollback;
405
    $schema->storage->txn_rollback;
406
406
407
    subtest 'librarian access test' => sub {
407
    subtest 'librarian access test' => sub {
408
        plan tests => 5;
408
        plan tests => 8;
409
409
410
        $schema->storage->txn_begin;
410
        $schema->storage->txn_begin;
411
411
Lines 425-430 subtest 'delete() tests' => sub { Link Here
425
425
426
        my $patron = $builder->build_object({ class => 'Koha::Patrons' });
426
        my $patron = $builder->build_object({ class => 'Koha::Patrons' });
427
427
428
        t::lib::Mocks::mock_preference('AnonymousPatron', $patron->borrowernumber);
429
        $t->delete_ok("//$userid:$password@/api/v1/patrons/" . $patron->borrowernumber)
430
          ->status_is(403, 'Anonymous patron cannot be deleted')
431
          ->json_is( { error => 'Anonymous patron cannot be deleted' } );
432
433
        t::lib::Mocks::mock_preference('AnonymousPatron', 0); # back to default
428
        $t->delete_ok("//$userid:$password@/api/v1/patrons/" . $patron->borrowernumber)
434
        $t->delete_ok("//$userid:$password@/api/v1/patrons/" . $patron->borrowernumber)
429
          ->status_is(204, 'SWAGGER3.2.4')
435
          ->status_is(204, 'SWAGGER3.2.4')
430
          ->content_is('', 'SWAGGER3.3.4');
436
          ->content_is('', 'SWAGGER3.3.4');
431
- 

Return to bug 14708