Skip to content

Commit

Permalink
Merge branch 'master' into capt-1632-implement-review-apps
Browse files Browse the repository at this point in the history
  • Loading branch information
vacabor authored Jun 3, 2024
2 parents c2da698 + ab22be6 commit 70897f7
Show file tree
Hide file tree
Showing 41 changed files with 486 additions and 371 deletions.
25 changes: 15 additions & 10 deletions app/forms/bank_details_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class BankDetailsForm < Form
validates :banking_name, presence: {message: i18n_error_message(:enter_banking_name)}
validates :bank_sort_code, presence: {message: i18n_error_message(:enter_sort_code)}
validates :bank_account_number, presence: {message: i18n_error_message(:enter_account_number)}
validates :building_society_roll_number, presence: {message: i18n_error_message(:enter_roll_number)}, if: -> { claim.building_society? }
validates :building_society_roll_number, presence: {message: i18n_error_message(:enter_roll_number)}, if: -> { answers.building_society? }

validate :bank_account_number_must_be_eight_digits
validate :bank_sort_code_must_be_six_digits
Expand All @@ -26,13 +26,15 @@ class BankDetailsForm < Form
def save
return false unless valid?

update!(
banking_name:,
bank_sort_code:,
bank_account_number:,
building_society_roll_number:,
hmrc_bank_validation_succeeded:
journey_session.answers.assign_attributes(
banking_name: banking_name,
bank_sort_code: normalised_bank_detail(bank_sort_code),
bank_account_number: normalised_bank_detail(bank_account_number),
building_society_roll_number: building_society_roll_number,
hmrc_bank_validation_succeeded: hmrc_bank_validation_succeeded
)

journey_session.save!
end

def hmrc_bank_validation_succeeded
Expand All @@ -50,7 +52,7 @@ def hmrc_api_validation_succeeded?
private

def normalised_bank_detail(bank_detail)
bank_detail.gsub(/\s|-/, "")
bank_detail&.gsub(/\s|-/, "")
end

def bank_account_number_must_be_eight_digits
Expand Down Expand Up @@ -93,8 +95,11 @@ def bank_account_is_valid
response = e.response
@hmrc_api_response_error = true
ensure
new_hmrc_bank_validation_responses_value = claim.hmrc_bank_validation_responses.dup << {code: response.code, body: response.body}
claim.update_attribute :hmrc_bank_validation_responses, new_hmrc_bank_validation_responses_value
new_hmrc_bank_validation_responses_value = journey_session.answers.hmrc_bank_validation_responses.dup << {code: response.code, body: response.body}
journey_session.answers.assign_attributes(
hmrc_bank_validation_responses: new_hmrc_bank_validation_responses_value
)
journey_session.save!
end
end

Expand Down
21 changes: 18 additions & 3 deletions app/forms/bank_or_building_society_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,23 @@ class BankOrBuildingSocietyForm < Form
def save
return false unless valid?

claim.assign_attributes(bank_or_building_society:)
claim.reset_dependent_answers
claim.save!
if bank_or_building_society_changed?
journey_session.answers.assign_attributes(
banking_name: nil,
bank_account_number: nil,
bank_sort_code: nil,
building_society_roll_number: nil
)
end

journey_session.answers.assign_attributes(bank_or_building_society:)

journey_session.save!
end

private

def bank_or_building_society_changed?
answers.bank_or_building_society != bank_or_building_society
end
end
38 changes: 21 additions & 17 deletions app/forms/claim_submission_base_form.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class ClaimSubmissionBaseForm
include ActiveModel::Model

attr_reader :journey_session
attr_reader :journey_session, :claim

validate :not_already_submitted
validate :email_address_is_preesent
Expand All @@ -12,12 +12,14 @@ class ClaimSubmissionBaseForm

def initialize(journey_session:)
@journey_session = journey_session
@claim = build_claim
end

def save
return false unless valid?

ApplicationRecord.transaction do
set_attributes_for_claim_submission
main_eligibility.save!
claim.save!
end
Expand All @@ -26,24 +28,13 @@ def save
ClaimVerifierJob.perform_later(claim)
end

def claim
@claim ||= build_claim
end

private

delegate :answers, to: :journey_session

def build_claim
claim = Claim.new

# Temp conditional while we're working with the shim
claim.journey_session = if journey_session.is_a?(ClaimJourneySessionShim)
journey_session.journey_session
else
journey_session
end

claim.eligibility = main_eligibility

answers.attributes.each do |name, value|
Expand All @@ -52,9 +43,6 @@ def build_claim
end
end

claim.policy_options_provided = generate_policy_options_provided
claim.reference = generate_reference
claim.submitted_at = Time.zone.now
claim
end

Expand All @@ -75,6 +63,18 @@ def set_eligibility_attributes(eligibility)
end
end

def set_attributes_for_claim_submission
# Temp conditional while we're working with the shim
claim.journey_session = if journey_session.is_a?(ClaimJourneySessionShim)
journey_session.journey_session
else
journey_session
end
claim.policy_options_provided = generate_policy_options_provided
claim.reference = generate_reference
claim.submitted_at = Time.zone.now
end

def generate_reference
loop {
ref = Reference.new.to_s
Expand Down Expand Up @@ -111,9 +111,13 @@ def mobile_number_verified
end
end

# TODO RL move the logic out of the claim rather than use send
def mobile_number_verified?
claim.send(:submittable_mobile_details?)
return true if answers.using_mobile_number_from_tid?
return true if answers.provide_mobile_number && answers.mobile_number.present? && answers.mobile_verified == true
return true if answers.provide_mobile_number == false && answers.mobile_number.nil? && answers.mobile_verified == false
return true if answers.provide_mobile_number == false && answers.mobile_verified.nil?

false
end

def claim_is_eligible
Expand Down
6 changes: 4 additions & 2 deletions app/forms/mobile_number_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ def save
Time.now
end

update!(
journey_session.answers.assign_attributes(
mobile_number: mobile_number,
mobile_verified: nil,
sent_one_time_password_at: sent_one_time_password_at
)

journey_session.save!
end

private
Expand All @@ -39,6 +41,6 @@ def send_sms_message
end

def mobile_number_changed?
mobile_number != claim.mobile_number
mobile_number != answers.mobile_number
end
end
8 changes: 5 additions & 3 deletions app/forms/mobile_verification_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class MobileVerificationForm < Form
attribute :one_time_password

# Required for shared partial in the view
delegate :mobile_number, to: :claim
delegate :mobile_number, to: :answers

validate :otp_validate

Expand All @@ -13,15 +13,17 @@ class MobileVerificationForm < Form
def save
return false unless valid?

update!(mobile_verified: true)
journey_session.answers.assign_attributes(mobile_verified: true)

journey_session.save!
end

private

def otp_validate
otp = OneTimePassword::Validator.new(
one_time_password,
claim.sent_one_time_password_at
answers.sent_one_time_password_at
)

errors.add(:one_time_password, otp.warning) unless otp.valid?
Expand Down
21 changes: 17 additions & 4 deletions app/forms/provide_mobile_number_form.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
class ProvideMobileNumberForm < Form
attribute :provide_mobile_number, :boolean

# FIXME RL consider moving this to a subclass rather than querying the session
validates :provide_mobile_number,
inclusion: {
in: [true, false],
message: "Select yes if you would like to provide your mobile number"
},
if: -> { claim.has_ecp_or_lupp_policy? }
if: -> { answers.class.module_parent == Journeys::AdditionalPaymentsForTeaching }

def save
return false unless valid?

claim.assign_attributes(provide_mobile_number:)
claim.reset_eligibility_dependent_answers(["provide_mobile_number"])
claim.save!
if provide_mobile_number_changed?
journey_session.answers.assign_attributes(mobile_verified: nil)
end

journey_session.answers.assign_attributes(
provide_mobile_number: provide_mobile_number
)

journey_session.save!
end

private

def provide_mobile_number_changed?
answers.provide_mobile_number != provide_mobile_number
end
end
8 changes: 4 additions & 4 deletions app/forms/select_mobile_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ def save

case mobile_check
when "use"
claim.update(
journey_session.answers.assign_attributes(
mobile_number: phone_number,
provide_mobile_number: true,
mobile_check: mobile_check,
mobile_verified: nil
)
when "alternative"
claim.update(
journey_session.answers.assign_attributes(
mobile_number: nil,
provide_mobile_number: true,
mobile_check: mobile_check,
mobile_verified: nil
)
when "declined"
claim.update(
journey_session.answers.assign_attributes(
mobile_number: nil,
provide_mobile_number: false,
mobile_check: mobile_check,
Expand All @@ -40,6 +40,6 @@ def save
fail "Invalid mobile_check: #{mobile_check}"
end

true
journey_session.save!
end
end
Loading

0 comments on commit 70897f7

Please sign in to comment.