Skip to content

Commit

Permalink
feat(credit-note): Add estimate route before creation
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet committed Nov 2, 2023
1 parent 1d2ea9a commit a6cc16d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
19 changes: 19 additions & 0 deletions lib/lago/api/resources/credit_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,25 @@ def void(credit_note_id)

JSON.parse(response.to_json, object_class: OpenStruct).credit_note
end

def estimate(params)
uri = URI("#{client.base_api_url}#{api_resource}/estimate")

payload = whitelist_estimate_params(params)
response = connection.post(payload, uri)['estimated_credit_note']

JSON.parse(response.to_json, object_class: OpenStruct)
end

def whitelist_estimate_params(params)
result_hash = { invoice_id: params[:invoice_id] }.compact

whitelist_items(params[:items] || []).tap do |items|
result_hash[:items] = items unless items.empty?
end

{ root_name => result_hash }
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/factories/credit_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@
factory :update_credit_note, class: OpenStruct do
refund_status { 'pending' }
end

factory :estimate_credit_note, class: OpenStruct do
invoice_id { '1a901a90-1a90-1a90-1a90-1a901a901a90' }
items { build_list(:create_credit_note_item, 2).map(&:to_h) }
end
end
2 changes: 1 addition & 1 deletion spec/fixtures/api/credit_note.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"taxes_amount_cents": 40,
"taxes_rate": "20.0",
"sub_total_excluding_taxes_amount_cents": 200,
"coupons_adjustement_amount_cents": 0,
"coupons_adjustment_amount_cents": 0,
"created_at": "2022-10-04 16:21:00",
"updated_at": "2022-10-04 16:21:00",
"items": [
Expand Down
19 changes: 19 additions & 0 deletions spec/fixtures/api/credit_note_estimated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"estimated_credit_note": {
"lago_invoice_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"invoice_number": "LAG15",
"currency": "EUR",
"max_creditable_amount_cents": 100,
"max_refundable_amount_cents": 100,
"taxes_amount_cents": 40,
"taxes_rate": "20.0",
"sub_total_excluding_taxes_amount_cents": 200,
"coupons_adjustment_amount_cents": 0,
"items": [
{
"lago_fee_id": "1a901a90-1a90-1a90-1a90-1a901a901a90",
"amount_cents": 100
}
]
}
}
17 changes: 17 additions & 0 deletions spec/lago/api/resources/credit_note_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,21 @@
expect(response.lago_id).to eq(credit_note_id)
end
end

describe '#estimate' do
let(:estimate_response) { load_fixture(:credit_note_estimated) }
let(:params) { create(:estimate_credit_note) }

before do
stub_request(:post, "https://api.getlago.com/api/v1/credit_notes/estimate")
.with(body: { credit_note: params.to_h })
.to_return(body: estimate_response, status: 200)
end

it 'returns an estimated credit_note' do
response = resource.estimate(params)

expect(response.sub_total_excluding_taxes_amount_cents).to eq(200)
end
end
end

0 comments on commit a6cc16d

Please sign in to comment.