-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
billing:cancel_overdue_invoices
rake task
- Loading branch information
Artur Beljajev
committed
Oct 22, 2018
1 parent
4ce3c00
commit 2d38028
Showing
5 changed files
with
77 additions
and
46 deletions.
There are no files selected for viewing
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
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
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,14 @@ | ||
namespace :billing do | ||
task cancel_overdue_invoices: [:environment] do | ||
cancel_from = (Time.zone.now - Setting.days_to_keep_overdue_invoices_active.days).to_date | ||
|
||
overdue_invoices = Invoice.unbinded.where('due_date < ? AND cancelled_at IS NULL', cancel_from) | ||
overdue_invoice_ids = overdue_invoices.ids | ||
overdue_invoice_count = overdue_invoice_ids.size | ||
|
||
overdue_invoices.update_all(cancelled_at: Time.zone.now) | ||
|
||
puts overdue_invoice_ids.map { |invoice_id| "Invoice ##{invoice_id} is cancelled" }.join("\n") | ||
puts "Cancelled total: #{overdue_invoice_count}" | ||
end | ||
end |
56 changes: 56 additions & 0 deletions
56
test/integration/tasks/billing/cancel_overdue_invoices_test.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,56 @@ | ||
require 'test_helper' | ||
|
||
class CancelOverdueInvoicesTest < ActiveSupport::TestCase | ||
setup do | ||
@invoice = invoices(:valid) | ||
eliminate_effect_of_other_invoices | ||
@original_days_to_keep_overdue_invoices_active_setting = Setting.days_to_keep_overdue_invoices_active | ||
end | ||
|
||
teardown do | ||
Setting.days_to_keep_overdue_invoices_active = @original_days_to_keep_overdue_invoices_active_setting | ||
end | ||
|
||
def test_cancels_overdue_invoices | ||
Setting.days_to_keep_overdue_invoices_active = 1 | ||
travel_to Time.zone.parse('2010-07-05') | ||
@invoice.update_columns(due_date: '2010-07-03') | ||
|
||
capture_io { run_task } | ||
@invoice.reload | ||
|
||
assert @invoice.cancelled? | ||
assert_equal Time.zone.parse('2010-07-05 00:00'), @invoice.cancelled_at | ||
end | ||
|
||
def test_does_not_cancel_not_overdue_invoices | ||
Setting.days_to_keep_overdue_invoices_active = 1 | ||
travel_to Time.zone.parse('2010-07-05') | ||
@invoice.update_columns(due_date: '2010-07-04') | ||
|
||
capture_io { run_task } | ||
@invoice.reload | ||
|
||
assert_not @invoice.cancelled? | ||
end | ||
|
||
def test_outputs_results | ||
Setting.days_to_keep_overdue_invoices_active = 1 | ||
travel_to Time.zone.parse('2010-07-05') | ||
@invoice.update_columns(due_date: '2010-07-03') | ||
|
||
assert_output("Invoice ##{@invoice.id} is cancelled\nCancelled total: 1\n") { run_task } | ||
end | ||
|
||
private | ||
|
||
def eliminate_effect_of_other_invoices | ||
Invoice.connection.disable_referential_integrity do | ||
Invoice.delete_all("id != #{@invoice.id}") | ||
end | ||
end | ||
|
||
def run_task | ||
Rake::Task['billing:cancel_overdue_invoices'].execute | ||
end | ||
end |
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