@@ -, +, @@ --- Koha/REST/V1/Patrons/Account.pm | 34 ++++++++++++++++ api/v1/swagger/paths/patrons_account.yaml | 47 +++++++++++++++++++++++ 2 files changed, 81 insertions(+) --- a/Koha/REST/V1/Patrons/Account.pm +++ a/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; --- a/api/v1/swagger/paths/patrons_account.yaml +++ a/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 --