diff --git a/app/controllers/api/base_controller.rb b/app/controllers/api/base_controller.rb index 1279c087834..49cb94104f7 100644 --- a/app/controllers/api/base_controller.rb +++ b/app/controllers/api/base_controller.rb @@ -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 diff --git a/app/controllers/api/v1/events_controller.rb b/app/controllers/api/v1/events_controller.rb index 767feb5acf1..fd917a5a16a 100644 --- a/app/controllers/api/v1/events_controller.rb +++ b/app/controllers/api/v1/events_controller.rb @@ -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 diff --git a/app/serializers/v1/event_serializer.rb b/app/serializers/v1/event_serializer.rb new file mode 100644 index 00000000000..1b992804d22 --- /dev/null +++ b/app/serializers/v1/event_serializer.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 33571b690b8..be8934ed5c4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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] resources :invoices, only: %i[update] diff --git a/spec/factories/event_factory.rb b/spec/factories/event_factory.rb index ba562a3a131..ccac078274d 100644 --- a/spec/factories/event_factory.rb +++ b/spec/factories/event_factory.rb @@ -7,5 +7,6 @@ transaction_id { SecureRandom.uuid } code { Faker::Name.name.underscore } + timestamp { Time.current } end end diff --git a/spec/requests/api/v1/events_spec.rb b/spec/requests/api/v1/events_spec.rb index 25b9f148349..d1510b4a330 100644 --- a/spec/requests/api/v1/events_spec.rb +++ b/spec/requests/api/v1/events_spec.rb @@ -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