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';
32 use Scalar::Util qw(blessed);
34 use List::Util 'shuffle';
42 Method that handles adding a new Koha::Hold object
47 my $c = shift->openapi->valid_input or return;
50 my $body = $c->validation->param('body');
51 my $club_id = $c->validation->param('club_id');
55 my $biblio_id = $body->{biblio_id};
56 my $pickup_library_id = $body->{pickup_library_id};
57 my $item_id = $body->{item_id};
58 my $item_type = $body->{item_type};
59 my $expiration_date = $body->{expiration_date};
60 my $notes = $body->{notes};
61 my $default_patron_home = $body->{default_patron_home};
63 if ( $item_id and $biblio_id ) {
65 # check they are consistent
66 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
71 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
75 $biblio = Koha::Biblios->find($biblio_id);
79 my $item = Koha::Items->find($item_id);
84 openapi => { error => "item_id not found." }
88 $biblio = $item->biblio;
92 $biblio = Koha::Biblios->find($biblio_id);
97 openapi => { error => "At least one of biblio_id, item_id should be given" }
104 openapi => "Biblio not found."
108 # AddReserve expects date to be in syspref format
109 if ($expiration_date) {
110 $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
113 my $club_hold = Koha::Club::Hold::add({
115 biblio_id => $biblio->biblionumber,
117 pickup_library_id => $pickup_library_id,
118 expiration_date => $expiration_date,
120 item_type => $item_type,
121 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($_);
145 Helper function that maps unblessed Koha::Club::Hold objects into REST api
151 my $club_hold = shift;
154 foreach my $column ( keys %{ $Koha::REST::V1::Clubs::Holds::to_api_mapping } ) {
155 my $mapped_column = $Koha::REST::V1::Clubs::Holds::to_api_mapping->{$column};
156 if ( exists $club_hold->{ $column }
157 && defined $mapped_column )
160 $club_hold->{ $mapped_column } = delete $club_hold->{ $column };
162 elsif ( exists $club_hold->{ $column }
163 && !defined $mapped_column )
166 delete $club_hold->{ $column };
170 # Calculate the 'restricted' field
174 =head3 $to_api_mapping
178 our $to_api_mapping = {
179 id => 'club_hold_id',
180 club_id => 'club_id',
181 biblio_id => 'biblio_id',