1 package Koha::REST::V1::Clubs::Holds;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Mojo::Base 'Mojolicious::Controller';
29 use Koha::DateUtils qw( dt_from_string output_pref );
31 use Scalar::Util qw( blessed );
32 use Try::Tiny qw( catch try );
40 Method that handles adding a new Koha::Hold object
45 my $c = shift->openapi->valid_input or return;
48 my $body = $c->validation->param('body');
49 my $club_id = $c->validation->param('club_id');
53 my $biblio_id = $body->{biblio_id};
54 my $pickup_library_id = $body->{pickup_library_id};
55 my $item_id = $body->{item_id};
56 my $item_type = $body->{item_type};
57 my $expiration_date = $body->{expiration_date};
58 my $notes = $body->{notes};
59 my $default_patron_home = $body->{default_patron_home};
61 if ( $item_id and $biblio_id ) {
63 # check they are consistent
64 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
69 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
73 $biblio = Koha::Biblios->find($biblio_id);
77 my $item = Koha::Items->find($item_id);
82 openapi => { error => "Item not found" }
86 $biblio = $item->biblio;
90 $biblio = Koha::Biblios->find($biblio_id);
95 openapi => { error => "At least one of biblio_id, item_id should be given" }
102 openapi => { error => "Biblio not found" }
106 # AddReserve expects date to be in syspref format
107 if ($expiration_date) {
108 $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
111 my $club_hold = Koha::Club::Hold::add(
114 biblio_id => $biblio->biblionumber,
116 pickup_library_id => $pickup_library_id,
117 expiration_date => $expiration_date,
119 item_type => $item_type,
120 default_patron_home => $default_patron_home
126 openapi => $club_hold->to_api
131 if ($_->isa('Koha::Exceptions::ClubHold::NoPatrons')) {
134 openapi => { error => $_->description }
139 $c->unhandled_exception($_);