From f0d0f8801d5097b10a7f22a120243d0019c331f2 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Mon, 30 Mar 2015 17:00:51 +0200 Subject: [PATCH] Bug 13935: Add API route to list borrower's accountlines GET /borrowers/{borrowernumber}/accountlines GET /borrowers/{borrowernumber}/accountlines?accounttype={accounttype} Unit tests in t/db_dependent/api/v1/borrowers/accountlines.t --- Koha/Accountline.pm | 30 +++++++++ Koha/Accountlines.pm | 35 ++++++++++ Koha/REST/V1/Borrowers/Accountlines.pm | 30 +++++++++ api/v1/swagger.json | 93 ++++++++++++++++++++++++++ t/db_dependent/api/v1/borrowers/accountlines.t | 60 +++++++++++++++++ 5 files changed, 248 insertions(+) create mode 100644 Koha/Accountline.pm create mode 100644 Koha/Accountlines.pm create mode 100644 Koha/REST/V1/Borrowers/Accountlines.pm create mode 100644 t/db_dependent/api/v1/borrowers/accountlines.t diff --git a/Koha/Accountline.pm b/Koha/Accountline.pm new file mode 100644 index 0000000..be48049 --- /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..88214e1 --- /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/Borrowers/Accountlines.pm b/Koha/REST/V1/Borrowers/Accountlines.pm new file mode 100644 index 0000000..a997178 --- /dev/null +++ b/Koha/REST/V1/Borrowers/Accountlines.pm @@ -0,0 +1,30 @@ +package Koha::REST::V1::Borrowers::Accountlines; + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use Koha::Borrowers; +use Koha::Accountlines; + +use C4::Accounts; + +sub get_borrower_accountlines { + my ($c, $args, $cb) = @_; + + my $borrower = Koha::Borrowers->find($args->{borrowernumber}); + + unless ($borrower) { + return $c->$cb({error => "Borrower not found"}, 404); + } + + my $search_params = { borrowernumber => $borrower->borrowernumber }; + if (defined $args->{accounttype}) { + $search_params->{accounttype} = $args->{accounttype}; + } + my $accountlines = Koha::Accountlines->search($search_params); + + return $c->$cb($accountlines->unblessed, 200); +} + +1; diff --git a/api/v1/swagger.json b/api/v1/swagger.json index 5e97d03..5164822 100644 --- a/api/v1/swagger.json +++ b/api/v1/swagger.json @@ -63,6 +63,39 @@ } } } + }, + "/borrowers/{borrowernumber}/accountlines": { + "get": { + "x-mojo-controller": "Koha::REST::V1::Borrowers::Accountlines", + "operationId": "getBorrowerAccountlines", + "tags": ["borrowers", "accountlines"], + "parameters": [ + { "$ref": "#/parameters/borrowernumberPathParam" }, + { + "name": "accounttype", + "in": "query", + "description": "Filter by accountline type", + "type": "string" + } + ], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "A list of borrower accountlines", + "schema": { + "$ref": "#/definitions/accountlines" + } + }, + "404": { + "description": "Borrower not found", + "schema": { + "$ref": "#/definitions/error" + } + } + } + } } }, "definitions": { @@ -86,6 +119,66 @@ "borrowernumber": { "description": "Borrower internal identifier" }, + "accountlines": { + "type": "array", + "items": { "$ref": "#/definitions/accountline" } + }, + "accountline": { + "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" + } + } + }, "error": { "type": "object", "properties": { diff --git a/t/db_dependent/api/v1/borrowers/accountlines.t b/t/db_dependent/api/v1/borrowers/accountlines.t new file mode 100644 index 0000000..1fb61fb --- /dev/null +++ b/t/db_dependent/api/v1/borrowers/accountlines.t @@ -0,0 +1,60 @@ +#!/usr/bin/env perl + +use Modern::Perl; + +use Test::More tests => 17; +use Test::Mojo; + +use C4::Context; + +use Koha::Database; +use Koha::Borrower; + +my $dbh = C4::Context->dbh; +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +my $t = Test::Mojo->new('Koha::REST::V1'); + +my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode(); +my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode(); + +my $borrower = Koha::Borrower->new; +$borrower->categorycode( $categorycode ); +$borrower->branchcode( $branchcode ); +$borrower->surname("Test Surname"); +$borrower->store; +my $borrowernumber = $borrower->borrowernumber; + +$dbh->do(q| DELETE FROM accountlines |); +$dbh->do(q| + INSERT INTO accountlines (borrowernumber, amount, accounttype) + VALUES (?, 20, 'A'), (?, 40, 'F'), (?, 80, 'F') +|, undef, $borrowernumber, $borrowernumber, $borrowernumber); + +$t->get_ok("/v1/borrowers/$borrowernumber/accountlines") + ->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'); + +$t->get_ok("/v1/borrowers/$borrowernumber/accountlines?accounttype=A") + ->status_is(200); +$json = $t->tx->res->json; +ok(ref $json eq 'ARRAY', 'response is a JSON array'); +ok(scalar @$json == 1, 'response array contains 1 element'); +is($json->[0]->{amount}, '20.000000', 'right amount returned'); + +$t->get_ok("/v1/borrowers/$borrowernumber/accountlines?accounttype=F") + ->status_is(200); +$json = $t->tx->res->json; +ok(ref $json eq 'ARRAY', 'response is a JSON array'); +ok(scalar @$json == 2, 'response array contains 2 elements'); + +$t->get_ok("/v1/borrowers/$borrowernumber/accountlines?accounttype=Pay") + ->status_is(200); +$json = $t->tx->res->json; +ok(ref $json eq 'ARRAY', 'response is a JSON array'); +ok(scalar @$json == 0, 'response array is empty'); + +$dbh->rollback; -- 1.9.1