Skip to content

Commit

Permalink
Ceased or revoked - start form (#624)
Browse files Browse the repository at this point in the history
# Ceased or revoked - start form 

From: https://eaflood.atlassian.net/browse/RUBY-807

This adds all the code that will deal with the start of the journey for the ceased and revoked form.
  • Loading branch information
cintamani committed Dec 30, 2019
1 parent 0c7ed72 commit 83c98af
Show file tree
Hide file tree
Showing 14 changed files with 402 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

module WasteCarriersEngine
class CeaseOrRevokeFormsController < FormsController
def new
super(CeaseOrRevokeForm, "cease_or_revoke_form")
end

def create
super(CeaseOrRevokeForm, "cease_or_revoke_form")
end

private

def transient_registration_attributes
params.fetch(:cease_or_revoke_form).permit(metaData: %i[status revoked_reason])
end

# rubocop:disable Naming/MemoizedInstanceVariableName
def find_or_initialize_transient_registration(token)
@transient_registration ||= CeasedOrRevokedRegistration.where(reg_identifier: token).first ||
CeasedOrRevokedRegistration.where(token: token).first ||
CeasedOrRevokedRegistration.new(reg_identifier: token)
end
# rubocop:enable Naming/MemoizedInstanceVariableName
end
end
40 changes: 40 additions & 0 deletions app/forms/waste_carriers_engine/cease_or_revoke_form.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module WasteCarriersEngine
class CeaseOrRevokeForm < BaseForm
def self.can_navigate_flexibly?
false
end

delegate :metaData, to: :transient_registration
delegate :status, :revoked_reason, to: :metaData, allow_nil: true
delegate :contact_address, :company_name, :registration_type, :tier, to: :transient_registration

validate :validate_status
validate :validate_revoked_reason

private

def validate_status
return true if %w[INACTIVE REVOKED].include?(status)

errors.add(:status, :presence)

false
end

def validate_revoked_reason
if revoked_reason.blank?
errors.add(:revoked_reason, :presence)

false
elsif revoked_reason.size > 500
errors.add(:revoked_reason, :length)

false
end

true
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module WasteCarriersEngine
class CeasedOrRevokedConfirmForm < BaseForm
# TODO
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CeasedOrRevokedRegistration < TransientRegistration

validates :reg_identifier, "waste_carriers_engine/reg_identifier": true

delegate :status, to: :registration
delegate :company_name, :registration_type, :tier, :contact_address, to: :registration

def registration
@_registration ||= Registration.find_by(reg_identifier: reg_identifier)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module WasteCarriersEngine
class CeasedOrRevokedRegistrationPermissionChecksService < BaseRegistrationPermissionChecksService

private

def all_checks_pass?
transient_registration_is_valid? && user_has_permission? && registation_is_active?
end

def user_has_permission?
return true if can?(:revoke, registration) && can?(:cease, registration)

permission_check_result.needs_permissions!

false
end

def registation_is_active?
return true if registration.active?

permission_check_result.invalid!

false
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def run_flow_setup_checks
RenewingRegistrationPermissionChecksService.run(params)
when OrderCopyCardsRegistration
OrderCopyCardsRegistrationPermissionChecksService.run(params)
when CeasedOrRevokedRegistration
CeasedOrRevokedRegistrationPermissionChecksService.run(params)
else
raise MissingFlowPermissionChecksService, "No permission service found for #{transient_registration.class}"
end
Expand Down
62 changes: 62 additions & 0 deletions app/views/waste_carriers_engine/cease_or_revoke_forms/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<%= render("waste_carriers_engine/shared/back", back_path: Rails.application.routes.url_helpers.registration_path(@transient_registration.reg_identifier)) %>

<div class="text">
<%= form_for(@cease_or_revoke_form) do |f| %>
<%= f.fields_for :metaData do |f| %>
<%= render("waste_carriers_engine/shared/errors", object: @cease_or_revoke_form) %>

<h1 class="heading-large"><%= t(".heading", reg_identifier: @transient_registration.reg_identifier) %></h1>

<div class="panel wcr-panel wcr-panel-border-all">
<h2 class="heading-medium"><%= @cease_or_revoke_form.company_name %></h2>

<p class="lede">
<%= t(".tier.#{@cease_or_revoke_form.tier.downcase}") %> -
<%= t(".registration_type.#{@cease_or_revoke_form.registration_type}") %>
</p>
<p>
<%= displayable_address(@cease_or_revoke_form.contact_address).join(", ") %>
</p>
</div>

<div class="form-group <%= "form-group-error" if @cease_or_revoke_form.errors[:status].any? %>">
<fieldset>
<legend>
<p>
<%= t(".cease_or_revoke.legend") %>
</p>
</legend>

<% if @cease_or_revoke_form.errors[:status].any? %>
<span class="error-message"><%= @cease_or_revoke_form.errors[:status].join(", ") %></span>
<% end %>

<div class="multiple-choice">
<%= f.radio_button :status, "REVOKED", checked: @transient_registration.metaData&.status == "REVOKED" %>
<%= f.label :status, t(".cease_or_revoke.options.revoke"), value: "REVOKED" %>
</div>

<div class="multiple-choice">
<%= f.radio_button :status, "INACTIVE", checked: @transient_registration.metaData&.status == "INACTIVE" %>
<%= f.label :status, t(".cease_or_revoke.options.cease"), value: "INACTIVE" %>
</div>
</fieldset>
</div>

<div class="form-group <%= "form-group-error" if @cease_or_revoke_form.errors[:revoked_reason].any? %>">
<fieldset>
<% if @cease_or_revoke_form.errors[:revoked_reason].any? %>
<span class="error-message"><%= @cease_or_revoke_form.errors[:revoked_reason].join(", ") %></span>
<% end %>
<%= f.label :revoked_reason, t(".cease_or_revoke.reason"), class: "form-label" %>
<%= f.text_area :revoked_reason, class: "form-control form-control-3-4", rows: 5, value: @transient_registration.metaData&.revoked_reason %>
</fieldset>
</div>

<div class="form-group">
<%= f.submit t(".next_button"), class: "button" %>
</div>
<% end %>
<% end %>
</div>
34 changes: 34 additions & 0 deletions config/locales/forms/cease_or_revoke_forms/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
en:
waste_carriers_engine:
cease_or_revoke_forms:
new:
heading: "Revoke or cease registration %{reg_identifier}"
tier:
upper: "Upper tier"
lower: "Lower tier"
registration_type:
carrier_dealer: "Carrier and dealer"
broker_dealer: "Broker and dealer"
carrier_broker_dealer: "Carrier, broker and dealer"
reason:
label: "Reason"
help_text: "Maximum 500 characters"
cease_or_revoke:
legend: "What do you want to do?"
options:
revoke: "Revoke"
cease: "Cease"
reason: "Reason"
contact_address:
heading: "Contact address:"
next_button: "Confirm"
activemodel:
errors:
models:
waste_carriers_engine/cease_or_revoke_form:
attributes:
status:
presence: "Select revoke or cease"
revoked_reason:
presence: "Enter a reason"
length: "Reason must be 500 characters or fewer"
10 changes: 10 additions & 0 deletions config/locales/models/ceased_or_revoked_registrations/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
en:
activemodel:
errors:
models:
waste_carriers_engine/ceased_or_revoked_registration:
attributes:
reg_identifier:
invalid_format: "The registration ID is not in a valid format"
no_registration: "There is no registration matching this ID"
renewal_in_progress: "This renewal is already in progress"
8 changes: 4 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
# End of order copy cards flow

# Ceased or revoked flow
resources :cease_or_revoke_form,
resources :cease_or_revoke_forms,
only: %i[new create],
path: "ceased-or-revoked",
path: "cease-or-revoke",
path_names: { new: "" }

resources :ceased_or_revoked_confirm_form,
resources :ceased_or_revoked_confirm_forms,
only: %i[new create],
path: "ceased-or-revoked-confirm",
path_names: { new: "" } do
Expand All @@ -53,7 +53,7 @@
on: :collection
end

resources :ceased_or_revoked_completed_form,
resources :ceased_or_revoked_completed_forms,
only: %i[create],
path: "ceased-or-revoked-complete"
# End of ceased or revoked flow
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy/app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ def initialize(user)
can :read, WasteCarriersEngine::Registration, account_email: user.email
can :manage, WasteCarriersEngine::RenewingRegistration, account_email: user.email
can :order_copy_cards, WasteCarriersEngine::Registration
can :cease, WasteCarriersEngine::Registration
can :revoke, WasteCarriersEngine::Registration
end
end
2 changes: 1 addition & 1 deletion spec/factories/ceased_or_revoked_registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

FactoryBot.define do
factory :ceased_or_revoked_registration, class: WasteCarriersEngine::CeasedOrRevokedRegistration do
initialize_with { new(reg_identifier: build(:registration, :has_required_data, :is_active).reg_identifier) }
initialize_with { new(reg_identifier: create(:registration, :has_required_data, :is_active).reg_identifier) }
end
end
Loading

0 comments on commit 83c98af

Please sign in to comment.