From 634f62c409f6206b24839e3f17e4380825cbcd9b Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Tue, 10 Nov 2015 16:43:35 +0100 Subject: [PATCH] [SIGNED-OFF] Bug 15165 - Add API route to edit accountlines PUT /accountlines/{accountlines_id} (edit) Test plan: 1. Open a browser tab on Koha staff and log in (to create CGISESSID cookie). You should have permission updatecharges. 2. Send PUT request to http://yourlibrary/api/v1/accountlines/YYY where YYY is one of your accountlines_id. See the body definition in definitions/editAccountlineBody.json and use this in your PUT request. 3. Run unit tests in t/db_dependent/api/v1/accountlines.t Signed-off-by: Josef Moravec --- Koha/REST/V1/Accountline.pm | 17 ++++++++ api/v1/swagger/parameters.json | 3 ++ api/v1/swagger/parameters/accountline.json | 9 +++++ api/v1/swagger/paths.json | 3 ++ api/v1/swagger/paths/accountlines.json | 62 ++++++++++++++++++++++++++++++ t/db_dependent/api/v1/accountlines.t | 46 +++++++++++++++++++++- 6 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 api/v1/swagger/parameters/accountline.json diff --git a/Koha/REST/V1/Accountline.pm b/Koha/REST/V1/Accountline.pm index 4acf294..23c2491 100644 --- a/Koha/REST/V1/Accountline.pm +++ b/Koha/REST/V1/Accountline.pm @@ -31,4 +31,21 @@ sub list { return $c->$cb($accountlines->unblessed, 200); } + +sub edit { + my ($c, $args, $cb) = @_; + + my $accountline = Koha::Account::Lines->find($args->{accountlines_id}); + unless ($accountline) { + return $c->$cb({error => "Accountline not found"}, 404); + } + + my $body = $c->req->json; + + $accountline->set( $body ); + $accountline->store(); + + return $c->$cb($accountline->unblessed(), 200); +} + 1; diff --git a/api/v1/swagger/parameters.json b/api/v1/swagger/parameters.json index eeb156a..f13b91d 100644 --- a/api/v1/swagger/parameters.json +++ b/api/v1/swagger/parameters.json @@ -1,4 +1,7 @@ { + "accountlinesIdPathParam": { + "$ref": "parameters/accountline.json#/accountlinesIdPathParam" + }, "borrowernumberPathParam": { "$ref": "parameters/patron.json#/borrowernumberPathParam" }, diff --git a/api/v1/swagger/parameters/accountline.json b/api/v1/swagger/parameters/accountline.json new file mode 100644 index 0000000..d1fb361 --- /dev/null +++ b/api/v1/swagger/parameters/accountline.json @@ -0,0 +1,9 @@ +{ + "accountlinesIdPathParam": { + "name": "accountlines_id", + "in": "path", + "description": "Internal accountline identifier", + "required": true, + "type": "integer" + } +} diff --git a/api/v1/swagger/paths.json b/api/v1/swagger/paths.json index 3ebaf61..8cfcd39 100644 --- a/api/v1/swagger/paths.json +++ b/api/v1/swagger/paths.json @@ -8,6 +8,9 @@ "/accountlines": { "$ref": "paths/accountlines.json#/~1accountlines" }, + "/accountlines/{accountlines_id}": { + "$ref": "paths/accountlines.json#/~1accountlines~1{accountlines_id}" + }, "/holds": { "$ref": "paths/holds.json#/~1holds" }, diff --git a/api/v1/swagger/paths/accountlines.json b/api/v1/swagger/paths/accountlines.json index d7aff5c..d89056d 100644 --- a/api/v1/swagger/paths/accountlines.json +++ b/api/v1/swagger/paths/accountlines.json @@ -31,5 +31,67 @@ } } } + }, + "/accountlines/{accountlines_id}": { + "put": { + "operationId": "editAccountlines", + "tags": ["accountlines"], + "produces": [ + "application/json" + ], + "parameters": [ + { "$ref": "../parameters.json#/accountlinesIdPathParam" }, + { + "name": "body", + "in": "body", + "description": "A JSON object containing fields to modify", + "required": true, + "schema": { + "type": "object", + "properties": { + "amount": { + "description": "Amount" + }, + "amountoutstanding": { + "description": "Amount outstanding" + }, + "note": { + "description": "Accountline note" + }, + "meansofpayment": { + "description": "Means of payment" + } + } + } + } + ], + "consumes": ["application/json"], + "produces": ["application/json"], + "responses": { + "200": { + "description": "Updated accountline", + "schema": { "$ref": "../definitions.json#/accountline" } + }, + "400": { + "description": "Missing or wrong parameters", + "schema": { "$ref": "../definitions.json#/error" } + }, + "403": { + "description": "Access forbidden", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "404": { + "description": "Accountline not found", + "schema": { "$ref": "../definitions.json#/error" } + } + }, + "x-koha-authorization": { + "permissions": { + "updatecharges": "1" + } + } + } } } diff --git a/t/db_dependent/api/v1/accountlines.t b/t/db_dependent/api/v1/accountlines.t index ceafef0..d395603 100644 --- a/t/db_dependent/api/v1/accountlines.t +++ b/t/db_dependent/api/v1/accountlines.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 12; +use Test::More tests => 22; use Test::Mojo; use t::lib::TestBuilder; @@ -25,6 +25,7 @@ use C4::Auth; use C4::Context; use Koha::Database; +use Koha::Account::Line; my $builder = t::lib::TestBuilder->new(); @@ -41,6 +42,9 @@ my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; $t->get_ok('/api/v1/accountlines') ->status_is(401); +$t->put_ok("/api/v1/accountlines/11224409" => json => {'amount' => -5}) + ->status_is(401); + my $loggedinuser = $builder->build({ source => 'Borrower', value => { @@ -115,4 +119,44 @@ $json = $t->tx->res->json; ok(ref $json eq 'ARRAY', 'response is a JSON array'); ok(scalar @$json == 4, 'response array contains 3 elements'); +# Editing accountlines tests +my $put_data = { + 'amount' => -19, + 'amountoutstanding' => -19 +}; + +$tx = $t->ua->build_tx( + PUT => "/api/v1/accountlines/11224409" + => {Accept => '*/*'} + => json => $put_data); +$tx->req->cookies({name => 'CGISESSID', value => $session->id}); +$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); +$t->request_ok($tx) + ->status_is(404); + +my $accountline_to_edit = Koha::Account::Lines->search({'borrowernumber' => $borrowernumber2})->unblessed()->[0]; + +$tx = $t->ua->build_tx(PUT => "/api/v1/accountlines/$accountline_to_edit->{accountlines_id}" => json => $put_data); +$tx->req->cookies({name => 'CGISESSID', value => $borrowersession->id}); +$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); +$t->request_ok($tx) + ->status_is(403); + +$tx = $t->ua->build_tx( + PUT => "/api/v1/accountlines/$accountline_to_edit->{accountlines_id}" + => {Accept => '*/*'} + => json => $put_data); +$tx->req->cookies({name => 'CGISESSID', value => $session->id}); +$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); +$t->request_ok($tx) + ->status_is(200); + +my $accountline_edited = Koha::Account::Lines->search({'borrowernumber' => $borrowernumber2})->unblessed()->[0]; + +is($accountline_edited->{amount}, '-19.000000'); +is($accountline_edited->{amountoutstanding}, '-19.000000'); + + +# Payment tests + $dbh->rollback; -- 2.1.4