Class: PhoneVerificationForMemberService

Inherits:
PhoneVerificationService show all
Defined in:
app/services/phone_verification_for_member_service.rb

Overview

Member otp request and verification

Instance Attribute Summary collapse

Attributes inherited from PhoneVerificationService

#expiry_time, #first_request, #session, #verification_code

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods inherited from PhoneVerificationService

#build_otp_record, #generated_code, #get_domain_name, #otp_time_update, #phone_data_temp, #request_data_otp, #send_sms, #send_sms_otp, #user_verification_enabled?

Methods included from DefaultErrorContainer

#error, #error_message_simple, #merge_errors

Methods inherited from ApplicationService

#execute, #execute!

Constructor Details

#initialize(user_id, verification_code = nil, **options) ⇒ PhoneVerificationForMemberService

Returns a new instance of PhoneVerificationForMemberService.



5
6
7
8
9
10
11
12
13
# File 'app/services/phone_verification_for_member_service.rb', line 5

def initialize(user_id, verification_code = nil, **options)
  @user = User.find user_id
  @phone = "#{user.calling_code}#{user.phone_v2}"
  @verification_code = verification_code.to_i
  session = options[:session]
  expiry_time = options.fetch(:expiry_time, 183.seconds)
  first_request = options.fetch(:first_request, false)
  super(@phone, @verification_code, session: session, expiry_time: expiry_time, first_request: first_request)
end

Instance Attribute Details

#userObject

Returns the value of attribute user.



3
4
5
# File 'app/services/phone_verification_for_member_service.rb', line 3

def user
  @user
end

Instance Method Details

#match?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/services/phone_verification_for_member_service.rb', line 37

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

#request_codeObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/services/phone_verification_for_member_service.rb', line 15

def request_code
  errors.clear
  if user.present? && user.phone_is_verified
    raise InvalidPhoneVerification, 'Phone number has been verified'
  end

  if user.calling_code != '66' || !user_verification_enabled?(user)
    raise InvalidPhoneVerification, 'Phone number has been verified'
  end

  build_otp_record

  if errors.present?
    ServiceResult.new errors: errors, message: error_message_simple
  else
    ServiceResult.new data: phone_data_temp
  end
rescue InvalidPhoneVerification => e
  errors.add :base, e.message
  ServiceResult.new errors: errors, message: error_message_simple
end

#verifyObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/phone_verification_for_member_service.rb', line 41

def verify
  errors.clear
  raise InvalidPhoneVerification, 'Failed to verify phone number' if user.phone_is_verified

  if otp_expired?
    raise InvalidPhoneVerification, 'Verification code has expired'
  end

  raise InvalidPhoneVerification, 'Verification code is wrong' unless match?

  ActiveRecord::Base.transaction do
    phone_data_temp(true, phone_data_temp[:code])
    if user.present?
      user.phone_is_verified = true
      user.save
    end
    true
  end

  if errors.present?
    ServiceResult.new errors: errors, message: error_message_simple
  else
    ServiceResult.new data: phone_data_temp
  end
rescue InvalidPhoneVerification => e
  errors.add :base, e.message
  ServiceResult.new errors: errors, message: error_message_simple
end