Untitled

 avatar
unknown
ruby
2 years ago
5.1 kB
12
Indexable
def move_child_console(crm_type, from_id, team_id, new_creator_id, mover_id, validate, only_handover_data)
  mover = User.with_deleted.find_by(id: mover_id)
  update_method = validate ? 'update_attributes' : 'update_columns'
  redis_key = only_handover_data ? "handover_data_user_#{from_id}" : "move_data_user_#{from_id}"
  redis_ttl = 5_400 # 90 minutes
  Audited.audit_class.as_user(mover) do
    #change creator id
    case crm_type
    when "Crm::Deal"
      #DEAL
        Crm::Deal.where(creator_id: from_id, team_id: team_id).update_all(creator_id: new_creator_id)
        Crm::Deal.where(last_modifier_by_id: from_id, team_id: team_id).update_all(last_modifier_by_id: new_creator_id)
        deals = Crm::Deal.where(team_id: team_id, creator_id: new_creator_id)
        deals.each(&:cache_to_redis)
        deals.each { |x| x.reindex(:creator_related_fields) rescue x.reindex }
        $redis.setex("#{redis_key}_#{crm_type}", redis_ttl, 'done')
    when "Crm::Company"
      #COMPANY
        Crm::Company.where(creator_id: from_id, team_id: team_id).update_all(creator_id: new_creator_id)
        Crm::Company.where(last_modifier_by_id: from_id, team_id:team_id).update_all(last_modifier_by_id: new_creator_id)
        companies = Crm::Company.where(team_id: team_id, creator_id: new_creator_id)
        companies.each(&:cache_to_redis)
        companies.each { |x| x.reindex(:creator_related_fields) rescue x.reindex }
        $redis.setex("#{redis_key}_#{crm_type}", redis_ttl, 'done')
    when "Crm::Lead"
      #LEAD
        Crm::Person.where(creator_id: from_id, team_id: team_id).update_all(creator_id: new_creator_id)
        Crm::Person.where(last_modifier_id: from_id, team_id: team_id).update_all(last_modifier_id: new_creator_id)
        Crm::Person.where(team_id: team_id, creator_id: new_creator_id).each { |x| x.reindex(:creator_related_fields) rescue x.reindex }
        $redis.setex("#{redis_key}_#{crm_type}", redis_ttl, 'done')
    when "Crm::Task"
      #TASK
        Crm::Task.where(user_id: from_id, team_id: team_id).update_all(user_id: new_creator_id)
        Crm::Task.where(creator_id: from_id, team_id: team_id).update_all(creator_id: new_creator_id)
        Crm::Task.where(last_modified_by_id: from_id, team_id: team_id).update_all(last_modified_by_id: new_creator_id)
        Crm::Task.where(team_id: team_id, user_id: new_creator_id).each { |x| x.reindex(:creator_related_fields) rescue x.reindex }
        $redis.setex("#{redis_key}_#{crm_type}", redis_ttl, 'done')
    when "Crm::Note"
      #NOTE
        Crm::Note.where(creator_id: from_id, team_id: team_id).update_all(creator_id: new_creator_id)
        Crm::Note.where(last_modifier_id: from_id, team_id: team_id).update_all(last_modifier_id: new_creator_id)
        Crm::Note.where(team_id: team_id, creator_id: new_creator_id).each { |x| x.reindex(:creator_related_fields) rescue x.reindex }
        $redis.setex("#{redis_key}_#{crm_type}", redis_ttl, 'done')
    when "Crm::Product"
      #PRODUCT
        Crm::Product.where(creator_id: from_id, team_id: team_id).update_all(creator_id: new_creator_id)
        Crm::Product.where(last_modifier_by_id: from_id, team_id: team_id).update_all(last_modifier_by_id: new_creator_id)
        $redis.setex("#{redis_key}_#{crm_type}", redis_ttl, 'done')
    when "Crm::LogEmail"
      #LOGEMAIL
        Crm::LogEmail.where(creator_id: from_id, team_id: team_id).update_all(creator_id: new_creator_id)
        Crm::LogEmail.where(last_modified_by_id: from_id, team_id: team_id).update_all(last_modified_by_id: new_creator_id)
        Crm::LogEmail.where(user_id: from_id, team_id: team_id).update_all(user_id: new_creator_id)
    when "Crm::Automation"
      #AUTOMATION
        Crm::Automation.where(task_user_id: from_id, team_id: team_id).update_all(task_user_id: new_creator_id)
    when "Crm::DownloadLog"
      #DOWNLOAD LOG
        Crm::DownloadLog.where(user_id: from_id, team_id: team_id).update_all(user_id: new_creator_id)
    when "Crm::Upload"
      #UPLOAD
        Crm::Upload.where(user_id: from_id, team_id: team_id).update_all(user_id: new_creator_id)
    when "Crm::Document"
      #DOCUMENT
        Crm::Document.where(user_id: from_id, team_id: team_id).update_all(user_id: new_creator_id)
    when "Crm::Template"
      #TEMPLATE
        Crm::Template.where(user_id: from_id, team_id: team_id).update_all(user_id: new_creator_id)
    when "Crm::ShortCode"
      #SHORT CODE
        Crm::ShortCode.where(user_id: from_id, team_id: team_id).update_all(user_id: new_creator_id)
    when "B3Integration"
      ja = JasnitaAuth.find_by(user_id: from_id)
      b3 = B3networksAuth.find_by(user_id: from_id)
      return if (ja.blank? || b3.blank?)
      begin
        ja.update_columns(user_id: new_creator_id)
        b3.update_columns(user_id: new_creator_id)
      rescue ActiveRecord::RecordNotUnique
        ja.destroy
        b3.destroy
      end
    end
  end
end
queue = Sidekiq::Queue.new('handover_data')
job_args = []
queue.each do |job|
  if job.klass == 'MoveDataUserChildWorker'
    job_args << job.args
  end
end
job_args.each do |ja|
  move_child_console(*ja)
end
Editor is loading...