Skip to content

Commit

Permalink
Add batch endpoint for instant estimate
Browse files Browse the repository at this point in the history
  • Loading branch information
nudded committed Jan 23, 2025
1 parent a1c2a3c commit e36286f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/controllers/api/v1/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ def estimate_instant_fees
end
end

def batch_estimate_instant_fees
fees = []
batch_params[:events].each do |create_params|
fees += Fees::EstimateInstantPayInAdvanceService.call!(
organization: current_organization,
params: create_params
).fees
end

render(
json: {fees: fees}
)
end

def estimate_fees
result = Fees::EstimatePayInAdvanceService.call(
organization: current_organization,
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
resources :events, only: %i[create show index] do
post :estimate_fees, on: :collection
post :estimate_instant_fees, on: :collection
post :batch_estimate_instant_fees, on: :collection
end
resources :applied_coupons, only: %i[create index]
resources :fees, only: %i[show update index destroy]
Expand Down
48 changes: 48 additions & 0 deletions spec/requests/api/v1/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,54 @@
end
end

describe 'POST /api/v1/events/batch_estimate_instant_fees' do
subject do
post_with_token(organization, '/api/v1/events/batch_estimate_instant_fees', events: batch_params)
end

let(:metric) { create(:sum_billable_metric, organization:) }
let(:charge) { create(:percentage_charge, :pay_in_advance, plan:, billable_metric: metric, properties: {rate: '0.1', fixed_amount: '0'}) }

let(:event_params) do
{
code: metric.code,
organization_id: organization.id,
external_subscription_id: subscription.external_id,
transaction_id: SecureRandom.uuid,
properties: {
metric.field_name => 400
}
}
end

let(:batch_params) { [event_params] }

before do
charge
end

include_examples 'requires API permission', 'event', 'write'

it 'returns a success' do
subject

expect(response).to have_http_status(:success)

expect(json[:fees].count).to eq(1)

fee = json[:fees].first
expect(fee[:lago_id]).to be_nil
expect(fee[:lago_group_id]).to be_nil
expect(fee[:item][:type]).to eq('charge')
expect(fee[:item][:code]).to eq(metric.code)
expect(fee[:item][:name]).to eq(metric.name)
expect(fee[:amount_cents]).to eq('40.0')
expect(fee[:amount_currency]).to eq('EUR')
expect(fee[:units]).to eq('400.0')
expect(fee[:events_count]).to eq(1)
end
end

describe 'POST /api/v1/events/estimate_instant_fees' do
subject do
post_with_token(organization, '/api/v1/events/estimate_instant_fees', event: event_params)
Expand Down

0 comments on commit e36286f

Please sign in to comment.