Class: Dscf::Marketplace::OtpVerification

Inherits:
ApplicationRecord show all
Defined in:
app/models/dscf/marketplace/otp_verification.rb

Constant Summary collapse

DEFAULT_CODE =

Placeholder until a real SMS/OTP provider is wired up (see Dscf::Core::NotificationService::Adapters::SmsStub) — every OTP is the same known code so the flow can be exercised end-to-end without a real phone. Swapping in random generation later only touches generate_for.

"123456"
TTL =
10.minutes
MAX_ATTEMPTS =
5

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_for(verifiable, phone:) ⇒ Object



19
20
21
22
# File 'app/models/dscf/marketplace/otp_verification.rb', line 19

def self.generate_for(verifiable, phone:)
  verifiable.otp_verifications.pending.update_all(status: statuses[:expired])
  create!(verifiable: verifiable, phone: phone, code: DEFAULT_CODE, expires_at: TTL.from_now)
end

Instance Method Details

#past_expiry?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/models/dscf/marketplace/otp_verification.rb', line 40

def past_expiry?
  pending? && expires_at < Time.current
end

#verify(code) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/dscf/marketplace/otp_verification.rb', line 24

def verify(code)
  return :verified if verified?
  return :expired if past_expiry?
  return :max_attempts_exceeded if failed? || attempts >= MAX_ATTEMPTS

  increment!(:attempts)

  if code.to_s == self.code
    update!(status: :verified, verified_at: Time.current)
    :verified
  else
    update!(status: :failed) if attempts >= MAX_ATTEMPTS
    :invalid
  end
end