Converting accounts jobs to classes, which allows initialization with start and end dates

This commit is contained in:
Rob Harrington
2015-07-08 23:00:31 +08:00
parent cc26321ab2
commit 1b2a06572b
6 changed files with 35 additions and 80 deletions

View File

@@ -1,7 +1,14 @@
FinalizeUserInvoices = Struct.new("FinalizeUserInvoices", :year, :month) do
class FinalizeUserInvoices
attr_reader :start_date, :end_date
def initialize(start_date = nil, end_date = nil)
@start_date = start_date || Time.now.beginning_of_month - 1.month
@end_date = end_date || Time.now.beginning_of_month
end
def before(job)
UpdateBillablePeriods.new.perform(start_date.year, start_date.month)
UpdateUserInvoices.new.perform(start_date.year, start_date.month)
UpdateBillablePeriods.new(start_date, end_date).perform
UpdateUserInvoices.new(start_date, end_date).perform
end
def perform
@@ -28,26 +35,4 @@ FinalizeUserInvoices = Struct.new("FinalizeUserInvoices", :year, :month) do
invoice.next
end
end
private
def start_date
# Start at the beginning of the specified month
# or at the beginning of last month if no month is specified
@start_date ||= if month && year
Time.new(year, month)
else
Time.now.beginning_of_month - 1.month
end
end
def end_date
# Stop at the end of the specified month
# or at the beginning of this month if no month is specified
@end_date ||= if month && year
Time.new(year, month) + 1.month
else
Time.now.beginning_of_month
end
end
end

View File

@@ -1,4 +1,11 @@
UpdateBillablePeriods = Struct.new("UpdateBillablePeriods", :year, :month) do
class UpdateBillablePeriods
attr_reader :start_date, :end_date
def initialize(start_date = nil, end_date = nil)
@start_date = start_date || (Time.now - 1.day).beginning_of_month
@end_date = end_date || Time.now.beginning_of_day
end
def perform
return unless end_date <= Time.now
@@ -92,26 +99,4 @@ UpdateBillablePeriods = Struct.new("UpdateBillablePeriods", :year, :month) do
obsolete_billable_periods.each(&:delete)
end
private
def start_date
# Start at the beginning of the specified month
# or at the beginning of the month (prior to midnight last night) if none specified
@start_date ||= if month && year
Time.new(year, month)
else
(Time.now - 1.day).beginning_of_month
end
end
def end_date
# Stop at the end of the specified month
# or at midnight last night if no month is specified
@end_date ||= if month && year
Time.new(year, month) + 1.month
else
Time.now.beginning_of_day
end
end
end

View File

@@ -1,6 +1,13 @@
UpdateUserInvoices = Struct.new("UpdateUserInvoices", :year, :month) do
class UpdateUserInvoices
attr_reader :start_date, :end_date
def initialize(start_date = nil, end_date = nil)
@start_date = start_date || (Time.now - 1.day).beginning_of_month
@end_date = end_date || Time.now.beginning_of_day
end
def before(job)
UpdateBillablePeriods.new.perform(start_date.year, start_date.month)
UpdateBillablePeriods.new(start_date, end_date).perform
end
def perform
@@ -76,26 +83,4 @@ UpdateUserInvoices = Struct.new("UpdateUserInvoices", :year, :month) do
invoice.destroy
end
end
private
def start_date
# Start at the beginning of the specified month
# or at the beginning of the month (prior to midnight last night) if none specified
@start_date ||= if month && year
Time.new(year, month)
else
(Time.now - 1.day).beginning_of_month
end
end
def end_date
# Stop at the end of the specified month
# or at midnight last night if no month is specified
@end_date ||= if month && year
Time.new(year, month) + 1.month
else
Time.now.beginning_of_day
end
end
end

View File

@@ -63,7 +63,7 @@ describe FinalizeUserInvoices do
end
context "when necessary global config setting have been set" do
context "and no arguments are passed to the job" do
context "and no date arguments are passed to the job" do
travel_to(3.days)
it "finalizes the uncompleted orders for accounts_distributor created in the previous calendar month (or on the 1st of this month)" do
@@ -76,8 +76,8 @@ describe FinalizeUserInvoices do
end
end
context "and a specfic year and month are passed as arguments" do
let!(:finalizer) { FinalizeUserInvoices.new(Time.now.year, 6) }
context "and specfic start and end dates are passed as arguments" do
let!(:finalizer) { FinalizeUserInvoices.new(start_of_july - 1.month, start_of_july) }
before do
allow(finalizer).to receive(:finalizer)

View File

@@ -17,7 +17,7 @@ describe UpdateBillablePeriods do
allow(Enterprise).to receive(:select) { [enterprise] }
end
context "when no arguments are passed to the job" do
context "when no date arguments are passed to the job" do
before do
expect(updater).to receive(:clean_up_untouched_billable_periods_for).once
end
@@ -43,8 +43,8 @@ describe UpdateBillablePeriods do
end
end
context "when a specfic year and month are passed as arguments" do
let!(:updater) { UpdateBillablePeriods.new(Time.now.year, 6) }
context "when specfic start and end dates are passed as arguments" do
let!(:updater) { UpdateBillablePeriods.new(start_of_july - 1.month, start_of_july) }
before do
allow(updater).to receive(:split_for_trial)

View File

@@ -58,8 +58,8 @@ describe UpdateUserInvoices do
end
end
context "when a specfic year and month are passed as arguments" do
let!(:updater) { UpdateUserInvoices.new(Time.now.year, 7) }
context "when specfic start and end dates are passed as arguments" do
let!(:updater) { UpdateUserInvoices.new(start_of_july, start_of_july + 1.month) }
before do
allow(updater).to receive(:update_invoice_for)