From e339222c1240d826f34549256549abdc7ec9a660 Mon Sep 17 00:00:00 2001
From: Alex Arnaud <alex.arnaud@biblibre.com>
Date: Thu, 6 Jun 2019 17:05:24 +0200
Subject: [PATCH] Bug 22249: Mana - Move comment process in a dedicated sub

https://bugs.koha-community.org/show_bug.cgi?id=22259
---
 Koha/SharedContent.pm                              | 19 ++++++++++---
 .../prog/en/includes/mana/mana-comment-status.inc  |  1 -
 koha-tmpl/intranet-tmpl/prog/js/mana.js            | 15 +++++-----
 svc/mana/share                                     | 32 ++++------------------
 t/db_dependent/Koha/SharedContent.t                |  6 +++-
 5 files changed, 32 insertions(+), 41 deletions(-)

diff --git a/Koha/SharedContent.pm b/Koha/SharedContent.pm
index 7145e95..5b6e659 100644
--- a/Koha/SharedContent.pm
+++ b/Koha/SharedContent.pm
@@ -76,11 +76,9 @@ sub increment_entity_value {
 =cut
 
 sub send_entity {
-    my ($lang, $loggedinuser, $resourceid, $resourcetype, $content) = @_;
+    my ($lang, $loggedinuser, $resourceid, $resourcetype) = @_;
 
-    unless ( $content ) {
-        $content = prepare_entity_data($lang, $loggedinuser, $resourceid, $resourcetype);
-    }
+    my $content = prepare_entity_data($lang, $loggedinuser, $resourceid, $resourcetype);
 
     my $result = process_request(build_request('post', $resourcetype, $content));
 
@@ -92,6 +90,19 @@ sub send_entity {
     return $result;
 }
 
+=head3 comment_entity
+
+=cut
+
+sub comment_entity {
+    my ($resourceid, $resourcetype, $comment) = @_;
+
+    my $result = process_request(build_request('post', 'resource_comment',
+            { resource_id => $resourceid, resource_type => $resourcetype, message => $comment }));
+
+    return $result;
+}
+
 =head3 prepare_entity_data
 
 =cut
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-comment-status.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-comment-status.inc
index edd7300..cb37acb 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-comment-status.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-comment-status.inc
@@ -6,5 +6,4 @@
 </div>
 <div id="mana_comment_failed" class="dialog alert mana_comment_status" style="display:none">
     Your comment could not be submitted. Please try again later.
-    <div id="mana_comment_errortext"></div>
 </div>
\ No newline at end of file
diff --git a/koha-tmpl/intranet-tmpl/prog/js/mana.js b/koha-tmpl/intranet-tmpl/prog/js/mana.js
index c36faec..b8542b8 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/mana.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/mana.js
@@ -29,16 +29,15 @@ function mana_comment( target_id, manamsg, resource_type ) {
         type: "POST",
         url: "/cgi-bin/koha/svc/mana/share",
         data: { message: manamsg, resource: resource_type, resource_id: target_id },
-        datatype: "json",
+        dataType: "json",
     })
-        .done(function() {
-            $(".mana_comment_status").hide();
-            $("#mana_comment_success").show();
-        })
-        .fail(function( error ) {
+        .done(function( data ) {
             $(".mana_comment_status").hide();
-            $("#mana_comment_errortext").html( error.status + " " + error.statusText );
-            $("#mana_comment_failed").show();
+            if (data.code == "201" || data.code == "200") {
+                $("#mana_comment_success").show();
+            } else {
+                $("#mana_comment_failed").show();
+            }
         })
         .always(function() {
             $("#selected_id").val("");
diff --git a/svc/mana/share b/svc/mana/share
index 508eec2..504b201 100755
--- a/svc/mana/share
+++ b/svc/mana/share
@@ -26,7 +26,6 @@ use C4::Auth qw(check_cookie_auth);
 use CGI;
 use JSON;
 
-
 my $input = new CGI;
 binmode STDOUT, ":encoding(UTF-8)";
 print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
@@ -40,30 +39,9 @@ if ( $auth_status ne "ok" ) {
 }
 
 
-my $content;
-$content->{resource_id} = $input->param("resource_id");
-$content->{resource_type} = $input->param("resource");
-$content->{message} = $input->param("message");
-Koha::SharedContent::send_entity('', undef, undef, 'resource_comment', $content);
-my $package = "Koha::".ucfirst($input->param('resource'));
-my $resource;
-my $result;
-eval{
-    $result = Koha::SharedContent::get_entity_by_id(
-        scalar $input->param('resource'),
-        scalar $input->param('id')
-    );
-};
-if ( $@ or $result->{code} == 500 ){
-    $resource->{errmsg} =  "Error: mana access got broken, please try again later\n\n ( error: $@ )";
-}
-else{
-    if ( $input->param( 'saveinbase' )) {
-        $resource = { id => $package->new_from_mana($result->{data})->id };
-    }
-    else{
-        $resource = $result->{data};
-    }
-}
+my $resource_id = $input->param("resource_id");
+my $resource_type = $input->param("resource");
+my $comment = $input->param("message");
+my $result = Koha::SharedContent::comment_entity($resource_id, $resource_type, $comment);
 
-print(to_json($resource));
+print(to_json($result));
diff --git a/t/db_dependent/Koha/SharedContent.t b/t/db_dependent/Koha/SharedContent.t
index 2739afb..c9b7e25 100644
--- a/t/db_dependent/Koha/SharedContent.t
+++ b/t/db_dependent/Koha/SharedContent.t
@@ -23,7 +23,7 @@ use t::lib::TestBuilder;
 use t::lib::Mocks;
 use Test::MockModule;
 use Test::MockObject;
-use Test::More tests => 44;
+use Test::More tests => 45;
 use Koha::Database;
 use Koha::Patrons;
 use Koha::Subscriptions;
@@ -165,6 +165,10 @@ is($result->{code}, 200, 'send_entity success');
 my $s = Koha::Subscriptions->find($subscription->{subscriptionid});
 is($s->mana_id, 5, 'Mana id is set');
 
+$content = { resource_id => $subscription->{mana_id}, resource_type => 'subscription', message => 'My comment'};
+$result = Koha::SharedContent::comment_entity('resource_comment', $content);
+is($result->{code}, 200, 'Comment success');
+
 my $data = Koha::SharedContent::prepare_entity_data(
     '',
     $loggedinuser->borrowernumber,
-- 
2.7.4