From e564621ce93ec41065efbc2aafa631776aecfdf9 Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Wed, 6 Apr 2016 13:12:51 +0200 Subject: [PATCH] Bug 16213: Allow to select hold's itemtype when using API Depends on bug 15533 and bug 13903 Test plan: 1/ Use your usual "REST testing" tool to place a title-level hold with an itemtype. The request should look like this: POST /api/v1/reserves { "borrowernumber": 1234, "biblionumber": 456, "branchcode": "CPL", "itemtype": "A" } 2/ Check that the hold was placed and the itemtype is correctly selected 3/ prove t/db_dependent/api/v1/reserves.t Signed-off-by: Benjamin Rokseth --- Koha/REST/V1/Reserve.pm | 4 +++- api/v1/swagger.json | 4 ++++ t/db_dependent/api/v1/reserves.t | 37 ++++++++++++++++++++++++++++++------- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/Koha/REST/V1/Reserve.pm b/Koha/REST/V1/Reserve.pm index 69e6e98..cceb997 100644 --- a/Koha/REST/V1/Reserve.pm +++ b/Koha/REST/V1/Reserve.pm @@ -49,6 +49,8 @@ sub add { my $itemnumber = $body->{itemnumber}; my $branchcode = $body->{branchcode}; my $expirationdate = $body->{expirationdate}; + my $itemtype = $body->{itemtype}; + my $borrower = Koha::Patrons->find($borrowernumber); unless ($borrower) { return $c->$cb({error => "Borrower not found"}, 404); @@ -98,7 +100,7 @@ sub add { my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber, $biblionumber, undef, $priority, undef, $expirationdate, undef, - $biblio->{title}, $itemnumber); + $biblio->{title}, $itemnumber, undef, $itemtype); unless ($reserve_id) { return $c->$cb({ diff --git a/api/v1/swagger.json b/api/v1/swagger.json index b5c6d0a..7a9d281 100644 --- a/api/v1/swagger.json +++ b/api/v1/swagger.json @@ -131,6 +131,10 @@ "description": "Reserve end date", "type": "string", "format": "date" + }, + "itemtype": { + "description": "Limit reserve on one itemtype (ignored for item-level holds)", + "type": "string" } } } diff --git a/t/db_dependent/api/v1/reserves.t b/t/db_dependent/api/v1/reserves.t index eeccbfd..02d18ae 100644 --- a/t/db_dependent/api/v1/reserves.t +++ b/t/db_dependent/api/v1/reserves.t @@ -17,8 +17,9 @@ use Modern::Perl; -use Test::More tests => 30; +use Test::More tests => 39; use Test::Mojo; +use t::lib::TestBuilder; use DateTime; @@ -38,6 +39,8 @@ my $t = Test::Mojo->new('Koha::REST::V1'); my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode(); my $branchcode = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode(); +my $builder = t::lib::TestBuilder->new(); +my $itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype }; my $borrower = Koha::Patron->new; $borrower->categorycode( $categorycode ); @@ -54,7 +57,10 @@ $borrower2->store; my $borrowernumber2 = $borrower2->borrowernumber; my $biblionumber = create_biblio('RESTful Web APIs'); -my $itemnumber = create_item($biblionumber, 'TEST000001'); +my $itemnumber = create_item($biblionumber, { + barcode => 'TEST000001', + itype => $itemtype +}); my $reserve_id = C4::Reserves::AddReserve($branchcode, $borrowernumber, $biblionumber, undef, 1, undef, undef, undef, '', $itemnumber); @@ -126,6 +132,27 @@ $t->post_ok("/api/v1/reserves" => json => $post_data) ->json_like('/error', qr/tooManyReserves/); +$t->delete_ok("/api/v1/reserves/$reserve_id") + ->status_is(200); + +$post_data = { + borrowernumber => int($borrowernumber), + biblionumber => int($biblionumber), + branchcode => $branchcode, + itemtype => $itemtype, +}; + +$t->post_ok("/api/v1/reserves" => json => $post_data) + ->status_is(201) + ->json_has('/reserve_id'); + +$reserve_id = $t->tx->res->json->{reserve_id}; + +$t->get_ok("/api/v1/reserves?borrowernumber=$borrowernumber") + ->status_is(200) + ->json_is('/0/reserve_id', $reserve_id) + ->json_is('/0/itemtype', $itemtype); + $dbh->rollback; sub create_biblio { @@ -142,11 +169,7 @@ sub create_biblio { } sub create_item { - my ($biblionumber, $barcode) = @_; - - my $item = { - barcode => $barcode, - }; + my ($biblionumber, $item) = @_; my $itemnumber = C4::Items::AddItem($item, $biblionumber); -- 2.1.4