From 03ec9fc5856fb0db84cc48a75e28901c23c670a9 Mon Sep 17 00:00:00 2001 From: Jan Kissig Date: Mon, 21 Oct 2024 12:38:06 +0000 Subject: [PATCH] Bug 37253: Enhance POST /checkouts endpoint to accept barcode or item_id This patch adds external_id as a body param in POST /checkouts which acts as the items barcode. This enhances the checkouts route to checkout items directly via barcode which could be useful for external tools like Koha Offline Circulation Tool. When item_id and external_id are given a check is performed that both identifiers belong to the same item. Test plan: a) apply patch b) enable system preference RESTBasicAuth c) check out an item via an API testing tool. Be sure that item is not checked out already. Auth: username: koha & password: koha Body JSON: { "external_id" : "39999000011418", "library_id": "CPL", "patron_id": 5 } POST http://localhost:8081/api/v1/checkouts d) check response code 201 and response content Signed-off-by: Matt Blenkinsop --- Koha/REST/V1/Checkouts.pm | 18 +++++++++++++++++- t/db_dependent/api/v1/checkouts.t | 14 +++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Koha/REST/V1/Checkouts.pm b/Koha/REST/V1/Checkouts.pm index fdda8ec04f..39d3b715a7 100644 --- a/Koha/REST/V1/Checkouts.pm +++ b/Koha/REST/V1/Checkouts.pm @@ -219,9 +219,10 @@ sub add { if ($item_id) { $item = Koha::Items->find($item_id); } - if ($barcode) { + else { $item = Koha::Items->find( { barcode => $barcode } ); } + unless ($item) { return $c->render( status => 409, @@ -232,6 +233,21 @@ sub add { ); } + # check that item matches when barcode and item_id were given + if ( $item_id + and $barcode + and ( $item->barcode ne $barcode or $item->id != $item_id ) ) + { + return $c->render( + status => 409, + openapi => { + error => 'Item id and external id belong to different items', + error_code => 'ITEM_ID_NOT_MATCHING_EXTERNAL_ID', + } + ); + } + + my $patron = Koha::Patrons->find($patron_id); unless ($patron) { return $c->render( diff --git a/t/db_dependent/api/v1/checkouts.t b/t/db_dependent/api/v1/checkouts.t index ec0ac749a7..779f00156a 100755 --- a/t/db_dependent/api/v1/checkouts.t +++ b/t/db_dependent/api/v1/checkouts.t @@ -439,7 +439,7 @@ subtest 'get_availability' => sub { subtest 'add checkout' => sub { - plan tests => 12; + plan tests => 14; $schema->storage->txn_begin; my $librarian = $builder->build_object( @@ -469,6 +469,10 @@ subtest 'add checkout' => sub { my $item1_id = $item1->id; my $item1_barcode = $item1->barcode; + my $item2 = $builder->build_sample_item; + my $item2_id = $item2->id; + my $item2_barcode = $item2->barcode; + my %issuingimpossible = (); my %needsconfirmation = (); my %alerts = (); @@ -496,6 +500,14 @@ subtest 'add checkout' => sub { "//$userid:$password@/api/v1/checkouts" => json => { external_id => $item1_barcode, patron_id => $patron_id } ) ->status_is(201); + # mismatch of item_id and barcode when both given + $t->post_ok( + "//$userid:$password@/api/v1/checkouts" => json => { + external_id => $item1_barcode, + item_id => $item2_id, + patron_id => $patron_id + } + )->status_is(409); # Needs confirm %needsconfirmation = ( confirm1 => 1, confirm2 => 'please' ); -- 2.39.5