From 99f5d9132a3d46fa93eed2fa5446a35604b4cede Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Tue, 10 Nov 2015 16:43:35 +0100 Subject: [PATCH] Bug 15165 - Add API route to edit accoulines --- Koha/REST/V1/Accountline.pm | 22 ++++++++++++ api/v1/definitions/editAccountlineBody.json | 17 ++++++++++ api/v1/definitions/index.json | 1 + api/v1/swagger.json | 48 +++++++++++++++++++++++++++ t/db_dependent/api/v1/accountlines.t | 40 +++++++++++++++++++++- 5 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 api/v1/definitions/editAccountlineBody.json diff --git a/Koha/REST/V1/Accountline.pm b/Koha/REST/V1/Accountline.pm index 04a92de..7f33511 100644 --- a/Koha/REST/V1/Accountline.pm +++ b/Koha/REST/V1/Accountline.pm @@ -36,4 +36,26 @@ sub list { return $c->$cb($accountlines->unblessed, 200); } + +sub edit { + my ($c, $args, $cb) = @_; + + my $user = $c->stash('koha.user'); + unless ($user && haspermission($user->userid, {updatecharges => 1})) { + return $c->$cb({error => "You don't have the required permission"}, 403); + } + + my $accountline = Koha::Accountlines->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/definitions/editAccountlineBody.json b/api/v1/definitions/editAccountlineBody.json new file mode 100644 index 0000000..6cb2f2f --- /dev/null +++ b/api/v1/definitions/editAccountlineBody.json @@ -0,0 +1,17 @@ +{ + "type": "object", + "properties": { + "amount": { + "description": "Amount" + }, + "amountoutstanding": { + "description": "Amount outstanding" + }, + "note": { + "description": "Accountline note" + }, + "meansofpayment": { + "description": "Means of payment" + } + } +} diff --git a/api/v1/definitions/index.json b/api/v1/definitions/index.json index e05f0c2..80b43e0 100644 --- a/api/v1/definitions/index.json +++ b/api/v1/definitions/index.json @@ -1,5 +1,6 @@ { "patron": { "$ref": "patron.json" }, "accountline": { "$ref": "accountline.json" }, + "editAccountlineBody": { "$ref": "editAccountlineBody.json" }, "error": { "$ref": "error.json" } } diff --git a/api/v1/swagger.json b/api/v1/swagger.json index 37e4baa..f8ba465 100644 --- a/api/v1/swagger.json +++ b/api/v1/swagger.json @@ -99,6 +99,47 @@ } } } + }, + "/accountlines/{accountlines_id}": { + "put": { + "operationId": "editAccountlines", + "tags": ["accountlines"], + "produces": [ + "application/json" + ], + "parameters": [ + { "$ref": "#/parameters/accountlinesIdPathParam" }, + { + "name": "body", + "in": "body", + "description": "A JSON object containing fields to modify", + "required": true, + "schema": { "$ref": "#/definitions/editAccountlineBody" } + } + ], + "consumes": ["application/json"], + "produces": ["application/json"], + "responses": { + "200": { + "description": "Updated accountline", + "schema": { "$ref": "#/definitions/accountline" } + }, + "400": { + "description": "Missing or wrong parameters", + "schema": { "$ref": "#/definitions/error" } + }, + "403": { + "description": "Access forbidden", + "schema": { + "$ref": "#/definitions/error" + } + }, + "404": { + "description": "Accountline not found", + "schema": { "$ref": "#/definitions/error" } + } + } + } } }, "definitions": { @@ -111,6 +152,13 @@ "description": "Internal patron identifier", "required": true, "type": "integer" + }, + "accountlinesIdPathParam": { + "name": "accountlines_id", + "in": "path", + "description": "Internal accountline identifier", + "required": true, + "type": "integer" } } } diff --git a/t/db_dependent/api/v1/accountlines.t b/t/db_dependent/api/v1/accountlines.t index cbe98c8..30b7a08 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 => 10; +use Test::More tests => 18; use Test::Mojo; use t::lib::TestBuilder; @@ -41,6 +41,9 @@ my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; $t->get_ok('/api/v1/accountlines') ->status_is(403); +$t->put_ok("/api/v1/accountlines/11224409" => json => {'amount' => -5}) + ->status_is(403); + my $loggedinuser = $builder->build({ source => 'Borrower', value => { @@ -101,4 +104,39 @@ $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::Accountlines->search({'borrowernumber' => $borrowernumber2})->unblessed()->[0]; + +$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::Accountlines->search({'borrowernumber' => $borrowernumber2})->unblessed()->[0]; + +is($accountline_edited->{amount}, '-19.000000'); +is($accountline_edited->{amountoutstanding}, '-19.000000'); + + +# Payment tests + $dbh->rollback; -- 1.7.10.4