From 4c4d246988fe50197147c43f966c3ae81b8a569c Mon Sep 17 00:00:00 2001 From: pezholio Date: Tue, 24 Sep 2024 14:00:14 +0100 Subject: [PATCH] Ensure only live organisations appear in join MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When embedded content has a primary publication organisation, the JOIN was pulling in all draft and superseded editions in, resulting in duplicate rows. This alters the JOIN to only pull in the live edition of a primary publishing organisation, meaning we only get one row per embedded content. I’ve also updated the tests to create draft and superseded editions for the organisation, so we can ensure that we have a test suite that better represents reality. --- app/queries/get_embedded_content.rb | 2 +- spec/queries/get_embedded_content_spec.rb | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/app/queries/get_embedded_content.rb b/app/queries/get_embedded_content.rb index 20b72c43e..73850a274 100644 --- a/app/queries/get_embedded_content.rb +++ b/app/queries/get_embedded_content.rb @@ -26,7 +26,7 @@ def host_editions .joins(:links) .joins("LEFT JOIN links AS primary_links ON primary_links.edition_id = editions.id AND primary_links.link_type = 'primary_publishing_organisation'") .joins("LEFT JOIN documents ON documents.content_id = primary_links.target_content_id") - .joins("LEFT JOIN editions AS org_editions ON org_editions.document_id = documents.id") + .joins("LEFT JOIN editions AS org_editions ON org_editions.document_id = documents.id AND org_editions.state = 'published'") .where(links: { link_type: embedded_link_type, target_content_id: }) .select( "editions.id, editions.title, editions.base_path, editions.document_type, editions.publishing_app", diff --git a/spec/queries/get_embedded_content_spec.rb b/spec/queries/get_embedded_content_spec.rb index dc2841881..25cafc752 100644 --- a/spec/queries/get_embedded_content_spec.rb +++ b/spec/queries/get_embedded_content_spec.rb @@ -1,11 +1,19 @@ RSpec.describe Queries::GetEmbeddedContent do describe "#call" do let(:organisation) do - create(:live_edition, - title: "bar", - document_type: "organisation", - schema_name: "organisation", - base_path: "/government/organisations/bar") + edition_params = { + title: "bar", + document: create(:document), + document_type: "organisation", + schema_name: "organisation", + base_path: "/government/organisations/bar", + } + + create(:superseded_edition, **edition_params) + live_edition = create(:live_edition, **edition_params.merge({ user_facing_version: 2 })) + create(:draft_edition, **edition_params.merge({ user_facing_version: 3 })) + + live_edition end let(:content_block) do