From 0f82d31f3bdf0c1c6abe78f889896d61f5fae848 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 1 Apr 2024 20:52:46 +0000 Subject: [PATCH] Bug 36481: Add GET /libraries/:library_id/cash_registers This patch adds the mentioned route. For the task it: * Adds Koha::Cash::Register->to_api_mapping with trivial mappings * Adds a cash_register object definition on the API spec * Adds a controller to handle requests * Adds tests for the new endpoint To test: 1. Apply this patch 2. Run: $ ktd --shell k$ qa => SUCCESS: All green! Tests pass! 3. Play with Postman! 4. Sign off :-D Signed-off-by: David Nind Signed-off-by: Pedro Amorim Signed-off-by: Katrin Fischer (cherry picked from commit ee52b5c252c4fc5defadc28922c422ffd85b1877) Signed-off-by: Fridolin Somers --- Koha/Cash/Register.pm | 14 ++++++ Koha/REST/V1/Libraries.pm | 31 ++++++++++++ api/v1/swagger/definitions/cash_register.yaml | 33 +++++++++++++ api/v1/swagger/paths/libraries.yaml | 42 +++++++++++++++++ api/v1/swagger/swagger.yaml | 7 +++ t/db_dependent/api/v1/libraries.t | 47 ++++++++++++++++++- 6 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 api/v1/swagger/definitions/cash_register.yaml diff --git a/Koha/Cash/Register.pm b/Koha/Cash/Register.pm index 72288bdadb..ef83180f32 100644 --- a/Koha/Cash/Register.pm +++ b/Koha/Cash/Register.pm @@ -233,6 +233,20 @@ sub add_cashup { return Koha::Cash::Register::Cashup->_new_from_dbic($rs); } +=head3 to_api_mapping + +This method returns the mapping for representing a Koha::Cash::Register object +on the API. + +=cut + +sub to_api_mapping { + return { + branch => 'library_id', + id => 'cash_register_id', + }; +} + =head2 Internal methods =cut diff --git a/Koha/REST/V1/Libraries.pm b/Koha/REST/V1/Libraries.pm index c4b4f0e7ef..492a3495c3 100644 --- a/Koha/REST/V1/Libraries.pm +++ b/Koha/REST/V1/Libraries.pm @@ -200,4 +200,35 @@ sub list_desks { }; } +=head3 list_cash_registers + +Controller function that handles retrieving the library's cash registers + +=cut + +sub list_cash_registers { + my $c = shift->openapi->valid_input or return; + + return $c->render( status => 404, openapi => { error => "Feature disabled" } ) + unless C4::Context->preference('UseCashRegisters'); + + return try { + my $library = Koha::Libraries->find( $c->param('library_id') ); + + unless ($library) { + return $c->render( + status => 404, + openapi => { error => "Library not found" } + ); + } + + return $c->render( + status => 200, + openapi => $c->objects->to_api( $library->cash_registers ) + ); + } catch { + $c->unhandled_exception($_); + }; +} + 1; diff --git a/api/v1/swagger/definitions/cash_register.yaml b/api/v1/swagger/definitions/cash_register.yaml new file mode 100644 index 0000000000..6eb39b5a33 --- /dev/null +++ b/api/v1/swagger/definitions/cash_register.yaml @@ -0,0 +1,33 @@ +--- +type: object +properties: + cash_register_id: + type: integer + description: Internal cash register identifier + name: + type: string + description: The cash register display name + library_id: + type: string + description: Internally assigned library identifier + maxLength: 10 + minLength: 1 + description: + type: string + description: A description + starting_float: + type: + - number + - "null" + description: The starting float this account register should be assigned + archived: + type: boolean + description: If this till is archived + branch_default: + type: boolean + description: If this till is the branch default +additionalProperties: false +required: + - cash_register_id + - name + - library_id diff --git a/api/v1/swagger/paths/libraries.yaml b/api/v1/swagger/paths/libraries.yaml index fb53f637f1..230ee72dcd 100644 --- a/api/v1/swagger/paths/libraries.yaml +++ b/api/v1/swagger/paths/libraries.yaml @@ -346,6 +346,48 @@ x-koha-authorization: permissions: catalogue: 1 +"/libraries/{library_id}/cash_registers": + get: + x-mojo-to: Libraries#list_cash_registers + operationId: listLibraryCashRegisters + tags: + - cash_registers + summary: List the library's cash registers + parameters: + - $ref: "../swagger.yaml#/parameters/match" + - $ref: "../swagger.yaml#/parameters/order_by" + - $ref: "../swagger.yaml#/parameters/page" + - $ref: "../swagger.yaml#/parameters/per_page" + - $ref: "../swagger.yaml#/parameters/q_param" + - $ref: "../swagger.yaml#/parameters/q_body" + - $ref: "../swagger.yaml#/parameters/request_id_header" + produces: + - application/json + responses: + 200: + description: A list of desks for the library + schema: + type: array + items: + $ref: "../swagger.yaml#/definitions/cash_register" + 404: + description: Resource 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: + catalogue: 1 /public/libraries: get: x-mojo-to: Libraries#list diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index 47ce826f5e..823723c589 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -22,6 +22,8 @@ definitions: $ref: ./definitions/booking.yaml bundle_link: $ref: ./definitions/bundle_link.yaml + cash_register: + $ref: ./definitions/cash_register.yaml cashup: $ref: ./definitions/cashup.yaml checkout: @@ -409,6 +411,8 @@ paths: $ref: ./paths/libraries.yaml#/~1libraries "/libraries/{library_id}": $ref: "./paths/libraries.yaml#/~1libraries~1{library_id}" + "/libraries/{library_id}/cash_registers": + $ref: "./paths/libraries.yaml#/~1libraries~1{library_id}~1cash_registers" "/libraries/{library_id}/desks": $ref: "./paths/libraries.yaml#/~1libraries~1{library_id}~1desks" "/oauth/login/{provider_code}/{interface}": @@ -1027,6 +1031,9 @@ tags: - description: "Manage bibliographic records\n" name: biblios x-displayName: Biblios + - description: "Manage cash registers\n" + name: cash_registers + x-displayName: Cash registers - description: "Manage cash register cashups\n" name: cashups x-displayName: Cashups diff --git a/t/db_dependent/api/v1/libraries.t b/t/db_dependent/api/v1/libraries.t index 02f28e034d..e285bb1d99 100755 --- a/t/db_dependent/api/v1/libraries.t +++ b/t/db_dependent/api/v1/libraries.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 6; +use Test::More tests => 7; use Test::Mojo; use Test::Warn; @@ -356,3 +356,48 @@ subtest 'list_desks() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'list_cash_registers() tests' => sub { + + plan tests => 11; + + $schema->storage->txn_begin; + + my $library = $builder->build_object( { class => 'Koha::Libraries' } ); + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 4 } + } + ); + my $password = 'thePassword123'; + $patron->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $patron->userid; + + t::lib::Mocks::mock_preference( 'UseCashRegisters', 0 ); + + $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode . "/cash_registers" )->status_is(404) + ->json_is( '/error' => q{Feature disabled} ); + + my $non_existent_code = $library->branchcode; + $library->delete; + + t::lib::Mocks::mock_preference( 'UseCashRegisters', 1 ); + + $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $non_existent_code . "/cash_registers" )->status_is(404) + ->json_is( '/error' => 'Library not found' ); + + my $cash_register_1 = + $builder->build_object( { class => 'Koha::Cash::Registers', value => { branch => $library->id } } ); + my $cash_register_2 = + $builder->build_object( { class => 'Koha::Cash::Registers', value => { branch => $library->id } } ); + + my $res = + $t->get_ok( "//$userid:$password@/api/v1/libraries/" . $library->branchcode . "/cash_registers" ) + ->status_is(200)->json_is( '/0/cash_register_id' => $cash_register_1->id ) + ->json_is( '/1/cash_register_id' => $cash_register_2->id )->tx->res->json; + + is( scalar @{$res}, 2 ); + + $schema->storage->txn_rollback; +}; -- 2.39.5