dawarich/app/controllers/family/memberships_controller.rb

71 lines
2.2 KiB
Ruby
Raw Normal View History

2025-09-27 07:23:33 -04:00
# frozen_string_literal: true
class Family::MembershipsController < ApplicationController
2025-09-27 07:23:33 -04:00
before_action :authenticate_user!
2025-09-28 07:10:07 -04:00
before_action :ensure_family_feature_enabled!
before_action :set_family, except: %i[create]
2025-10-04 10:17:26 -04:00
before_action :set_membership, only: %i[destroy]
before_action :set_invitation, only: %i[create]
def create
authorize @invitation, policy_class: Family::MembershipPolicy
service = Families::AcceptInvitation.new(
invitation: @invitation,
user: current_user
)
if service.call
redirect_to family_path, notice: 'Welcome to the family!'
else
redirect_to root_path, alert: service.error_message || 'Unable to accept invitation'
end
rescue Pundit::NotAuthorizedError
2025-10-22 14:39:02 -04:00
alert = case
when @invitation.expired? then 'This invitation is no longer valid or has expired'
when !@invitation.pending? then 'This invitation has already been processed'
when @invitation.email != current_user.email then 'This invitation is not for your email address'
else 'You are not authorized to accept this invitation'
end
redirect_to root_path, alert: alert
rescue StandardError => e
Rails.logger.error "Error accepting family invitation: #{e.message}"
2025-10-22 14:39:02 -04:00
redirect_to root_path, alert: 'An unexpected error occurred. Please try again later'
end
2025-09-27 07:23:33 -04:00
def destroy
authorize @membership
2025-10-04 16:39:47 -04:00
member_user = @membership.user
service = Families::Memberships::Destroy.new(user: current_user, member_to_remove: member_user)
if service.call
if member_user == current_user
redirect_to new_family_path, notice: 'You have left the family'
else
redirect_to family_path, notice: "#{member_user.email} has been removed from the family"
end
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 remove member'
2025-09-27 07:23:33 -04:00
end
end
private
def set_family
@family = current_user.family
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
def set_membership
@membership = @family.family_memberships.find(params[:id])
end
def set_invitation
@invitation = Family::Invitation.find_by!(token: params[:token])
end
2025-09-27 07:23:33 -04:00
end