From 16d05ac945938110e98fdac66c9f2ec487c05a78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johanna=20R=C3=A4is=C3=A4?= Date: Thu, 24 Oct 2024 11:10:07 +0300 Subject: [PATCH] Bug 38253: Add toggle holds lowest priority via REST API This patch adds a new endpoint to toggle the lowest priority of a hold via the REST API. To test: 1) Apply the patch 2) perl build-resources.PL 3) prove t/db_dependent/api/v1/holds.t Sponsored-by: Koha-Suomi Oy --- Koha/REST/V1/Holds.pm | 24 +++++++++++++++ api/v1/swagger/paths/holds.yaml | 51 +++++++++++++++++++++++++++++++ api/v1/swagger/swagger.yaml | 2 ++ t/db_dependent/api/v1/holds.t | 54 ++++++++++++++++++++++++++++++++- 4 files changed, 130 insertions(+), 1 deletion(-) diff --git a/Koha/REST/V1/Holds.pm b/Koha/REST/V1/Holds.pm index ac9979e0ba..ba11d3d567 100644 --- a/Koha/REST/V1/Holds.pm +++ b/Koha/REST/V1/Holds.pm @@ -554,4 +554,28 @@ sub update_pickup_location { }; } +=head3 toggle_lowest_priority + +Method that handles toggling the lowest priority of a Koha::Hold object + +=cut + +sub toggle_lowest_priority { + my $c = shift->openapi->valid_input or return; + + my $hold_id = $c->param('hold_id'); + my $hold = Koha::Holds->find($hold_id); + + return $c->render_resource_not_found("Hold") + unless $hold; + + return try { + C4::Reserves::ToggleLowestPriority($hold_id); + $hold->discard_changes; # refresh + return $c->render( status => 200, openapi => $hold_id ); + } catch { + $c->unhandled_exception($_); + }; +} + 1; diff --git a/api/v1/swagger/paths/holds.yaml b/api/v1/swagger/paths/holds.yaml index abf00fd726..d7bfc082e5 100644 --- a/api/v1/swagger/paths/holds.yaml +++ b/api/v1/swagger/paths/holds.yaml @@ -747,3 +747,54 @@ x-koha-authorization: permissions: reserveforothers: place_holds +"/holds/{hold_id}/toggle_lowest_priority": + put: + x-mojo-to: Holds#toggle_lowest_priority + operationId: toggleLowestPriority + tags: + - holds + summary: Toggle holds lowest priority + parameters: + - $ref: "../swagger.yaml#/parameters/hold_id_pp" + - name: body + in: body + description: An integer representing the new priority to be set for the hold + required: false + schema: + type: integer + produces: + - application/json + responses: + "200": + description: Toggle the holds lowest priority value + schema: + type: integer + "401": + description: Authentication required + schema: + $ref: "../swagger.yaml#/definitions/error" + "403": + description: Access forbidden + schema: + $ref: "../swagger.yaml#/definitions/error" + "404": + description: Hold not found + schema: + $ref: "../swagger.yaml#/definitions/error" + "409": + description: Unable to perform action on hold + 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: + reserveforothers: modify_holds_priority diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index dd72361380..32f3000d15 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -391,6 +391,8 @@ paths: $ref: "./paths/holds.yaml#/~1holds~1{hold_id}~1priority" "/holds/{hold_id}/suspension": $ref: "./paths/holds.yaml#/~1holds~1{hold_id}~1suspension" + "/holds/{hold_id}/toggle_lowest_priority": + $ref: "./paths/holds.yaml#/~1holds~1{hold_id}~1toggle_lowest_priority" /ill/backends: $ref: ./paths/ill_backends.yaml#/~1ill~1backends "/ill/backends/{ill_backend_id}": diff --git a/t/db_dependent/api/v1/holds.t b/t/db_dependent/api/v1/holds.t index 8632c25940..3f609ed9cb 100755 --- a/t/db_dependent/api/v1/holds.t +++ b/t/db_dependent/api/v1/holds.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 14; +use Test::More tests => 15; use Test::MockModule; use Test::Mojo; use t::lib::TestBuilder; @@ -1547,3 +1547,55 @@ subtest 'delete() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'PUT /holds/{hold_id}/toggle_lowest_priority tests' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + + my $password = 'AbcdEFG123'; + + my $library_1 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); + + my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 0 } } ); + $patron->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $patron->userid; + $builder->build( + { + source => 'UserPermission', + value => { + borrowernumber => $patron->borrowernumber, + module_bit => 6, + code => 'modify_holds_priority', + }, + } + ); + + # Disable logging + t::lib::Mocks::mock_preference( 'HoldsLog', 0 ); + t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); + + my $biblio = $builder->build_sample_biblio; + + # biblio-level hold + my $hold = Koha::Holds->find( + AddReserve( + { + branchcode => $library_1->branchcode, + borrowernumber => $patron->borrowernumber, + biblionumber => $biblio->biblionumber, + priority => 1, + itemnumber => undef, + } + ) + ); + + $t->put_ok( "//$userid:$password@/api/v1/holds/0" . "/toggle_lowest_priority" )->status_is(404); + + $t->put_ok( "//$userid:$password@/api/v1/holds/" . $hold->id . "/toggle_lowest_priority" )->status_is(200); + + is( $hold->discard_changes->lowest_priority, 1, 'Priority set to lowest' ); + + $schema->storage->txn_rollback; +}; -- 2.34.1