Skip to content

Commit

Permalink
feat(data): Convert usage_date in customer's timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Dec 22, 2024
1 parent b4d4d04 commit fee780b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
8 changes: 5 additions & 3 deletions app/services/daily_usages/compute_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
15 changes: 14 additions & 1 deletion spec/services/daily_usages/compute_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

0 comments on commit fee780b

Please sign in to comment.