Class: Heya::CampaignMembership

Inherits:
ApplicationRecord show all
Defined in:
app/models/heya/campaign_membership.rb

Class Method Summary collapse

Class Method Details

.migrate_next_step!Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/heya/campaign_membership.rb', line 58

def self.migrate_next_step!
  find_each do |membership|
    campaign = GlobalID::Locator.locate(membership.campaign_gid)
    receipt = campaign && CampaignReceipt.where(user: membership.user, step_gid: campaign.steps.map(&:gid)).order("created_at desc").first

    next_step = if receipt
      last_step = GlobalID::Locator.locate(receipt.step_gid)
      current_index = campaign.steps.index(last_step)
      campaign.steps[current_index + 1]
    else
      campaign&.steps&.first
    end

    if next_step
      membership.update(step_gid: next_step.gid)
    else
      membership.destroy
    end
  end

  CampaignReceipt.where(sent_at: nil).destroy_all
end