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)


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

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



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

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



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

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)


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

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)


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

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



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

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



106
107
108
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 106

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

#expired?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 118

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

#expires_in_secondsObject



102
103
104
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 102

def expires_in_seconds
  30 * 60 # 30 mins
end

#increment_attemptObject



110
111
112
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 110

def increment_attempt
  self.number_of_attempt += 1
end

#max_attempt_allowedObject



98
99
100
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 98

def max_attempt_allowed
  3
end

#number_of_attempt_reached?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 114

def number_of_attempt_reached?
  self.number_of_attempt >= max_attempt_allowed
end

#readable_typeObject



122
123
124
# File 'app/models/spree_cm_commissioner/pin_code.rb', line 122

def readable_type
  type_mapper[type]
end

#set_expireObject



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

def set_expire
  return if user_validation_check
  return if long_life_pin_code

  self.expired_at = Time.zone.now
end

#type_mapperObject



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

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'),
    'SpreeCmCommissioner::PinCodeTelegram' => I18n.t('pincode.readable_type.telegram_verification')
  }
end