From 6e74e3dcc8629e0cd7e8b6289a17d6005f6147b8 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 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Joonas Kylmälä Signed-off-by: Bouzid Fergani --- 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 c8ded14..26d1da0 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 95278ec..803e3b5 100644 --- a/api/v1/swagger/paths.json +++ b/api/v1/swagger/paths.json @@ -20,6 +20,9 @@ "/checkouts/{checkout_id}/renewal": { "$ref": "paths/checkouts.json#/~1checkouts~1{checkout_id}~1renewal" }, + "/checkouts/history": { + "$ref": "paths/checkouts.json#/~1checkouts~1history" + }, "/cities": { "$ref": "paths/cities.json#/~1cities" }, diff --git a/api/v1/swagger/paths/checkouts.json b/api/v1/swagger/paths/checkouts.json index ca6d970..9634e0c 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 5a34474..a1df3a6 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.7.4