Files
Dena/app/models/deposit_limit.rb
2026-08-13 19:50:53 +03:30

68 lines
2.0 KiB
Ruby

# frozen_string_literal: true
class DepositLimit < ApplicationRecord
# Default value for kyc_level, group name and currency_id in DepositLimit table;
ANY = 'any'
enum kind: { fiat: 0, crypto: 1, both: 2 }
# == Relationships ========================================================
belongs_to :currency, optional: true
# == Validations ==========================================================
validates :kyc_level,
presence: true,
uniqueness: { scope: %i[group kind] }
validates :group,
presence: true
validates :limit_24_hour,
:limit_1_month,
presence: true
# == Scopes ===============================================================
# == Callbacks ============================================================
before_create { self.group = self.group.strip.downcase }
after_commit :wipe_cache
# == Class Methods ========================================================
class << self
# Get withdrawal limit for specific withdraw that based on member kyc_level and group.
# WithdrawLimit record selected with the next priorities:
# 1. kyc_level, group match
# 2. kyc_level match
# 3. group match
# 5. kyc_level, group are 'any'
# 6. default (zero limits)
def for(kyc_level:, group:, kind:)
where(kyc_level: [kyc_level, ANY], group: [group, ANY], kind: kind).max_by(&:weight) || new
end
end
# == Instance Methods =====================================================
# deposit limit suitability expressed in weight.
# deposit limit with the greatest weight selected.
# Kyc_level has greater weight then group match.
# E.g deposit for member with kyc_level 2, group 'vip-0''
# (kyc_level == 2 && group == 'vip-0') >
# (kyc_level == 2 && group == 'any') >
# (kyc_level == 'any' && group == 'vip-0') >
# (kyc_level == 'any' && group == 'any') >
def weight
(kyc_level == 'any' ? 0 : 10) + (group == 'any' ? 0 : 1)
end
def wipe_cache
Rails.cache.delete_matched("deposit_limits_fees*")
end
end
#