From c3b86d9de7be84c86ef8ba86ea759450e604a01d Mon Sep 17 00:00:00 2001 From: Bruce Bolt Date: Mon, 22 Jan 2024 12:11:04 +0000 Subject: [PATCH 1/2] Use edition links for worldwide organisation people We are switching worldwide organisations to use edition links which do not support multi-level link expanstion. Therefore we need to switch the rendering code to use the new link fields that have been added, in the absence of multi-level links. This change adds support for edition links, but also retains compatibility with non-edition links. This will allow us to deploy the frontend changes in advance of republishing the content items, preventing any downtime of these pages. The code for backward compatibility will be removed once the content items have all been republished with edition links. --- .../worldwide_organisation_presenter.rb | 36 +++++- .../worldwide_organisation_presenter_test.rb | 108 +++++++++++++++++- 2 files changed, 138 insertions(+), 6 deletions(-) diff --git a/app/presenters/worldwide_organisation_presenter.rb b/app/presenters/worldwide_organisation_presenter.rb index b7f98fd07..c4296b927 100644 --- a/app/presenters/worldwide_organisation_presenter.rb +++ b/app/presenters/worldwide_organisation_presenter.rb @@ -48,14 +48,18 @@ def person_in_primary_role return unless content_item["links"]["primary_role_person"] person = content_item.dig("links", "primary_role_person").first - current_roles = person.dig("links", "role_appointments").select { |role_app| role_app.dig("details", "current") } + current_roles = if person.dig("links", "role_appointments") + person.dig("links", "role_appointments").select { |role_app| role_app.dig("details", "current") } # To be removed once switched to edition links + else + roles_for_person(person["content_id"]) + end { name: person["title"], href: person["web_url"], image_url: person["details"]["image"]["url"], image_alt: person["details"]["image"]["alt_text"], - description: organisation_roles_for(current_roles), + description: person.dig("links", "role_appointments") ? organisation_roles_for(current_roles) : presented_title_for_roles(current_roles), # Call to `organisation_roles_for` to be removed once switched to edition links } end @@ -66,12 +70,16 @@ def people_in_non_primary_roles return [] unless people.any? people.map do |person| - current_roles = person.dig("links", "role_appointments").select { |role_app| role_app.dig("details", "current") } + current_roles = if person.dig("links", "role_appointments") + person.dig("links", "role_appointments").select { |role_app| role_app.dig("details", "current") } # To be removed once switched to edition links + else + roles_for_person(person["content_id"]) + end { name: person["title"], href: person["web_url"], - description: organisation_roles_for(current_roles), + description: person.dig("links", "role_appointments") ? organisation_roles_for(current_roles) : presented_title_for_roles(current_roles), # Call to `organisation_roles_for` to be removed once switched to edition links } end end @@ -128,6 +136,12 @@ def sponsoring_organisations private + def presented_title_for_roles(roles) + roles + .map { |role| role["title"] } + .compact.join(", ") + end + def organisation_roles_for(current_appointments) current_appointments .map { |role_appointment| role_appointment.dig("links", "role").first } @@ -140,6 +154,20 @@ def organisation_role_ids content_item.dig("links", "roles")&.map { |role| role["content_id"] } || [] end + def roles_for_person(person_content_id) + content_item + .dig("details", "people_role_associations") + .select { |people_role_association| people_role_association["person_content_id"] == person_content_id } + .first["role_appointments"] + .pluck("role_content_id") + .map { |role_content_id| + content_item.dig("links", "roles").select do |role| + role["content_id"] == role_content_id + end + } + .flatten + end + def world_locations content_item.dig("links", "world_locations") || [] end diff --git a/test/presenters/worldwide_organisation_presenter_test.rb b/test/presenters/worldwide_organisation_presenter_test.rb index cb5e93ee7..17054d897 100644 --- a/test/presenters/worldwide_organisation_presenter_test.rb +++ b/test/presenters/worldwide_organisation_presenter_test.rb @@ -5,21 +5,125 @@ def schema_name "worldwide_organisation" end - test "description of primary_role_person should have spaces between roles" do + test "description of primary_role_person should have spaces between roles with non-edition links" do presenter = create_presenter(WorldwideOrganisationPresenter, content_item: { "links" => { "primary_role_person" => [{ "details" => { "image" => {} }, "links" => { "role_appointments" => [{ "details" => { "current" => true }, "links" => { "role" => [{ "content_id" => "1", "title" => "Example Role 1" }] } }, { "details" => { "current" => true }, "links" => { "role" => [{ "content_id" => "2", "title" => "Example Role 2" }] } }] } }], "roles" => [{ "content_id" => "1" }, { "content_id" => "2" }] } }) assert_equal "Example Role 1, Example Role 2", presenter.person_in_primary_role[:description] end + test "description of primary_role_person should have spaces between roles with edition links" do + presenter = create_presenter(WorldwideOrganisationPresenter, content_item: { + "details" => { "people_role_associations" => [ + { + "person_content_id" => "person_1", + "role_appointments" => [ + { + "role_appointment_content_id" => "role_apppointment_1", + "role_content_id" => "role_1", + }, + { + "role_appointment_content_id" => "role_apppointment_2", + "role_content_id" => "role_2", + }, + ], + }, + ] }, + "links" => { + "primary_role_person" => [ + { + "content_id" => "person_1", + "details" => { "image" => {} }, + "links" => {}, + }, + ], + "role_appointments" => [ + { + "content_id" => "role_apppointment_1", + "details" => { "current" => true }, + "links" => {}, + }, + { + "content_id" => "role_apppointment_2", + "details" => { "current" => true }, + "links" => {}, + }, + ], + "roles" => [ + { + "content_id" => "role_1", + "title" => "Example Role 1", + }, + { + "content_id" => "role_2", + "title" => "Example Role 2", + }, + ], + }, + }) + assert_equal "Example Role 1, Example Role 2", presenter.person_in_primary_role[:description] + end + test "description of primary_role_person should only show roles that are associated with the organisation" do presenter = create_presenter(WorldwideOrganisationPresenter, content_item: { "links" => { "primary_role_person" => [{ "details" => { "image" => {} }, "links" => { "role_appointments" => [{ "details" => { "current" => true }, "links" => { "role" => [{ "content_id" => "1", "title" => "Example Role 1" }] } }, { "details" => { "current" => true }, "links" => { "role" => [{ "content_id" => "2", "title" => "Example Role 2" }] } }] } }], "roles" => [{ "content_id" => "1" }] } }) assert_equal "Example Role 1", presenter.person_in_primary_role[:description] end - test "description of people_in_non_primary_roles should have spaces between roles" do + test "description of people_in_non_primary_roles should have spaces between roles with non-edition links" do presenter = create_presenter(WorldwideOrganisationPresenter, content_item: { "links" => { "secondary_role_person" => [{ "details" => { "image" => {} }, "links" => { "role_appointments" => [{ "details" => { "current" => true }, "links" => { "role" => [{ "content_id" => "1", "title" => "Example Role 1" }] } }, { "details" => { "current" => true }, "links" => { "role" => [{ "content_id" => "2", "title" => "Example Role 2" }] } }] } }], "roles" => [{ "content_id" => "1" }, { "content_id" => "2" }] } }) assert_equal "Example Role 1, Example Role 2", presenter.people_in_non_primary_roles.first[:description] end + test "description of people_in_non_primary_roles should have spaces between roles with edition links" do + presenter = create_presenter(WorldwideOrganisationPresenter, content_item: { + "details" => { "people_role_associations" => [ + { + "person_content_id" => "person_1", + "role_appointments" => [ + { + "role_appointment_content_id" => "role_apppointment_1", + "role_content_id" => "role_1", + }, + { + "role_appointment_content_id" => "role_apppointment_2", + "role_content_id" => "role_2", + }, + ], + }, + ] }, + "links" => { + "secondary_role_person" => [ + { + "content_id" => "person_1", + "details" => { "image" => {} }, + "links" => {}, + }, + ], + "role_appointments" => [ + { + "content_id" => "role_apppointment_1", + "details" => { "current" => true }, + "links" => {}, + }, + { + "content_id" => "role_apppointment_2", + "details" => { "current" => true }, + "links" => {}, + }, + ], + "roles" => [ + { + "content_id" => "role_1", + "title" => "Example Role 1", + }, + { + "content_id" => "role_2", + "title" => "Example Role 2", + }, + ], + }, + }) + assert_equal "Example Role 1, Example Role 2", presenter.people_in_non_primary_roles.first[:description] + end + test "description of people_in_non_primary_roles should only show roles that are associated with the organisation" do presenter = create_presenter(WorldwideOrganisationPresenter, content_item: { "links" => { "secondary_role_person" => [{ "details" => { "image" => {} }, "links" => { "role_appointments" => [{ "details" => { "current" => true }, "links" => { "role" => [{ "content_id" => "1", "title" => "Example Role 1" }] } }, { "details" => { "current" => true }, "links" => { "role" => [{ "content_id" => "2", "title" => "Example Role 2" }] } }] } }], "roles" => [{ "content_id" => "1" }] } }) assert_equal "Example Role 1", presenter.people_in_non_primary_roles.first[:description] From ef6aa6a4ea9c40d62501838c03ceb0df7f505958 Mon Sep 17 00:00:00 2001 From: Bruce Bolt Date: Mon, 22 Jan 2024 13:38:15 +0000 Subject: [PATCH 2/2] Use edition links for worldwide organisation offices We are switching worldwide organisations to use edition links which do not support multi-level link expanstion. Therefore we need to switch the rendering code to use the new link fields that have been added, in the absence of multi-level links. This change adds support for edition links, but also retains compatibility with non-edition links. This will allow us to deploy the frontend changes in advance of republishing the content items, preventing any downtime of these pages. The code for backward compatibility will be removed once the content items have all been republished with edition links. --- .../worldwide_organisation_presenter.rb | 27 +++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/app/presenters/worldwide_organisation_presenter.rb b/app/presenters/worldwide_organisation_presenter.rb index c4296b927..c8b2134e8 100644 --- a/app/presenters/worldwide_organisation_presenter.rb +++ b/app/presenters/worldwide_organisation_presenter.rb @@ -86,7 +86,13 @@ def people_in_non_primary_roles def main_office return unless (office_item = content_item.dig("links", "main_office")&.first) - return unless (office_contact_item = office_item.dig("links", "contact")&.first) + + office_contact_item = if office_item.dig("links", "contact") + office_item.dig("links", "contact")&.first # To be removed once switched to edition links + else + contact_for_office(office_item["content_id"]) + end + return unless office_contact_item WorldwideOffice.new( contact: WorldwideOrganisation::LinkedContactPresenter.new(office_contact_item), @@ -101,7 +107,12 @@ def home_page_offices return [] unless content_item.dig("links", "home_page_offices") content_item.dig("links", "home_page_offices").map { |office| - next unless (contact = office.dig("links", "contact")&.first) + contact = if office.dig("links", "contact") + office.dig("links", "contact")&.first # To be removed once switched to edition links + else + contact_for_office(office["content_id"]) + end + next unless contact WorldwideOrganisation::LinkedContactPresenter.new(contact) }.compact @@ -136,6 +147,18 @@ def sponsoring_organisations private + def contact_for_office(office_content_id) + contact_mapping = content_item.dig("details", "office_contact_associations").select { |office_contact_association| + office_contact_association["office_content_id"] == office_content_id + }.first + + return unless contact_mapping + + content_item.dig("links", "contacts").select { |contact| + contact["content_id"] == contact_mapping["contact_content_id"] + }.first + end + def presented_title_for_roles(roles) roles .map { |role| role["title"] }