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
124 openapi => $club_hold->to_api
128 if ( blessed $_ and $_->isa('Koha::Exceptions::Object') ) {
129 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
130 my $broken_fk = $_->broken_fk;
132 if ( grep { $_ eq $broken_fk } keys %{$Koha::REST::V1::Clubs::Holds::to_api_mapping} ) {
135 openapi => $Koha::REST::V1::Clubs::Holds::to_api_mapping->{$broken_fk} . ' not found.'
141 openapi => { error => "Uncaught exception: $_" }
148 openapi => { error => "$_" }
152 elsif (blessed $_ and $_->isa('Koha::Exceptions::ClubHold')) {
155 openapi => { error => $_->description }
161 openapi => { error => "Something went wrong. check the logs." }
169 Helper function that maps unblessed Koha::Club::Hold objects into REST api
175 my $club_hold = shift;
178 foreach my $column ( keys %{ $Koha::REST::V1::Clubs::Holds::to_api_mapping } ) {
179 my $mapped_column = $Koha::REST::V1::Clubs::Holds::to_api_mapping->{$column};
180 if ( exists $club_hold->{ $column }
181 && defined $mapped_column )
184 $club_hold->{ $mapped_column } = delete $club_hold->{ $column };
186 elsif ( exists $club_hold->{ $column }
187 && !defined $mapped_column )
190 delete $club_hold->{ $column };
194 # Calculate the 'restricted' field
198 =head3 $to_api_mapping
202 our $to_api_mapping = {
203 id => 'club_hold_id',
204 club_id => 'club_id',
205 biblio_id => 'biblio_id',