From b3f5756d94c2f63e9d9b460d9294eed6b1adadc6 Mon Sep 17 00:00:00 2001 From: Caio Almeida <117518+caiosba@users.noreply.github.com> Date: Fri, 13 Dec 2024 16:08:04 -0300 Subject: [PATCH] Make sure that `null` is not returned as the item when creating an imported fact-check with original media in GraphQL API. (#2153) When handling an existing media to decide if the imported fact-check is going to be added or appended to the existing media, there is a case where the media already exists with a fact-check in the same language that is trying to be imported. That case was returning nil. This PR fixes it by making sure that the new object is returned in that case, and then let the validations take care of returning a persisted or failed object. Fixes: CV2-5804. --- app/models/project_media.rb | 3 ++- test/models/project_media_7_test.rb | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/models/project_media.rb b/app/models/project_media.rb index e29adddb8..e1c18a7a8 100644 --- a/app/models/project_media.rb +++ b/app/models/project_media.rb @@ -430,7 +430,7 @@ def apply_rules_and_actions_on_update self.team.apply_rules_and_actions(self, rule_ids) end - def self.handle_fact_check_for_existing_claim(existing_pm,new_pm) + def self.handle_fact_check_for_existing_claim(existing_pm, new_pm) if existing_pm.fact_check.blank? existing_pm.append_fact_check_from(new_pm) return existing_pm @@ -440,6 +440,7 @@ def self.handle_fact_check_for_existing_claim(existing_pm,new_pm) return new_pm end end + new_pm end def append_fact_check_from(new_pm) diff --git a/test/models/project_media_7_test.rb b/test/models/project_media_7_test.rb index d83e68c18..9466ddadc 100644 --- a/test/models/project_media_7_test.rb +++ b/test/models/project_media_7_test.rb @@ -163,4 +163,13 @@ def setup sleep 1 assert_equal [pm1.fact_check_id, pm2.fact_check_id, pm3.fact_check_id].sort, pm_i.get_similar_articles.map(&:id).sort end + + test "should not return null when handling fact-check for existing media" do + t = create_team + pm1 = create_project_media team: t + c = create_claim_description project_media: pm1 + create_fact_check claim_description: c, language: 'en' + pm2 = ProjectMedia.new team: t, set_fact_check: { 'language' => 'en' } + assert_not_nil ProjectMedia.handle_fact_check_for_existing_claim(pm1, pm2) + end end