From 4476cb23b5a997abdbc06090d08995124c38b0cb Mon Sep 17 00:00:00 2001
From: Agustin Moyano <agustinmoyano@theke.io>
Date: Fri, 9 Oct 2020 09:59:41 -0300
Subject: [PATCH] Bug 26635: Expand authorised values in to_api method

This patch adds the posibility to expand authorised values when to_api
method is called.

The classes where authorised values should expand must implememnt the
_fetch_authorised_values method, and must return a hash like the
following

{
  column_name => Koha::AuthorisedValue->unblessed
  ...
}

This patch will be used in bug 8179, so please test there.

Sponsored-by: Virginia Polytechnic Institute and State University

Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 Koha/Object.pm              | 45 +++++++++++++++++++++++++++++++++++++
 Koha/REST/Plugin/Objects.pm | 11 +++++----
 2 files changed, 52 insertions(+), 4 deletions(-)

diff --git a/Koha/Object.pm b/Koha/Object.pm
index ef554d5e56..384e7fd59a 100644
--- a/Koha/Object.pm
+++ b/Koha/Object.pm
@@ -581,6 +581,8 @@ sub to_api {
         }
     }
 
+    my $av_expand = $params->{av_expand};
+
     # Make sure we duplicate the $params variable to avoid
     # breaking calls in a loop (Koha::Objects->to_api)
     $params    = {%$params};
@@ -627,6 +629,49 @@ sub to_api {
         }
     }
 
+    if ( $av_expand && $self->can('_fetch_authorised_values') ) {
+
+        # _fetch_authorised_values should return a hash as the following
+        # {
+        #  column_name => <authorised_value>->unblessed
+        #  ...
+        # }
+        my $avs = $self->_fetch_authorised_values($av_expand);
+
+        # Language selection will be implemented when lang overlay for av is ready
+        # Now we will just fetch plain authorised values from the Koha::AuthorisedValues
+        $avs = $self->_do_api_mapping($avs);
+
+        $json_object->{_authorised_values} = $avs || {};
+    }
+
+    return $json_object;
+}
+
+=head3 _do_api_mapping
+
+=cut
+
+sub _do_api_mapping {
+    my ($self, $json_object) = @_;
+    # Rename attributes if there's a mapping
+    if ( $self->can('to_api_mapping') ) {
+        foreach my $column ( keys %{ $self->to_api_mapping } ) {
+            my $mapped_column = $self->to_api_mapping->{$column};
+            if ( exists $json_object->{$column}
+                && defined $mapped_column )
+            {
+                # key != undef
+                $json_object->{$mapped_column} = delete $json_object->{$column};
+            }
+            elsif ( exists $json_object->{$column}
+                && !defined $mapped_column )
+            {
+                # key == undef
+                delete $json_object->{$column};
+            }
+        }
+    }
     return $json_object;
 }
 
diff --git a/Koha/REST/Plugin/Objects.pm b/Koha/REST/Plugin/Objects.pm
index 4a5d29143c..c9a2a26106 100644
--- a/Koha/REST/Plugin/Objects.pm
+++ b/Koha/REST/Plugin/Objects.pm
@@ -53,7 +53,9 @@ the requested object. It passes through any embeds if specified.
             my $attributes = {};
 
             # Look for embeds
-            my $embed = $c->stash('koha.embed');
+            my $embed     = $c->stash('koha.embed');
+            my $av_expand = $c->req->headers->header('x-koha-av-expand');
+
             # Generate prefetches for embedded stuff
             $c->dbic_merge_prefetch(
                 {
@@ -66,7 +68,7 @@ the requested object. It passes through any embeds if specified.
 
             return unless $object;
 
-            return $object->to_api({ embed => $embed });
+            return $object->to_api({ embed => $embed, av_expand => $av_expand });
         }
     );
 
@@ -97,7 +99,8 @@ shouldn't be called twice in it.
             # Privileged reques?
             my $is_public = $c->stash('is_public');
             # Look for embeds
-            my $embed = $c->stash('koha.embed');
+            my $embed     = $c->stash('koha.embed');
+            my $av_expand = $c->req->headers->header('x-koha-av-expand');
 
             # Merge sorting into query attributes
             $c->dbic_merge_sorting(
@@ -202,7 +205,7 @@ shouldn't be called twice in it.
                 }
             );
 
-            return $objects->to_api({ embed => $embed, public => $is_public });
+            return $objects->to_api({ embed => $embed, public => $is_public, av_expand => $av_expand });
         }
     );
 }
-- 
2.34.1