Line 0
Link Here
|
|
|
1 |
package Koha::REST::V1::Clubs::Holds; |
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
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 |
8 |
# version. |
9 |
# |
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. |
13 |
# |
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. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Mojo::Base 'Mojolicious::Controller'; |
21 |
|
22 |
use C4::Biblio; |
23 |
use C4::Reserves; |
24 |
|
25 |
use Koha::Items; |
26 |
use Koha::Patrons; |
27 |
use Koha::Holds; |
28 |
use Koha::Clubs; |
29 |
use Koha::Club::Hold; |
30 |
use Koha::DateUtils; |
31 |
|
32 |
use Scalar::Util qw(blessed); |
33 |
use Try::Tiny; |
34 |
use List::Util 'shuffle'; |
35 |
|
36 |
=head1 API |
37 |
|
38 |
=head2 Class methods |
39 |
|
40 |
=head3 add |
41 |
|
42 |
Method that handles adding a new Koha::Hold object |
43 |
|
44 |
=cut |
45 |
|
46 |
sub add { |
47 |
my $c = shift->openapi->valid_input or return; |
48 |
|
49 |
return try { |
50 |
my $body = $c->validation->param('body'); |
51 |
my $club_id = $c->validation->param('club_id'); |
52 |
|
53 |
my $biblio; |
54 |
|
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 |
|
62 |
if ( $item_id and $biblio_id ) { |
63 |
|
64 |
# check they are consistent |
65 |
unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } ) |
66 |
->count > 0 ) |
67 |
{ |
68 |
return $c->render( |
69 |
status => 400, |
70 |
openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" } |
71 |
); |
72 |
} |
73 |
else { |
74 |
$biblio = Koha::Biblios->find($biblio_id); |
75 |
} |
76 |
} |
77 |
elsif ($item_id) { |
78 |
my $item = Koha::Items->find($item_id); |
79 |
|
80 |
unless ($item) { |
81 |
return $c->render( |
82 |
status => 404, |
83 |
openapi => { error => "item_id not found." } |
84 |
); |
85 |
} |
86 |
else { |
87 |
$biblio = $item->biblio; |
88 |
} |
89 |
} |
90 |
elsif ($biblio_id) { |
91 |
$biblio = Koha::Biblios->find($biblio_id); |
92 |
} |
93 |
else { |
94 |
return $c->render( |
95 |
status => 400, |
96 |
openapi => { error => "At least one of biblio_id, item_id should be given" } |
97 |
); |
98 |
} |
99 |
|
100 |
unless ($biblio) { |
101 |
return $c->render( |
102 |
status => 400, |
103 |
openapi => "Biblio not found." |
104 |
); |
105 |
} |
106 |
|
107 |
# AddReserve expects date to be in syspref format |
108 |
if ($expiration_date) { |
109 |
$expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) ); |
110 |
} |
111 |
|
112 |
my $club_hold = Koha::Club::Hold::add({ |
113 |
club_id => $club_id, |
114 |
biblio_id => $biblio->biblionumber, |
115 |
item_id => $item_id, |
116 |
pickup_library_id => $pickup_library_id, |
117 |
expiration_date => $expiration_date, |
118 |
notes => $notes, |
119 |
item_type => $item_type |
120 |
}); |
121 |
|
122 |
my $mapping = _to_api($club_hold->unblessed); |
123 |
|
124 |
return $c->render( status => 201, openapi => $mapping ); |
125 |
} |
126 |
catch { |
127 |
if ( blessed $_ and $_->isa('Koha::Exceptions::Object') ) { |
128 |
if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) { |
129 |
my $broken_fk = $_->broken_fk; |
130 |
|
131 |
if ( grep { $_ eq $broken_fk } keys %{$Koha::REST::V1::Clubs::Holds::to_api_mapping} ) { |
132 |
$c->render( |
133 |
status => 404, |
134 |
openapi => $Koha::REST::V1::Clubs::Holds::to_api_mapping->{$broken_fk} . ' not found.' |
135 |
); |
136 |
} |
137 |
else { |
138 |
return $c->render( |
139 |
status => 500, |
140 |
openapi => { error => "Uncaught exception: $_" } |
141 |
); |
142 |
} |
143 |
} |
144 |
else { |
145 |
return $c->render( |
146 |
status => 500, |
147 |
openapi => { error => "$_" } |
148 |
); |
149 |
} |
150 |
} |
151 |
elsif (blessed $_ and $_->isa('Koha::Exceptions::ClubHold')) { |
152 |
return $c->render( |
153 |
status => 500, |
154 |
openapi => { error => $_->description } |
155 |
); |
156 |
} |
157 |
else { |
158 |
return $c->render( |
159 |
status => 500, |
160 |
openapi => { error => "Something went wrong. check the logs." } |
161 |
); |
162 |
} |
163 |
}; |
164 |
} |
165 |
|
166 |
=head3 _to_api |
167 |
|
168 |
Helper function that maps unblessed Koha::Club::Hold objects into REST api |
169 |
attribute names. |
170 |
|
171 |
=cut |
172 |
|
173 |
sub _to_api { |
174 |
my $club_hold = shift; |
175 |
|
176 |
# Rename attributes |
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 ) |
181 |
{ |
182 |
# key != undef |
183 |
$club_hold->{ $mapped_column } = delete $club_hold->{ $column }; |
184 |
} |
185 |
elsif ( exists $club_hold->{ $column } |
186 |
&& !defined $mapped_column ) |
187 |
{ |
188 |
# key == undef |
189 |
delete $club_hold->{ $column }; |
190 |
} |
191 |
} |
192 |
|
193 |
# Calculate the 'restricted' field |
194 |
return $club_hold; |
195 |
} |
196 |
|
197 |
=head3 $to_api_mapping |
198 |
|
199 |
=cut |
200 |
|
201 |
our $to_api_mapping = { |
202 |
id => 'club_hold_id', |
203 |
club_id => 'club_id', |
204 |
biblio_id => 'biblio_id', |
205 |
item_id => 'item_id' |
206 |
}; |
207 |
|
208 |
|
209 |
1; |