# frozen_string_literal: true # Resposible for storing configurations class Label < ApplicationRecord # acts_as_eventable prefix: 'label', on: %i[create update] belongs_to :user SCOPES = HashWithIndifferentAccess.new(public: 'public', private: 'private') SCOPES.keys.each do |name| define_method "#{name}?" do scope == SCOPES[name] end end scope :with_private_scope, -> { where(scope: 'private') } validates :user_id, :key, :value, :scope, presence: true validates :scope, inclusion: { in: SCOPES.keys } validates :key, length: 3..255, format: { with: /\A[a-z0-9_-]+\z/ }, uniqueness: { scope: %i[user_id scope] } validates :value, length: 3..255, format: { with: /\A[a-z0-9_-]+\z/ } validate :regard_kyc_steps after_commit :update_state_if_label_defined, :update_level_if_label_defined, on: %i[create update] after_commit :run_owner_mobile, on: %i[create update] after_destroy :update_state_if_label_defined, :destroy_level_if_label_deleted before_validation :normalize_fields def as_json_for_event_api { id: id, key: key, value: value, description: description, user: user.as_json_for_event_api } end def kyc_wanted_point point = Level.find_by(key: key) raise "not found this key: `#{key}` in levels" unless point.present? [point.id, Level.sum_ex_points(point.id)] end private def normalize_fields self.key = key.to_s.downcase.squish self.value = value.to_s.downcase.squish end def update_state_if_label_defined return unless scope == 'private' || previous_changes[:scope]&.include?('private') user.update_state end def update_level_if_label_defined return unless scope == 'private' || previous_changes[:scope]&.include?('private') user.update_level # TODO must be clear witch step need notification # send_document_review_notification if key == 'document' end def destroy_level_if_label_deleted # just kyc labels if value == 'verified' && Level::KYC_LEVEL_KEYS.include?(key) raise "can not delete kyc key: `#{key}`" end update_level_if_label_defined end # TODO: Fix it when EventAPI will be added. def send_document_review_notification if value == 'verified' EventAPI.notify('system.document.verified', record: as_json_for_event_api) elsif value == 'rejected' EventAPI.notify('system.document.rejected', record: as_json_for_event_api) end end # level confirm by labels # labels create from LEVEL table by key # every key in level table has points as id # so user must not has label out of its now level def regard_kyc_steps return unless scope == 'private' || previous_changes[:scope]&.include?('private') # just kyc labels return unless ::BarongConfig.list['kyc_levels'].values.flatten.include?(key) next_level = user.level + 1 # this means user has latest level now and dont need check it more return if next_level > Level::LEVEL_POINTS.keys&.last&.to_i ask_point, ex_points = kyc_wanted_point # check wanted point in correct area raise 'regard steps in KYC' if (ask_point + ex_points) > Level::LEVEL_POINTS.dig(next_level.to_s) end # just trigger for check owner mobile def run_owner_mobile return unless user.access_phone.present? if value == 'verified' && key == 'selfie' KYC.const_get(Barong::App.config.kyc_provider.capitalize, false)::OwnerMobileWorker.perform_async(user.access_phone.id) end end end # == Schema Information # Schema version: 20210218135634 # # Table name: labels # # id :bigint not null, primary key # user_id :bigint unsigned, not null # key :string(255) not null # value :string(255) not null # scope :string(255) default("public"), not null # description :string(255) # created_at :datetime not null # updated_at :datetime not null # # Indexes # # index_labels_on_user_id (user_id) # index_labels_on_user_id_and_key_and_scope (user_id,key,scope) #