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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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};
62 if ( $item_id and $biblio_id ) {
64 # check they are consistent
65 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
70 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
74 $biblio = Koha::Biblios->find($biblio_id);
78 my $item = Koha::Items->find($item_id);
83 openapi => { error => "item_id not found." }
87 $biblio = $item->biblio;
91 $biblio = Koha::Biblios->find($biblio_id);
96 openapi => { error => "At least one of biblio_id, item_id should be given" }
103 openapi => "Biblio not found."
107 # AddReserve expects date to be in syspref format
108 if ($expiration_date) {
109 $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
112 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
122 my $mapping = _to_api($club_hold->unblessed);
124 return $c->render( status => 201, openapi => $mapping );
127 if ( blessed $_ and $_->isa('Koha::Exceptions::Object') ) {
128 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
129 my $broken_fk = $_->broken_fk;
131 if ( grep { $_ eq $broken_fk } keys %{$Koha::REST::V1::Clubs::Holds::to_api_mapping} ) {
134 openapi => $Koha::REST::V1::Clubs::Holds::to_api_mapping->{$broken_fk} . ' not found.'
140 openapi => { error => "Uncaught exception: $_" }
147 openapi => { error => "$_" }
151 elsif (blessed $_ and $_->isa('Koha::Exceptions::ClubHold')) {
154 openapi => { error => $_->description }
160 openapi => { error => "Something went wrong. check the logs." }
168 Helper function that maps unblessed Koha::Club::Hold objects into REST api
174 my $club_hold = shift;
177 foreach my $column ( keys %{ $Koha::REST::V1::Clubs::Holds::to_api_mapping } ) {
178 my $mapped_column = $Koha::REST::V1::Clubs::Holds::to_api_mapping->{$column};
179 if ( exists $club_hold->{ $column }
180 && defined $mapped_column )
183 $club_hold->{ $mapped_column } = delete $club_hold->{ $column };
185 elsif ( exists $club_hold->{ $column }
186 && !defined $mapped_column )
189 delete $club_hold->{ $column };
193 # Calculate the 'restricted' field
197 =head3 $to_api_mapping
201 our $to_api_mapping = {
202 id => 'club_hold_id',
203 club_id => 'club_id',
204 biblio_id => 'biblio_id',