From 135430ce4ad4ed0db9bacb019fb93b66b9a1a027 Mon Sep 17 00:00:00 2001 From: Johanna Raisa Date: Tue, 22 Oct 2019 14:41:38 +0300 Subject: [PATCH] Bug 18795: DELETE (anonymize) checkout history Anonymize patron's checkout history via REST API DELETE /checkouts/history?patron_id=123 To test: 1. prove t/db_dependent/api/v1/checkouts.t 2. Manually send DELETE request to /api/v1/checkouts/history?patron_id=123 where 123 is your patron's borrowernumber Sponsored-by: Koha-Suomi Oy --- Koha/REST/V1/Checkouts.pm | 26 ++++++++++++++++++++++++++ api/v1/swagger/paths.json | 3 +++ api/v1/swagger/paths/checkouts.json | 28 ++++++++++++++++++++++++++++ t/db_dependent/api/v1/checkouts.t | 19 ++++++++++++++++++- 4 files changed, 75 insertions(+), 1 deletion(-) diff --git a/Koha/REST/V1/Checkouts.pm b/Koha/REST/V1/Checkouts.pm index b3a0699006..8418f51ec1 100644 --- a/Koha/REST/V1/Checkouts.pm +++ b/Koha/REST/V1/Checkouts.pm @@ -182,6 +182,32 @@ sub allows_renewal { ); } +=head3 delete_history + +Anonymize patron's checkout history. + +=cut + +sub delete_history { + my $c = shift->openapi->valid_input or return; + + my $patron_id = $c->validation->param('patron_id'); + + my $patrons = Koha::Patrons->search({ + 'me.borrowernumber' => $patron_id + }); + + $patrons->anonymise_issue_history; + + unless ($patrons->count) { + return $c->render( status => 404, openapi => { + error => "Patron doesn't exist" + }); + } + + return $c->render( status => 204, openapi => "" ); +} + =head3 _to_api Helper function that maps a hashref of Koha::Checkout attributes into REST api diff --git a/api/v1/swagger/paths.json b/api/v1/swagger/paths.json index d705760f63..62ad73b824 100644 --- a/api/v1/swagger/paths.json +++ b/api/v1/swagger/paths.json @@ -53,6 +53,9 @@ "/checkouts/{checkout_id}/allows_renewal": { "$ref": "paths/checkouts.json#/~1checkouts~1{checkout_id}~1allows_renewal" }, + "/checkouts/history": { + "$ref": "paths/checkouts.json#/~1checkouts~1history" + }, "/items/{item_id}": { "$ref": "paths/items.json#/~1items~1{item_id}" }, diff --git a/api/v1/swagger/paths/checkouts.json b/api/v1/swagger/paths/checkouts.json index ca6d970dbb..9634e0c139 100644 --- a/api/v1/swagger/paths/checkouts.json +++ b/api/v1/swagger/paths/checkouts.json @@ -135,5 +135,33 @@ } } } + }, + "/checkouts/history": { + "delete": { + "x-mojo-to": "Checkouts#delete_history", + "operationId": "delete_historyCheckouts", + "tags": ["patrons", "checkouts"], + "parameters": [{ + "$ref": "../parameters.json#/patron_id_qp" + }], + "produces": ["application/json"], + "responses": { + "204": { + "description": "Checkout history deleted successfully", + "schema": { + "type": "string" + } + }, + "404": { + "description": "Patron not found", + "schema": { "$ref": "../definitions.json#/error" } + } + }, + "x-koha-authorization": { + "permissions": { + "circulate": "circulate_remaining_permissions" + } + } + } } } diff --git a/t/db_dependent/api/v1/checkouts.t b/t/db_dependent/api/v1/checkouts.t index 5a3447412e..a1df3a6254 100644 --- a/t/db_dependent/api/v1/checkouts.t +++ b/t/db_dependent/api/v1/checkouts.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 93; +use Test::More tests => 99; use Test::MockModule; use Test::Mojo; use t::lib::Mocks; @@ -216,3 +216,20 @@ $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/all current_renewals => 1, error => 'too_many' }); + +$t->delete_ok( "//$userid:$password@/api/v1/checkouts/history?patron_id=$patron_id") + ->status_is(204) + ->content_is(''); + +my $patron_to_delete = $builder->build_object({ + class => 'Koha::Patrons', + value => { flags => 0 } +}); + +my $patron_to_delete_id = $patron_to_delete->borrowernumber; + +$patron_to_delete->delete; + +$t->delete_ok( "//$userid:$password@/api/v1/checkouts/history?patron_id=$patron_to_delete_id") + ->status_is(404) + ->json_is({error => "Patron doesn't exist"}); -- 2.17.1