Bug 22249: Mana - Move comment process in a dedicated sub
authorAlex Arnaud <alex.arnaud@biblibre.com>
Thu, 6 Jun 2019 15:05:24 +0000 (17:05 +0200)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 26 Jul 2019 15:08:08 +0000 (16:08 +0100)
Test plan:
  - Go to a subscription's detail page,
  - click on "Report mistake" => "New comment",
  - check Koha logs file,
  - you can see the following warning:
    Can't locate object method "find" via package "Koha::Resource_comments"
  - Apply this patch,
  - same test again,
  - no wraning

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Koha/SharedContent.pm
koha-tmpl/intranet-tmpl/prog/en/includes/mana/mana-comment-status.inc
koha-tmpl/intranet-tmpl/prog/js/mana.js
svc/mana/share
t/db_dependent/Koha/SharedContent.t

index 580fef2..04cb675 100644 (file)
@@ -93,11 +93,9 @@ Share a Koha entity (i.e subscription or report) to Mana KB.
 =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));
 
@@ -109,6 +107,23 @@ sub send_entity {
     return $result;
 }
 
+=head3 comment_entity
+
+my $result = Koha::SharedContent::comment_entity($resource_id, $resource_type, $comment);
+
+Send a comment about a Mana 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;
+}
+
 =head2 prepare_entity_data
 
 $data = prepare_entity_data($language, $borrowernumber, $mana_entity_id, $entity_type);
index edd7300..cb37acb 100644 (file)
@@ -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
index c36faec..b8542b8 100644 (file)
@@ -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("");
index 508eec2..504b201 100755 (executable)
@@ -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));
index 2739afb..c9b7e25 100644 (file)
@@ -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,