destroy bulk visitors

PHOTO EMBED

Wed Jul 07 2021 09:19:01 GMT+0000 (UTC)

Saved by @gacho #plaintext

visitors = p.visitors.customers.where(tags: 'UNSUBSCRIBE 2021')
visitor_ids = visitors.pluck(:id);1

STATUSES_FOR_DELETION = [Automations::Trigger::Status::NOT_EXECUTED, Automations::Trigger::Status::RETRIGGERED].freeze
scheduled_steps = Automations::ScheduledStep.in(campaign_id: campaign_ids).in('triggers.visitor_id': visitor_ids)
scheduled_steps.map do |step|
  step.triggers.where(visitor: @visitor).in(status: STATUSES_FOR_DELETION).destroy_all
end

Mails::SingleEmail.where(project: p, sent_at: nil, processing: false).in(recipient_id: visitor_ids).destroy_all

Debug::MergeLog.where(project: p).in(legacy_visitor: visitor_ids).destroy_all

visitors.each.with_index do |visitor, index|
  visitor.events.destroy_all
  visitor.destroy

  puts index if index % 1000 == 0
end
content_copyCOPY