Bug 18729: Add PUT /holds/{hold_id}/pickup_location
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 1 Dec 2020 11:29:38 +0000 (12:29 +0100)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 16 Apr 2021 14:27:29 +0000 (16:27 +0200)
This patch adds a route to overwrite the current pickup location.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Koha/REST/V1/Holds.pm
api/v1/swagger/paths.json
api/v1/swagger/paths/holds.json
koha-tmpl/intranet-tmpl/prog/js/holds.js
t/db_dependent/api/v1/holds.t

index 1adc997..a431b55 100644 (file)
@@ -508,4 +508,36 @@ sub pickup_locations {
     };
 }
 
+=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 $hold = Koha::Holds->find($hold_id);
+
+    unless ($hold) {
+        return $c->render(
+            status  => 404,
+            openapi => { error => "Hold not found" }
+        );
+    }
+
+    return try {
+        my $pickup_location = $c->req->json;
+
+        $hold->branchcode($pickup_location)->store;
+
+        return $c->render( status => 200, openapi => $pickup_location );
+    }
+    catch {
+        $c->unhandled_exception($_);
+    };
+}
+
+
 1;
index 3381e5a..dd9562c 100644 (file)
@@ -71,6 +71,9 @@
   "/holds/{hold_id}/pickup_locations": {
     "$ref": "paths/holds.json#/~1holds~1{hold_id}~1pickup_locations"
   },
+  "/holds/{hold_id}/pickup_location": {
+    "$ref": "paths/holds.json#/~1holds~1{hold_id}~1pickup_location"
+  },
   "/items": {
     "$ref": "paths/items.json#/~1items"
   },
index c71fe1c..ebaed33 100644 (file)
         }
       }
     }
+  },
+  "/holds/{hold_id}/pickup_location": {
+    "put": {
+      "x-mojo-to": "Holds#update_pickup_location",
+      "operationId": "updateHoldPickupLocation",
+      "tags": ["holds"],
+      "parameters": [
+        {
+          "$ref": "../parameters.json#/hold_id_pp"
+        },
+        {
+          "name": "body",
+          "in": "body",
+          "description": "Pickup location",
+          "required": true,
+          "schema": {
+            "type": "string"
+          }
+        }
+      ],
+      "produces": [
+        "application/json"
+      ],
+      "responses": {
+        "200": {
+          "description": "The new pickup location value for the hold",
+          "schema": {
+            "type": "string"
+          }
+        },
+        "401": {
+          "description": "Authentication required",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
+        "403": {
+          "description": "Access forbidden",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
+        "404": {
+          "description": "Hold not found",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
+        "409": {
+          "description": "Unable to perform action on hold",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
+        "500": {
+          "description": "Internal error",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        },
+        "503": {
+          "description": "Under maintenance",
+          "schema": {
+            "$ref": "../definitions.json#/error"
+          }
+        }
+      },
+      "x-koha-authorization": {
+        "permissions": {
+          "reserveforothers": "place_holds"
+        }
+      }
+    }
   }
 }
index 134194b..3641835 100644 (file)
@@ -224,12 +224,11 @@ $(document).ready(function() {
                     var cur_select = $(this);
                     var res_id = $(this).attr('reserve_id');
                     $(this).after('<div id="updating_reserveno'+res_id+'" class="waiting"><img src="/intranet-tmpl/prog/img/spinner-small.gif" alt="" /><span class="waiting_msg"></span></div>');
-                    var api_url = '/api/v1/holds/'+res_id;
-                    var update_info = JSON.stringify({ pickup_library_id: $(this).val(), priority: parseInt($(this).attr("priority"),10) });
+                    var api_url = '/api/v1/holds/' + encodeURIComponent(res_id) + '/pickup_location';
                     $.ajax({
                         method: "PUT",
                         url: api_url,
-                        data: update_info ,
+                        data: $(this).val(),
                         success: function( data ){ holdsTable.api().ajax.reload(); },
                         error: function( jqXHR, textStatus, errorThrown) {
                             alert('There was an error:'+textStatus+" "+errorThrown);
index 1eaddbc..7645fa0 100755 (executable)
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 12;
+use Test::More tests => 13;
 use Test::MockModule;
 use Test::Mojo;
 use t::lib::TestBuilder;
@@ -1054,3 +1054,55 @@ subtest 'add() tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+
+subtest 'PUT /holds/{hold_id}/pickup_location tests' => sub {
+
+    plan tests => 4;
+
+    $schema->storage->txn_begin;
+
+    my $password = 'AbcdEFG123';
+
+    my $library_1   =  $builder->build_object({ class => 'Koha::Libraries' });
+    my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
+
+    my $patron = $builder->build_object(
+        { class => 'Koha::Patrons', value => { flags => 0 } } );
+    $patron->set_password( { password => $password, skip_validation => 1 } );
+    my $userid = $patron->userid;
+    $builder->build(
+        {
+            source => 'UserPermission',
+            value  => {
+                borrowernumber => $patron->borrowernumber,
+                module_bit     => 6,
+                code           => 'place_holds',
+            },
+        }
+    );
+
+    # Disable logging
+    t::lib::Mocks::mock_preference( 'HoldsLog',      0 );
+    t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
+
+    my $biblio = $builder->build_sample_biblio;
+    my $hold = Koha::Holds->find(
+        AddReserve(
+            {
+                branchcode     => $library_1->branchcode,
+                borrowernumber => $patron->borrowernumber,
+                biblionumber   => $biblio->biblionumber,
+                priority       => 1,
+            }
+        )
+    );
+
+    $t->put_ok( "//$userid:$password@/api/v1/holds/"
+          . $hold->id
+          . "/pickup_location" => json => $library_2->branchcode )->status_is(200)->json_is($library_2->branchcode);
+
+    is( $hold->discard_changes->branchcode->branchcode, $library_2->branchcode, 'pickup library adjusted correctly' );
+
+    $schema->storage->txn_rollback;
+};