Allow specifying the tenant in budget tasks

The `budgets📧selected` and `budgets📧unselected` tasks are
supposed to be run manually because they only make sense at a specific
point during the life of a budget.

However, they would only run on the default tenant, and it was
impossible to run them on a different tenant.

So we're introducing an argument in the rake task accepting the name of
the tenant whose users we want to send emails to.
This commit is contained in:
Javi Martín
2022-10-08 21:51:18 +02:00
parent 796214528e
commit fe9463cb5f
2 changed files with 32 additions and 4 deletions

View File

@@ -1,13 +1,13 @@
namespace :budgets do namespace :budgets do
namespace :email do namespace :email do
desc "Sends emails to authors of selected investments" desc "Sends emails to authors of selected investments"
task selected: :environment do task :selected, [:tenant] => :environment do |_, args|
Budget.current.email_selected Tenant.switch(args[:tenant]) { Budget.current.email_selected }
end end
desc "Sends emails to authors of unselected investments" desc "Sends emails to authors of unselected investments"
task unselected: :environment do task :unselected, [:tenant] => :environment do |_, args|
Budget.current.email_unselected Tenant.switch(args[:tenant]) { Budget.current.email_unselected }
end end
end end
end end

View File

@@ -13,6 +13,20 @@ describe "budget tasks" do
expect(ActionMailer::Base.deliveries.count).to eq 1 expect(ActionMailer::Base.deliveries.count).to eq 1
expect(ActionMailer::Base.deliveries.last.to).to eq ["selectme@consul.dev"] expect(ActionMailer::Base.deliveries.last.to).to eq ["selectme@consul.dev"]
end end
it "accepts specifying the tenant" do
create(:budget_investment, :selected, author: create(:user, email: "default@consul.dev"))
create(:tenant, schema: "different")
Tenant.switch("different") do
create(:budget_investment, :selected, author: create(:user, email: "different@consul.dev"))
end
Rake.application.invoke_task("budgets:email:selected[different]")
expect(ActionMailer::Base.deliveries.count).to eq 1
expect(ActionMailer::Base.deliveries.last.to).to eq ["different@consul.dev"]
end
end end
describe "rake budgets:email:unselected" do describe "rake budgets:email:unselected" do
@@ -27,5 +41,19 @@ describe "budget tasks" do
expect(ActionMailer::Base.deliveries.count).to eq 1 expect(ActionMailer::Base.deliveries.count).to eq 1
expect(ActionMailer::Base.deliveries.last.to).to eq ["ignorme@consul.dev"] expect(ActionMailer::Base.deliveries.last.to).to eq ["ignorme@consul.dev"]
end end
it "accepts specifying the tenant" do
create(:budget_investment, author: create(:user, email: "default@consul.dev"))
create(:tenant, schema: "different")
Tenant.switch("different") do
create(:budget_investment, author: create(:user, email: "different@consul.dev"))
end
Rake.application.invoke_task("budgets:email:unselected[different]")
expect(ActionMailer::Base.deliveries.count).to eq 1
expect(ActionMailer::Base.deliveries.last.to).to eq ["different@consul.dev"]
end
end end
end end