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

(-)a/Koha/REST/V1/Checkout.pm (+24 lines)
Lines 279-284 sub expanded { Link Here
279
    return $c->render( status => 200, openapi => $checkouts_json );
279
    return $c->render( status => 200, openapi => $checkouts_json );
280
}
280
}
281
281
282
sub deletehistory {
283
    my $c = shift->openapi->valid_input or return;
284
285
    my $borrowernumber = $c->validation->param('borrowernumber');
286
    my $patron;
287
    return try {
288
        my $patrons = Koha::Patrons->search({
289
            'me.borrowernumber' => $borrowernumber
290
        });
291
        $patrons->anonymise_issue_history;
292
        $patron = $patrons->next;
293
294
        return $c->render( status => 200, openapi => {} );
295
    }
296
    catch {
297
        unless ($patron) {
298
            return $c->render( status => 404, openapi => {
299
                error => "Patron doesn't exist"
300
            });
301
        }
302
        Koha::Exceptions::rethrow_exception($_);
303
    };
304
}
305
282
sub _opac_renewal_allowed {
306
sub _opac_renewal_allowed {
283
    my ($user, $borrowernumber) = @_;
307
    my ($user, $borrowernumber) = @_;
284
308
(-)a/api/v1/swagger/paths/checkouts.json (+54 lines)
Lines 346-351 Link Here
346
          "circulate_remaining_permissions": "1"
346
          "circulate_remaining_permissions": "1"
347
        }
347
        }
348
      }
348
      }
349
    },
350
    "delete": {
351
      "x-mojo-to": "Checkout#deletehistory",
352
      "operationId": "deletehistoryCheckouts",
353
      "tags": ["patrons", "checkouts"],
354
      "parameters": [
355
        { "$ref": "../parameters.json#/borrowernumberQueryParam" }
356
      ],
357
      "produces": [
358
        "application/json"
359
      ],
360
      "responses": {
361
        "200": {
362
          "description": "Checkout history deleted successfully",
363
          "schema": {
364
            "type": "object"
365
          }
366
        },
367
        "401": {
368
          "description": "Authentication required",
369
          "schema": {
370
            "$ref": "../definitions.json#/error"
371
          }
372
        },
373
        "403": {
374
          "description": "Access forbidden",
375
          "schema": { "$ref": "../definitions.json#/error" }
376
        },
377
        "404": {
378
          "description": "Borrower not found",
379
          "schema": {
380
            "$ref": "../definitions.json#/error"
381
          }
382
        },
383
        "500": {
384
          "description": "Internal error",
385
          "schema": {
386
            "$ref": "../definitions.json#/error"
387
          }
388
        },
389
        "503": {
390
          "description": "Under maintenance",
391
          "schema": {
392
            "$ref": "../definitions.json#/error"
393
          }
394
        }
395
      },
396
      "x-koha-authorization": {
397
        "allow-owner": true,
398
        "allow-guarantor": true,
399
        "permissions": {
400
          "circulate_remaining_permissions": "1"
401
        }
402
      }
349
    }
403
    }
350
  },
404
  },
351
  "/checkouts/history/{checkout_id}": {
405
  "/checkouts/history/{checkout_id}": {
(-)a/t/db_dependent/api/v1/checkoutshistory.t (-2 / +55 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 35;
20
use Test::More tests => 36;
21
use Test::MockModule;
21
use Test::MockModule;
22
use Test::Mojo;
22
use Test::Mojo;
23
use t::lib::Mocks;
23
use t::lib::Mocks;
Lines 281-286 subtest 'test sorting, limit and offset' => sub { Link Here
281
      ->json_is('/records/3/issue_id' => $issue4->issue_id);
281
      ->json_is('/records/3/issue_id' => $issue4->issue_id);
282
};
282
};
283
283
284
subtest 'delete() tests' => sub {
285
    plan tests => 8;
286
287
    my $anonymous_patron = $builder->build({
288
        source => 'Borrower'
289
    })->{borrowernumber};
290
    t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous_patron);
291
292
    my $id = Koha::Old::Checkouts->search({}, {
293
        order_by => {'-desc' => 'issue_id'}})->next;
294
    $id = ($id) ? $id->issue_id+1 : 1;
295
296
    my $issue1 = Koha::Old::Checkout->new({
297
        issue_id => $id,
298
        borrowernumber => $nopermission->{borrowernumber},
299
        itemnumber => $itemnumber1,
300
    })->store;
301
    my $issue2 = Koha::Old::Checkout->new({
302
        issue_id => $id+1,
303
        borrowernumber => $nopermission->{borrowernumber},
304
        itemnumber => $itemnumber1,
305
    })->store;
306
307
    $tx = $t->ua->build_tx(DELETE => "/api/v1/checkouts/history"
308
        ."?borrowernumber=".($borrowernumber+1));
309
    $tx->req->cookies({name => 'CGISESSID', value => $session_nopermission->id});
310
    $t->request_ok($tx)
311
      ->status_is(403);
312
313
    $tx = $t->ua->build_tx(DELETE => "/api/v1/checkouts/history"
314
        ."?borrowernumber=".$nopermission->{borrowernumber});
315
    $tx->req->cookies({name => 'CGISESSID', value => $session_nopermission->id});
316
    $t->request_ok($tx)
317
      ->status_is(200);
318
319
    is(Koha::Old::Checkouts->search({
320
        borrowernumber => $anonymous_patron,
321
        issue_id => { 'in' => [$id, $id+1] }
322
    })->count, 2, 'Found anonymized checkouts (anonymous patron)');
323
324
    t::lib::Mocks::mock_preference('AnonymousPatron', undef);
325
326
    $tx = $t->ua->build_tx(DELETE => "/api/v1/checkouts/history"
327
        ."?borrowernumber=$anonymous_patron");
328
    $tx->req->cookies({name => 'CGISESSID', value => $session->id});
329
    $t->request_ok($tx)
330
      ->status_is(200);
331
332
    is(Koha::Old::Checkouts->search({
333
        borrowernumber => undef,
334
        issue_id => { 'in' => [$id, $id+1] }
335
    })->count, 2, 'Found anonymized checkouts (undef patron)');
336
};
337
284
Koha::Patrons->find($borrowernumber)->delete();
338
Koha::Patrons->find($borrowernumber)->delete();
285
339
286
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$borrowernumber");
340
$tx = $t->ua->build_tx(GET => "/api/v1/checkouts/history?borrowernumber=$borrowernumber");
287
- 

Return to bug 18795