-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ProgressiveBilling): Expose Invoice#AppliedUsageThresholds in RE…
…ST API (#2511) ## Context AI companies want their users to pay before the end of a period if usage skyrockets. The problem being that self-serve companies can overuse their API without paying, triggering lots of costs on their side. ## Description This PR adds adds the new `applied_usage_thresholds` in `V1::InvoiceSerializer`
- Loading branch information
1 parent
ddd4908
commit 8863ce5
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module V1 | ||
class AppliedUsageThresholdSerializer < ModelSerializer | ||
def serialize | ||
payload = { | ||
lifetime_usage_amount_cents: model.lifetime_usage_amount_cents, | ||
created_at: model.created_at.iso8601 | ||
} | ||
|
||
payload.merge!(usage_treshold) | ||
payload | ||
end | ||
|
||
private | ||
|
||
def usage_treshold | ||
{ | ||
usage_threshold: ::V1::UsageThresholdSerializer.new(model.usage_threshold).serialize | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
spec/serializers/v1/applied_usage_threshold_serializer_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_String_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe V1::AppliedUsageThresholdSerializer do | ||
subject(:serializer) { described_class.new(applied_usage_threshold, root_name: 'applied_usage_threshold') } | ||
|
||
let(:applied_usage_threshold) { create(:applied_usage_threshold) } | ||
|
||
it 'serialize the object' do | ||
result = JSON.parse(serializer.to_json) | ||
|
||
aggregate_failures do | ||
expect(result['applied_usage_threshold']).to include( | ||
'lifetime_usage_amount_cents' => applied_usage_threshold.lifetime_usage_amount_cents, | ||
'created_at' => applied_usage_threshold.created_at.iso8601 | ||
) | ||
|
||
expect(result['applied_usage_threshold']['usage_threshold']).to include( | ||
'lago_id' => applied_usage_threshold.usage_threshold.id, | ||
'threshold_display_name' => applied_usage_threshold.usage_threshold.threshold_display_name, | ||
'amount_cents' => applied_usage_threshold.usage_threshold.amount_cents, | ||
'recurring' => applied_usage_threshold.usage_threshold.recurring | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters