diff --git a/app/graphql/types/role_type.rb b/app/graphql/types/role_type.rb index b45069199..9be60c96a 100644 --- a/app/graphql/types/role_type.rb +++ b/app/graphql/types/role_type.rb @@ -11,42 +11,49 @@ class Organisation < Types::BaseObject class RoleAppointment < Types::BaseObject class Person < Types::BaseObject - field :base_path, String - field :biography, String - field :title, String, null: false + class PersonDetails < Types::BaseObject + field :body, String - def biography - Presenters::EditionPresenter - .new(object) - .present - .dig(:details, :body) - .find { |body| body[:content_type] == "text/html" }[:content] - end - end + def body + govspeak = object.fetch(:body, []) + .filter { _1[:content_type] == "text/govspeak" } + .map { _1[:content] } + .first - field :ended_on, GraphQL::Types::ISO8601DateTime - field :person, Person - field :started_on, GraphQL::Types::ISO8601DateTime + Govspeak::Document.new(govspeak).to_html if govspeak.present? + end + end - def ended_on - object.details[:ended_on] + field :base_path, String + field :details, PersonDetails + field :title, String, null: false end - def person - Edition - .live - .joins(document: { reverse_links: :link_set }) - .where( - document: { locale: "en" }, - link_set: { content_id: object.content_id }, - reverse_links: { link_type: "person" }, - ) - .first + class RoleAppointmentDetails < Types::BaseObject + field :current, Boolean + field :ended_on, GraphQL::Types::ISO8601DateTime + field :started_on, GraphQL::Types::ISO8601DateTime end - def started_on - object.details[:started_on] + field :details, RoleAppointmentDetails + + class RoleAppointmentLinks < Types::BaseObject + field :person, [Person] + + def person + Edition + .live + .joins(document: { reverse_links: :link_set }) + .where( + document: { locale: "en" }, + link_set: { content_id: object.content_id }, + reverse_links: { link_type: "person" }, + ) + .limit(1) + end end + + field :links, RoleAppointmentLinks, method: :itself end class Translation < Types::BaseObject @@ -54,60 +61,53 @@ class Translation < Types::BaseObject field :base_path, String end - field :available_translations, [Translation] - field :current_role_appointment, RoleAppointment - field :ordered_parent_organisations, [Organisation] - field :past_role_appointments, [RoleAppointment] - field :responsibilities, String - field :supports_historical_accounts, Boolean + class RoleDetails < Types::BaseObject + field :body, String + field :supports_historical_accounts, Boolean - def available_translations - Presenters::Queries::AvailableTranslations.by_edition(object) - .translations.fetch(:available_translations, []) - end + def body + govspeak = object.fetch(:body, []) + .filter { _1[:content_type] == "text/govspeak" } + .map { _1[:content] } + .first - def current_role_appointment - Edition - .live - .joins(document: :link_set_links) - .where( - document: { locale: "en" }, - link_set_links: { target_content_id: object.content_id, link_type: "role" }, - ) - .where("details ->> 'current' = 'true'") - .first + Govspeak::Document.new(govspeak).to_html if govspeak.present? + end end - def ordered_parent_organisations - Edition - .live - .joins(document: { reverse_links: :link_set }) - .where( - document: { locale: "en" }, - link_set: { content_id: object.content_id }, - reverse_links: { link_type: "ordered_parent_organisations" }, - ) - end + class RoleLinks < Types::BaseObject + field :available_translations, [Translation] + field :ordered_parent_organisations, [Organisation] + field :role_appointments, [RoleAppointment] - def past_role_appointments - Edition - .live - .joins(document: :link_set_links) - .where( - document: { locale: "en" }, - link_set_links: { target_content_id: object.content_id, link_type: "role" }, - ) - .where("details ->> 'current' = 'false'") - end + def available_translations + Presenters::Queries::AvailableTranslations.by_edition(object) + .translations.fetch(:available_translations, []) + end - def responsibilities - presented_edition - .dig(:details, :body) - .find { |body| body[:content_type] == "text/html" }[:content] - end + def ordered_parent_organisations + Edition + .live + .joins(document: { reverse_links: :link_set }) + .where( + document: { locale: "en" }, + link_set: { content_id: object.content_id }, + reverse_links: { link_type: "ordered_parent_organisations" }, + ) + end - def supports_historical_accounts - object.details[:supports_historical_accounts] + def role_appointments + Edition + .live + .joins(document: :link_set_links) + .where( + document: { locale: "en" }, + link_set_links: { target_content_id: object.content_id, link_type: "role" }, + ) + end end + + field :details, RoleDetails + field :links, RoleLinks, method: :itself end end diff --git a/spec/integration/graphql_spec.rb b/spec/integration/graphql_spec.rb index 170f7a39d..533ed1692 100644 --- a/spec/integration/graphql_spec.rb +++ b/spec/integration/graphql_spec.rb @@ -354,91 +354,115 @@ edition(basePath: \"/government/ministers/prime-minister\") { ... on Role { basePath - title - responsibilities - supportsHistoricalAccounts locale + title - availableTranslations { - basePath - locale + details { + body + supportsHistoricalAccounts } - currentRoleAppointment { - person { - ...basePersonInfo - biography + links { + availableTranslations { + basePath + locale } - } - - pastRoleAppointments { - endedOn - startedOn - person { - ...basePersonInfo + roleAppointments { + details { + current + endedOn + startedOn + } + + links { + person { + basePath + title + + details { + body + } + } + } } - } - orderedParentOrganisations { - basePath - title + orderedParentOrganisations { + basePath + title + } } } } - } - - fragment basePersonInfo on Person { - basePath - title }", } expected = { data: { edition: { - availableTranslations: [ - { - basePath: "/government/ministers/prime-minister", - locale: "en", - }, - { - basePath: "/government/ministers/prime-minister.es", - locale: "es", - }, - ], basePath: "/government/ministers/prime-minister", - currentRoleAppointment: { - person: { - basePath: "/government/people/keir-starmer", - biography: "
Sir Keir Starmer became Prime Minister on 5 July 2024.
\n", - title: "The Rt Hon Sir Keir Starmer KCB KC MP", - }, - }, locale: "en", - orderedParentOrganisations: [ - { - basePath: "/government/organisations/cabinet-office", - title: "Cabinet Office", - }, - { - basePath: "/government/organisations/prime-ministers-office-10-downing-street", - title: "Prime Minister's Office, 10 Downing Street", - }, - ], - pastRoleAppointments: [ - { - endedOn: "2024-07-05T00:00:00Z", - person: { - basePath: "/government/people/rishi-sunak", - title: "The Rt Hon Rishi Sunak MP", - }, - startedOn: "2022-10-25T00:00:00Z", - }, - ], - responsibilities: "The Prime Minister is the leader of His Majesty’s Government
\n", - supportsHistoricalAccounts: true, title: "Prime Minister", + details: { + body: "The Prime Minister is the leader of His Majesty’s Government
\n", + supportsHistoricalAccounts: true, + }, + links: { + availableTranslations: [ + { + basePath: "/government/ministers/prime-minister", + locale: "en", + }, + { + basePath: "/government/ministers/prime-minister.es", + locale: "es", + }, + ], + roleAppointments: [ + { + details: { + current: true, + endedOn: nil, + startedOn: "2024-07-05T00:00:00Z", + }, + links: { + person: [ + basePath: "/government/people/keir-starmer", + title: "The Rt Hon Sir Keir Starmer KCB KC MP", + details: { + body: "Sir Keir Starmer became Prime Minister on 5 July 2024.
\n", + }, + ], + }, + }, + { + details: { + current: false, + endedOn: "2024-07-05T00:00:00Z", + startedOn: "2022-10-25T00:00:00Z", + }, + links: { + person: [ + basePath: "/government/people/rishi-sunak", + details: { + body: "Rishi Sunak was Prime Minister between 25 October 2022 and 5 July 2024.
\n", + }, + title: "The Rt Hon Rishi Sunak MP", + ], + }, + }, + ], + orderedParentOrganisations: [ + { + basePath: "/government/organisations/cabinet-office", + title: "Cabinet Office", + }, + { + basePath: "/government/organisations/prime-ministers-office-10-downing-street", + title: "Prime Minister's Office, 10 Downing Street", + }, + ], + }, }, }, }