From 0b33bb540b0d0f59d01e29671348a0c324ba391d Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Tue, 19 Jul 2022 11:56:56 +0100
Subject: [PATCH] Bug 33146: Add public items lookup route

This patch adds a /public equivilent to the item listing endpoint.

This allows us to search for an item by it's external_id (barcode).

Test plan
1. Apply patch
2. Perform a GET on /api/v1/public/items?external_id=some_barcode
3. Confirm that the above endpoint correctly returns items that should
   be visible in the OPAC
---
 Koha/REST/V1/Items.pm           | 26 +++++++++++++++++
 api/v1/swagger/paths/items.yaml | 51 +++++++++++++++++++++++++++++++++
 api/v1/swagger/swagger.yaml     |  2 ++
 3 files changed, 79 insertions(+)

diff --git a/Koha/REST/V1/Items.pm b/Koha/REST/V1/Items.pm
index 70abdfeeb2..c4acea54a0 100644
--- a/Koha/REST/V1/Items.pm
+++ b/Koha/REST/V1/Items.pm
@@ -58,6 +58,32 @@ sub list {
     };
 }
 
+=head3 list_public
+
+Controller function that handles listing Koha::Item objects available to the opac
+
+=cut
+
+sub list_public {
+    my $c = shift->openapi->valid_input or return;
+
+    return try {
+        my $patron = $c->stash('koha.user');
+
+        my $items_set =
+          Koha::Items->filter_by_visible_in_opac( { patron => $patron } );
+        my $items = $c->objects->search($items_set);
+
+        return $c->render(
+            status  => 200,
+            openapi => $items
+        );
+    }
+    catch {
+        $c->unhandled_exception($_);
+    };
+}
+
 =head3 get
 
 Controller function that handles retrieving a single Koha::Item
diff --git a/api/v1/swagger/paths/items.yaml b/api/v1/swagger/paths/items.yaml
index 2bd8b21210..3f15c3a278 100644
--- a/api/v1/swagger/paths/items.yaml
+++ b/api/v1/swagger/paths/items.yaml
@@ -376,3 +376,54 @@
     x-koha-authorization:
       permissions:
         reserveforothers: place_holds
+"/public/items":
+  get:
+    x-mojo-to: Items#list_public
+    operationId: listItemsPublic
+    tags:
+      - items
+    summary: List items publically visible
+    parameters:
+      - name: external_id
+        in: query
+        description: Search on the item's barcode
+        required: false
+        type: string
+      - $ref: "../swagger.yaml#/parameters/match"
+      - $ref: "../swagger.yaml#/parameters/order_by"
+      - $ref: "../swagger.yaml#/parameters/page"
+      - $ref: "../swagger.yaml#/parameters/per_page"
+      - $ref: "../swagger.yaml#/parameters/q_param"
+      - $ref: "../swagger.yaml#/parameters/q_body"
+      - $ref: "../swagger.yaml#/parameters/q_header"
+      - $ref: "../swagger.yaml#/parameters/request_id_header"
+    consumes:
+      - application/json
+    produces:
+      - application/json
+    responses:
+      "200":
+        description: A list of item
+        schema:
+          type: array
+          items:
+            $ref: "../swagger.yaml#/definitions/item"
+      "401":
+        description: Authentication required
+        schema:
+          $ref: "../swagger.yaml#/definitions/error"
+      "403":
+        description: Access forbidden
+        schema:
+          $ref: "../swagger.yaml#/definitions/error"
+      "500":
+        description: |
+          Internal server error. Possible `error_code` attribute values:
+
+          * `internal_server_error`
+        schema:
+          $ref: "../swagger.yaml#/definitions/error"
+      "503":
+        description: Under maintenance
+        schema:
+          $ref: "../swagger.yaml#/definitions/error"
diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml
index c728f9d6d7..a534925bf4 100644
--- a/api/v1/swagger/swagger.yaml
+++ b/api/v1/swagger/swagger.yaml
@@ -283,6 +283,8 @@ paths:
     $ref: "./paths/patrons_password.yaml#/~1patrons~1{patron_id}~1password~1expiration_date"
   "/public/biblios/{biblio_id}":
     $ref: "./paths/biblios.yaml#/~1public~1biblios~1{biblio_id}"
+  "/public/items":
+    $ref: "./paths/items.yaml#/~1public~1items"
   "/public/biblios/{biblio_id}/items":
     $ref: "./paths/biblios.yaml#/~1public~1biblios~1{biblio_id}~1items"
   "/public/biblios/{biblio_id}/ratings":
-- 
2.39.2