From 9bbc1fc7b10000e65ba68b51781b01c9bba4ec7d Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 2 Aug 2018 16:20:51 +0100 Subject: [PATCH] Bug 20942: Revert - Too many missing dependancies Signed-off-by: Martin Renvoize --- Koha/REST/V1/Patrons/Account.pm | 165 ---------------- api/v1/swagger/definitions.json | 7 +- api/v1/swagger/definitions/account_line.json | 81 -------- .../swagger/definitions/patron_balance.json | 40 ---- api/v1/swagger/paths.json | 3 - api/v1/swagger/paths/patrons_account.json | 65 ------- t/db_dependent/api/v1/patrons_accounts.t | 178 ------------------ 7 files changed, 2 insertions(+), 537 deletions(-) delete mode 100644 Koha/REST/V1/Patrons/Account.pm delete mode 100644 api/v1/swagger/definitions/account_line.json delete mode 100644 api/v1/swagger/definitions/patron_balance.json delete mode 100644 api/v1/swagger/paths/patrons_account.json delete mode 100644 t/db_dependent/api/v1/patrons_accounts.t diff --git a/Koha/REST/V1/Patrons/Account.pm b/Koha/REST/V1/Patrons/Account.pm deleted file mode 100644 index a431998628..0000000000 --- a/Koha/REST/V1/Patrons/Account.pm +++ /dev/null @@ -1,165 +0,0 @@ -package Koha::REST::V1::Patrons::Account; - -# 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 Try::Tiny; - -=head1 NAME - -Koha::REST::V1::Patrons::Account - -=head1 API - -=head2 Methods - -=head3 get - -Controller function that handles retrieving a patron's account balance - -=cut - -sub get { - my $c = shift->openapi->valid_input or return; - - my $patron_id = $c->validation->param('patron_id'); - my $patron = Koha::Patrons->find($patron_id); - - unless ($patron) { - return $c->render( status => 404, openapi => { error => "Patron not found." } ); - } - - my $account = $patron->account; - my $balance; - - $balance->{balance} = $account->balance; - - # get outstanding debits and credits - my $debits = $account->outstanding_debits; - my $credits = $account->outstanding_credits; - - my @debit_lines = map { _to_api( $_->TO_JSON ) } @{ $debits->as_list }; - $balance->{outstanding_debits} = { - total => $debits->total_outstanding, - lines => \@debit_lines - }; - - my @credit_lines = map { _to_api( $_->TO_JSON ) } @{ $credits->as_list }; - $balance->{outstanding_credits} = { - total => $credits->total_outstanding, - lines => \@credit_lines - }; - - return $c->render( status => 200, openapi => $balance ); -} - -=head3 _to_api - -Helper function that maps unblessed Koha::Account::Line objects -into REST API attribute names. - -=cut - -sub _to_api { - my $account_line = shift; - - # Rename attributes - foreach my $column ( keys %{ $Koha::REST::V1::Patrons::Account::to_api_mapping } ) { - my $mapped_column = $Koha::REST::V1::Patrons::Account::to_api_mapping->{$column}; - if ( exists $account_line->{ $column } - && defined $mapped_column ) - { - # key != undef - $account_line->{ $mapped_column } = delete $account_line->{ $column }; - } - elsif ( exists $account_line->{ $column } - && !defined $mapped_column ) - { - # key == undef - delete $account_line->{ $column }; - } - } - - return $account_line; -} - -=head3 _to_model - -Helper function that maps REST API objects into Koha::Account::Line -attribute names. - -=cut - -sub _to_model { - my $account_line = shift; - - foreach my $attribute ( keys %{ $Koha::REST::V1::Patrons::Account::to_model_mapping } ) { - my $mapped_attribute = $Koha::REST::V1::Patrons::Account::to_model_mapping->{$attribute}; - if ( exists $account_line->{ $attribute } - && defined $mapped_attribute ) - { - # key => !undef - $account_line->{ $mapped_attribute } = delete $account_line->{ $attribute }; - } - elsif ( exists $account_line->{ $attribute } - && !defined $mapped_attribute ) - { - # key => undef / to be deleted - delete $account_line->{ $attribute }; - } - } - - return $account_line; -} - -=head2 Global variables - -=head3 $to_api_mapping - -=cut - -our $to_api_mapping = { - accountlines_id => 'account_line_id', - accountno => undef, # removed - accounttype => 'account_type', - amountoutstanding => 'amount_outstanding', - borrowernumber => 'patron_id', - dispute => undef, - issue_id => 'checkout_id', - itemnumber => 'item_id', - manager_id => 'user_id', - note => 'internal_note', -}; - -=head3 $to_model_mapping - -=cut - -our $to_model_mapping = { - account_line_id => 'accountlines_id', - account_type => 'accounttype', - amount_outstanding => 'amountoutstanding', - checkout_id => 'issue_id', - internal_note => 'note', - item_id => 'itemnumber', - patron_id => 'borrowernumber', - user_id => 'manager_id' -}; - -1; diff --git a/api/v1/swagger/definitions.json b/api/v1/swagger/definitions.json index b9c0a29ec5..340b9a978b 100644 --- a/api/v1/swagger/definitions.json +++ b/api/v1/swagger/definitions.json @@ -1,7 +1,4 @@ { - "account_line": { - "$ref": "definitions/account_line.json" - }, "city": { "$ref": "definitions/city.json" }, @@ -17,8 +14,8 @@ "patron": { "$ref": "definitions/patron.json" }, - "patron_balance": { - "$ref": "definitions/patron_balance.json" + "error": { + "$ref": "definitions/error.json" }, "vendor": { "$ref": "definitions/vendor.json" diff --git a/api/v1/swagger/definitions/account_line.json b/api/v1/swagger/definitions/account_line.json deleted file mode 100644 index ba1c7172d4..0000000000 --- a/api/v1/swagger/definitions/account_line.json +++ /dev/null @@ -1,81 +0,0 @@ -{ - "type": "object", - "properties": { - "account_line_id": { - "type": "integer", - "description": "Internal account line identifier" - }, - "checkout_id": { - "type": [ - "integer", - "null" - ], - "description": "Internal identifier for the checkout the account line is related to" - }, - "patron_id": { - "type": "integer", - "description": "Internal identifier for the patron the account line belongs to" - }, - "item_id": { - "type": [ - "integer", - "null" - ], - "description": "Internal identifier for the item the account line is related to" - }, - "date": { - "type": "string", - "format": "date", - "description": "Date the account line was created" - }, - "amount": { - "type": "number", - "description": "Account line amount" - }, - "description": { - "type": [ - "string", - "null" - ], - "description": "Account line description" - }, - "account_type": { - "type": "string", - "description": "Account line type" - }, - "payment_type": { - "type": [ - "string", - "null" - ], - "description": "Payment type" - }, - "amount_outstanding": { - "type": "number", - "description": "Outstanding amount" - }, - "last_increment": { - "type": [ - "number", - "null" - ], - "description": "The amount the line was increased last time" - }, - "timestamp": { - "type": "string", - "format": "date-time", - "description": "Timestamp for the latest line update" - }, - "internal_note": { - "type": [ - "string", - "null" - ], - "description": "Internal note" - }, - "user_id": { - "type": "integer", - "description": "Internal patron identifier for the staff member that introduced the account line" - } - } -} diff --git a/api/v1/swagger/definitions/patron_balance.json b/api/v1/swagger/definitions/patron_balance.json deleted file mode 100644 index b8c0330ef1..0000000000 --- a/api/v1/swagger/definitions/patron_balance.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "type": "object", - "properties": { - "balance": { - "type": "number", - "description": "Signed decimal number" - }, - "outstanding_credits": { - "properties": { - "total": { - "type": "number" - }, - "lines": { - "type": "array", - "items": { - "$ref": "account_line.json" - } - } - } - }, - "outstanding_debits": { - "type": "object", - "properties": { - "total": { - "type": "number" - }, - "lines": { - "type": "array", - "items": { - "$ref": "account_line.json" - } - } - } - } - }, - "additionalProperties": false, - "required": [ - "balance" - ] -} diff --git a/api/v1/swagger/paths.json b/api/v1/swagger/paths.json index fb5f7a30a5..6451ec3a37 100644 --- a/api/v1/swagger/paths.json +++ b/api/v1/swagger/paths.json @@ -26,9 +26,6 @@ "/patrons/{patron_id}": { "$ref": "paths/patrons.json#/~1patrons~1{patron_id}" }, - "/patrons/{patron_id}/account": { - "$ref": "paths/patrons_account.json#/~1patrons~1{patron_id}~1account" - }, "/illrequests": { "$ref": "paths/illrequests.json#/~1illrequests" } diff --git a/api/v1/swagger/paths/patrons_account.json b/api/v1/swagger/paths/patrons_account.json deleted file mode 100644 index 5a2a0d0056..0000000000 --- a/api/v1/swagger/paths/patrons_account.json +++ /dev/null @@ -1,65 +0,0 @@ -{ - "/patrons/{patron_id}/account": { - "get": { - "x-mojo-to": "Patrons::Account#get", - "operationId": "getPatronAccount", - "tags": [ - "patron" - ], - "parameters": [ - { - "$ref": "../parameters.json#/patron_id_pp" - } - ], - "produces": [ - "application/json" - ], - "responses": { - "200": { - "description": "Patron's account balance", - "schema": { - "$ref": "../definitions.json#/patron_balance" - } - }, - "401": { - "description": "Authentication required", - "schema": { - "$ref": "../definitions.json#/error" - } - }, - "403": { - "description": "Access forbidden", - "schema": { - "$ref": "../definitions.json#/error" - } - }, - "404": { - "description": "Patron not found", - "schema": { - "$ref": "../definitions.json#/error" - } - }, - "500": { - "description": "Internal server error", - "schema": { - "$ref": "../definitions.json#/error" - } - }, - "503": { - "description": "Under maintenance", - "schema": { - "$ref": "../definitions.json#/error" - } - } - }, - "x-koha-authorization": { - "allow-owner": true, - "allow-guarantor": true, - "permissions": { - "borrowers": "edit_borrowers", - "updatecharges": "remaining_permissions" - } - } - } - } -} diff --git a/t/db_dependent/api/v1/patrons_accounts.t b/t/db_dependent/api/v1/patrons_accounts.t deleted file mode 100644 index 977890c337..0000000000 --- a/t/db_dependent/api/v1/patrons_accounts.t +++ /dev/null @@ -1,178 +0,0 @@ -#!/usr/bin/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 => 1; - -use Test::Mojo; -use Test::Warn; - -use t::lib::TestBuilder; -use t::lib::Mocks; - -use C4::Accounts qw(manualinvoice); -use C4::Auth; -use Koha::Account::Line; - -my $schema = Koha::Database->new->schema; -my $builder = t::lib::TestBuilder->new; - -# FIXME: sessionStorage defaults to mysql, but it seems to break transaction handling -# this affects the other REST api tests -t::lib::Mocks::mock_preference( 'SessionStorage', 'tmp' ); - -my $remote_address = '127.0.0.1'; -my $t = Test::Mojo->new('Koha::REST::V1'); - -subtest 'get_balance() tests' => sub { - - plan tests => 12; - - $schema->storage->txn_begin; - - my ( $patron_id, $session_id ) = create_user_and_session({ authorized => 0 }); - my $patron = Koha::Patrons->find($patron_id); - my $account = $patron->account; - - my $tx = $t->ua->build_tx(GET => "/api/v1/patrons/$patron_id/account"); - $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_is( - { balance => 0.00, - outstanding_debits => { total => 0, lines => [] }, - outstanding_credits => { total => 0, lines => [] } - } - ); - - my $account_line_1 = Koha::Account::Line->new( - { - borrowernumber => $patron->borrowernumber, - date => \'NOW()', - amount => 50, - description => "A description", - accounttype => "N", # New card - amountoutstanding => 50, - manager_id => $patron->borrowernumber, - } - )->store(); - $account_line_1->discard_changes; - - my $account_line_2 = Koha::Account::Line->new( - { - borrowernumber => $patron->borrowernumber, - date => \'NOW()', - amount => 50.01, - description => "A description", - accounttype => "N", # New card - amountoutstanding => 50.01, - manager_id => $patron->borrowernumber, - } - )->store(); - $account_line_2->discard_changes; - - $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" ); - $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_is( - { balance => 100.01, - outstanding_debits => { - total => 100.01, - lines => [ - Koha::REST::V1::Patrons::Account::_to_api( $account_line_1->TO_JSON ), - Koha::REST::V1::Patrons::Account::_to_api( $account_line_2->TO_JSON ) - ] - }, - outstanding_credits => { - total => 0, - lines => [] - } - } - ); - - $account->pay( - { amount => 100.01, - note => 'He paid!', - description => 'Finally!', - library_id => $patron->branchcode, - account_type => 'Pay', - offset_type => 'Payment' - } - ); - - $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" ); - $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_is( - { balance => 0, - outstanding_debits => { total => 0, lines => [] }, - outstanding_credits => { total => 0, lines => [] } - } - ); - - # add a credit - my $credit_line = $account->add_credit({ amount => 10, user_id => $patron->id }); - # re-read from the DB - $credit_line->discard_changes; - $tx = $t->ua->build_tx( GET => "/api/v1/patrons/$patron_id/account" ); - $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_is( - { balance => -10, - outstanding_debits => { - total => 0, - lines => [] - }, - outstanding_credits => { - total => -10, - lines => [ Koha::REST::V1::Patrons::Account::_to_api( $credit_line->TO_JSON ) ] - } - } - ); - - $schema->storage->txn_rollback; -}; - -sub create_user_and_session { - - my $args = shift; - my $flags = ( $args->{authorized} ) ? 16 : 0; - - my $user = $builder->build( - { - source => 'Borrower', - value => { - flags => $flags, - gonenoaddress => 0, - lost => 0, - email => 'nobody@example.com', - emailpro => 'nobody@example.com', - B_email => 'nobody@example.com' - } - } - ); - - # Create a session for the authorized user - my $session = C4::Auth::get_session(''); - $session->param( 'number', $user->{borrowernumber} ); - $session->param( 'id', $user->{userid} ); - $session->param( 'ip', '127.0.0.1' ); - $session->param( 'lasttime', time() ); - $session->flush; - - return ( $user->{borrowernumber}, $session->id ); -} -- 2.39.5