Bug 20006: (follow-up) Fix holds.js
[koha.git] / Koha / REST / V1 / Hold.pm
blobcdc7aa12d8e292b98ace303f1356eb005ba7c242
1 package Koha::REST::V1::Hold;
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
8 # version.
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.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Controller';
22 use C4::Biblio;
23 use C4::Reserves;
25 use Koha::Items;
26 use Koha::Patrons;
27 use Koha::Holds;
28 use Koha::DateUtils;
30 use Try::Tiny;
32 =head1 API
34 =head2 Class methods
36 =head3 list
38 Mehtod that handles listing Koha::Hold objects
40 =cut
42 sub list {
43 my $c = shift->openapi->valid_input or return;
45 return try {
46 my $holds_set = Koha::Holds->new;
47 my $holds = $c->objects->search( $holds_set, \&_to_model, \&_to_api );
48 return $c->render( status => 200, openapi => $holds );
50 catch {
51 if ( blessed $_ && $_->isa('Koha::Exceptions') ) {
52 return $c->render(
53 status => 500,
54 openapi => { error => "$_" }
57 else {
58 return $c->render(
59 status => 500,
60 openapi => { error => "Something went wrong, check Koha logs for details." }
66 =head3 add
68 Method that handles adding a new Koha::Hold object
70 =cut
72 sub add {
73 my $c = shift->openapi->valid_input or return;
75 return try {
76 my $body = $c->validation->param('body');
78 my $biblio;
80 my $biblio_id = $body->{biblio_id};
81 my $pickup_library_id = $body->{pickup_library_id};
82 my $item_id = $body->{item_id};
83 my $patron_id = $body->{patron_id};
84 my $item_type = $body->{item_type};
85 my $expiration_date = $body->{expiration_date};
86 my $notes = $body->{notes};
88 if ( $item_id and $biblio_id ) {
90 # check they are consistent
91 unless ( Koha::Items->search( { itemnumber => $item_id, biblionumber => $biblio_id } )
92 ->count > 0 )
94 return $c->render(
95 status => 400,
96 openapi => { error => "Item $item_id doesn't belong to biblio $biblio_id" }
99 else {
100 $biblio = Koha::Biblios->find($biblio_id);
103 elsif ($item_id) {
104 my $item = Koha::Items->find($item_id);
106 unless ($item) {
107 return $c->render(
108 status => 404,
109 openapi => { error => "item_id not found." }
112 else {
113 $biblio = $item->biblio;
116 elsif ($biblio_id) {
117 $biblio = Koha::Biblios->find($biblio_id);
119 else {
120 return $c->render(
121 status => 400,
122 openapi => { error => "At least one of biblio_id, item_id should be given" }
126 unless ($biblio) {
127 return $c->render(
128 status => 400,
129 openapi => "Biblio not found."
133 my $can_place_hold
134 = $item_id
135 ? C4::Reserves::CanItemBeReserved( $patron_id, $item_id )
136 : C4::Reserves::CanBookBeReserved( $patron_id, $biblio_id );
138 unless ( $can_place_hold->{status} eq 'OK' ) {
139 return $c->render(
140 status => 403,
141 openapi =>
142 { error => "Hold cannot be placed. Reason: " . $can_place_hold->{status} }
146 my $priority = C4::Reserves::CalculatePriority($biblio_id);
148 # AddReserve expects date to be in syspref format
149 if ($expiration_date) {
150 $expiration_date = output_pref( dt_from_string( $expiration_date, 'rfc3339' ) );
153 my $hold_id = C4::Reserves::AddReserve(
154 $pickup_library_id,
155 $patron_id,
156 $biblio_id,
157 undef, # $bibitems param is unused
158 $priority,
159 undef, # hold date, we don't allow it currently
160 $expiration_date,
161 $notes,
162 $biblio->title,
163 $item_id,
164 undef, # TODO: Why not?
165 $item_type
168 unless ($hold_id) {
169 return $c->render(
170 status => 500,
171 openapi => 'Error placing the hold. See Koha logs for details.'
175 my $hold = Koha::Holds->find($hold_id);
177 return $c->render( status => 201, openapi => _to_api($hold->TO_JSON) );
179 catch {
180 if ( blessed $_ and $_->isa('Koha::Exceptions') ) {
181 if ( $_->isa('Koha::Exceptions::Object::FKConstraint') ) {
182 my $broken_fk = $_->broken_fk;
184 if ( grep { $_ eq $broken_fk } keys %{$Koha::REST::V1::Hold::to_api_mapping} ) {
185 $c->render(
186 status => 404,
187 openapi => $Koha::REST::V1::Hold::to_api_mapping->{$broken_fk} . ' not found.'
190 else {
191 return $c->render(
192 status => 500,
193 openapi => { error => "Uncaught exception: $_" }
197 else {
198 return $c->render(
199 status => 500,
200 openapi => { error => "$_" }
204 else {
205 return $c->render(
206 status => 500,
207 openapi => { error => "Something went wrong. check the logs." }
213 =head3 edit
215 Method that handles modifying a Koha::Hold object
217 =cut
219 sub edit {
220 my $c = shift->openapi->valid_input or return;
222 my $hold_id = $c->validation->param('hold_id');
223 my $hold = Koha::Holds->find( $hold_id );
225 unless ($hold) {
226 return $c->render( status => 404,
227 openapi => {error => "Hold not found"} );
230 my $body = $c->req->json;
232 my $pickup_library_id = $body->{pickup_library_id};
233 my $priority = $body->{priority};
234 my $suspended_until = $body->{suspended_until};
236 if ($suspended_until) {
237 $suspended_until = output_pref(dt_from_string($suspended_until, 'rfc3339'));
240 my $params = {
241 reserve_id => $hold_id,
242 branchcode => $pickup_library_id,
243 rank => $priority,
244 suspend_until => $suspended_until,
245 itemnumber => $hold->itemnumber
248 C4::Reserves::ModReserve($params);
249 $hold->discard_changes; # refresh
251 return $c->render( status => 200, openapi => _to_api( $hold->TO_JSON ) );
254 =head3 delete
256 Method that handles deleting a Koha::Hold object
258 =cut
260 sub delete {
261 my $c = shift->openapi->valid_input or return;
263 my $hold_id = $c->validation->param('hold_id');
264 my $hold = Koha::Holds->find($hold_id);
266 unless ($hold) {
267 return $c->render( status => 404, openapi => { error => "Hold not found." } );
270 $hold->cancel;
272 return $c->render( status => 200, openapi => {} );
276 =head3 _to_api
278 Helper function that maps unblessed Koha::Hold objects into REST api
279 attribute names.
281 =cut
283 sub _to_api {
284 my $hold = shift;
286 # Rename attributes
287 foreach my $column ( keys %{ $Koha::REST::V1::Hold::to_api_mapping } ) {
288 my $mapped_column = $Koha::REST::V1::Hold::to_api_mapping->{$column};
289 if ( exists $hold->{ $column }
290 && defined $mapped_column )
292 # key != undef
293 $hold->{ $mapped_column } = delete $hold->{ $column };
295 elsif ( exists $hold->{ $column }
296 && !defined $mapped_column )
298 # key == undef
299 delete $hold->{ $column };
303 return $hold;
306 =head3 _to_model
308 Helper function that maps REST api objects into Koha::Hold
309 attribute names.
311 =cut
313 sub _to_model {
314 my $hold = shift;
316 foreach my $attribute ( keys %{ $Koha::REST::V1::Hold::to_model_mapping } ) {
317 my $mapped_attribute = $Koha::REST::V1::Hold::to_model_mapping->{$attribute};
318 if ( exists $hold->{ $attribute }
319 && defined $mapped_attribute )
321 # key => !undef
322 $hold->{ $mapped_attribute } = delete $hold->{ $attribute };
324 elsif ( exists $hold->{ $attribute }
325 && !defined $mapped_attribute )
327 # key => undef / to be deleted
328 delete $hold->{ $attribute };
332 if ( exists $hold->{lowestPriority} ) {
333 $hold->{lowestPriority} = ($hold->{lowestPriority}) ? 1 : 0;
336 if ( exists $hold->{suspend} ) {
337 $hold->{suspend} = ($hold->{suspend}) ? 1 : 0;
340 if ( exists $hold->{reservedate} ) {
341 $hold->{reservedate} = output_pref({ str => $hold->{reservedate}, dateformat => 'sql' });
344 if ( exists $hold->{cancellationdate} ) {
345 $hold->{cancellationdate} = output_pref({ str => $hold->{cancellationdate}, dateformat => 'sql' });
348 if ( exists $hold->{timestamp} ) {
349 $hold->{timestamp} = output_pref({ str => $hold->{timestamp}, dateformat => 'sql' });
352 if ( exists $hold->{waitingdate} ) {
353 $hold->{waitingdate} = output_pref({ str => $hold->{waitingdate}, dateformat => 'sql' });
356 if ( exists $hold->{expirationdate} ) {
357 $hold->{expirationdate} = output_pref({ str => $hold->{expirationdate}, dateformat => 'sql' });
360 if ( exists $hold->{suspend_until} ) {
361 $hold->{suspend_until} = output_pref({ str => $hold->{suspend_until}, dateformat => 'sql' });
364 return $hold;
367 =head2 Global variables
369 =head3 $to_api_mapping
371 =cut
373 our $to_api_mapping = {
374 reserve_id => 'hold_id',
375 borrowernumber => 'patron_id',
376 reservedate => 'hold_date',
377 biblionumber => 'biblio_id',
378 branchcode => 'pickup_library_id',
379 notificationdate => undef,
380 reminderdate => undef,
381 cancellationdate => 'cancelation_date',
382 reservenotes => 'notes',
383 found => 'status',
384 itemnumber => 'item_id',
385 waitingdate => 'waiting_date',
386 expirationdate => 'expiration_date',
387 lowestPriority => 'lowest_priority',
388 suspend => 'suspended',
389 suspend_until => 'suspended_until',
390 itemtype => 'item_type',
393 =head3 $to_model_mapping
395 =cut
397 our $to_model_mapping = {
398 hold_id => 'reserve_id',
399 patron_id => 'borrowernumber',
400 hold_date => 'reservedate',
401 biblio_id => 'biblionumber',
402 pickup_library_id => 'branchcode',
403 cancelation_date => 'cancellationdate',
404 notes => 'reservenotes',
405 status => 'found',
406 item_id => 'itemnumber',
407 waiting_date => 'waitingdate',
408 expiration_date => 'expirationdate',
409 lowest_priority => 'lowestPriority',
410 suspended => 'suspend',
411 suspended_until => 'suspend_until',
412 item_type => 'itemtype',