Skip to content

Commit

Permalink
Merge pull request #5731 from alphagov/automate-changing-statuses
Browse files Browse the repository at this point in the history
Display upcoming COVID status changes on the results page
  • Loading branch information
leenagupte authored Jan 24, 2022
2 parents a19f5e9 + cbe06fb commit 28cf595
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
margin_bottom: 4,
} %>

<% if country.next_covid_status.present? %>
<%= render partial: "country/status_change", locals: { country: country } %>
<% end %>

<% if calculator.countries_with_content_headers_converted.include?(country.slug) %>
<p class="govuk-body">You should read the following sections of the <%= country.title %> entry requirements guidance on:</p>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%
next_status_date = country.next_covid_status_applies_at.to_s(:govuk_date)
next_status_time = country.next_covid_status_applies_at.to_s(:govuk_time)
%>

<% if country.next_covid_status == "red" %>
<%= render "govuk_publishing_components/components/warning_text", {
text: "#{country.title} will move to the red list for travel to England at #{next_status_time} on #{next_status_date}. Until then you should follow the rules below for returning to England."
} %>
<% else %>
<%= render "govuk_publishing_components/components/warning_text", {
text: "#{country.title} will be removed from the red list for travel to England at #{next_status_time} on #{next_status_date}. Until then you should follow the rules below for returning to England."
} %>
<% end %>
54 changes: 45 additions & 9 deletions app/models/world_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,61 @@ def self.travel_rules
@travel_rules ||= YAML.load_file(Rails.root.join("config/smart_answers/check_travel_during_coronavirus_data.yml"))
end

def initialize(location)
@title = location.fetch("title", "")
@details = location.fetch("details", {})
@slug = @details.fetch("slug", "")
end

alias_method :name, :title

def covid_status
current_covid_status_data&.dig("covid_status")
end

def next_covid_status
next_covid_status_data&.dig("covid_status")
end

def next_covid_status_applies_at
Time.zone.parse(next_covid_status_data["covid_status_applies_at"])
rescue NoMethodError, TypeError
nil
end

def current_covid_status_data
current_statuses = covid_status_data_for_location&.select do |status|
start_date = Time.zone.parse(status["covid_status_applies_at"])
start_date.past?
end

return if current_statuses.blank?

current_statuses.max_by { |status| status["covid_status_applies_at"] }
end

def next_covid_status_data
future_statuses = covid_status_data_for_location&.select do |status|
start_date = Time.zone.parse(status["covid_status_applies_at"])
start_date.future?
end

return if future_statuses.blank?

future_statuses.min_by { |status| status["covid_status_applies_at"] }
end

def covid_status_data_for_location
# Once the world location api returns the covid status, we should be able
# to replace this line with:
# location.fetch("england_coronavirus_travel", "")
rules = self.class.travel_rules["results"].select { |country| country["details"]["slug"] == slug }.first

return if rules.blank?

rules["england_coronavirus_travel"]["covid_status"]
rules["england_coronavirus_travel"]
end

def initialize(location)
@title = location.fetch("title", "")
@details = location.fetch("details", {})
@slug = @details.fetch("slug", "")
end

alias_method :name, :title

def ==(other)
other.is_a?(self.class) && other.slug == @slug
end
Expand Down
1 change: 1 addition & 0 deletions config/initializers/time_formats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
shared_formats = {
govuk_date: "%-d %B %Y", # '4 December 2009'
govuk_date_with_day: "%A, %d %B %Y", # 'Friday, 4 December 2009'
govuk_time: "%l:%M%P", # '2:30am'
}

Time::DATE_FORMATS.merge! shared_formats
Expand Down
16 changes: 8 additions & 8 deletions config/smart_answers/check_travel_during_coronavirus_data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ results:
details:
slug: italy
england_coronavirus_travel:
covid_status: green
next_covid_status:
next_covid_status_applies_at:
status_out_of_date: false
- covid_status: not_red
covid_status_applies_at: "2021-12-20T:02:00.000+00:00"
- covid_status: red
covid_status_applies_at: "2022-02-20T:02:30.000+00:00"
- title: South Africa
details:
slug: south-africa
england_coronavirus_travel:
covid_status: red
next_covid_status:
next_covid_status_applies_at:
status_out_of_date: false
- covid_status: red
covid_status_applies_at: "2021-12-20T:02:00.000+00:00"
- covid_status: not_red
covid_status_applies_at: "2022-02-20T:16:00.000+00:00"
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "test_helper"
require "support/flow_test_helper"

class CheckTravelDuringCoronavirusTest < ActiveSupport::TestCase
class CheckTravelDuringCoronavirusFlowTest < ActiveSupport::TestCase
include FlowTestHelper

setup do
Expand Down Expand Up @@ -218,6 +218,89 @@ class CheckTravelDuringCoronavirusTest < ActiveSupport::TestCase
assert_rendered_outcome text: "You are travelling through"
end

context "content for countries changing covid status" do
should "render 'move to the red list' content for countries moving to the red list" do
travel_to("2022-01-01") do
WorldLocation.stubs(:travel_rules).returns({
"results" => [
{
"title" => "Poland",
"details" => {
"slug" => "poland",
},
"england_coronavirus_travel" => [
{
"covid_status" => "not_red",
"covid_status_applies_at" => "2021-12-20T:02:00.000+00:00",
},
{
"covid_status" => "red",
"covid_status_applies_at" => "2022-02-20T:02:00.000+00:00",
},
],
},
],
})
assert_rendered_outcome text: "Poland will move to the red list for travel to England"
end
end

should "render 'removed from the red list' content for countries moving from the red list" do
travel_to("2022-01-01") do
WorldLocation.stubs(:travel_rules).returns({
"results" => [
{
"title" => "Poland",
"details" => {
"slug" => "poland",
},
"england_coronavirus_travel" => [
{
"covid_status" => "red",
"covid_status_applies_at" => "2021-12-20T:02:00.000+00:00",
},
{
"covid_status" => "not_red",
"covid_status_applies_at" => "2022-02-20T:02:00.000+00:00",
},
],
},
],
})

add_responses going_to_countries_within_10_days: "no"
assert_rendered_outcome text: "Poland will be removed from the red list for travel to England"
end
end

should "not render changing status content if most recent status is in the past" do
travel_to("2022-03-01") do
WorldLocation.stubs(:travel_rules).returns({
"results" => [
{
"title" => "Poland",
"details" => {
"slug" => "poland",
},
"england_coronavirus_travel" => [
{
"covid_status" => "red",
"covid_status_applies_at" => "2021-12-20T:02:00.000+00:00",
},
{
"covid_status" => "not_red",
"covid_status_applies_at" => "2022-02-20T:02:00.000+00:00",
},
],
},
],
})

assert_no_match "Poland will be removed from the red list for travel to England", @test_flow.outcome_text
end
end
end

context "country specific content that has had the headers converted" do
setup do
SmartAnswer::Calculators::CheckTravelDuringCoronavirusCalculator.any_instance.stubs(:countries_with_content_headers_converted).returns(%w[spain italy])
Expand Down
55 changes: 48 additions & 7 deletions test/unit/world_location_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,25 +275,66 @@ class WorldLocationTest < ActiveSupport::TestCase

context "england_coronavirus_travel" do
setup do
stub_worldwide_api_has_location("italy")
@future_status_date = "2022-02-20T:02:00.000+00:00"
WorldLocation.stubs(:travel_rules).returns({
"results" => [
{
"title" => "Italy",
"details" => {
"slug" => "italy",
},
"england_coronavirus_travel" => {
"covid_status" => "red",
},
"england_coronavirus_travel" => [
{
"covid_status" => "red",
"covid_status_applies_at" => "2021-12-20T:02:00.000+00:00",
},
{
"covid_status" => "not_red",
"covid_status_applies_at" => @future_status_date,
},
],
},
],
})
@location = WorldLocation.find("italy")
travel_to("2022-01-01")
end

should "find the covid status for a location" do
assert_equal "red", @location.covid_status
context "covid statuses exist" do
setup do
stub_worldwide_api_has_location("italy")
@location = WorldLocation.find("italy")
end

should "find the current covid status for a location" do
assert_equal "red", @location.covid_status
end

should "find the next covid status for a location" do
assert_equal "not_red", @location.next_covid_status
end

should "find the next covid status applies at date for a location" do
assert_equal Time.zone.parse(@future_status_date), @location.next_covid_status_applies_at
end
end

context "covid statuses do not exist" do
setup do
stub_worldwide_api_has_location("spain")
@location = WorldLocation.find("spain")
end

should "return covid status of nil if covid statuses unknown for location" do
assert_nil @location.covid_status
end

should "return a next covid status of nil if covid statuses unknown for location" do
assert_nil @location.next_covid_status
end

should "return a next covid status date of nil if covid statuses unknown for location" do
assert_nil @location.next_covid_status_applies_at
end
end
end
end

0 comments on commit 28cf595

Please sign in to comment.