+=head3 pickup_locations
+
+Method that returns the possible pickup_locations for a given hold
+used for building the dropdown selector
+
+=cut
+
+sub pickup_locations {
+ my $c = shift->openapi->valid_input or return;
+
+ my $hold_id = $c->validation->param('hold_id');
+ my $hold = Koha::Holds->find( $hold_id, { prefetch => [ 'patron' ] } );
+
+ unless ($hold) {
+ return $c->render(
+ status => 404,
+ openapi => { error => "Hold not found" }
+ );
+ }
+
+ return try {
+ my $ps_set;
+
+ if ( $hold->itemnumber ) {
+ $ps_set = $hold->item->pickup_locations( { patron => $hold->patron } );
+ }
+ else {
+ $ps_set = $hold->biblio->pickup_locations( { patron => $hold->patron } );
+ }
+
+ 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_library_id => $pickup_library_id
+ }
+ );
+ }
+ catch {
+
+ if ( blessed $_ and $_->isa('Koha::Exceptions::Hold::InvalidPickupLocation') ) {
+ return $c->render(
+ status => 400,
+ openapi => {
+ error => "$_"
+ }
+ );
+ }
+
+ $c->unhandled_exception($_);
+ };
+}
+
+