From 2db98432d85568943c92bb1cd092960edb1e64eb Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 11 Mar 2021 15:47:10 -0300 Subject: [PATCH] Bug 27932: Add GET /biblios/:biblio_id/pickup_locations route This patch adds the mentioned route. It does so by: - Adding the new path in paths.json - Adding the full route spec in biblios.json - Adds a controller method that takes care of the task To test: 1. Apply this patches 2. Run: $ kshell k$ prove t/db_dependent/api/v1/biblios.t => SUCCESS: Tests pass! 3. Play with your favourite REST tool. Pay special care to the AllowHoldPolicyOverride syspref and the expected behaviors. => SUCCESS: All works as expected 4. Sign off :-D Signed-off-by: Martin Renvoize Signed-off-by: Jonathan Druart --- Koha/REST/V1/Biblios.pm | 72 +++++++++++++++++++++ api/v1/swagger/paths.json | 3 + api/v1/swagger/paths/biblios.json | 101 ++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) diff --git a/Koha/REST/V1/Biblios.pm b/Koha/REST/V1/Biblios.pm index efe7c5dfe8..aa44f30e64 100644 --- a/Koha/REST/V1/Biblios.pm +++ b/Koha/REST/V1/Biblios.pm @@ -23,6 +23,7 @@ use Koha::Biblios; use Koha::RecordProcessor; use C4::Biblio qw(DelBiblio); +use List::MoreUtils qw(any); use MARC::Record::MiJ; use Try::Tiny; @@ -268,4 +269,75 @@ sub get_items { }; } +=head3 pickup_locations + +Method that returns the possible pickup_locations for a given biblio +used for building the dropdown selector + +=cut + +sub pickup_locations { + my $c = shift->openapi->valid_input or return; + + my $biblio_id = $c->validation->param('biblio_id'); + my $biblio = Koha::Biblios->find( $biblio_id ); + + unless ($biblio) { + return $c->render( + status => 404, + openapi => { error => "Biblio not found" } + ); + } + + my $patron_id = delete $c->validation->output->{patron_id}; + my $patron = Koha::Patrons->find( $patron_id ); + + unless ($patron) { + return $c->render( + status => 400, + openapi => { error => "Patron not found" } + ); + } + + return try { + + my $ps_set = $biblio->pickup_locations( { patron => $patron } ); + + my $pickup_locations = $c->objects->search( $ps_set ); + my @response = (); + + if ( C4::Context->preference('AllowHoldPolicyOverride') ) { + + my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } ); + my $libraries = $c->objects->search($libraries_rs); + + @response = map { + my $library = $_; + $library->{needs_override} = ( + any { $_->{library_id} eq $library->{library_id} } + @{$pickup_locations} + ) + ? Mojo::JSON->false + : Mojo::JSON->true; + $library; + } @{$libraries}; + + return $c->render( + status => 200, + openapi => \@response + ); + } + + @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations}; + + return $c->render( + status => 200, + openapi => \@response + ); + } + catch { + $c->unhandled_exception($_); + }; +} + 1; diff --git a/api/v1/swagger/paths.json b/api/v1/swagger/paths.json index 917100b670..ba0c1e8b0f 100644 --- a/api/v1/swagger/paths.json +++ b/api/v1/swagger/paths.json @@ -23,6 +23,9 @@ "/biblios/{biblio_id}/items": { "$ref": "paths/biblios.json#/~1biblios~1{biblio_id}~1items" }, + "/biblios/{biblio_id}/pickup_locations": { + "$ref": "paths/biblios.json#/~1biblios~1{biblio_id}~1pickup_locations" + }, "/cash_registers/{cash_register_id}/cashups": { "$ref": "paths/cash_registers.json#/~1cash_registers~1{cash_register_id}~1cashups" }, diff --git a/api/v1/swagger/paths/biblios.json b/api/v1/swagger/paths/biblios.json index 0b95576204..9781202c59 100644 --- a/api/v1/swagger/paths/biblios.json +++ b/api/v1/swagger/paths/biblios.json @@ -232,6 +232,107 @@ } } }, + "/biblios/{biblio_id}/pickup_locations": { + "get": { + "x-mojo-to": "Biblios#pickup_locations", + "operationId": "getBiblioPickupLocations", + "tags": [ + "biblios", + "pickup_locations" + ], + "parameters": [ + { + "$ref": "../parameters.json#/biblio_id_pp" + }, + { + "name": "patron_id", + "in": "query", + "description": "Internal patron identifier", + "required": true, + "type": "integer" + }, + { + "$ref": "../parameters.json#/match" + }, + { + "$ref": "../parameters.json#/order_by" + }, + { + "$ref": "../parameters.json#/page" + }, + { + "$ref": "../parameters.json#/per_page" + }, + { + "$ref": "../parameters.json#/q_param" + }, + { + "$ref": "../parameters.json#/q_body" + }, + { + "$ref": "../parameters.json#/q_header" + } + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "responses": { + "200": { + "description": "Biblio pickup locations", + "schema": { + "type": "array", + "items": { + "$ref": "../definitions.json#/library" + } + } + }, + "400": { + "description": "Missing or wrong parameters", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "401": { + "description": "Authentication required", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "403": { + "description": "Access forbidden", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "404": { + "description": "Biblio not found", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "500": { + "description": "Internal server error", + "schema": { + "$ref": "../definitions.json#/error" + } + }, + "503": { + "description": "Under maintenance", + "schema": { + "$ref": "../definitions.json#/error" + } + } + }, + "x-koha-authorization": { + "permissions": { + "reserveforothers": "place_holds" + } + } + } + }, "/public/biblios/{biblio_id}": { "get": { "x-mojo-to": "Biblios#get_public", -- 2.39.5