Untitled
raw download clone
RUBY
views 32
,
size 1681 b
class Verification < ApplicationRecord
  belongs_to :verifiable, polymorphic: true
  belongs_to :verified_by, class_name: 'User',
                           foreign_key: 'verified_by',
                           optional: true
  belongs_to :rejected_by, class_name: 'User',
                           foreign_key: 'rejected_by',
                           optional: true

  after_save :report_cards_verified?

  def report_cards_verified?
    type_registration = 'pmdk'
    if verifiable.try(:registration).present?
      registration   = verifiable.registration
      current_status = registration.report_cards.map { |data| data.verifications[0].present? ? data.verifications[0].status : nil }

      if current_status.uniq.exclude?(false) && current_status.uniq.exclude?(nil)
        registration.update_document_verification('verified')

        # Uncomment if need sent email notifaction after documents is verified
        # VerificationPmdkJob.perform_later(registration.id, type_registration, 'verified')
      else
        registration.update_document_verification('not_verified')

        # Send email notifcation if document rejected
        if status == false
          VerificationPmdkJob.perform_later(registration.id, type_registration, 'not_verified')

          # Uncomment if need reset registration number and save to warehouse numbers
          # WarehouseNumbers::SaveVerificationNumberService.new(
          #   registration: registration
          # ).generate if current_status.uniq.exclude?(true) && current_status.uniq.exclude?(nil)
        end
      end
    end
  end

  def email
    verified_by.email
  end
end
close fullscreen
Login or Register to edit or fork this paste. It's free.