130 lines
3.6 KiB
Ruby
130 lines
3.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
# Treasury model
|
|
class Treasury < ApplicationRecord
|
|
|
|
belongs_to :user
|
|
|
|
enum state: { drafted: 0, submitted: 1, processing: 2, verified: 3, rejected: 4, disable: 5 }
|
|
enum kind: { card: 0, iban: 2 }
|
|
|
|
validates :title, uniqueness: { scope: [:user_id, :kind], case_sensitive: false }, allow_blank: true
|
|
|
|
validates :data, length: { is: 16 }, if: -> { card? }, allow_blank: false,
|
|
format: { with: /\A\d+\z/, message: 'Sixteen Integer only. No sign allowed.' }
|
|
validates :data, uniqueness: true, length: { is: 26 }, if: -> { iban? }, allow_blank: false,
|
|
format: { with: /\AIR\d+\z/i, message: 'Twenty-four Integer only. No sign allowed.' }
|
|
|
|
validate :data, :one_data_verified?
|
|
|
|
validates :title, presence: true
|
|
|
|
after_commit :start_treasury_kyc_verification, on: %i[create update]
|
|
before_update :check_at_least_one_verified, if: -> { state == 'disable' }
|
|
before_destroy :check_at_least_one_verified
|
|
|
|
|
|
scope :active, -> { where.not(state: 'disable') }
|
|
|
|
def one_data_verified?
|
|
errors.add(:data, 'this card is already verified') if Treasury.find_by(data: data, state: 'verified').present?
|
|
end
|
|
|
|
def sub_masked_data
|
|
data.sub(/(?<=\A.{6})(.*)/) { |match| '*' * match.length } if data
|
|
end
|
|
|
|
# update without callback
|
|
def ownership!
|
|
if ownership
|
|
update_column(:state, 'verified')
|
|
else
|
|
update_column(:state, 'rejected')
|
|
end
|
|
end
|
|
|
|
def ownership
|
|
# full_name = if kind == 'card'
|
|
# user.profiles.last.reverse_full_name.delete(' ')
|
|
# else
|
|
# user.profiles.last.full_name.delete(' ')
|
|
# end
|
|
return unless user.verified_profile.present?
|
|
|
|
full_name = user.verified_profile.full_name.delete(' ')
|
|
owner_name.delete(' ').percent_match(full_name) >= 80
|
|
end
|
|
|
|
def jresult
|
|
return nil unless result.present?
|
|
|
|
JSON.parse(result)
|
|
end
|
|
|
|
def old_owner_name
|
|
return nil unless jresult.present?
|
|
|
|
kind == 'iban' ? "#{jresult.dig('firstName')} #{jresult.dig('lastName')}" : jresult.dig('ownerName')
|
|
end
|
|
|
|
def owner_name
|
|
return unless jresult.present?
|
|
|
|
if kind == 'iban'
|
|
# JIBIT return array in names of owner
|
|
names = jresult.dig('ibanInfo', 'owners')&.last
|
|
"#{names.dig('firstName')} #{names.dig('lastName')}"
|
|
else
|
|
jresult.dig('cardInfo', 'ownerName')
|
|
end
|
|
end
|
|
|
|
def disable
|
|
update(state: 'disable')
|
|
end
|
|
|
|
def enable
|
|
update(state: 'submitted')
|
|
end
|
|
|
|
private
|
|
|
|
def start_treasury_kyc_verification
|
|
::KycService.treasury_label_update(self)
|
|
end
|
|
|
|
def check_at_least_one_verified
|
|
# return if state_was == 'verified'
|
|
|
|
raise 'verified treasury cant be deleted' if state == 'verified'
|
|
|
|
verified_treasuries = Treasury.where(user: user, kind: kind, state: 'verified')
|
|
raise 'at least one verified treasury' if verified_treasuries.present? && verified_treasuries.count <= 1
|
|
|
|
# delete Corresponding label if not verified
|
|
treasury_label = user.labels.find_by(key: kind)
|
|
treasury_label.destroy unless treasury_label.value == 'verified'
|
|
end
|
|
|
|
end
|
|
|
|
# == Schema Information
|
|
# Schema version: 20210811064241
|
|
#
|
|
# Table name: treasuries
|
|
#
|
|
# id :bigint not null, primary key
|
|
# user_id :bigint unsigned, not null
|
|
# title :string(128)
|
|
# data :string(255) not null
|
|
# result :text(65535)
|
|
# state :integer default("drafted"), unsigned, not null
|
|
# kind :integer default("card"), unsigned, not null
|
|
# created_at :datetime not null
|
|
# updated_at :datetime not null
|
|
#
|
|
# Indexes
|
|
#
|
|
# index_treasuries_on_user_id (user_id)
|
|
#
|