Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Event show action to the API #269

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/controllers/api/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ def validation_errors(error_result)
)
end

def not_found_error
render(
json: {
status: 404,
error: 'Not Found',
},
status: :not_found
)
end

def current_organization(api_key = nil)
@current_organization ||= Organization.find_by(api_key: api_key)
end
Expand Down
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 not_found_error 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