Skip to content

Commit

Permalink
Merge pull request #1280 from internetee/force-localize-sms-instruct-…
Browse files Browse the repository at this point in the history
…for-foreign-visitors

added force localize for sms confrimation code
  • Loading branch information
vohmar authored Jul 2, 2024
2 parents 4ac986a + a90c1d9 commit 9185c8e
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions app/models/phone_confirmation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@ def generate_and_send_code
return unless user.valid?
return if user.mobile_phone_confirmed_sms_send_at.present? && user.mobile_phone_confirmed_sms_send_at > TIME_LIMIT.ago

number = SecureRandom.random_number(10_000)
padded_number = format('%04d', number)
padded_number = generate_padded_number
assign_padded_number_to_user(padded_number)
locale = locale_for_sms(user)

user.update!(mobile_phone_confirmation_code: padded_number, mobile_phone_confirmed_sms_send_at: Time.zone.now)
message_sender = Messente::Omnimessage.new(
user.mobile_phone,
I18n.t('phone_confirmations.instructions', code: padded_number)
)
message_sender.send_message
send_sms_with_locale(locale, padded_number)
end

def code
Expand All @@ -41,4 +37,32 @@ def confirm

@user.update!(mobile_phone_confirmed_at: Time.zone.now)
end

private

def assign_padded_number_to_user(padded_number)
user.update!(mobile_phone_confirmation_code: padded_number, mobile_phone_confirmed_sms_send_at: Time.zone.now)
end

def send_sms_with_locale(locale, padded_number)
I18n.with_locale(locale) do
message_sender = Messente::Omnimessage.new(
user.mobile_phone,
I18n.t('phone_confirmations.instructions', code: padded_number)
)
message_sender.send_message
end
end

def generate_padded_number
number = SecureRandom.random_number(10_000)
format('%04d', number)
end

def locale_for_sms(user)
force_english_localize = !user.mobile_phone.include?('+372')
default_locale = user.locale.blank? ? I18n.default_locale : user.locale

force_english_localize ? :en : default_locale
end
end

0 comments on commit 9185c8e

Please sign in to comment.