|
| 1 | +class WorkshopInvitationController < ApplicationController |
| 2 | + include WorkshopInvitationConcerns |
| 3 | + |
| 4 | + # NOTE: This controller handles workshop invitations (WorkshopInvitation model). |
| 5 | + # It provides accept/reject RSVP actions for workshop attendees via token-based links. |
| 6 | + # Routes: /invitation/:token (legacy) and /workshop_invitation/:token |
| 7 | + |
| 8 | + def show |
| 9 | + @announcements = @invitation.member.announcements.active |
| 10 | + @tutorial_titles = Tutorial.all_titles |
| 11 | + |
| 12 | + @workshop = WorkshopPresenter.decorate(@invitation.workshop) |
| 13 | + |
| 14 | + render plain: @workshop.attendees_csv if request.format.csv? |
| 15 | + end |
| 16 | + |
| 17 | + def update |
| 18 | + @invitation.assign_attributes(invitation_params.merge!(attending: true, rsvp_time: Time.zone.now)) |
| 19 | + return back_with_message(@invitation.errors.full_messages) unless @invitation.valid? |
| 20 | + |
| 21 | + @invitation.update(invitation_params) |
| 22 | + back_with_message(t('messages.invitations.updated_details')) |
| 23 | + end |
| 24 | + |
| 25 | + # Inline accept from InvitationControllerConcerns |
| 26 | + def accept |
| 27 | + user = current_user || @invitation.member |
| 28 | + workshop = @invitation.workshop |
| 29 | + return back_with_message(t('messages.already_rsvped')) if @invitation.attending? |
| 30 | + |
| 31 | + @invitation.assign_attributes(invitation_params.merge!(attending: true, rsvp_time: Time.zone.now)) |
| 32 | + return back_with_message(@invitation.errors.full_messages) unless @invitation.valid? |
| 33 | + |
| 34 | + if user.has_existing_RSVP_on(workshop.date_and_time) |
| 35 | + return back_with_message(t('messages.invitations.rsvped_to_other_workshop')) |
| 36 | + end |
| 37 | + |
| 38 | + return back_with_message(t('messages.already_invited')) if attending_or_waitlisted?(workshop, user) |
| 39 | + |
| 40 | + @workshop = WorkshopPresenter.decorate(@invitation.workshop) |
| 41 | + if available_spaces?(@workshop, @invitation) |
| 42 | + @invitation.update(invitation_params.merge!(attending: true, rsvp_time: Time.zone.now)) |
| 43 | + @workshop.send_attending_email(@invitation) |
| 44 | + |
| 45 | + back_with_message(t('messages.accepted_invitation', name: @invitation.member.name)) |
| 46 | + else |
| 47 | + back_with_message(t('messages.no_available_seats')) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + # Inline reject from InvitationControllerConcerns |
| 52 | + def reject |
| 53 | + @workshop = WorkshopPresenter.decorate(@invitation.workshop) |
| 54 | + if @invitation.workshop.date_and_time - 3.5.hours >= Time.zone.now |
| 55 | + if @invitation.attending.eql? false |
| 56 | + redirect_back(fallback_location: invitation_path(@invitation), |
| 57 | + notice: t('messages.not_attending_already')) |
| 58 | + else |
| 59 | + @invitation.update_attribute(:attending, false) |
| 60 | + |
| 61 | + next_spot = WaitingList.next_spot(@invitation.workshop, @invitation.role) |
| 62 | + |
| 63 | + if next_spot.present? |
| 64 | + invitation = next_spot.invitation |
| 65 | + next_spot.destroy |
| 66 | + invitation.update(attending: true, rsvp_time: Time.zone.now, automated_rsvp: true) |
| 67 | + @workshop.send_attending_email(invitation, true) |
| 68 | + end |
| 69 | + |
| 70 | + redirect_back( |
| 71 | + fallback_location: invitation_path(@invitation), |
| 72 | + notice: t('messages.rejected_invitation', name: @invitation.member.name) |
| 73 | + ) |
| 74 | + end |
| 75 | + else |
| 76 | + redirect_back( |
| 77 | + fallback_location: invitation_path(@invitation), |
| 78 | + notice: 'You can only change your RSVP status up to 3.5 hours before the workshop' |
| 79 | + ) |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + private |
| 84 | + |
| 85 | + def invitation_params |
| 86 | + if params.key?(:workshop_invitation) |
| 87 | + params.require(:workshop_invitation).permit(:tutorial, :note) |
| 88 | + else |
| 89 | + {} |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + def available_spaces?(workshop, invitation) |
| 94 | + (invitation.role.eql?('Student') && workshop.student_spaces?) || |
| 95 | + (invitation.role.eql?('Coach') && workshop.coach_spaces?) |
| 96 | + end |
| 97 | + |
| 98 | + # Inline from InvitationControllerConcerns |
| 99 | + def attending_or_waitlisted?(workshop, user) |
| 100 | + workshop.attendee?(user) || workshop.waitlisted?(user) |
| 101 | + end |
| 102 | + |
| 103 | + def token |
| 104 | + params[:id] |
| 105 | + end |
| 106 | +end |
0 commit comments