Skip to content

Commit

Permalink
feat(analytics): Add overdue balance endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Jul 15, 2024
1 parent 418bd9f commit 37cc711
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/lago-ruby-client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
require 'lago/api/resources/invoiced_usage'
require 'lago/api/resources/mrr'
require 'lago/api/resources/organization'
require 'lago/api/resources/overdue_balance'
require 'lago/api/resources/plan'
require 'lago/api/resources/subscription'
require 'lago/api/resources/tax'
Expand Down
17 changes: 17 additions & 0 deletions lib/lago/api/resources/overdue_balance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Lago
module Api
module Resources
class OverdueBalance < Base
def api_resource
'analytics/overdue_balance'
end

def root_name
'overdue_balance'
end
end
end
end
end
6 changes: 4 additions & 2 deletions spec/fixtures/api/gross_revenue_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
{
"month": "2023-11-01T00:00:00.000Z",
"amount_cents": 100,
"currency": "EUR"
"currency": "EUR",
"invoices_count": 1
},
{
"month": "2023-12-01T00:00:00.000Z",
"amount_cents": 200,
"currency": "USD"
"currency": "USD",
"invoices_count": 2
}
]
}
16 changes: 16 additions & 0 deletions spec/fixtures/api/overdue_balance_index.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"overdue_balances": [
{
"month": "2023-11-01T00:00:00.000Z",
"amount_cents": 100,
"currency": "EUR",
"lago_invoice_ids": ["1a901a90-1a90-1a90-1a90-1a901a901a90"]
},
{
"month": "2023-12-01T00:00:00.000Z",
"amount_cents": 200,
"currency": "USD",
"lago_invoice_ids": ["1a901a90-1a90-1a90-1a90-1a901a901a90"]
}
]
}
60 changes: 60 additions & 0 deletions spec/lago/api/resources/overdue_balance_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Lago::Api::Resources::OverdueBalance do
subject(:resource) { described_class.new(client) }

let(:client) { Lago::Api::Client.new }

let(:error_response) do
{
'status' => 422,
'error' => 'Unprocessable Entity',
'message' => 'Validation error on the record',
}.to_json
end

describe '#get_all' do
let(:overdue_balances_response) { load_fixture('overdue_balance_index') }

context 'when there is no options' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/analytics/overdue_balance')
.to_return(body: overdue_balances_response, status: 200)
end

it 'returns overdue balance' do
response = resource.get_all

expect(response['overdue_balances'].first['currency']).to eq('EUR')
expect(response['overdue_balances'].first['amount_cents']).to eq(100)
end
end

context 'when options are present' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/analytics/overdue_balance?currency=EUR')
.to_return(body: overdue_balances_response, status: 200)
end

it 'returns overdue balance' do
response = resource.get_all({ currency: 'EUR' })

expect(response['overdue_balances'].first['currency']).to eq('EUR')
expect(response['overdue_balances'].first['amount_cents']).to eq(100)
end
end

context 'when there is an issue' do
before do
stub_request(:get, 'https://api.getlago.com/api/v1/analytics/overdue_balance')
.to_return(body: error_response, status: 422)
end

it 'raises an error' do
expect { resource.get_all }.to raise_error Lago::Api::HttpError
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require 'lago/api/resources/invoiced_usage'
require 'lago/api/resources/mrr'
require 'lago/api/resources/organization'
require 'lago/api/resources/overdue_balance'
require 'lago/api/resources/plan'
require 'lago/api/resources/subscription'
require 'lago/api/resources/tax'
Expand Down

0 comments on commit 37cc711

Please sign in to comment.