namespace :batch_user_add_projects do
  desc "batch add projects"
  task done: :environment do
    user_id = ENV['user_id']
    puts "user_id=================#{user_id}"
    next if user_id.blank?
    project_ids = Project.find_by_sql("SELECT id FROM projects WHERE forked_from_project_id IN (SELECT id FROM projects WHERE user_id IN (SELECT id FROM users WHERE login='OSKYCX'))").pluck(:id)
    if ENV['project_id'].present?
      project_ids = [ENV['project_id']]
    end
    next if project_ids.blank?
    puts "project_ids=================#{project_ids}"
    project_ids.each do |project_id|
      begin
        user = User.find_by(id: user_id)
        next if user.blank?
        project = Project.find_by(id: project_id)
        next if project.blank?
        Projects::AddMemberInteractor.call(project.owner, project, user, "admin")
      rescue Exception => e
        puts "batch add projects error: #{user_id}"
      end
    end
  end
end