From 983dd03efafc271a01f89603937c0b3af0c84510 Mon Sep 17 00:00:00 2001 From: Alex Arnaud Date: Thu, 6 Jun 2019 17:05:24 +0200 Subject: [PATCH] Bug 22249: Mana - Move comment process in a dedicated sub 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 Signed-off-by: Nick Clemens Signed-off-by: Martin Renvoize (cherry picked from commit a72d8dbb0ff3aa6f56aafcca2c3a0c820b5325f6) Signed-off-by: Fridolin Somers --- Koha/SharedContent.pm | 23 ++++++++++--- .../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, 36 insertions(+), 41 deletions(-) diff --git a/Koha/SharedContent.pm b/Koha/SharedContent.pm index 580fef245d..04cb67502d 100644 --- a/Koha/SharedContent.pm +++ b/Koha/SharedContent.pm @@ -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); 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 edd73008ee..cb37acbb82 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 @@ \ 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 c36faec82a..b8542b8077 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 508eec2716..504b201046 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 2739afbce6..c9b7e25c6a 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.39.5