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

Duplicate email fix #187437777 #4508

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

This file was deleted.

95 changes: 95 additions & 0 deletions app/controllers/state_file/questions/email_sign_up_controller.rb
@@ -0,0 +1,95 @@
module StateFile
module Questions
class EmailSignUpController < QuestionsController

def self.show?(intake)
intake.contact_preference == "email"
end

def edit
# Show the email address form
super
end

def create
# Send a verification code to the email address
# Show the form which will collect the verification code
@form = initialized_update_form
if @form.valid?
send_verification_code
else
after_update_failure
track_validation_error
render :edit
end
end

def update
@form = initialized_update_form
if @form.valid? && @form.verification_code_valid?
intake = current_intake
existing_intake = get_existing_intake(intake, @form.contact_info)
if existing_intake.present?
redirect_into_login(@form.contact_info, intake, existing_intake)
return
end
@form.save
after_update_success
track_question_answer
redirect_to(next_path)
else
after_update_failure
track_validation_error
render :create
end
end

private

def send_verification_code
RequestVerificationCodeEmailJob.perform_later(
email_address: @form.email_address,
locale: I18n.locale,
visitor_id: current_intake.visitor_id,
client_id: nil,
service_type: :statefile
)
end

def get_existing_intake(intake, contact_info)
search = intake.class.where.not(id: intake.id)
search = search.where(email_address: contact_info)
search.first
end

def redirect_into_login(contact_info, intake, existing_intake)
hashed_verification_code = VerificationCodeService.hash_verification_code_with_contact_info(
@form.contact_info, @form.verification_code
)
@form.intake = existing_intake
intake.destroy unless intake.id == existing_intake.id
sign_in existing_intake
if existing_intake.raw_direct_file_data.present?
redirect_to IntakeLoginsController.to_path_helper(
action: :edit,
id: hashed_verification_code,
us_state: params[:us_state]
)
else
redirect_to(next_path)
end
end

def after_update_success
messaging_service = StateFile::MessagingService.new(
message: StateFile::AutomatedMessage::Welcome,
intake: current_intake,
sms: false,
email: true,
body_args: {intake_id: current_intake.id}
)
messaging_service.send_message
end
end
end
end

This file was deleted.

@@ -0,0 +1,40 @@
module StateFile
module Questions
class PhoneNumberSignUpController < EmailSignUpController

def self.show?(intake)
intake.contact_preference == "text"
end

private

def send_verification_code
RequestVerificationCodeTextMessageJob.perform_later(
phone_number: @form.phone_number,
locale: I18n.locale,
visitor_id: current_intake.visitor_id,
client_id: nil,
service_type: :statefile
)
end

def get_existing_intake(intake, contact_info)
search = intake.class.where.not(id: intake.id)
search = search.where(phone_number: contact_info)
search.first
end

def after_update_success
messaging_service = StateFile::MessagingService.new(
message: StateFile::AutomatedMessage::Welcome,
intake: current_intake,
sms: true,
email: false,
body_args: {intake_id: current_intake.id}
)
messaging_service.send_message
end

end
end
end

This file was deleted.

42 changes: 42 additions & 0 deletions app/forms/state_file/email_sign_up_form.rb
@@ -0,0 +1,42 @@
module StateFile
class EmailSignUpForm < QuestionsForm
set_attributes_for :intake, :email_address
attr_accessor :verification_code

validates :email_address, 'valid_email_2/email': true
validates :email_address, presence: true

def save
@intake.update(attributes_for(:intake).merge(email_address_verified_at: DateTime.now))
end

def contact_info
email_address
end

def verification_code_valid?
hashed_verification_code = VerificationCodeService.hash_verification_code_with_contact_info(email_address, verification_code)
# Magic codes provide a way of bypassing security in a development context.
# The easiest way to do this was to update the last entry to actually have the magic code.
if Rails.configuration.allow_magic_verification_code && verification_code == "000000"
token = EmailAccessToken.where(email_address: email_address).last
if token.present?
token.update(
token: Devise.token_generator.digest(EmailAccessToken, :token, hashed_verification_code),
)
end
return true
end

valid_code = EmailAccessToken.lookup(hashed_verification_code).exists?

errors.add(:verification_code, I18n.t("views.questions.verification.error_message")) unless valid_code

valid_code.present?
end

def self.attribute_names
[:email_address, :verification_code]
end
end
end
37 changes: 37 additions & 0 deletions app/forms/state_file/phone_number_sign_up_form.rb
@@ -0,0 +1,37 @@
module StateFile
class PhoneNumberSignUpForm < QuestionsForm
set_attributes_for :intake, :phone_number

before_validation :normalize_phone_number
attr_accessor :verification_code
validates :phone_number, e164_phone: true

def save
@intake.update(attributes_for(:intake).merge(phone_number_verified_at: DateTime.now))
end

def contact_info
PhoneParser.formatted_phone_number(phone_number)
end

def normalize_phone_number
self.phone_number = PhoneParser.normalize(phone_number) if phone_number.present?
end

def verification_code_valid?
return true if Rails.configuration.allow_magic_verification_code && verification_code == "000000"

hashed_verification_code = VerificationCodeService.hash_verification_code_with_contact_info(phone_number, verification_code)

valid_code = TextMessageAccessToken.lookup(hashed_verification_code).exists?

errors.add(:verification_code, I18n.t("views.ctc.questions.verification.error_message")) unless valid_code

valid_code.present?
end

def self.attribute_names
[:phone_number, :verification_code]
end
end
end
44 changes: 0 additions & 44 deletions app/forms/state_file/verification_code_form.rb

This file was deleted.

5 changes: 2 additions & 3 deletions app/lib/navigation/state_file_az_question_navigation.rb
Expand Up @@ -15,9 +15,8 @@ class StateFileAzQuestionNavigation
]),
Navigation::NavigationSection.new("state_file.navigation.section_2", [
Navigation::NavigationStep.new(StateFile::Questions::ContactPreferenceController),
Navigation::NavigationStep.new(StateFile::Questions::PhoneNumberController),
Navigation::NavigationStep.new(StateFile::Questions::EmailAddressController),
Navigation::NavigationStep.new(StateFile::Questions::VerificationCodeController),
Navigation::NavigationStep.new(StateFile::Questions::PhoneNumberSignUpController),
Navigation::NavigationStep.new(StateFile::Questions::EmailSignUpController),
Navigation::NavigationStep.new(StateFile::Questions::CodeVerifiedController),
]),
Navigation::NavigationSection.new("state_file.navigation.section_3", [
Expand Down
5 changes: 2 additions & 3 deletions app/lib/navigation/state_file_ny_question_navigation.rb
Expand Up @@ -16,9 +16,8 @@ class StateFileNyQuestionNavigation
]),
Navigation::NavigationSection.new("state_file.navigation.section_2", [
Navigation::NavigationStep.new(StateFile::Questions::ContactPreferenceController),
Navigation::NavigationStep.new(StateFile::Questions::PhoneNumberController),
Navigation::NavigationStep.new(StateFile::Questions::EmailAddressController),
Navigation::NavigationStep.new(StateFile::Questions::VerificationCodeController),
Navigation::NavigationStep.new(StateFile::Questions::PhoneNumberSignUpController),
Navigation::NavigationStep.new(StateFile::Questions::EmailSignUpController),
Navigation::NavigationStep.new(StateFile::Questions::CodeVerifiedController),
]),
Navigation::NavigationSection.new("state_file.navigation.section_3", [
Expand Down