Class: SpreeCmCommissioner::PinCode

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree_cm_commissioner/pin_code.rb

Constant Summary collapse

PIN_CODE_NOT_FOUND =
'not_found'.freeze
PIN_CODE_EXPIRED =
'expired'.freeze
PIN_CODE_ATTEMPT_REACHED =
'reached_max_attempt'.freeze
PIN_CODE_MISMATCHED =
'not_match'.freeze
PIN_CODE_OK =
'ok'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#long_life_pin_codeObject

skip set expire pin code when validate on create user



13
14
15
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 13

def long_life_pin_code
  @long_life_pin_code
end

#user_validation_checkObject

skip set expire pin code when validate on create user



13
14
15
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 13

def user_validation_check
  @user_validation_check
end

Class Method Details

.check?(options) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 81

def self.check?(options)
  pin_code = find_by(token: options[:pin_code_token])

  return PIN_CODE_NOT_FOUND if pin_code.nil?

  if options[:phone_number].present?
    return PIN_CODE_NOT_FOUND unless pin_code.check_contact_phone(options[:phone_number], options[:country_code])
  elsif pin_code.contact != options[:email]
    return PIN_CODE_NOT_FOUND
  end

  pin_code.long_life_pin_code = options[:long_life_pin_code]
  pin_code.check?(options[:pin_code])
end

.intel_phone_number(phone_number, country_code = 'kh') ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 68

def self.intel_phone_number(phone_number, country_code = 'kh')
  return phone_number if phone_number.start_with?('+')

  country_code ||= 'kh'

  phone_parser = Phonelib.parse(phone_number, country_code)
  if phone_parser.valid?
    phone_parser.international.gsub!(/[() -]/, '')
  else
    phone_number
  end
end

.mark_expired!(token) ⇒ Object



52
53
54
55
56
57
58
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 52

def self.mark_expired!(token)
  pin_code = find_by(token: token)
  return if pin_code.blank?

  pin_code.expire
  pin_code.save
end

.pin_code_valid?(token) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 44

def self.pin_code_valid?(token)
  pin_code = find_by(token: token)

  return false if pin_code.blank? || pin_code.expired? || pin_code.number_of_attempt_reached?

  true
end

Instance Method Details

#check?(code) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 21

def check?(code)
  return PIN_CODE_EXPIRED if expired?
  return PIN_CODE_ATTEMPT_REACHED if number_of_attempt_reached?

  increment_attempt

  if self.code == code
    set_expire
    save
    PIN_CODE_OK
  else
    save
    PIN_CODE_MISMATCHED
  end
end

#check_contact_phone(phone_number, country_code) ⇒ Object



60
61
62
63
64
65
66
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 60

def check_contact_phone(phone_number, country_code)
  if contact.start_with?('+')
    contact == SpreeCmCommissioner::PinCode.intel_phone_number(phone_number, country_code)
  else
    contact.gsub(/\s/, '') == phone_number.gsub(/\s/, '')
  end
end

#expireObject



104
105
106
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 104

def expire
  self.expired_at = Time.zone.now
end

#expired?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 116

def expired?
  expired_at.present? || (created_at + expires_in.seconds < Time.zone.now)
end

#expires_in_secondsObject



100
101
102
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 100

def expires_in_seconds
  30 * 60 # 30 mins
end

#increment_attemptObject



108
109
110
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 108

def increment_attempt
  self.number_of_attempt += 1
end

#max_attempt_allowedObject



96
97
98
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 96

def max_attempt_allowed
  3
end

#number_of_attempt_reached?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 112

def number_of_attempt_reached?
  self.number_of_attempt >= max_attempt_allowed
end

#readable_typeObject



120
121
122
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 120

def readable_type
  type_mapper[type]
end

#set_expireObject



37
38
39
40
41
42
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 37

def set_expire
  return if user_validation_check
  return if long_life_pin_code

  self.expired_at = Time.zone.now
end

#type_mapperObject



124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 124

def type_mapper
  {
    'SpreeCmCommissioner::PinCodeContactUpdate' => I18n.t('pincode.readable_type.contact_update'),
    'SpreeCmCommissioner::PinCodeLogin' => I18n.t('pincode.readable_type.login'),
    'SpreeCmCommissioner::PinCodeForgetPassword' => I18n.t('pincode.readable_type.reset_password'),
    'SpreeCmCommissioner::PinCodeRegistration' => I18n.t('pincode.readable_type.registration_code'),
    'SpreeCmCommissioner::PinCodeMobileConfirm' => I18n.t('pincode.readable_type.confirmation_code'),
    'SpreeCmCommissioner::PinCodeUpdateUserLogin' => I18n.t('pincode.readable_type.update_user_login'),
    'SpreeCmCommissioner::PinCodeEmailConfirm' => I18n.t('pincode.readable_type.confirmation_code'),
    'SpreeCmCommissioner::OtpCode' => I18n.t('pincode.readable_type.otp_code')
  }
end