Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/invitation_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class InvitationLog < ApplicationRecord
belongs_to :loggable, polymorphic: true
belongs_to :initiator, class_name: 'Member', optional: true
belongs_to :chapter, optional: true
has_many :entries, class_name: 'InvitationLogEntry', dependent: :destroy
has_many :entries, class_name: 'InvitationLogEntry', dependent: :destroy, autosave: false

before_create :set_expires_at

Expand Down
14 changes: 6 additions & 8 deletions app/services/invitation_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def log_success(member, invitation = nil)
return unless @log

entry = find_or_build_entry(member, invitation, :success)
return entry if entry.persisted?
return entry if entry.processed_at

entry.assign_attributes(processed_at: Time.current)
save_entry(entry, :success_count)
Expand All @@ -33,7 +33,7 @@ def log_failure(member, invitation, error)
return unless @log

entry = find_or_build_entry(member, invitation, :failed)
return entry if entry.persisted?
return entry if entry.processed_at

entry.assign_attributes(
failure_reason: error.message,
Expand All @@ -46,7 +46,7 @@ def log_skipped(member, invitation, reason)
return unless @log

entry = find_or_build_entry(member, invitation, :skipped)
return entry if entry.persisted?
return entry if entry.processed_at

entry.assign_attributes(
failure_reason: reason,
Expand Down Expand Up @@ -78,11 +78,9 @@ def fail_batch(error)
private

def find_or_build_entry(member, invitation, status)
@log.entries.find_or_initialize_by(
member:,
invitation:,
status:
)
@log.entries.find_or_create_by(member: member, invitation: invitation) do |entry|
entry.status = status
end
end

def save_entry(entry, counter)
Expand Down
28 changes: 28 additions & 0 deletions spec/services/invitation_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,32 @@
expect(log.completed_at).to be_present
end
end

describe 'cross-status retry (PR #2558 fix)' do
let(:logger) { described_class.new(workshop, initiator, 'students', :invite) }
let!(:log) { logger.start_batch }

it 'returns existing failure entry when logging success for same member+invitation' do
error = StandardError.new('SMTP error')
failure_entry = logger.log_failure(member, invitation, error)

success_entry = logger.log_success(member, invitation)

expect(success_entry).to eq failure_entry
expect(success_entry.status).to eq 'failed'
expect(log.reload.success_count).to eq 0
expect(log.reload.failure_count).to eq 1
end

it 'returns existing success entry when logging failure for same member+invitation' do
logger.log_success(member, invitation)

error = StandardError.new('SMTP error')
failure_entry = logger.log_failure(member, invitation, error)

expect(failure_entry.status).to eq 'success'
expect(log.reload.success_count).to eq 1
expect(log.reload.failure_count).to eq 0
end
end
end