diff --git a/app/services/daily_usages/compute_service.rb b/app/services/daily_usages/compute_service.rb index ee3f5a45739..0b55a99762d 100644 --- a/app/services/daily_usages/compute_service.rb +++ b/app/services/daily_usages/compute_service.rb @@ -28,7 +28,7 @@ def call from_datetime: current_usage.from_datetime, to_datetime: current_usage.to_datetime, refreshed_at: timestamp, - usage_date: timestamp.to_date - 1.day + usage_date: date_in_timezone - 1.day ) daily_usage.usage_diff = diff_usage(daily_usage) @@ -74,8 +74,6 @@ def diff_usage(daily_usage) end def subscription_billing_day? - date_in_timezone = timestamp.in_time_zone(customer.applicable_timezone).to_date - previous_billing_date_in_timezone = Subscriptions::DatesService .new_instance(subscription, timestamp, current_usage: true) .previous_beginning_of_period @@ -84,5 +82,9 @@ def subscription_billing_day? date_in_timezone == previous_billing_date_in_timezone end + + def date_in_timezone + @date_in_timezone ||= timestamp.in_time_zone(customer.applicable_timezone).to_date + end end end diff --git a/app/services/daily_usages/fill_history_service.rb b/app/services/daily_usages/fill_history_service.rb index 731494ef608..c2b7e14b246 100644 --- a/app/services/daily_usages/fill_history_service.rb +++ b/app/services/daily_usages/fill_history_service.rb @@ -18,7 +18,7 @@ def call datetime = date.in_time_zone(subscription.customer.applicable_timezone).beginning_of_day.utc next if date == Time.zone.today || - subscription.daily_usages.where(usage_date: date - 1.day).exists? || + subscription.daily_usages.where(usage_date: datetime.to_date - 1.day).exists? || DailyUsage.refreshed_at_in_timezone(datetime).where(subscription_id: subscription.id).exists? Timecop.thread_safe = true @@ -46,7 +46,7 @@ def call to_datetime: usage.to_datetime, refreshed_at: datetime, usage_diff: {}, - usage_date: date - 1.day + usage_date: datetime.to_date - 1.day ) if date != from diff --git a/spec/services/daily_usages/compute_service_spec.rb b/spec/services/daily_usages/compute_service_spec.rb index 7bfcfe1ae1e..128d65fd3df 100644 --- a/spec/services/daily_usages/compute_service_spec.rb +++ b/spec/services/daily_usages/compute_service_spec.rb @@ -27,7 +27,7 @@ external_subscription_id: subscription.external_id, usage: Hash, usage_diff: Hash, - usage_date: timestamp.to_date - 1.day + usage_date: Date.parse("2024-10-21") ) expect(daily_usage.refreshed_at).to match_datetime(timestamp) expect(daily_usage.from_datetime).to match_datetime(timestamp.beginning_of_month) @@ -119,5 +119,18 @@ expect(daily_usage.to_datetime).to match_datetime(subscription.terminated_at) end end + + context "with customer timezone" do + let(:customer) { create(:customer, organization:, timezone: "Australia/Sydney") } + let(:timestamp) { Time.zone.parse("2024-10-21 15:05:00") } + + it "creates a daily usage with expected usage_date" do + expect { compute_service.call }.to change(DailyUsage, :count).by(1) + + daily_usage = DailyUsage.order(created_at: :asc).last + # Timestamp is 22 Oct 2024 02:05:00 AEDT + expect(daily_usage.usage_date).to eq(Date.parse("2024-10-21")) + end + end end end