Skip to content

Commit

Permalink
Introduce billing:cancel_overdue_invoices rake task
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Beljajev committed Oct 22, 2018
1 parent 4ce3c00 commit 2d38028
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 46 deletions.
22 changes: 0 additions & 22 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,6 @@ def set_invoice_number
false
end

class << self
def cancel_overdue_invoices
STDOUT << "#{Time.zone.now.utc} - Cancelling overdue invoices\n" unless Rails.env.test?

cancel_from = (Time.zone.now - Setting.days_to_keep_overdue_invoices_active.days).to_date

invoices = Invoice.unbinded.where(
'due_date < ? AND cancelled_at IS NULL', cancel_from
)

unless Rails.env.test?
invoices.each do |m|
STDOUT << "#{Time.zone.now.utc} Invoice.cancel_overdue_invoices: ##{m.id}\n"
end
end

count = invoices.update_all(cancelled_at: Time.zone.now)

STDOUT << "#{Time.zone.now.utc} - Successfully cancelled #{count} overdue invoices\n" unless Rails.env.test?
end
end

def binded?
account_activity.present?
end
Expand Down
2 changes: 1 addition & 1 deletion config/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
end

every :day, at: '12:10am' do
runner 'Invoice.cancel_overdue_invoices'
rake 'billing:cancel_overdue_invoices'
end

# TODO
Expand Down
14 changes: 14 additions & 0 deletions lib/tasks/billing/cancel_overdue_invoices.rake
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 test/integration/tasks/billing/cancel_overdue_invoices_test.rb
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
29 changes: 6 additions & 23 deletions test/models/invoice_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,30 +139,13 @@ def test_seller_address
assert_equal 'Main Street, Anytown', invoice.seller_address
end

def test_cancels_overdue_invoices
@original_days_to_keep_overdue_invoices_active_setting = Setting.days_to_keep_overdue_invoices_active
Setting.days_to_keep_overdue_invoices_active = 1
travel_to Time.zone.parse('2010-07-05')
@invoice.update_columns(due_date: '2010-07-03')

Invoice.cancel_overdue_invoices
@invoice.reload

assert @invoice.cancelled?

Setting.days_to_keep_overdue_invoices_active = @original_days_to_keep_overdue_invoices_active_setting
def test_cancelled_when_cancelled_at_is_present
invoice = Invoice.new(cancelled_at: '2010-07-05')
assert invoice.cancelled?
end

def test_keeps_unpaid_not_overdue_invoices_intact
@original_days_to_keep_overdue_invoices_active_setting = Setting.days_to_keep_overdue_invoices_active
Setting.days_to_keep_overdue_invoices_active = 1
travel_to Time.zone.parse('2010-07-05')
@invoice.update_columns(due_date: '2010-07-04')

Invoice.cancel_overdue_invoices

assert_not @invoice.cancelled?

Setting.days_to_keep_overdue_invoices_active = @original_days_to_keep_overdue_invoices_active_setting
def test_cancelled_when_cancelled_at_is_absent
invoice = Invoice.new(cancelled_at: nil)
assert_not invoice.cancelled?
end
end

0 comments on commit 2d38028

Please sign in to comment.