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
17 changes: 12 additions & 5 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ def rsvp

ticket = Services::Ticket.new(request, params)
member = Member.find_by(email: ticket.email)
invitation = member.invitations.where(event: @event, role: 'Student').first
invitation ||= Invitation.create_or_find_by(event: @event, member:, role: 'Student')
invitation = find_or_create_invitation(@event, member, 'Student')

invitation.update(attending: true)
head :ok
Expand All @@ -65,9 +64,17 @@ def latest_model_updated

def find_invitation_and_redirect_to_event(role)
set_event
invitation = Invitation.create_or_find_by(event: @event, member: current_user, role:)
invitation = Invitation.find_by(event: @event, member: current_user, role:) unless invitation.persisted?
redirect_to event_invitation_path(@event, invitation)
redirect_to event_invitation_path(@event, find_or_create_invitation(@event, current_user, role))
end

def find_or_create_invitation(event, member, role)
# Identity is event + member, matching InvitationManager; the member's
# role choice wins, so an existing invitation with the other role is updated.
invitation = Invitation.find_or_create_by!(event:, member:) { |record| record.role = role }
invitation.update!(role:) unless invitation.role.eql?(role)
invitation
rescue ActiveRecord::RecordNotUnique
Invitation.find_by(event:, member:)
end

def set_event
Expand Down
11 changes: 7 additions & 4 deletions app/controllers/workshops_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ def find_attending_invitation(workshop, user)
end

def find_or_create_invitation(workshop, user, role)
invitation = WorkshopInvitation.create_or_find_by(workshop:,
member: user,
role:)
invitation.persisted? ? invitation : WorkshopInvitation.find_by(workshop:, member: user, role:)
# Identity is workshop + member, matching InvitationManager; the member's
# role choice wins, so an existing invitation with the other role is updated.
invitation = WorkshopInvitation.find_or_create_by!(workshop:, member: user) { |record| record.role = role }
invitation.update!(role:) unless invitation.role.eql?(role)
invitation
rescue ActiveRecord::RecordNotUnique
WorkshopInvitation.find_by(workshop:, member: user)
end

def user_attending_or_waitlisted?(workshop, user)
Expand Down
4 changes: 3 additions & 1 deletion app/mailers/event_invitation_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def invite_coach(event, member, invitation)
@member = member
@invitation = invitation
@host_address = AddressPresenter.new(@event.venue.address) if @event.venue.present?
@everyone_is_invited = !event.audience
# Coach emails are labelled as such only when the event is actually for
# coaches; blank or missing audience means a general invitation.
@everyone_is_invited = !event.audience.eql?('Coaches')

mail_to_member(member, @everyone_is_invited ? "Invitation: #{@event.name}" : "Coach Invitation: #{@event.name}",
&:html)
Expand Down
6 changes: 4 additions & 2 deletions app/services/invitation_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,16 @@ def chapter_coaches(chapter)
end

def create_invitation(workshop, member, role)
WorkshopInvitation.find_or_create_by!(workshop:, member:, role:)
# Identity is workshop + member; role applies only on create so a member
# subscribed as both student and coach gets one invite, not one per role.
WorkshopInvitation.find_or_create_by!(workshop:, member:) { |invitation| invitation.role = role }
rescue StandardError => e
log_invitation_failure(workshop, member, role, e)
nil
end

def create_event_invitation(event, member, role)
Invitation.find_or_create_by!(event:, member:, role:)
Invitation.find_or_create_by!(event:, member:) { |invitation| invitation.role = role }
rescue StandardError => e
log_event_meeting_invitation_failure("event_id=#{event.id}", member, e)
nil
Expand Down
91 changes: 91 additions & 0 deletions spec/controllers/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@
expect(response).to redirect_to(event_invitation_path(event, invitation))
end
end

context 'when the member has a coach invitation for the event' do
let!(:invitation) { Fabricate(:coach_invitation, event:, member:, attending: nil) }

it 'does not create a second invitation' do
expect do
get :student, params: { event_id: event.slug }
end.not_to change(Invitation, :count)
end

it 'updates the existing invitation to the chosen role' do
get :student, params: { event_id: event.slug }

expect(invitation.reload.role).to eql('Student')
end

it 'redirects to the existing invitation page' do
get :student, params: { event_id: event.slug }

expect(response).to redirect_to(event_invitation_path(event, invitation.reload))
end
end
end

describe 'GET #coach' do
Expand Down Expand Up @@ -87,9 +109,78 @@
expect(response).to redirect_to(event_invitation_path(event, invitation))
end
end

context 'when the member has a student invitation for the event' do
let!(:invitation) { Fabricate(:invitation, event:, member:, role: 'Student', attending: nil) }

it 'does not create a second invitation' do
expect do
get :coach, params: { event_id: event.slug }
end.not_to change(Invitation, :count)
end

it 'updates the existing invitation to the chosen role' do
get :coach, params: { event_id: event.slug }

expect(invitation.reload.role).to eql('Coach')
end

it 'redirects to the existing invitation page' do
get :coach, params: { event_id: event.slug }

expect(response).to redirect_to(event_invitation_path(event, invitation.reload))
end
end
end

describe 'POST #rsvp' do
let(:event) { Fabricate(:event) }
let(:member) { Fabricate(:member) }
let(:ticket_params) { { event_id: event.slug, email: member.email } }

context 'when the member does not have an invitation for the event' do
it 'creates a student invitation and marks it attending' do
expect do
post :rsvp, params: ticket_params
end.to change(Invitation, :count).by(1)

invitation = Invitation.last
expect(invitation.role).to eql('Student')
expect(invitation.attending).to be(true)
end
end

context 'when the member has a coach invitation for the event' do
let!(:invitation) { Fabricate(:coach_invitation, event:, member:, attending: nil) }

it 'does not create a second invitation' do
expect do
post :rsvp, params: ticket_params
end.not_to change(Invitation, :count)
end

it 'updates the invitation to the student role and marks it attending' do
post :rsvp, params: ticket_params

expect(invitation.reload.role).to eql('Student')
expect(invitation.attending).to be(true)
end
end

context 'when the member already has a student invitation for the event' do
let!(:invitation) { Fabricate(:invitation, event:, member:, role: 'Student', attending: nil) }

it 'marks the existing invitation attending without creating a new one' do
expect do
post :rsvp, params: ticket_params
end.not_to change(Invitation, :count)

expect(invitation.reload.attending).to be(true)
end
end
end

describe 'POST #rsvp (RSVP window enforcement)' do
let(:member) { Fabricate(:member) }
let(:event) { Fabricate(:event) }

Expand Down
15 changes: 15 additions & 0 deletions spec/controllers/workshops_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@
end
end

context 'when the member has an invitation with a different role' do
let!(:invitation) do
Fabricate(:workshop_invitation, workshop:, member:, role: 'Student', attending: nil)
end

it 'updates the existing invitation to the requested role instead of creating a second one' do
expect do
post :rsvp, params: { id: workshop.id, role: 'Coach' }
end.not_to change(WorkshopInvitation, :count)

expect(invitation.reload.role).to eq('Coach')
expect(response).to redirect_to(invitation_path(invitation))
end
end

context 'when the member does not have an invitation for the workshop and role' do
it 'creates a new invitation and redirects' do
expect do
Expand Down
9 changes: 9 additions & 0 deletions spec/mailers/event_invitation_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@
expect(email.body.encoded).to match('hello@codebar.io')
end

it 'sends a generic invitation if the event audience is blank' do
blank_audience_event = Fabricate(:event, name: 'Test event', audience: '')
blank_invitation = Fabricate(:invitation, event: blank_audience_event, member:)

described_class.invite_coach(blank_audience_event, member, blank_invitation).deliver_now

expect(email.subject).to eq("Invitation: #{blank_audience_event.name}")
end

it 'sends a coach invitation of the event is for coaches' do
email_subject = "Coach Invitation: #{event.name}"
described_class.invite_coach(coach_event, member, invitation).deliver_now
Expand Down
Loading
Loading