Files
Dalan/app/models/level.rb
2026-08-13 19:56:46 +03:30

44 lines
1.5 KiB
Ruby

# frozen_string_literal: true
# Label level mapping model
class Level < ApplicationRecord
validates :key, :value, :description, presence: true
validates :value, uniqueness: { scope: :key }
# points calculate with sum ids of record
KYC_ONE_POINTS = ::BarongConfig.list['kyc_levels'][1].keys.sum + 1
KYC_TWO_POINTS = KYC_ONE_POINTS + ::BarongConfig.list['kyc_levels'][2].keys.sum
KYC_THREE_POINTS = KYC_TWO_POINTS + ::BarongConfig.list['kyc_levels'][3].keys.sum
LEVEL_POINTS = { '1' => KYC_ONE_POINTS, '2' => KYC_TWO_POINTS, '3' => KYC_THREE_POINTS }.freeze
LEVEL_ID_BOUNDS = { '0' => 1,
'1' => ::BarongConfig.list['kyc_levels'][1].keys.last,
'2' => ::BarongConfig.list['kyc_levels'][2].keys.last,
'3' => ::BarongConfig.list['kyc_levels'][3].keys.last}.freeze
KYC_LEVEL_IDS = (1..::BarongConfig.list['kyc_levels'].values.last.keys.last).freeze
KYC_LEVEL_KEYS = ::BarongConfig.list['kyc_levels'].values.map(&:values).flatten
scope :sum_ex_points, ->(wanted_id) { where('id < (?)', wanted_id).sum(:id) }
class << self
def what_level(points)
return 0 if points < LEVEL_POINTS.dig('1')
return 1 if points < LEVEL_POINTS.dig('2')
2
end
end
end
# == Schema Information
#
# Table name: levels
#
# id :bigint not null, primary key
# key :string(255) not null
# value :string(255)
# description :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#