From 30af5878c306b5d0ced89c4fd0789a8c9d0a789f Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Fri, 26 Feb 2021 10:51:52 -0300
Subject: [PATCH] Bug 27797: Make POST /holds use the stashed koha.overrides

Bug 26181 introduced a way to override behavior through a new header,
x-koha-override. And bug 27760 introduced a generic approach to handle
x-koha-override.

This patchset makes the POST /api/v1/holds route rely on this new way of
handling x-koha-override instead of doing it manually.

The header is added to the spec as a parameter.

Note: the header should be defined as:

    "type": "array",
    "collectionFormat": "csv",
    "items": {
        "type": "string",
        "enum": [
            "any"
        ]
    }

but the versions of JSON::Validator we use have lots of bugs related to
header type coercion, so it just doesn't work. The changelog for
JSON::Validator is fairly elocuent about it.

The override now takes a list of valid keywords (right now only 'any'
but it would be fairly straight-forward to add a fairly granular set of
options based on each possible AddReserve failure.

To test:
1. Apply this patches
2. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/holds.t
=> SUCCESS: Tests pass!
4. Sign off!

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
---
 Koha/REST/V1/Holds.pm           | 10 +++-------
 api/v1/swagger/paths/holds.json | 10 +++++++++-
 2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/Koha/REST/V1/Holds.pm b/Koha/REST/V1/Holds.pm
index 81d00239153..01bbf976a7b 100644
--- a/Koha/REST/V1/Holds.pm
+++ b/Koha/REST/V1/Holds.pm
@@ -171,14 +171,10 @@ sub add {
             $can_place_hold->{status} = 'tooManyReserves';
         }
 
-        my $override_header = $c->req->headers->header('x-koha-override');
-        $override_header = decode_json($override_header)
-          if $override_header;
+        my $overrides = $c->stash('koha.overrides');
+        my $can_override = $overrides->{any} and C4::Context->preference('AllowHoldPolicyOverride');
 
-        my $can_override = $override_header->{AllowHoldPolicyOverride}
-          and 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 =>
diff --git a/api/v1/swagger/paths/holds.json b/api/v1/swagger/paths/holds.json
index dd26dd2746d..fe72053c286 100644
--- a/api/v1/swagger/paths/holds.json
+++ b/api/v1/swagger/paths/holds.json
@@ -171,7 +171,8 @@
       "x-mojo-to": "Holds#add",
       "operationId": "addHold",
       "tags": ["patrons", "holds"],
-      "parameters": [{
+      "parameters": [
+        {
           "name": "body",
           "in": "body",
           "description": "A JSON object containing informations about the new hold",
@@ -215,6 +216,13 @@
             },
             "required": [ "patron_id", "pickup_library_id" ]
           }
+        },
+        {
+          "name": "x-koha-override",
+          "description": "Comma-separated list of overrides (valid values: any)",
+          "in": "header",
+          "type": "string",
+          "required": false
         }
       ],
       "consumes": ["application/json"],
-- 
2.31.1