Skip to content

Commit

Permalink
feat(dunning): Populate dunning_campaign_completed on existing customers
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Nov 27, 2024
1 parent cb98b1a commit d26aa2b
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

class SetDunningCampaignCompletedToCustomers < ActiveRecord::Migration[7.1]
def change
reversible do |dir|
dir.up do
safety_assured do
# Set dunning_campaign_completed when the explicit assigned campaign is completed.
execute <<-SQL
UPDATE customers c
SET dunning_campaign_completed = true
FROM dunning_campaigns dc
WHERE c.applied_dunning_campaign_id = dc.id
AND c.dunning_campaign_completed = false
AND c.last_dunning_campaign_attempt >= dc.max_attempts;
SQL

# Set dunning_campaign_completed when the campaign inherited from organization is completed.
execute <<-SQL
UPDATE customers c
SET dunning_campaign_completed = true
FROM dunning_campaigns dc
WHERE c.organization_id = dc.organization_id
AND dc.applied_to_organization = true
AND c.applied_dunning_campaign_id IS NULL
AND c.dunning_campaign_completed = false
AND c.exclude_from_dunning_campaign = false
AND c.last_dunning_campaign_attempt >= dc.max_attempts;
SQL
end
end
end
end
end

0 comments on commit d26aa2b

Please sign in to comment.