From d543ee323394f8217d835a4a994066a6c529bbc4 Mon Sep 17 00:00:00 2001
From: Matt Blenkinsop <matt.blenkinsop@openfifth.co.uk>
Date: Thu, 8 May 2025 11:02:29 +0100
Subject: [PATCH] Bug 33430: Add strings_map method

This patch adds a strings_map method to allow the API to return the required authorised values needed for table displays
---
 Koha/Suggestion.pm                         | 36 +++++++++++++
 api/v1/swagger/definitions/suggestion.yaml |  4 ++
 api/v1/swagger/paths/suggestions.yaml      | 10 ++++
 t/db_dependent/Koha/Suggestion.t           | 59 +++++++++++++++++++++-
 4 files changed, 108 insertions(+), 1 deletion(-)

diff --git a/Koha/Suggestion.pm b/Koha/Suggestion.pm
index 8a098189b20..f44342c687e 100644
--- a/Koha/Suggestion.pm
+++ b/Koha/Suggestion.pm
@@ -294,6 +294,42 @@ sub to_api_mapping {
     };
 }
 
+=head3 strings_map
+
+Returns a map of column name to string representations including the string.
+
+=cut
+
+sub strings_map {
+    my ( $self, $params ) = @_;
+
+    my $strings = {};
+
+    my $required_strings = {
+        STATUS       => 'SUGGEST_STATUS',
+        itemtype     => 'SUGGEST_FORMAT',
+        patronreason => 'OPAC_SUG',
+    };
+
+    foreach my $key ( keys %$required_strings ) {
+        my $av = Koha::AuthorisedValues->search(
+            { category => $required_strings->{$key}, authorised_value => $self->$key } );
+        my $status_str =
+              $av->count
+            ? $params->{public}
+                ? $av->next->opac_description
+                : $av->next->lib
+            : $self->$key;
+
+        $strings->{$key} = {
+            category => $required_strings->{$key},
+            str      => $status_str,
+            type     => 'av',
+        };
+    }
+    return $strings;
+}
+
 =head1 AUTHOR
 
 Kyle M Hall <kyle@bywatersolutions.com>
diff --git a/api/v1/swagger/definitions/suggestion.yaml b/api/v1/swagger/definitions/suggestion.yaml
index daa136ff23b..2a9bdfbfd5e 100644
--- a/api/v1/swagger/definitions/suggestion.yaml
+++ b/api/v1/swagger/definitions/suggestion.yaml
@@ -191,4 +191,8 @@ properties:
       - boolean
       - "null"
     description: archived (processed) suggestion
+  _strings:
+    type:
+      - object
+      - "null"
 additionalProperties: false
diff --git a/api/v1/swagger/paths/suggestions.yaml b/api/v1/swagger/paths/suggestions.yaml
index a76c382f226..a4d3515ea51 100644
--- a/api/v1/swagger/paths/suggestions.yaml
+++ b/api/v1/swagger/paths/suggestions.yaml
@@ -15,6 +15,16 @@
       - $ref: "../swagger.yaml#/parameters/q_param"
       - $ref: "../swagger.yaml#/parameters/q_body"
       - $ref: "../swagger.yaml#/parameters/request_id_header"
+      - name: x-koha-embed
+        in: header
+        required: false
+        description: Embed list sent as a request header
+        type: array
+        items:
+          type: string
+          enum:
+            - +strings
+        collectionFormat: csv
     produces:
       - application/json
     responses:
diff --git a/t/db_dependent/Koha/Suggestion.t b/t/db_dependent/Koha/Suggestion.t
index cf959d868ea..4f2f0a465a6 100755
--- a/t/db_dependent/Koha/Suggestion.t
+++ b/t/db_dependent/Koha/Suggestion.t
@@ -20,7 +20,7 @@
 use Modern::Perl;
 
 use Test::NoWarnings;
-use Test::More tests => 2;
+use Test::More tests => 3;
 
 use Koha::Database;
 use Koha::Suggestions;
@@ -49,3 +49,60 @@ subtest 'suggester() tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'strings_map() tests' => sub {
+    plan tests => 2;
+
+    $schema->txn_begin;
+
+    my $av_value1 = Koha::AuthorisedValue->new(
+        {
+            category         => "SUGGEST_FORMAT",
+            authorised_value => 'RECORD',
+            lib              => "Test format"
+        }
+    )->store;
+    my $av_value2 = Koha::AuthorisedValue->new(
+        {
+            category         => "SUGGEST_STATUS",
+            authorised_value => 'WANTED',
+            lib              => "Test status"
+        }
+    )->store;
+    my $suggestion = $builder->build_object(
+        { class => 'Koha::Suggestions', value => { suggestedby => undef, STATUS => 'WANTED', itemtype => 'RECORD' } } );
+
+    my $strings_map = $suggestion->strings_map( { public => 0 } );
+    is_deeply(
+        $strings_map,
+        {
+            STATUS       => { str => 'Test status',             type => 'av', category => 'SUGGEST_STATUS' },
+            itemtype     => { str => 'Test format',             type => 'av', category => 'SUGGEST_FORMAT' },
+            patronreason => { str => $suggestion->patronreason, type => 'av', category => 'OPAC_SUG' },
+        },
+        'Strings map is correct'
+    );
+
+    my $av_value3 = Koha::AuthorisedValue->new(
+        {
+            category         => "OPAC_SUG",
+            authorised_value => 'OPAC',
+            lib              => "An OPAC reason"
+        }
+    )->store;
+
+    $suggestion->patronreason('OPAC');
+    $strings_map = $suggestion->strings_map( { public => 0 } );
+    is_deeply(
+        $strings_map,
+        {
+            STATUS       => { str => 'Test status',    type => 'av', category => 'SUGGEST_STATUS' },
+            itemtype     => { str => 'Test format',    type => 'av', category => 'SUGGEST_FORMAT' },
+            patronreason => { str => 'An OPAC reason', type => 'av', category => 'OPAC_SUG' },
+        },
+        'Strings map is correct'
+    );
+
+    $schema->txn_rollback;
+
+};
-- 
2.48.1