@@ -, +, @@ response --- 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(-) --- a/Koha/REST/V1/Patrons/Account.pm +++ a/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($_); }; } --- a/api/v1/swagger/paths/patrons_account.yaml +++ a/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: --- a/t/db_dependent/api/v1/patrons_accounts.t +++ a/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; }; --