summaryrefslogtreecommitdiff
path: root/app/models/users/credit_card_validation.rb
blob: 272f31aa9ce20f4f60da2e5d0fa243ed20d7d01a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

module Users
  class CreditCardValidation < ApplicationRecord
    RELEASE_DAY = Date.new(2021, 5, 17)

    self.table_name = 'user_credit_card_validations'

    belongs_to :user

    validates :holder_name, length: { maximum: 50 }
    validates :network, length: { maximum: 32 }
    validates :last_digits, allow_nil: true, numericality: {
      greater_than_or_equal_to: 0, less_than_or_equal_to: 9999
    }

    def similar_records
      self.class.where(
        expiration_date: expiration_date,
        last_digits: last_digits,
        network: network
      ).order(credit_card_validated_at: :desc).includes(:user)
    end

    def similar_holder_names_count
      return 0 unless holder_name

      self.class.where('lower(holder_name) = lower(:value)', value: holder_name).count
    end
  end
end