Skip to content
Open
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
3 changes: 3 additions & 0 deletions app/controllers/admin/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def update
def invite
@event = Event.find_by(slug: params[:event_id])
authorize @event
unless @event.invitable?
return redirect_to admin_event_path(@event), flash: { warning: t('admin.event.not_invitable') }
end

@event.chapters.each do |chapter|
InvitationManager.new.send_event_emails(@event, chapter, current_user.id)
Expand Down
19 changes: 13 additions & 6 deletions app/controllers/admin/workshops_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,13 @@ def attendees_emails
def invite
set_workshop
authorize @workshop
audience = params[:for]

if @workshop.virtual?
InvitationManager.new.send_virtual_workshop_emails(@workshop, audience, current_user.id)
else
InvitationManager.new.send_workshop_emails(@workshop, audience, current_user.id)
unless @workshop.invitable?
return redirect_to admin_workshop_path(@workshop), flash: { warning: t('admin.workshop.not_invitable') }
end

audience = params[:for]
send_workshop_invitations(audience)

redirect_to admin_workshop_path(@workshop), notice: "Invitations to #{audience} are being emailed out."
end

Expand Down Expand Up @@ -162,6 +161,14 @@ def destroy_host

private

def send_workshop_invitations(audience)
if @workshop.virtual?
InvitationManager.new.send_virtual_workshop_emails(@workshop, audience, current_user.id)
else
InvitationManager.new.send_workshop_emails(@workshop, audience, current_user.id)
end
end

def resolve_chapter_name_to_id
chapter_value = params.dig(:workshop, :chapter_id)
return if chapter_value.blank? || chapter_value.match?(/\A\d+\z/)
Expand Down
6 changes: 3 additions & 3 deletions app/services/invitation_manager.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class InvitationManager
def send_event_emails(event, chapter, initiator_id = nil) # rubocop:disable Metrics/AbcSize
return 'The event is not invitable' unless event.invitable?
return unless event.invitable?

audience = event_audience(event)
logger = invitation_logger(event, initiator_id, audience, chapter.id)
Expand Down Expand Up @@ -51,7 +51,7 @@ def send_workshop_attendance_reminders(workshop)
handle_asynchronously :send_workshop_attendance_reminders

def send_workshop_emails(workshop, audience, initiator_id = nil)
return 'The workshop is not invitable' unless workshop.invitable?
return unless workshop.invitable?

logger = invitation_logger(workshop, initiator_id, audience, workshop.chapter_id)
result = start_invitation_batch(logger)
Expand All @@ -70,7 +70,7 @@ def send_workshop_emails(workshop, audience, initiator_id = nil)
handle_asynchronously :send_workshop_emails

def send_virtual_workshop_emails(workshop, audience, initiator_id = nil)
return 'The workshop is not invitable' unless workshop.invitable?
return unless workshop.invitable?

logger = invitation_logger(workshop, initiator_id, audience, workshop.chapter_id)
result = start_invitation_batch(logger)
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,8 @@ en:
members:
activity:
for: "for %{subject}"
event:
not_invitable: "The event is not invitable"
workshop:
manage_rsvps:
text: "RSVP Students and Coaches to the workshop. The attendance confirmation email will be triggered and they will be removed from the waiting list."
Expand Down
16 changes: 16 additions & 0 deletions spec/controllers/admin/events_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@

expect(flash[:notice]).to eq('Invitations will be emailed out soon.')
end

context 'when the event is not invitable' do
before { event.update_column(:invitable, false) }

it 'warns instead of claiming invitations are on their way, and enqueues nothing' do
manager = instance_spy(InvitationManager)
allow(InvitationManager).to receive(:new).and_return(manager)

post :invite, params: { event_id: event.slug }

expect(manager).not_to have_received(:send_event_emails)
expect(response).to redirect_to(admin_event_path(event))
expect(flash[:warning]).to eq('The event is not invitable')
expect(flash[:notice]).to be_nil
end
end
end

describe 'POST #invite with unauthorised user' do
Expand Down
37 changes: 37 additions & 0 deletions spec/controllers/admin/workshops_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,43 @@ def assigns(symbol)
controller.instance_variable_get("@#{symbol}")
end

describe 'POST #invite' do
let(:manager) { instance_spy(InvitationManager) }

before { allow(InvitationManager).to receive(:new).and_return(manager) }

it 'enqueues the invitation emails and sets a notice' do
post :invite, params: { workshop_id: workshop.id, for: 'students' }

expect(manager).to have_received(:send_workshop_emails).with(workshop, 'students', admin.id)
expect(response).to redirect_to(admin_workshop_path(workshop))
expect(flash[:notice]).to eq('Invitations to students are being emailed out.')
end

it 'enqueues the virtual invitation emails for a virtual workshop' do
virtual_workshop = Fabricate(:virtual_workshop, chapter: workshop.chapter)

post :invite, params: { workshop_id: virtual_workshop.id, for: 'students' }

expect(manager).to have_received(:send_virtual_workshop_emails).with(virtual_workshop, 'students', admin.id)
expect(manager).not_to have_received(:send_workshop_emails)
end

context 'when the workshop is not invitable' do
before { workshop.update_column(:invitable, false) }

it 'warns instead of claiming invitations are on their way, and enqueues nothing' do
post :invite, params: { workshop_id: workshop.id, for: 'students' }

expect(manager).not_to have_received(:send_workshop_emails)
expect(manager).not_to have_received(:send_virtual_workshop_emails)
expect(response).to redirect_to(admin_workshop_path(workshop))
expect(flash[:warning]).to eq('The workshop is not invitable')
expect(flash[:notice]).to be_nil
end
end
end

describe 'GET #show' do
it 'loads the workshop attendance page with attendees' do
Fabricate(:workshop_invitation, workshop:, attending: true)
Expand Down
Loading