Skip to content

Commit

Permalink
feat(events): Track event sent without external_subscription_id for /…
Browse files Browse the repository at this point in the history
…estimate_fees (#2271)

## Context

All events sent to Lago must include `external_subscription_id`.

## Description

Track event sent to `/api/v1/events/estimate_fees` without the
`external_subscription_id`

See #2056
  • Loading branch information
julienbourdeau authored Jul 10, 2024
1 parent 44ccecb commit 68fc131
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app/controllers/api/v1/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def show
end

def estimate_fees
if create_params[:external_subscription_id].blank?
Deprecation.report('estimate_fees_missing_external_subscription_id', current_organization.id)
end

result = Fees::EstimatePayInAdvanceService.call(
organization: current_organization,
params: create_params
Expand Down
28 changes: 27 additions & 1 deletion spec/requests/api/v1/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
before do
charge
tax
allow(Deprecation).to receive(:report)
end

it 'returns a success' do
Expand All @@ -148,6 +149,8 @@

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

expect(Deprecation).to have_received(:report).with('estimate_fees_missing_external_subscription_id', organization.id)

fee = json[:fees].first
expect(fee[:lago_id]).to be_nil
expect(fee[:lago_group_id]).to be_nil
Expand All @@ -171,19 +174,41 @@
'/api/v1/events/estimate_fees',
event: {
code: metric.code,
external_customer_id: nil,
external_subscription_id: nil,
properties: {
foo: 'bar'
}
}
)

aggregate_failures do
expect(Deprecation).to have_received(:report).with('estimate_fees_missing_external_subscription_id', organization.id)
expect(response).to have_http_status(:not_found)
end
end
end

context 'with external_subscription_id' do
it 'returns a success' do
post_with_token(
organization,
'/api/v1/events/estimate_fees',
event: {
code: metric.code,
external_subscription_id: subscription.external_id,
properties: {
foo: 'bar'
}
}
)

aggregate_failures do
expect(Deprecation).not_to have_received(:report)
expect(response).to have_http_status(:success)
end
end
end

context 'when metric code does not match an pay_in_advance charge' do
let(:charge) { create(:standard_charge, plan:, billable_metric: metric) }

Expand All @@ -201,6 +226,7 @@
)

aggregate_failures do
expect(Deprecation).to have_received(:report).with('estimate_fees_missing_external_subscription_id', organization.id)
expect(response).to have_http_status(:unprocessable_entity)
end
end
Expand Down

0 comments on commit 68fc131

Please sign in to comment.