From 70de4fd1fd0d947915da19c641d3aae005baeec5 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Fri, 3 Jul 2015 15:26:02 +0800 Subject: [PATCH] Touching unchanged billable_periods to prevent them from being marked for deletion --- app/jobs/update_billable_periods.rb | 5 +++- spec/jobs/update_billable_periods_spec.rb | 35 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/app/jobs/update_billable_periods.rb b/app/jobs/update_billable_periods.rb index c3229f52f0..8ad5c444ee 100644 --- a/app/jobs/update_billable_periods.rb +++ b/app/jobs/update_billable_periods.rb @@ -73,7 +73,7 @@ UpdateBillablePeriods = Struct.new("UpdateBillablePeriods") do turnover: orders.sum(&:total) }) - billable_period + billable_period.touch end def clean_up_untouched_billable_periods_for(enterprise, start_of_month, job_start_time) @@ -83,6 +83,9 @@ UpdateBillablePeriods = Struct.new("UpdateBillablePeriods") do if obsolete_billable_periods.any? current_billable_periods = enterprise.billable_periods.where('ends_at >= (?) AND updated_at >= (?)', start_of_month, job_start_time) + Delayed::Worker.logger.info "#{enterprise.name} #{start_of_month.strftime("%F %T")} #{job_start_time.strftime("%F %T")}" + Delayed::Worker.logger.info "#{obsolete_billable_periods.first.updated_at.strftime("%F %T")}" + Bugsnag.notify(RuntimeError.new("Duplicate BillablePeriod"), { current: current_billable_periods.map(&:as_json), obsolete: obsolete_billable_periods.map(&:as_json) diff --git a/spec/jobs/update_billable_periods_spec.rb b/spec/jobs/update_billable_periods_spec.rb index b89e724c5f..f4b57db6f3 100644 --- a/spec/jobs/update_billable_periods_spec.rb +++ b/spec/jobs/update_billable_periods_spec.rb @@ -325,6 +325,14 @@ describe UpdateBillablePeriods do let!(:existing) { create(:billable_period, enterprise: enterprise, begins_at: start_of_july) } + before do + allow(Spree::Order).to receive(:where) { [ + double(:order, total: 10), + double(:order, total: 20), + double(:order, total: 30) + ]} + end + context "when arguments match both 'begins_at' and 'enterprise_id' of an existing billable period" do it "updates the existing billable period" do expect{ @@ -335,6 +343,31 @@ describe UpdateBillablePeriods do expect(existing.ends_at).to eq start_of_july + 20.days expect(existing.sells).to eq enterprise.sells expect(existing.trial).to eq false + expect(existing.turnover).to eq 60 + end + + context "when there is nothing to update" do + before do + Timecop.freeze(start_of_july + 3.days) { + existing.update_attributes( + begins_at: start_of_july, + ends_at: start_of_july + 20.days, + trial: false, + sells: enterprise.sells, + turnover: 60 + ) + } + end + + it "changes updated_at anyway by touching the billable period" do + Timecop.freeze(start_of_july + 10.days) { + expect{ + updater.update_billable_period(enterprise, start_of_july, start_of_july + 20.days, false) + }.to change{ existing.reload.updated_at } + .from(start_of_july + 3.days) + .to(start_of_july + 10.days) + } + end end end @@ -351,6 +384,7 @@ describe UpdateBillablePeriods do expect(billable_period.ends_at).to eq start_of_july + 30.days expect(billable_period.sells).to eq enterprise.sells expect(billable_period.trial).to eq false + expect(billable_period.turnover).to eq 60 end end @@ -369,6 +403,7 @@ describe UpdateBillablePeriods do expect(billable_period.ends_at).to eq start_of_july + 20.days expect(billable_period.sells).to eq new_enterprise.sells expect(billable_period.trial).to eq false + expect(billable_period.turnover).to eq 60 end end end