Skip to content

Commit

Permalink
feat(ProgressiveBilling): Expose lifetime usage GET and PUT (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet authored Sep 5, 2024
1 parent 6abee8c commit 894ca40
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/lago/api/resources/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ def root_name
'subscription'
end

def lifetime_usage(external_subscription_id)
uri = URI(
"#{client.base_api_url}#{api_resource}/#{external_subscription_id}/lifetime_usage",
)
response = connection.get(uri, identifier: nil)
JSON.parse(response.to_json, object_class: OpenStruct).lifetime_usage
end

def update_lifetime_usage(external_subscription_id, params)
uri = URI(
"#{client.base_api_url}#{api_resource}/#{external_subscription_id}/lifetime_usage",
)

response = connection.put(
uri,
identifier: nil,
body: whitelist_lifetime_usage_params(params),
)
JSON.parse(response.to_json, object_class: OpenStruct).lifetime_usage
end

def whitelist_params(params)
{
root_name => {
Expand All @@ -26,6 +47,14 @@ def whitelist_params(params)
}.compact,
}
end

def whitelist_lifetime_usage_params(params)
{
lifetime_usage: {
external_historical_usage_amount_cents: params[:external_historical_usage_amount_cents],
},
}
end
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/lifetime_usage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
factory :update_lifetime_usage, class: OpenStruct do
external_historical_usage_amount_cents { 100 }
end
end
24 changes: 24 additions & 0 deletions spec/fixtures/api/subscription_lifetime_usage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"lifetime_usage": {
"lago_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"lago_subscription_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"external_subscription_id": "sub-12345",
"external_historical_usage_amount_cents": 20,
"invoiced_usage_amount_cents": 2000,
"current_usage_amount_cents": 100,
"from_datetime": "2022-04-29T08:59:51Z",
"to_datetime": "2024-02-10T08:59:51Z",
"usage_thresholds": [
{
"amount_cents": 100,
"completion_ratio": 1.0,
"reached_at": "2024-01-09T08:59:51Z"
},
{
"amount_cents": 3000,
"completion_ratio": "0.6733333",
"reached_at": null
}
]
}
}
78 changes: 78 additions & 0 deletions spec/lago/api/resources/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,82 @@
end
end
end

describe '#lifetime_usage' do
let(:json_response) { load_fixture('subscription_lifetime_usage') }
let(:lifetime_usage_response) { JSON.parse(json_response) }
let(:external_subscription_id) { lifetime_usage_response['lifetime_usage']['external_subscription_id'] }

context 'when lifetime usage is successfully retrieved' do
before do
stub_request(:get, "https://api.getlago.com/api/v1/subscriptions/#{external_subscription_id}/lifetime_usage")
.to_return(body: json_response, status: 200)
end

it 'returns lifetime usage' do
lifetime_usage = resource.lifetime_usage(external_subscription_id)

expect(lifetime_usage.external_subscription_id).to eq(external_subscription_id)
end
end

context 'when lifetime usage is not found' do
let(:not_found_response) do
{
'status' => 404,
'error' => 'Not Found',
'code' => 'credit_note_not_found',
}.to_json
end

before do
stub_request(:get, "https://api.getlago.com/api/v1/subscriptions/#{external_subscription_id}/lifetime_usage")
.to_return(body: not_found_response, status: 404)
end

it 'raises an error' do
expect { resource.lifetime_usage(external_subscription_id) }.to raise_error(Lago::Api::HttpError)
end
end
end

describe '#update_lifetime_usage' do
let(:params) { create(:update_lifetime_usage).to_h }

let(:json_response) { load_fixture('subscription_lifetime_usage') }
let(:lifetime_usage_response) { JSON.parse(json_response) }
let(:external_subscription_id) { lifetime_usage_response['lifetime_usage']['external_subscription_id'] }

before do
stub_request(:put, "https://api.getlago.com/api/v1/subscriptions/#{external_subscription_id}/lifetime_usage")
.with(body: { lifetime_usage: params })
.to_return(body: json_response, status: 200)
end

it 'returns lifetime usage' do
lifetime_usage = resource.update_lifetime_usage(external_subscription_id, params)

expect(lifetime_usage.external_subscription_id).to eq(external_subscription_id)
end

context 'when lifetime usage is not found' do
let(:not_found_response) do
{
'status' => 404,
'error' => 'Not Found',
'code' => 'credit_note_not_found',
}.to_json
end

before do
stub_request(:put, "https://api.getlago.com/api/v1/subscriptions/#{external_subscription_id}/lifetime_usage")
.with(body: { lifetime_usage: params })
.to_return(body: not_found_response, status: 404)
end

it 'raises an error' do
expect { resource.update_lifetime_usage(external_subscription_id, params) }.to raise_error(Lago::Api::HttpError)
end
end
end
end

0 comments on commit 894ca40

Please sign in to comment.