module PublishingApiHelper
  # state_history returns a hash like {"3"=>"draft", "2"=>"published", "1"=>"superseded"}
  # so we need to get the highest value for a key.
  def latest_edition_number(content_id, publication_state: "")
    latest_content_item = content_item(content_id).with_indifferent_access

    if publication_state.present?
      version = latest_content_item[:state_history].select { |_, hash| hash[publication_state] }
      return version.keys.first.to_i
    end

    latest_content_item[:state_history].keys.max.to_i
  end

  def content_item(content_id)
    Services.publishing_api.get_content(content_id)
  end
end

module PublishingApiHelper
  # state_history returns a hash like {"3"=>"draft", "2"=>"published", "1"=>"superseded"}
  # so we need to get the highest value for a key.
  def latest_edition_number(content_id, publication_state: "")
    latest_content_item = content_item(content_id).with_indifferent_access

    if publication_state.present?
      version = latest_content_item[:state_history].select { |_, hash| hash[publication_state] }
      return version.keys.first.to_i
    end

    latest_content_item[:state_history].keys.max.to_i
  end

  def content_item(content_id)
    Services.publishing_api.get_content(content_id).to_hash
  end
end

Fix latest edition number bug
Fix latest edition number bug

The fix applied in PR #502 didn’t completely fix the problem.

We need to parse the content returned by GDS API before we can apply Hash methods (with_indifferent_access) to it.

Error in [sentry](https://sentry.io/govuk/app-collections-publisher/issues/696647763/?query=is:unresolved)
