From bdd8adb8b7af1700670dac544eb1cfd15aa25be5 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 1 Mar 2023 15:26:30 +0000 Subject: [PATCH] Bug 21043: (follow-up) Handle exceptions and switch to debit response We were cheating a bit here and expecting a 'debit' to be sent in but a 'line' to be returned. We should really be sending a debit and returning a debit.. so I've update the paths schema as such.. but a little more work needs doing to get it all working. I've also added some code to catch exceptions that can be thrown by Koha::Account->add_debit and added the appropriate 400 errors into the path specs again. --- Koha/REST/V1/Patrons/Account.pm | 31 ++++++++++++++++++++--- api/v1/swagger/paths/patrons_account.yaml | 6 ++++- t/db_dependent/api/v1/patrons_accounts.t | 18 ++++++++++++- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/Koha/REST/V1/Patrons/Account.pm b/Koha/REST/V1/Patrons/Account.pm index ca2850752c..99e9831f9d 100644 --- a/Koha/REST/V1/Patrons/Account.pm +++ b/Koha/REST/V1/Patrons/Account.pm @@ -243,10 +243,12 @@ sub add_debit { Koha::Account::Debit->new_from_api( $c->validation->param('body') ) ->unblessed; - $data->{library_id} = $data->{branchcode}; - $data->{cash_register} = $data->{cash_register_id}; - $data->{item_id} = $data->{itemnumber}; - $data->{interface} = 'api' + $data->{library_id} = delete $data->{branchcode}; + $data->{type} = delete $data->{debit_type_code}; + $data->{cash_register} = delete $data->{cash_register_id}; + $data->{item_id} = delete $data->{itemnumber}; + $data->{transaction_type} = delete $data->{payment_type}; + $data->{interface} = 'api' ; # Should this always be API, or should we allow the API consumer to choose? $data->{user_id} = $patron->borrowernumber ; # Should this be API user OR staff the API may be acting on behalf of? @@ -263,6 +265,27 @@ sub add_debit { ); } catch { + if ( blessed $_ ) { + if ( $_->isa('Koha::Exceptions::Account::RegisterRequired') ) { + return $c->render( + status => 400, + openapi => { error => "$_" } + ); + } + elsif ( $_->isa('Koha::Exceptions::Account::AmountNotPositive') ) { + return $c->render( + status => 400, + openapi => { error => "$_" } + ); + } + elsif ( $_->isa('Koha::Exceptions::Account::UnrecognisedType') ) { + return $c->render( + status => 400, + openapi => { error => "$_" } + ); + } + } + $c->unhandled_exception($_); }; } diff --git a/api/v1/swagger/paths/patrons_account.yaml b/api/v1/swagger/paths/patrons_account.yaml index 9b05e11572..d67cfb55bb 100644 --- a/api/v1/swagger/paths/patrons_account.yaml +++ b/api/v1/swagger/paths/patrons_account.yaml @@ -197,7 +197,11 @@ "201": description: Debit added schema: - $ref: "../swagger.yaml#/definitions/account_line" + $ref: "../swagger.yaml#/definitions/debit" + "400": + description: Bad parameter + schema: + $ref: "../swagger.yaml#/definitions/error" "401": description: Authentication required schema: diff --git a/t/db_dependent/api/v1/patrons_accounts.t b/t/db_dependent/api/v1/patrons_accounts.t index c230b863ff..73b53df858 100755 --- a/t/db_dependent/api/v1/patrons_accounts.t +++ b/t/db_dependent/api/v1/patrons_accounts.t @@ -322,7 +322,7 @@ subtest 'add_debit() tests' => sub { my $debit = { amount => 100, description => "A description", - debit_type => "NEW_CARD", + type => "NEW_CARD", user_id => $patron->borrowernumber, interface => 'test', library_id => $library->id, @@ -368,5 +368,21 @@ subtest 'add_debit() tests' => sub { is( $account->outstanding_debits->total_outstanding, 175, "Debit added to total outstanding debits" ); + # Cash register handling and PAYOUTs + t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 ); + my $payout = { + amount => 10, + description => "A description", + type => "PAYOUT", + payout_type => "CASH", + user_id => $patron->borrowernumber, + interface => 'test', + library_id => $library->id, + }; + + $t->post_ok( + "//$userid:$password@/api/v1/patrons/$patron_id/account/debits" => + json => $payout )->status_is(201); + $schema->storage->txn_rollback; }; -- 2.39.2