From 03ffa412e969f46e81e238b9dd6e166b4c64df33 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Tue, 10 Nov 2015 09:52:45 +0100 Subject: [PATCH] Bug 15165 - Add API route to list accountlines --- Koha/Accountline.pm | 30 ++++++++++ Koha/Accountlines.pm | 35 ++++++++++++ Koha/REST/V1/Accountline.pm | 39 +++++++++++++ api/v1/definitions/accountline.json | 56 +++++++++++++++++++ api/v1/definitions/index.json | 1 + api/v1/swagger.json | 26 +++++++++ t/db_dependent/api/v1/accountlines.t | 104 +++++++++++++++++++++++++++++++++++ 7 files changed, 291 insertions(+) create mode 100644 Koha/Accountline.pm create mode 100644 Koha/Accountlines.pm create mode 100644 Koha/REST/V1/Accountline.pm create mode 100644 api/v1/definitions/accountline.json create mode 100644 t/db_dependent/api/v1/accountlines.t diff --git a/Koha/Accountline.pm b/Koha/Accountline.pm new file mode 100644 index 0000000..04254d7 --- /dev/null +++ b/Koha/Accountline.pm @@ -0,0 +1,30 @@ +package Koha::Accountline; + +# Copyright 2015 BibLibre +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Koha::Database; + +use base qw(Koha::Object); + +sub _type { + return 'Accountline'; +} + +1; diff --git a/Koha/Accountlines.pm b/Koha/Accountlines.pm new file mode 100644 index 0000000..6d84094 --- /dev/null +++ b/Koha/Accountlines.pm @@ -0,0 +1,35 @@ +package Koha::Accountlines; + +# Copyright 2015 BibLibre +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Koha::Database; +use Koha::Accountline; + +use base qw(Koha::Objects); + +sub _type { + return 'Accountline'; +} + +sub object_class { + return 'Koha::Accountline'; +} + +1; diff --git a/Koha/REST/V1/Accountline.pm b/Koha/REST/V1/Accountline.pm new file mode 100644 index 0000000..04a92de --- /dev/null +++ b/Koha/REST/V1/Accountline.pm @@ -0,0 +1,39 @@ +package Koha::REST::V1::Accountline; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use C4::Auth qw( haspermission ); +use Koha::Accountlines; + +sub list { + 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 $params = $c->req->params->to_hash; + my $accountlines = Koha::Accountlines->search($params); + + return $c->$cb($accountlines->unblessed, 200); +} + +1; diff --git a/api/v1/definitions/accountline.json b/api/v1/definitions/accountline.json new file mode 100644 index 0000000..d4b612e --- /dev/null +++ b/api/v1/definitions/accountline.json @@ -0,0 +1,56 @@ +{ + "type": "object", + "properties": { + "accountlines_id": { + "description": "Internal account line identifier" + }, + "borrowernumber": { + "description": "Internal borrower identifier" + }, + "accountno": { + "description": "?" + }, + "itemnumber": { + "description": "Internal item identifier" + }, + "date": { + "description": "Date when the account line was created" + }, + "time": { + "description": "Time when the account line was created" + }, + "amount": { + "description": "Amount" + }, + "description": { + "description": "Description of account line" + }, + "accounttype": { + "description": "Type of accountline" + }, + "amountoutstanding": { + "description": "Amount outstanding" + }, + "lastincrement": { + "description": "?" + }, + "timestamp": { + "description": "When the account line was last updated" + }, + "notify_id": { + "description": "?" + }, + "notify_level": { + "description": "?" + }, + "note": { + "description": "Accountline note" + }, + "manager_id": { + "description": "Borrowernumber of user that created the account line" + }, + "meansofpayment": { + "description": "Means of payment" + } + } +} diff --git a/api/v1/definitions/index.json b/api/v1/definitions/index.json index e940eee..e05f0c2 100644 --- a/api/v1/definitions/index.json +++ b/api/v1/definitions/index.json @@ -1,4 +1,5 @@ { "patron": { "$ref": "patron.json" }, + "accountline": { "$ref": "accountline.json" }, "error": { "$ref": "error.json" } } diff --git a/api/v1/swagger.json b/api/v1/swagger.json index e821c44..37e4baa 100644 --- a/api/v1/swagger.json +++ b/api/v1/swagger.json @@ -73,6 +73,32 @@ } } } + }, + "/accountlines": { + "get": { + "operationId": "listAccountlines", + "tags": ["accountlines"], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "A list of accountlines", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/accountline" + } + } + }, + "403": { + "description": "Access forbidden", + "schema": { + "$ref": "#/definitions/error" + } + } + } + } } }, "definitions": { diff --git a/t/db_dependent/api/v1/accountlines.t b/t/db_dependent/api/v1/accountlines.t new file mode 100644 index 0000000..cbe98c8 --- /dev/null +++ b/t/db_dependent/api/v1/accountlines.t @@ -0,0 +1,104 @@ +#!/usr/bin/env perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Test::More tests => 10; +use Test::Mojo; +use t::lib::TestBuilder; + +use C4::Auth; +use C4::Context; + +use Koha::Database; + +my $builder = t::lib::TestBuilder->new(); + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +$ENV{REMOTE_ADDR} = '127.0.0.1'; +my $t = Test::Mojo->new('Koha::REST::V1'); + +my $categorycode = $builder->build({ source => 'Category' })->{ categorycode }; +my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode }; + +$t->get_ok('/api/v1/accountlines') + ->status_is(403); + +my $loggedinuser = $builder->build({ + source => 'Borrower', + value => { + branchcode => $branchcode, + categorycode => $categorycode, + flags => 1024 + } +}); + +my $borrower = $builder->build({ + source => 'Borrower', + value => { + branchcode => $branchcode, + categorycode => $categorycode, + } +}); + +my $borrower2 = $builder->build({ + source => 'Borrower', + value => { + branchcode => $branchcode, + categorycode => $categorycode, + } +}); +my $borrowernumber = $borrower->{borrowernumber}; +my $borrowernumber2 = $borrower2->{borrowernumber}; + +$dbh->do(q| DELETE FROM accountlines |); +$dbh->do(q| + INSERT INTO accountlines (borrowernumber, amount, accounttype) + VALUES (?, 20, 'A'), (?, 40, 'F'), (?, 80, 'F'), (?, 10, 'F') + |, undef, $borrowernumber, $borrowernumber, $borrowernumber, $borrowernumber2); + +my $session = C4::Auth::get_session(''); +$session->param('number', $loggedinuser->{ borrowernumber }); +$session->param('id', $loggedinuser->{ userid }); +$session->param('ip', '127.0.0.1'); +$session->param('lasttime', time()); +$session->flush; + +my $tx = $t->ua->build_tx(GET => "/api/v1/accountlines?borrowernumber=$borrowernumber"); +$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 $json = $t->tx->res->json; +ok(ref $json eq 'ARRAY', 'response is a JSON array'); +ok(scalar @$json == 3, 'response array contains 3 elements'); + +$tx = $t->ua->build_tx(GET => "/api/v1/accountlines"); +$tx->req->cookies({name => 'CGISESSID', value => $session->id}); +$tx->req->env({REMOTE_ADDR => '127.0.0.1'}); +$t->request_ok($tx) + ->status_is(200); + +$json = $t->tx->res->json; +ok(ref $json eq 'ARRAY', 'response is a JSON array'); +ok(scalar @$json == 4, 'response array contains 3 elements'); + +$dbh->rollback; -- 2.1.4