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

Commit

Permalink
Extend Registration History to include actor types (#590)
Browse files Browse the repository at this point in the history
* add action_id and action_type to registration_history

* overwrite registration_history timestamp on worker create

* correctly change History initializer

* fix rubocop

* allow 6 parameters for event_registration

* use Time.now.utc

* add action type for Payment Refund

* run robocop
  • Loading branch information
FinnIckler authored Jun 10, 2024
1 parent 136bb3c commit 7e60e68
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 21 deletions.
7 changes: 7 additions & 0 deletions app/controllers/internal_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,15 @@ def update_payment_status
iso_amount = params.require(:iso_amount)
currency_iso = params.require(:currency_iso)
payment_status = params.require(:payment_status)
acting_id = params.require(:acting_id)
acting_type = params.require(:acting_type)
registration = Registration.find(attendee_id)
registration.update_payment_lane(payment_id, iso_amount, currency_iso, payment_status)
if payment_status == 'refund'
registration.history.add_entry({ payment_status: payment_status, iso_amount: iso_amount }, acting_type, acting_id, 'Payment Refund')
else
registration.history.add_entry({ payment_status: payment_status, iso_amount: iso_amount }, acting_type, acting_id, 'Payment')
end
render json: { status: 'ok' }
end

Expand Down
11 changes: 9 additions & 2 deletions app/controllers/registration_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
require 'time'

class RegistrationController < ApplicationController
skip_before_action :validate_jwt_token, only: [:list, :list_waiting]
skip_before_action :validate_jwt_token, only: [:list, :list_waiting, :count]
# The order of the validations is important to not leak any non public info via the API
# That's why we should always validate a request first, before taking any other before action
# before_actions are triggered in the order they are defined
Expand All @@ -25,6 +25,12 @@ def show
render_error(e.http_status, e.error)
end

def count
competition_id = params.require(:competition_id)

render json: { count: Registration.accepted_competitors_count(competition_id) }
end

def create
queue_url = ENV['QUEUE_URL'] || $sqs.get_queue_url(queue_name: 'registrations.fifo').queue_url
event_ids = params.dig('competing', 'event_ids')
Expand All @@ -33,6 +39,7 @@ def create
id = SecureRandom.uuid

step_data = {
created_at: Time.now.utc,
attendee_id: "#{@competition_id}-#{@user_id}",
user_id: @user_id,
competition_id: @competition_id,
Expand Down Expand Up @@ -119,7 +126,7 @@ def process_update(update_request)
registration = Registration.find("#{@competition_id}-#{user_id}")
old_status = registration.competing_status
updated_registration = registration.update_competing_lane!({ status: status, comment: comment, event_ids: event_ids, admin_comment: admin_comment, guests: guests })
registration.history.add_entry(update_changes(update_request), @current_user, action_type(update_request))
registration.history.add_entry(update_changes(update_request), 'user', @current_user, action_type(update_request))
if old_status == 'accepted' && status != 'accepted'
Registration.decrement_competitors_count(@competition_id)
elsif old_status != 'accepted' && status == 'accepted'
Expand Down
10 changes: 0 additions & 10 deletions app/models/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,6 @@ def init_payment_lane(amount, currency_code, id, donation)
def update_payment_lane(id, iso_amount, currency_iso, status)
updated_lanes = lanes.map do |lane|
if lane.lane_name == 'payment'
old_details = lane.lane_details
# TODO: Should we only add payments to the payment_history
# if there is a new payment_id?
lane.lane_details['payment_history'].append({
status: lane.lane_state,
payment_id: old_details['payment_id'],
currency_code: old_details['currency_code'],
amount_lowest_denominator: old_details['amount_lowest_denominator'],
last_updated: old_details['last_updated'],
})
lane.lane_state = status
lane.lane_details['payment_id'] = id
lane.lane_details['amount_lowest_denominator'] = iso_amount
Expand Down
4 changes: 2 additions & 2 deletions app/models/registration_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class RegistrationHistory

field :entries, :array, of: History

def add_entry(changed_attributes, actor_user_id, action)
entry = History.new({ 'changed_attributes' => changed_attributes, 'actor_user_id' => actor_user_id, 'action' => action })
def add_entry(changed_attributes, actor_type, actor_id, action)
entry = History.new({ 'changed_attributes' => changed_attributes, 'actor_type' => actor_type, 'actor_id' => actor_id, 'action' => action })
if entries.empty?
update_attributes(entries: [entry])
else
Expand Down
1 change: 0 additions & 1 deletion app/worker/queue_poller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require 'zeitwerk'
loader = Zeitwerk::Loader.new
loader.push_dir("#{__dir__}/../../lib")
loader.push_dir("#{__dir__}/../helpers")
loader.push_dir("#{__dir__}/../models")
loader.setup

Expand Down
16 changes: 11 additions & 5 deletions app/worker/registration_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,31 @@ def process_message(message)
message['user_id'],
message['step_details']['event_ids'],
message['step_details']['comment'],
message['step_details']['guests'])
message['step_details']['guests'],
message['created_at'])
end
end

private

def event_registration(competition_id, user_id, event_ids, comment, guests)
# rubocop:disable Metrics/ParameterLists
def event_registration(competition_id, user_id, event_ids, comment, guests, created_at)
# Event Registration might not be the first lane that is completed
# TODO: When we add another lane, we need to update the registration history instead of creating it
registration = begin
Registration.find("#{competition_id}-#{user_id}")
rescue Dynamoid::Errors::RecordNotFound
initial_history = History.new({ 'changed_attributes' =>
{ event_ids: event_ids, comment: comment, guests: guests, status: 'pending' },
'actor_user_id' => user_id,
'action' => 'Worker processed' })
'actor_type' => 'user',
'actor_id' => user_id,
'action' => 'Worker processed',
'timestamp' => created_at })
RegistrationHistory.create(attendee_id: "#{competition_id}-#{user_id}", entries: [initial_history])
Registration.new(attendee_id: "#{competition_id}-#{user_id}",
competition_id: competition_id,
user_id: user_id)
user_id: user_id,
created_at: created_at)
end
competing_lane = LaneFactory.competing_lane(event_ids: event_ids, comment: comment)
if registration.lanes.nil?
Expand All @@ -55,4 +60,5 @@ def event_registration(competition_id, user_id, event_ids, comment, guests)
EmailApi.send_creation_email(competition_id, user_id)
end
end
# rubocop:enable Metrics/ParameterLists
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
get '/api/v1/registrations/:competition_id/admin', to: 'registration#list_admin'
get '/api/v1/registrations/:competition_id/waiting', to: 'registration#list_waiting'
get '/api/v1/:competition_id/payment', to: 'registration#payment_ticket'
get '/api/v1/:competition_id/count', to: 'registration#count'
post '/api/v1/:competition_id/import', to: 'registration#import'
end
3 changes: 2 additions & 1 deletion lib/history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class History

def initialize(args)
@changed_attributes = args['changed_attributes'] || {}
@actor_user_id = args['actor_user_id'] || ''
@actor_type = args['actor_type'] || ''
@actor_id = args['actor_id'] || ''
@timestamp = args['timestamp'] || Time.now.utc
@action = args['action'] || 'unknown'
end
Expand Down

0 comments on commit 7e60e68

Please sign in to comment.