Class: PhoneVerificationService

Inherits:
ApplicationService show all
Includes:
DefaultErrorContainer
Defined in:
app/services/phone_verification_service.rb

Overview

To verify user on booking process, especially for Delivery order

Defined Under Namespace

Classes: InvalidPhoneVerification, MySmsError

Instance Attribute Summary collapse

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Methods inherited from ApplicationService

#execute, #execute!

Constructor Details

#initialize(phone, verification_code = nil, session:, expiry_time: 183.seconds, first_request: false) ⇒ PhoneVerificationService

Returns a new instance of PhoneVerificationService.



8
9
10
11
12
13
14
15
# File 'app/services/phone_verification_service.rb', line 8

def initialize(phone, verification_code = nil, session:, expiry_time: 183.seconds, first_request: false)
  @ph = Phonelib.parse(phone)
  @phone = "#{@ph.country_code}#{@ph.raw_national}"
  @verification_code = verification_code.to_i
  @expiry_time = expiry_time
  @session = session
  @first_request = first_request
end

Instance Attribute Details

#expiry_timeObject (readonly)

Returns the value of attribute expiry_time.



6
7
8
# File 'app/services/phone_verification_service.rb', line 6

def expiry_time
  @expiry_time
end

#first_requestObject (readonly)

Returns the value of attribute first_request.



6
7
8
# File 'app/services/phone_verification_service.rb', line 6

def first_request
  @first_request
end

#sessionObject (readonly)

Returns the value of attribute session.



6
7
8
# File 'app/services/phone_verification_service.rb', line 6

def session
  @session
end

#verification_codeObject (readonly)

Returns the value of attribute verification_code.



6
7
8
# File 'app/services/phone_verification_service.rb', line 6

def verification_code
  @verification_code
end

Instance Method Details

#build_otp_recordObject



64
65
66
67
68
69
# File 'app/services/phone_verification_service.rb', line 64

def build_otp_record
  if phone_data_temp.nil? || otp_time_update.nil? || first_request
    phone_data_temp(false, generated_code)
    send_sms(@phone, phone_data_temp[:code])
  end
end

#generated_codeObject



36
37
38
# File 'app/services/phone_verification_service.rb', line 36

def generated_code
  SecureRandom.random_number(111_111..999_999)
end

#get_domain_nameObject



31
32
33
34
# File 'app/services/phone_verification_service.rb', line 31

def get_domain_name
  url = AdminSetting.web_v2_host
  URI.parse(url).host.to_s
end

#match?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'app/services/phone_verification_service.rb', line 27

def match?
  phone_data_temp.present? && phone_data_temp[:code].to_i == verification_code
end

#otp_time_update(time = nil) ⇒ Object

only allowed one message per 3 minutes



41
42
43
44
45
# File 'app/services/phone_verification_service.rb', line 41

def otp_time_update(time = nil)
  key = "sent_at-#{@phone}"
  Rails.cache.write(key, time + expiry_time, expires_in: expiry_time) if time.present?
  Rails.cache.read(key)
end

#phone_data_temp(is_verified = nil, code = nil) ⇒ Object

build temporary phone data



48
49
50
51
52
53
54
55
# File 'app/services/phone_verification_service.rb', line 48

def phone_data_temp(is_verified = nil, code = nil)
  key = "phone_data-#{@phone}"
  if code.present?
    Rails.cache.write(key, { phone: @phone, code: code, is_verified: is_verified },
                      expires_in: expiry_time)
  end
  Rails.cache.read(key)
end

#request_data_otp(cache = nil) ⇒ Object

build temporary otp data request



58
59
60
61
62
# File 'app/services/phone_verification_service.rb', line 58

def request_data_otp(cache = nil)
  key = "ongoing-#{@phone}"
  Rails.cache.write(key, cache) if cache.present?
  Rails.cache.read(key)
end

#send_sms(phone, code) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'app/services/phone_verification_service.rb', line 71

def send_sms(phone, code)
  return false unless otp_time_update.nil? || otp_time_update < Time.zone.now || first_request

  otp_time_update(Time.zone.now)
  time = otp_time_update.in_time_zone('Asia/Bangkok')
  msg = I18n.t('verification_code_msg', url: get_domain_name, code: code)
  send_sms_otp(msg, phone)
  request_data_otp({ phone: phone, code: code, time: time, msg: msg })
end

#send_sms_otp(msg, phone) ⇒ Object



81
82
83
# File 'app/services/phone_verification_service.rb', line 81

def send_sms_otp(msg, phone)
  SmsWorker.perform_async(msg, phone)
end

#user_verification_enabled?(user = nil) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
# File 'app/services/phone_verification_service.rb', line 17

def user_verification_enabled?(user = nil)
  if Flipper.enabled? :user_verification
    true
  elsif user.present?
    Flipper.enabled?(:user_verification, user)
  else
    false
  end
end