Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add registration already renewed screen for magic link error #839

Merged
merged 5 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/controllers/waste_carriers_engine/renews_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ def new
private

def validate_renew_token
return render(:already_renewed) if registration.already_renewed?

# TODO
# return render(:invalid_magic_link, status: 404) unless registration.present?
# return render(:already_renewed) if registration.already_renewed?
# return render(:past_renewal_window) if registration.past_renewal_window?
end

Expand Down
9 changes: 8 additions & 1 deletion app/models/waste_carriers_engine/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ def generate_renew_token!
save!
end

def already_renewed?
check_service.date_can_renew_from > Time.now.in_time_zone("London").to_date
cintamani marked this conversation as resolved.
Show resolved Hide resolved
end

def expire!
metaData.status = "EXPIRED"

Expand All @@ -73,11 +77,14 @@ def renewable_status?
end

def renewable_date?
check_service = ExpiryCheckService.new(self)
return true if check_service.in_expiry_grace_window?
return false if check_service.expired?

check_service.in_renewal_window?
end

def check_service
@_check_service ||= ExpiryCheckService.new(self)
end
end
end
41 changes: 25 additions & 16 deletions app/services/waste_carriers_engine/expiry_check_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,38 @@ module WasteCarriersEngine
# Contains methods related to dealing with dates in the service, for example
# whether a date would be considered as expired.
class ExpiryCheckService
attr_reader :expiry_date, :registration_date
# attr_reader :expiry_date, :registration_date
cintamani marked this conversation as resolved.
Show resolved Hide resolved
attr_reader :registration

delegate :expires_on, to: :registration

def initialize(registration)
raise "ExpiryCheckService expects a registration" if registration.nil?

@registration_date = registration.metaData.date_registered
@expires_on = registration.expires_on
@expiry_date = corrected_expires_on
@registration = registration
end

# For more details about the renewal window check out
# https://github.com/DEFRA/dst-guides/blob/master/services/wcr/renewal_window.md
def date_can_renew_from
(@expiry_date.to_date - Rails.configuration.renewal_window.months)
(expiry_date.to_date - Rails.configuration.renewal_window.months)
end

def expiry_date_after_renewal
@expiry_date.to_date + Rails.configuration.expires_after.years
expiry_date.to_date + Rails.configuration.expires_after.years
end

def expired?
# Registrations are expired on the date recorded for their expiry date e.g.
# an expiry date of Mar 25 2018 means the registration was active up till
# 24:00 on Mar 24 2018.
@expiry_date.to_date <= current_day
expiry_date.to_date <= current_day
end

def in_renewal_window?
# If the registration expires in more than x months from now, its outside
# the renewal window
@expiry_date.to_date < Rails.configuration.renewal_window.months.from_now
expiry_date.to_date < Rails.configuration.renewal_window.months.from_now
end

# Its important to note that a registration is expired on its expires_on date.
Expand All @@ -44,9 +45,17 @@ def in_renewal_window?
# till Oct 4 (i.e. 1 + 3) when in fact we need to include the 1st as one of
# our grace window days.
def in_expiry_grace_window?
last_day_of_grace_window = (@expiry_date.to_date + Rails.configuration.grace_window.days) - 1.day
last_day_of_grace_window = (expiry_date.to_date + Rails.configuration.grace_window.days) - 1.day

current_day >= expiry_date.to_date && current_day <= last_day_of_grace_window
end

def registration_date
@_registration_date ||= registration.metaData.date_registered
end

current_day >= @expiry_date.to_date && current_day <= last_day_of_grace_window
def expiry_date
@_expiry_date ||= corrected_expires_on
end

private
Expand All @@ -57,11 +66,11 @@ def in_expiry_grace_window?
# not be the same as the UK date. So compensate to avoid flagging something
# as expired on the wrong date.
def corrected_expires_on
return Date.new(1970, 1, 1) if @expires_on.nil?
return @expires_on + 1.hour if registered_in_bst_and_expires_in_gmt?
return @expires_on - 1.hour if registered_in_gmt_and_expires_in_bst?
return Date.new(1970, 1, 1) if expires_on.nil?
return expires_on + 1.hour if registered_in_bst_and_expires_in_gmt?
return expires_on - 1.hour if registered_in_gmt_and_expires_in_bst?

@expires_on
expires_on
end

def registered_in_bst_and_expires_in_gmt?
Expand All @@ -73,11 +82,11 @@ def registered_in_gmt_and_expires_in_bst?
end

def registered_in_daylight_savings?
@registration_date.in_time_zone("London").dst?
registration_date.in_time_zone("London").dst?
end

def expires_on_in_daylight_savings?
@expires_on.in_time_zone("London").dst?
expires_on.in_time_zone("London").dst?
end

# We store dates and times in UTC, but want to use the current date in the
Expand Down
21 changes: 21 additions & 0 deletions app/views/waste_carriers_engine/renews/already_renewed.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<div class="text">
<h1 class="heading-large">
<%= t(".heading") %>
</h1>

<p>
<%= t(".paragraph1", reg_identifier: @registration.reg_identifier) %>
</p>

<p>
<%= t(".paragraph2_html", link_to_new_registration: new_start_form_path) %>
</p>

<p>
<%= t(".paragraph3") %>
</p>

<p>
<%= t(".paragraph4_html") %>
</p>
</div>
9 changes: 9 additions & 0 deletions config/locales/forms/renews/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
en:
waste_carriers_engine:
renews:
already_renewed:
heading: "That registration has already been renewed"
paragraph1: "Our records show that registration %{reg_identifier} has already been renewed."
paragraph2_html: "If you need to register other waste exemptions you should <a href=\"%{link_to_new_registration}\">make a new registration.</a>"
cintamani marked this conversation as resolved.
Show resolved Hide resolved
paragraph3: "If you want to discuss this registration, contact the Environment Agency. You will need your registration number."
paragraph4_html: "Environment Agency helpline: 03708 506 506 (Monday to Friday, 8am to 6pm) <a href=\"mailto:enquiries@environment-agency.gov.uk\">enquiries@environment-agency.gov.uk</a>"
4 changes: 4 additions & 0 deletions spec/factories/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
end
end

trait :already_renewed do
expires_on { Rails.configuration.expires_after.years.from_now.to_date }
end

trait :lower_tier do
tier { "LOWER" }
end
Expand Down
20 changes: 20 additions & 0 deletions spec/models/waste_carriers_engine/registration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,26 @@ module WasteCarriersEngine
end
end

describe "#already_renewed?" do
let(:registration) { build(:registration, :has_required_data, expires_on: expires_on) }

context "when the renewal start date is in the future" do
let(:expires_on) { 10.years.from_now }

it "returns true" do
expect(registration).to be_already_renewed
end
end

context "when the renewal start date is not in the future" do
let(:expires_on) { Time.now }

it "returns false" do
expect(registration).to_not be_already_renewed
end
end
end

describe "scopes" do
describe ".active" do
it "returns active registrations" do
Expand Down
15 changes: 15 additions & 0 deletions spec/requests/waste_carriers_engine/renews_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ module WasteCarriersEngine
end
end
end

context "when the registration has already been renewed" do
let(:registration) { create(:registration, :has_required_data, :already_renewed) }

it "returns a 200 response code and the correct template" do
allow(Rails.configuration).to receive(:renewal_window).and_return(3)

registration.generate_renew_token!

get renew_path(token: registration.renew_token)

expect(response).to have_http_status(200)
expect(response).to render_template(:already_renewed)
end
end
end
end
end