2025-09-27 07:23:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2025-10-04 14:48:44 -04:00
|
|
|
class Family::InvitationsController < ApplicationController
|
2025-10-05 13:40:42 -04:00
|
|
|
before_action :authenticate_user!, except: %i[show]
|
|
|
|
|
before_action :ensure_family_feature_enabled!, except: %i[show]
|
2025-10-07 12:38:06 -04:00
|
|
|
before_action :set_family, except: %i[show]
|
2025-10-04 16:39:47 -04:00
|
|
|
before_action :set_invitation_by_id_and_family, only: %i[destroy]
|
2025-09-27 07:23:33 -04:00
|
|
|
|
|
|
|
|
def index
|
|
|
|
|
authorize @family, :show?
|
|
|
|
|
|
|
|
|
|
@pending_invitations = @family.family_invitations.active
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def show
|
2025-10-07 12:38:06 -04:00
|
|
|
@invitation = Family::Invitation.find_by!(token: params[:token])
|
2025-10-05 08:24:45 -04:00
|
|
|
|
2025-09-27 07:23:33 -04:00
|
|
|
if @invitation.expired?
|
2025-09-28 14:53:50 -04:00
|
|
|
redirect_to root_path, alert: 'This invitation has expired.' and return
|
2025-09-27 07:23:33 -04:00
|
|
|
end
|
|
|
|
|
|
2025-09-28 14:53:50 -04:00
|
|
|
unless @invitation.pending?
|
|
|
|
|
redirect_to root_path, alert: 'This invitation is no longer valid.' and return
|
|
|
|
|
end
|
2025-09-27 07:23:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
|
|
|
|
authorize @family, :invite?
|
|
|
|
|
|
|
|
|
|
service = Families::Invite.new(
|
|
|
|
|
family: @family,
|
|
|
|
|
email: invitation_params[:email],
|
|
|
|
|
invited_by: current_user
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if service.call
|
2025-10-04 16:39:47 -04:00
|
|
|
redirect_to family_path, notice: 'Invitation sent successfully!'
|
2025-09-27 07:23:33 -04:00
|
|
|
else
|
2025-10-04 16:39:47 -04:00
|
|
|
redirect_to family_path, alert: service.error_message || 'Failed to send invitation'
|
2025-09-27 07:23:33 -04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
|
authorize @family, :manage_invitations?
|
|
|
|
|
|
2025-10-04 14:31:36 -04:00
|
|
|
begin
|
|
|
|
|
if @invitation.update(status: :cancelled)
|
2025-10-04 16:39:47 -04:00
|
|
|
redirect_to family_path, notice: 'Invitation cancelled'
|
2025-10-04 14:31:36 -04:00
|
|
|
else
|
2025-10-04 16:39:47 -04:00
|
|
|
redirect_to family_path, alert: 'Failed to cancel invitation. Please try again'
|
2025-10-04 14:31:36 -04:00
|
|
|
end
|
|
|
|
|
rescue StandardError => e
|
|
|
|
|
Rails.logger.error "Error cancelling family invitation: #{e.message}"
|
2025-10-04 16:39:47 -04:00
|
|
|
redirect_to family_path, alert: 'An unexpected error occurred while cancelling the invitation'
|
2025-09-28 07:10:07 -04:00
|
|
|
end
|
2025-09-27 07:23:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def set_family
|
|
|
|
|
@family = current_user.family
|
2025-09-27 15:14:40 -04:00
|
|
|
|
2025-10-04 16:39:47 -04:00
|
|
|
redirect_to new_family_path, alert: 'You are not in a family' and return unless @family
|
2025-09-27 07:23:33 -04:00
|
|
|
end
|
|
|
|
|
|
2025-10-04 16:39:47 -04:00
|
|
|
def set_invitation_by_id_and_family
|
|
|
|
|
# For authenticated nested routes: /families/:family_id/invitations/:id
|
|
|
|
|
# The :id param contains the token value
|
|
|
|
|
@family = current_user.family
|
|
|
|
|
@invitation = @family.family_invitations.find_by!(token: params[:id])
|
2025-09-28 12:50:02 -04:00
|
|
|
end
|
|
|
|
|
|
2025-09-27 07:23:33 -04:00
|
|
|
def invitation_params
|
|
|
|
|
params.require(:family_invitation).permit(:email)
|
|
|
|
|
end
|
|
|
|
|
end
|