-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dunning): Populate dunning_campaign_completed on existing customers
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
db/migrate/20241126102447_set_dunning_campaign_completed_to_customers.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |