From 8d37c975dc10ffc0f95d5b9e6d4f48f0eb912c78 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 20 Jan 2025 16:46:33 -0300 Subject: [PATCH] Bug 38931: Add endpoints for individual credits and debits As the title says, we are missing such endpoints and this patch adds them. To test: 1. Apply this patches 2. Run: $ ktd --shell k$ yarn api:bundle k$ prove t/db_dependent/api/v1/patrons_accounts.t => SUCCESS: The spec builds! => SUCCESS: The tests pass! 3. Verify all the codepaths are covered by tests 4. Sign off :-D Signed-off-by: Tomas Cohen Arazi --- Koha/REST/V1/Patrons/Account.pm | 48 +++++++++++++ api/v1/swagger/paths/patrons_account.yaml | 82 +++++++++++++++++++++++ api/v1/swagger/swagger.yaml | 16 +++++ 3 files changed, 146 insertions(+) diff --git a/Koha/REST/V1/Patrons/Account.pm b/Koha/REST/V1/Patrons/Account.pm index b80217b4bb8..338a4e9e136 100644 --- a/Koha/REST/V1/Patrons/Account.pm +++ b/Koha/REST/V1/Patrons/Account.pm @@ -178,6 +178,30 @@ sub add_credit { }; } +=head3 get_credit + +=cut + +sub get_credit { + my $c = shift->openapi->valid_input or return; + + my $patron = Koha::Patrons->find( $c->param('patron_id') ); + + return $c->render_resource_not_found("Patron") + unless $patron; + + return try { + my $credit = $c->objects->find( $patron->account->credits, $c->param('credit_id') ); + + return $c->render_resource_not_found("Credit") + unless $credit; + + return $c->render( status => 200, openapi => $credit ); + } catch { + $c->unhandled_exception($_); + }; +} + =head3 list_debits =cut @@ -199,6 +223,30 @@ sub list_debits { }; } +=head3 get_debit + +=cut + +sub get_debit { + my $c = shift->openapi->valid_input or return; + + my $patron = Koha::Patrons->find( $c->param('patron_id') ); + + return $c->render_resource_not_found("Patron") + unless $patron; + + return try { + my $debit = $c->objects->find( $patron->account->debits, $c->param('debit_id') ); + + return $c->render_resource_not_found("Debit") + unless $debit; + + return $c->render( status => 200, openapi => $debit ); + } catch { + $c->unhandled_exception($_); + }; +} + =head3 add_debit =cut diff --git a/api/v1/swagger/paths/patrons_account.yaml b/api/v1/swagger/paths/patrons_account.yaml index 1ebdf8b8830..3e4bc806e01 100644 --- a/api/v1/swagger/paths/patrons_account.yaml +++ b/api/v1/swagger/paths/patrons_account.yaml @@ -144,6 +144,47 @@ x-koha-authorization: permissions: updatecharges: remaining_permissions +"/patrons/{patron_id}/account/credits/{credit_id}": + get: + x-mojo-to: Patrons::Account#get_credit + operationId: getPatronCredit + tags: + - patrons + summary: Get a patron credit + produces: + - application/json + parameters: + - $ref: "../swagger.yaml#/parameters/patron_id_pp" + - $ref: "../swagger.yaml#/parameters/credit_id_pp" + responses: + "200": + description: A patron credit + schema: + $ref: "../swagger.yaml#/definitions/credit" + "400": + description: Bad request + 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 error + schema: + $ref: "../swagger.yaml#/definitions/error" + "503": + description: Under maintenance + schema: + $ref: "../swagger.yaml#/definitions/error" + x-koha-authorization: + permissions: + borrowers: edit_borrowers + updatecharges: remaining_permissions "/patrons/{patron_id}/account/debits": get: x-mojo-to: Patrons::Account#list_debits @@ -246,3 +287,44 @@ x-koha-authorization: permissions: updatecharges: remaining_permissions +"/patrons/{patron_id}/account/debits/{debit_id}": + get: + x-mojo-to: Patrons::Account#get_debit + operationId: getPatronDebit + tags: + - patrons + summary: Get a patron debit + produces: + - application/json + parameters: + - $ref: "../swagger.yaml#/parameters/patron_id_pp" + - $ref: "../swagger.yaml#/parameters/debit_id_pp" + responses: + "200": + description: A patron debit + schema: + $ref: "../swagger.yaml#/definitions/debit" + "400": + description: Bad request + 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 error + schema: + $ref: "../swagger.yaml#/definitions/error" + "503": + description: Under maintenance + schema: + $ref: "../swagger.yaml#/definitions/error" + x-koha-authorization: + permissions: + borrowers: edit_borrowers + updatecharges: remaining_permissions diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index ff7b94a174e..2a53ab8894b 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -457,6 +457,10 @@ paths: $ref: "./paths/patrons_account.yaml#/~1patrons~1{patron_id}~1account" "/patrons/{patron_id}/account/credits": $ref: "./paths/patrons_account.yaml#/~1patrons~1{patron_id}~1account~1credits" + "/patrons/{patron_id}/account/credits/{credit_id}": + $ref: "./paths/patrons_account.yaml#/~1patrons~1{patron_id}~1account~1credits~1{credit_id}" + "/patrons/{patron_id}/account/debits/{debit_id}": + $ref: "./paths/patrons_account.yaml#/~1patrons~1{patron_id}~1account~1debits~1{debit_id}" "/patrons/{patron_id}/account/debits": $ref: "./paths/patrons_account.yaml#/~1patrons~1{patron_id}~1account~1debits" "/patrons/{patron_id}/extended_attributes": @@ -687,6 +691,18 @@ parameters: name: club_id required: true type: integer + credit_id_pp: + description: Internal credit identifier + in: path + name: credit_id + required: true + type: integer + debit_id_pp: + description: Internal debit identifier + in: path + name: debit_id + required: true + type: integer eholdings_title_id_pp: description: Title internal identifier in: path -- 2.48.1