dawarich/app/controllers/family/invitations_controller.rb

77 lines
2.1 KiB
Ruby
Raw Normal View History

2025-09-27 07:23:33 -04:00
# frozen_string_literal: true
class Family::InvitationsController < ApplicationController
before_action :authenticate_user!, except: %i[show]
before_action :ensure_family_feature_enabled!, except: %i[show]
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
@invitation = Family::Invitation.find_by!(token: params[:token])
2025-09-27 07:23:33 -04:00
if @invitation.expired?
redirect_to root_path, alert: 'This invitation has expired.' and return
2025-09-27 07:23:33 -04:00
end
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