use Mojo::Base 'Mojolicious::Controller';
-use C4::Biblio;
+use Mojo::JSON;
+
use C4::Reserves;
use Koha::Items;
use Koha::Patrons;
use Koha::Holds;
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string output_pref );
-use List::MoreUtils qw(any);
-use Try::Tiny;
+use List::MoreUtils qw( any );
+use Try::Tiny qw( catch try );
=head1 API
=head3 list
-Mehtod that handles listing Koha::Hold objects
+Method that handles listing Koha::Hold objects
=cut
my $hold_date = $body->{hold_date};
my $non_priority = $body->{non_priority};
+ my $overrides = $c->stash('koha.overrides');
+ my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
+
if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
return $c->render(
status => 400,
openapi => {
error => 'The supplied pickup location is not valid'
}
- ) unless $valid_pickup_location;
+ ) unless $valid_pickup_location || $can_override;
my $can_place_hold
= $item_id
$can_place_hold->{status} = 'tooManyReserves';
}
- my $can_override = C4::Context->preference('AllowHoldPolicyOverride');
-
- unless ($can_override || $can_place_hold->{status} eq 'OK' ) {
+ unless ( $can_override || $can_place_hold->{status} eq 'OK' ) {
return $c->render(
status => 403,
openapi =>
my $hold = Koha::Holds->find( $hold_id );
unless ($hold) {
- return $c->render( status => 404,
- openapi => {error => "Hold not found"} );
+ return $c->render(
+ status => 404,
+ openapi => { error => "Hold not found" }
+ );
}
- my $body = $c->req->json;
+ my $overrides = $c->stash('koha.overrides');
+ my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
+
+ my $body = $c->validation->output->{body};
my $pickup_library_id = $body->{pickup_library_id};
if (
defined $pickup_library_id
- and not $hold->is_pickup_location_valid({ library_id => $pickup_library_id })
+ && ( !$hold->is_pickup_location_valid({ library_id => $pickup_library_id }) && !$can_override )
)
{
return $c->render(
);
}
- $pickup_library_id //= $hold->branchcode;
- my $priority = $body->{priority} // $hold->priority;
+ $pickup_library_id //= $hold->branchcode;
+ my $priority = $body->{priority} // $hold->priority;
# suspended_until can also be set to undef
my $suspended_until = exists $body->{suspended_until} ? $body->{suspended_until} : $hold->suspend_until;
my $c = shift->openapi->valid_input or return;
my $hold_id = $c->validation->param('hold_id');
- # FIXME: We should really skip the path params in $c->objects->search
- delete $c->validation->output->{hold_id};
my $hold = Koha::Holds->find( $hold_id, { prefetch => [ 'patron' ] } );
unless ($hold) {
}
my $pickup_locations = $c->objects->search( $ps_set );
+ my @response = ();
+
+ if ( C4::Context->preference('AllowHoldPolicyOverride') ) {
+
+ my $libraries_rs = Koha::Libraries->search( { pickup_location => 1 } );
+ my $libraries = $c->objects->search($libraries_rs);
+
+ @response = map {
+ my $library = $_;
+ $library->{needs_override} = (
+ any { $_->{library_id} eq $library->{library_id} }
+ @{$pickup_locations}
+ )
+ ? Mojo::JSON->false
+ : Mojo::JSON->true;
+ $library;
+ } @{$libraries};
+
+ return $c->render(
+ status => 200,
+ openapi => \@response
+ );
+ }
+
+ @response = map { $_->{needs_override} = Mojo::JSON->false; $_; } @{$pickup_locations};
+
+ return $c->render(
+ status => 200,
+ openapi => \@response
+ );
+ }
+ catch {
+ $c->unhandled_exception($_);
+ };
+}
+
+=head3 update_pickup_location
+
+Method that handles modifying the pickup location of a Koha::Hold object
+
+=cut
+
+sub update_pickup_location {
+ my $c = shift->openapi->valid_input or return;
+
+ my $hold_id = $c->validation->param('hold_id');
+ my $body = $c->validation->param('body');
+ my $pickup_library_id = $body->{pickup_library_id};
+
+ my $hold = Koha::Holds->find($hold_id);
+
+ unless ($hold) {
+ return $c->render(
+ status => 404,
+ openapi => { error => "Hold not found" }
+ );
+ }
+
+ return try {
+
+ my $overrides = $c->stash('koha.overrides');
+ my $can_override = $overrides->{any} && C4::Context->preference('AllowHoldPolicyOverride');
+
+ $hold->set_pickup_location(
+ {
+ library_id => $pickup_library_id,
+ force => $can_override
+ }
+ );
return $c->render(
status => 200,
- openapi => $pickup_locations
+ openapi => {
+ pickup_library_id => $pickup_library_id
+ }
);
}
catch {
+
+ if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::InvalidPickupLocation') ) {
+ return $c->render(
+ status => 400,
+ openapi => {
+ error => "$_"
+ }
+ );
+ }
+
$c->unhandled_exception($_);
};
}
+
1;