Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

Commit

Permalink
Add human readable payment to payment object and fix bulk update API (#…
Browse files Browse the repository at this point in the history
…605)

* add human readable payment to payment object

* only use money functions and not money-rails

* changes to bulk update

* change request factory

* fix tests

* remove puts
  • Loading branch information
FinnIckler authored Jun 14, 2024
1 parent 9352b80 commit 10712fd
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ gem 'jbuilder'
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
gem 'rack-cors'

gem 'money-rails'

# much better gem for http requests than the native ruby one
gem 'httparty'

Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ GEM
method_source (1.0.0)
mini_mime (1.1.5)
minitest (5.23.1)
monetize (1.13.0)
money (~> 6.12)
money (6.19.0)
i18n (>= 0.6.4, <= 2)
money-rails (1.15.0)
activesupport (>= 3.0)
monetize (~> 1.9)
money (~> 6.13)
railties (>= 3.0)
msgpack (1.7.2)
multi_xml (0.7.1)
bigdecimal (~> 3.1)
Expand Down Expand Up @@ -359,6 +368,7 @@ DEPENDENCIES
jbuilder
jwt
kredis
money-rails
overcommit
prometheus_exporter
pry
Expand Down
5 changes: 3 additions & 2 deletions app/controllers/registration_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ def bulk_update
end

def validate_bulk_update_request
@competition_id = params[:requests][0]['competition_id']
RegistrationChecker.bulk_update_allowed!(params, CompetitionApi.find!(@competition_id), params[:submitted_by])
@competition_id = params.require('competition_id')
RegistrationChecker.bulk_update_allowed!(params, CompetitionApi.find!(@competition_id), @current_user)
rescue BulkUpdateError => e
render_error(e.http_status, e.errors)
rescue NoMethodError
Expand Down Expand Up @@ -340,6 +340,7 @@ def get_single_registration(user_id, competition_id)
},
payment: {
payment_status: registration.payment_status,
payment_amount_human_readable: registration.payment_amount_human_readable,
updated_at: registration.payment_date,
},
history: registration.history.entries,
Expand Down
13 changes: 13 additions & 0 deletions app/models/registration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'time'
require 'money-rails'

class Registration
include Dynamoid::Document
Expand Down Expand Up @@ -97,6 +98,18 @@ def payment_status
payment_lane&.lane_state
end

def payment_amount
payment_lane&.lane_details&.[]('amount_lowest_denominator')
end

def payment_amount_human_readable
payment_details = payment_lane&.lane_details
unless payment_details.nil?
money = Money.from_cents(payment_details['amount_lowest_denominator'], payment_details['currency_code'])
"#{money.format} (#{money.currency.name})"
end
end

def payment_date
payment_lane&.lane_details&.[]('last_updated')
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/registration_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.update_registration_allowed!(update_request, competition_info, requesti
@competition_info = competition_info
@requestee_user_id = @request['user_id']
@requester_user_id = requesting_user
@registration = Registration.find("#{update_request['competition_id']}-#{update_request['user_id']}")
@registration = Registration.find("#{competition_info.id}-#{update_request['user_id']}")

user_can_modify_registration!
validate_guests!
Expand All @@ -36,7 +36,7 @@ def self.update_registration_allowed!(update_request, competition_info, requesti
end

def self.bulk_update_allowed!(bulk_update_request, competition_info, requesting_user)
raise RegistrationError.new(:unauthorized, ErrorCodes::USER_INSUFFICIENT_PERMISSIONS) unless
raise BulkUpdateError.new(:unauthorized, [ErrorCodes::USER_INSUFFICIENT_PERMISSIONS]) unless
competition_info.is_organizer_or_delegate?(requesting_user)

errors = {}
Expand Down
4 changes: 4 additions & 0 deletions lib/competition_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def name
@competition_json['name']
end

def id
@competition_json['id']
end

def registration_edits_allowed?
@competition_json['allow_registration_edits'] && within_event_change_deadline?
end
Expand Down
4 changes: 2 additions & 2 deletions lib/error_codes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ module ErrorCodes
MUST_ACCEPT_WAITING_LIST_LEADER = -4011

# Payment Errors
PAYMENT_NOT_ENABLED = -3001
PAYMENT_NOT_READY = -3002
PAYMENT_NOT_ENABLED = -6001
PAYMENT_NOT_READY = -6002
end
5 changes: 3 additions & 2 deletions spec/controllers/registration_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
patch :update, params: update_request, as: :json

@response = response

@body = response.parsed_body
@updated_registration = Registration.find("#{@competition['id']}-#{@registration[:user_id]}")
end
Expand Down Expand Up @@ -158,7 +159,7 @@
expect(response.code).to eq('200')
end

it 'returns 422 if blank json submitted' do
it 'returns 400 if blank json submitted' do
registration = FactoryBot.create(:registration)
bulk_update_request = FactoryBot.build(:bulk_update_request, user_ids: [registration[:user_id]])

Expand All @@ -167,7 +168,7 @@

request.headers['Authorization'] = bulk_update_request['jwt_token']
patch :bulk_update, params: {}, as: :json
expect(response.code).to eq('422')
expect(response.code).to eq('400')
end
end
end
4 changes: 2 additions & 2 deletions spec/factories/competition_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
name { 'CubingZA National Championship 2023' }
event_ids { events }
registration_open { '2023-05-05T04:00:00.000Z' }
registration_close { '2024-06-14T00:00:00.000Z' }
registration_close { 1.week.from_now.iso8601 }
announced_at { '2023-05-01T15:59:53.000Z' }
start_date { '2023-06-16' }
end_date { '2023-06-18' }
Expand All @@ -27,7 +27,7 @@
country_iso2 { 'ZA' }
guest_entry_status { 'restricted' }
guests_per_registration_limit { 2 }
event_change_deadline_date { '2024-06-14T00:00:00.000Z' }
event_change_deadline_date { 1.week.from_now.iso8601 }
events_per_registration_limit { 'null' }
using_payment_integrations? { true }
competition_series_ids { nil }
Expand Down
1 change: 1 addition & 0 deletions spec/factories/request_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
end

submitted_by { 1306 }
competition_id { 'CubingZANationalChampionship2023' }
jwt_token { fetch_jwt_token(submitted_by) }
requests do
user_ids.map do |user_id|
Expand Down
4 changes: 2 additions & 2 deletions spec/services/registration_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1262,8 +1262,8 @@

expect {
RegistrationChecker.bulk_update_allowed!(bulk_update_request, competition_info, bulk_update_request['submitted_by'])
}.to raise_error(RegistrationError) do |error|
expect(error.error).to eq(ErrorCodes::USER_INSUFFICIENT_PERMISSIONS)
}.to raise_error(BulkUpdateError) do |error|
expect(error.errors).to eq([ErrorCodes::USER_INSUFFICIENT_PERMISSIONS])
expect(error.http_status).to eq(:unauthorized)
end
end
Expand Down

0 comments on commit 10712fd

Please sign in to comment.