diff --git a/app/presenters/worldwide_organisation_presenter.rb b/app/presenters/worldwide_organisation_presenter.rb index b7f98fd07..c8b2134e8 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,19 +70,29 @@ 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 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), @@ -93,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 @@ -128,6 +147,24 @@ 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"] } + .compact.join(", ") + end + def organisation_roles_for(current_appointments) current_appointments .map { |role_appointment| role_appointment.dig("links", "role").first } @@ -140,6 +177,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]