diff --git a/app/controllers/internal_controller.rb b/app/controllers/internal_controller.rb index f0787a15..0f647679 100644 --- a/app/controllers/internal_controller.rb +++ b/app/controllers/internal_controller.rb @@ -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 diff --git a/app/controllers/registration_controller.rb b/app/controllers/registration_controller.rb index 846ba304..b9e1b1b1 100644 --- a/app/controllers/registration_controller.rb +++ b/app/controllers/registration_controller.rb @@ -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 @@ -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') @@ -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, @@ -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' diff --git a/app/models/registration.rb b/app/models/registration.rb index 7873138a..d2baa036 100644 --- a/app/models/registration.rb +++ b/app/models/registration.rb @@ -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 diff --git a/app/models/registration_history.rb b/app/models/registration_history.rb index aa1186f6..768847cb 100644 --- a/app/models/registration_history.rb +++ b/app/models/registration_history.rb @@ -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 diff --git a/app/worker/queue_poller.rb b/app/worker/queue_poller.rb index 7f32f8de..6a67e457 100644 --- a/app/worker/queue_poller.rb +++ b/app/worker/queue_poller.rb @@ -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 diff --git a/app/worker/registration_processor.rb b/app/worker/registration_processor.rb index e5dca13b..a026c763 100644 --- a/app/worker/registration_processor.rb +++ b/app/worker/registration_processor.rb @@ -24,13 +24,15 @@ 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 @@ -38,12 +40,15 @@ def event_registration(competition_id, user_id, event_ids, comment, guests) 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? @@ -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 diff --git a/config/routes.rb b/config/routes.rb index aa5b66e4..75fdbdac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/lib/history.rb b/lib/history.rb index 79d5c871..897ec19a 100644 --- a/lib/history.rb +++ b/lib/history.rb @@ -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