Skip to content

Commit

Permalink
use Float to define if received timestamp is float
Browse files Browse the repository at this point in the history
  • Loading branch information
annvelents committed Dec 18, 2024
1 parent 6c876d9 commit c8ed898
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
7 changes: 0 additions & 7 deletions app/controllers/api/v1/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module Api
module V1
class EventsController < Api::BaseController
before_action :validate_timestamp_format, only: [:create, :estimate_fees]

def create
result = ::Events::CreateService.call(
Expand Down Expand Up @@ -109,12 +108,6 @@ def estimate_fees

private

def validate_timestamp_format
return if params[:event][:timestamp].to_f.to_s == params[:event][:timestamp].to_s

render json: { error: 'Invalid timestamp format' }, status: :unprocessable_entity
end

def create_params
params
.require(:event)
Expand Down
4 changes: 3 additions & 1 deletion app/services/events/create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def call
event.external_subscription_id = params[:external_subscription_id]
event.properties = params[:properties] || {}
event.metadata = metadata || {}
event.timestamp = Time.zone.at(params[:timestamp] ? params[:timestamp].to_f : timestamp)
event.timestamp = Time.zone.at(params[:timestamp] ? Float(params[:timestamp]) : timestamp)
event.precise_total_amount_cents = params[:precise_total_amount_cents]

CalculateExpressionService.call(organization:, event:).raise_if_error!
Expand All @@ -35,6 +35,8 @@ def call
result.record_validation_failure!(record: e.record)
rescue ActiveRecord::RecordNotUnique
result.single_validation_failure!(field: :transaction_id, error_code: 'value_already_exist')
rescue ArgumentError
result.single_validation_failure!(field: :timestamp, error_code: 'invalid_format')
end

private
Expand Down
12 changes: 12 additions & 0 deletions app/services/events/validate_creation_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,21 @@ def validate_create
return missing_subscription_error
end

return invalid_timestamp_error unless valid_timestamp?
return transaction_id_error unless valid_transaction_id?
return invalid_code_error unless valid_code?
return invalid_properties_error unless valid_properties?

nil
end

def valid_timestamp?
return true if params[:timestamp].blank?

# timestamp is a number of seconds
valid_number?(params[:timestamp])
end

def valid_transaction_id?
return false if params[:transaction_id].blank?

Expand Down Expand Up @@ -92,6 +100,10 @@ def invalid_customer_error
result.not_found_failure!(resource: 'customer')
end

def invalid_timestamp_error
result.validation_failure!(errors: {timestamp: ['invalid_format']})
end

def billable_metric
@billable_metric ||= organization.billable_metrics.find_by(code: params[:code])
end
Expand Down
1 change: 1 addition & 0 deletions spec/requests/api/v1/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
expect { subject }.not_to change(Event, :count)

expect(response).to have_http_status(:unprocessable_entity)
expect(json[:error_details]).to eq({timestamp: ["invalid_format"]})
end
end
end
Expand Down

0 comments on commit c8ed898

Please sign in to comment.