Skip to content

Commit

Permalink
Add Event show action to the API
Browse files Browse the repository at this point in the history
Implements getlago/lago#48
  • Loading branch information
prognostikos committed Jun 17, 2022
1 parent aef011b commit 9bcb6fa
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/controllers/api/v1/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ def create
head(:ok)
end

def show
event = Event.find_by(
organization: current_organization,
transaction_id: params[:id]
)

return head(:not_found) unless event

render(
json: ::V1::EventSerializer.new(
event,
root_name: 'event',
)
)
end

private

def create_params
Expand Down
17 changes: 17 additions & 0 deletions app/serializers/v1/event_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true
#
module V1
class EventSerializer < ModelSerializer
def serialize
{
lago_id: model.id,
transaction_id: model.transaction_id,
customer_id: model.customer_id,
code: model.code,
timestamp: model.timestamp.iso8601,
properties: model.properties,
created_at: model.created_at.iso8601
}
end
end
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
resources :subscriptions, only: %i[create]
delete '/subscriptions', to: 'subscriptions#terminate', as: :terminate

resources :events, only: %i[create]
resources :events, only: %i[create show]
resources :applied_coupons, only: %i[create]
resources :applied_add_ons, only: %i[create]

Expand Down
1 change: 1 addition & 0 deletions spec/factories/event_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@

transaction_id { SecureRandom.uuid }
code { Faker::Name.name.underscore }
timestamp { Time.current }
end
end
30 changes: 30 additions & 0 deletions spec/requests/api/v1/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,34 @@
end
end
end

describe 'GET /events/:id' do
let(:event) { create(:event) }

it 'returns an event' do
get_with_token(
event.organization,
'/api/v1/events/' + event.transaction_id
)

expect(response).to have_http_status(:ok)

api_event = JSON.parse(response.body)['event']

%w[code transaction_id customer_id].each do |property|
expect(api_event[property]).to eq event.attributes[property]
end
end

context 'with a non-existing transaction_id' do
it 'returns not found' do
get_with_token(
organization,
'/api/v1/events/' + SecureRandom.uuid
)

expect(response).to have_http_status(:not_found)
end
end
end
end

0 comments on commit 9bcb6fa

Please sign in to comment.