Bugzilla – Attachment 147458 Details for
Bug 21043
Add POST endpoint for patron debits
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 21043: (follow-up) fix minor bugs and add test case
Bug-21043-follow-up-fix-minor-bugs-and-add-test-ca.patch (text/plain), 5.36 KB, created by
Agustín Moyano
on 2023-02-27 15:39:09 UTC
(
hide
)
Description:
Bug 21043: (follow-up) fix minor bugs and add test case
Filename:
MIME Type:
Creator:
Agustín Moyano
Created:
2023-02-27 15:39:09 UTC
Size:
5.36 KB
patch
obsolete
>From 646106118bbf6a3c028afd157be6272bc9ff115f Mon Sep 17 00:00:00 2001 >From: Agustin Moyano <agustinmoyano@theke.io> >Date: Mon, 27 Feb 2023 15:33:20 +0000 >Subject: [PATCH] Bug 21043: (follow-up) fix minor bugs and add test case > >--- > Koha/REST/V1/Patrons/Account.pm | 8 ++- > api/v1/swagger/definitions/debit.yaml | 1 - > api/v1/swagger/paths/patrons_account.yaml | 2 +- > t/db_dependent/api/v1/patrons_accounts.t | 73 ++++++++++++++++++++++- > 4 files changed, 80 insertions(+), 4 deletions(-) > >diff --git a/Koha/REST/V1/Patrons/Account.pm b/Koha/REST/V1/Patrons/Account.pm >index bed62e2b1f..16a55d9e70 100644 >--- a/Koha/REST/V1/Patrons/Account.pm >+++ b/Koha/REST/V1/Patrons/Account.pm >@@ -220,15 +220,21 @@ sub add_debit { > } > > return try { >- my $data = Koha::Patron::Account::Debit->new_from_api( >+ my $data = Koha::Account::Debit->new_from_api( > $c->validation->param('body') )->unblessed; > >+ $data->{library_id} = $data->{branchcode}; >+ $data->{type} = $data->{debit_type_code}; >+ $data->{cash_register} = $data->{register_id}; >+ $data->{item_id} = $data->{itemnumber}; > $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? > > my $debit = $patron->account->add_debit($data); > > $c->res->headers->location( $c->req->url->to_string . '/' . $debit->id ); >+ $debit->discard_changes; >+ > return $c->render( > status => 201, > openapi => $debit->to_api >diff --git a/api/v1/swagger/definitions/debit.yaml b/api/v1/swagger/definitions/debit.yaml >index 4ffe2b8f4a..aa21ab9379 100644 >--- a/api/v1/swagger/definitions/debit.yaml >+++ b/api/v1/swagger/definitions/debit.yaml >@@ -32,7 +32,6 @@ properties: > type: > - string > - "null" >- readOnly: true > description: Account line description > account_type: > type: >diff --git a/api/v1/swagger/paths/patrons_account.yaml b/api/v1/swagger/paths/patrons_account.yaml >index 584eccc762..9b05e11572 100644 >--- a/api/v1/swagger/paths/patrons_account.yaml >+++ b/api/v1/swagger/paths/patrons_account.yaml >@@ -190,7 +190,7 @@ > description: A JSON object containing debit information > required: true > schema: >- $ref: "../swagger.yaml#/definitions/patron_account_debit" >+ $ref: "../swagger.yaml#/definitions/debit" > produces: > - application/json > responses: >diff --git a/t/db_dependent/api/v1/patrons_accounts.t b/t/db_dependent/api/v1/patrons_accounts.t >index cada141ef3..5ee6540c0e 100755 >--- a/t/db_dependent/api/v1/patrons_accounts.t >+++ b/t/db_dependent/api/v1/patrons_accounts.t >@@ -17,7 +17,7 @@ > > use Modern::Perl; > >-use Test::More tests => 2; >+use Test::More tests => 3; > > use Test::Mojo; > >@@ -268,3 +268,74 @@ subtest 'add_credit() tests' => sub { > > $schema->storage->txn_rollback; > }; >+ >+subtest 'add_debit() tests' => sub { >+ >+ plan tests => 13; >+ >+ $schema->storage->txn_begin; >+ >+ my $patron = $builder->build_object({ >+ class => 'Koha::Patrons', >+ value => { flags => 1 } >+ }); >+ my $password = 'thePassword123'; >+ $patron->set_password({ password => $password, skip_validation => 1 }); >+ my $userid = $patron->userid; >+ >+ my $library = $builder->build_object({ class => 'Koha::Libraries' }); >+ my $patron_id = $patron->id; >+ my $account = $patron->account; >+ >+ is( $account->outstanding_debits->count, 0, 'No outstanding debits for patron' ); >+ is( $account->outstanding_credits->count, 0, 'No outstanding credits for patron' ); >+ >+ my $debit = { >+ amount => 100, >+ description => "A description", >+ debit_type => "NEW_CARD", >+ user_id => $patron->borrowernumber, >+ interface => 'test', >+ library_id => $library->id, >+ }; >+ >+ my $ret = $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/debits" => json => $debit) >+ ->status_is(201)->tx->res->json; >+ my $account_line = Koha::Account::Lines->find( $ret->{account_line_id} ); >+ >+ is_deeply( $ret, $account_line->to_api, 'Line returned correctly' ); >+ >+ is( $account_line->branchcode, >+ $library->id, 'Library id is sored correctly' ); >+ >+ my $outstanding_debits = $account->outstanding_debits; >+ is( $outstanding_debits->count, 1 ); >+ is( $outstanding_debits->total_outstanding, 100 ); >+ >+ my $credit_1 = $account->add_credit( >+ { >+ amount => 10, >+ interface => 'test', >+ } >+ )->store()->apply({ debits => [ $account_line ] }); >+ my $credit_2 = $account->add_credit( >+ { >+ amount => 15, >+ interface => 'test' >+ } >+ )->store()->apply({ debits => [ $account_line ]}); >+ >+ is( $account->outstanding_credits->total_outstanding, 0 ); >+ is( $account->outstanding_debits->total_outstanding, >+ 75, "Credits partially cancelled debit" ); >+ >+ my $account_line_id = $ret->{account_line_id}; >+ >+ $t->post_ok("//$userid:$password@/api/v1/patrons/$patron_id/account/debits" => json => $debit) >+ ->status_is(201); >+ >+ is( $account->outstanding_debits->total_outstanding, >+ 175, "Debit added to total outstanding debits" ); >+ >+ $schema->storage->txn_rollback; >+}; >\ No newline at end of file >-- >2.25.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 21043
:
144615
|
144623
|
147457
|
147458
|
147459
|
147498
|
147566
|
147567
|
147568
|
147642
|
147643
|
150765
|
150766