From d74b226cc55e2a5128912a7cebe4e964c46da319 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 15 Dec 2022 15:50:55 +0000 Subject: [PATCH] Bug 21043: Add debit REST endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch adds an endpoint to create a debit for a patron. Testplan 1. Create a new account debit type (Administration > Debit types) 2. Add a fee with this debit type to a patron’s account via API 3. Make sure that this fee is shown in the accounting overview in the patron’s account in the staff interface 4. Make sure that it is possible to make a payment for this fee --- Koha/REST/V1/Patrons/Account.pm | 34 ++++++++++++++++ api/v1/swagger/paths/patrons_account.yaml | 47 +++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/Koha/REST/V1/Patrons/Account.pm b/Koha/REST/V1/Patrons/Account.pm index 2238ffc075..bed62e2b1f 100644 --- a/Koha/REST/V1/Patrons/Account.pm +++ b/Koha/REST/V1/Patrons/Account.pm @@ -205,4 +205,38 @@ sub list_debits { }; } +=head3 add_debit + +=cut + +sub add_debit { + 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." } ); + } + + return try { + my $data = Koha::Patron::Account::Debit->new_from_api( + $c->validation->param('body') )->unblessed; + + $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 ); + return $c->render( + status => 201, + openapi => $debit->to_api + ); + } + catch { + $c->unhandled_exception($_); + }; +} + 1; diff --git a/api/v1/swagger/paths/patrons_account.yaml b/api/v1/swagger/paths/patrons_account.yaml index 7c4e033826..584eccc762 100644 --- a/api/v1/swagger/paths/patrons_account.yaml +++ b/api/v1/swagger/paths/patrons_account.yaml @@ -177,3 +177,50 @@ permissions: borrowers: edit_borrowers updatecharges: remaining_permissions + post: + x-mojo-to: Patrons::Account#add_debit + operationId: addPatronDebit + tags: + - patrons + summary: Add debit to a patron's account + parameters: + - $ref: "../swagger.yaml#/parameters/patron_id_pp" + - name: body + in: body + description: A JSON object containing debit information + required: true + schema: + $ref: "../swagger.yaml#/definitions/patron_account_debit" + produces: + - application/json + responses: + "201": + description: Debit added + schema: + $ref: "../swagger.yaml#/definitions/account_line" + "401": + description: Authentication required + schema: + $ref: "../swagger.yaml#/definitions/error" + "403": + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + "404": + description: Patron not found + schema: + $ref: "../swagger.yaml#/definitions/error" + "500": + description: | + Internal server error. Possible `error_code` attribute values: + + * `internal_server_error` + schema: + $ref: "../swagger.yaml#/definitions/error" + "503": + description: Under maintenance + schema: + $ref: "../swagger.yaml#/definitions/error" + x-koha-authorization: + permissions: + updatecharges: remaining_permissions -- 2.25.1