Skip to content

Commit

Permalink
chore: error classes & rescue from DeleteTeamOwnerError
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellberg committed Jun 6, 2024
1 parent 0295eee commit 6c9addf
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 6 deletions.
3 changes: 3 additions & 0 deletions gems/kiqr/app/controllers/kiqr/account_users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def destroy
@account_user.destroy!
kiqr_flash_message(:alert, :account_user_destroyed)
redirect_to account_users_path
rescue Kiqr::Errors::DeleteTeamOwnerError
kiqr_flash_message(:alert, :account_user_is_owner)
redirect_to edit_account_user_path(@account_user)
end

private
Expand Down
2 changes: 1 addition & 1 deletion gems/kiqr/app/controllers/kiqr/invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def accept
Kiqr::Services::Invitations::Accept.call!(invitation: @invite, user: current_user)
kiqr_flash_message(:notice, :invitation_accepted, account: @invite.account.name)
redirect_to dashboard_or_root_path(account_id: @invite.account)
rescue Kiqr::Errors::InvitationExpired
rescue Kiqr::Errors::InvitationExpiredError
kiqr_flash_message(:alert, :invitation_expired)
redirect_back(fallback_location: dashboard_or_root_path)
end
Expand Down
2 changes: 1 addition & 1 deletion gems/kiqr/app/models/account_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AccountUser < ApplicationRecord # This will generate a public_uid for the

# Prevent the deletion of account owners.
def destroy
return if owner?
raise Kiqr::Errors::DeleteTeamOwnerError if owner?
super
end
end
1 change: 1 addition & 0 deletions gems/kiqr/config/locales/kiqr/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ en:
invitation_rejected: "You have rejected the invitation to join %{account}."
invitation_expired: "The invitation has expired."
account_user_destroyed: "Member has successfully been removed from the team."
account_user_is_owner: "Can't remove the owner of the team."
omniauth_email_taken: "An account with this email already exists. Please sign in to that account first and then link your %{provider} account."
settings_updated: "Your settings have been updated successfully."
cant_cancel_while_team_owner: "Can't cancel your user account while owner of a team."
Expand Down
4 changes: 3 additions & 1 deletion gems/kiqr/lib/kiqr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ module Users

module Errors
# Raised when an invitation has expired
class InvitationExpired < StandardError; end
# class InvitationExpired < StandardError; end
autoload :InvitationExpiredError, "kiqr/errors/invitation_expired_error"
autoload :DeleteTeamOwnerError, "kiqr/errors/delete_team_owner_error"
end

def self.config
Expand Down
9 changes: 9 additions & 0 deletions gems/kiqr/lib/kiqr/errors/delete_team_owner_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Kiqr
module Errors
class DeleteTeamOwnerError < StandardError
def initialize(msg = "Can't delete the owner of a team")
super
end
end
end
end
9 changes: 9 additions & 0 deletions gems/kiqr/lib/kiqr/errors/invitation_expired_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Kiqr
module Errors
class InvitationExpiredError < StandardError
def initialize(msg = "Invitation has already expired")
super
end
end
end
end
2 changes: 1 addition & 1 deletion gems/kiqr/lib/kiqr/services/invitations/accept.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def call(invitation:, user:)
@invitation, @user = invitation, user
@account = @invitation.account

raise Kiqr::Errors::InvitationExpired, "Invitation has already been used" if invitation.accepted_at?
raise Kiqr::Errors::InvitationExpiredError, "Invitation has already been used" if invitation.accepted_at?

account.transaction do
invitation.transaction do
Expand Down
4 changes: 2 additions & 2 deletions test/models/account_user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class AccountUserTest < ActiveSupport::TestCase
account = create(:account, name: "Team account")
account.account_users << AccountUser.create(user: user, owner: true)

assert_no_difference -> { AccountUser.count } do
account.account_users.first.destroy
assert_raise Kiqr::Errors::DeleteTeamOwnerError do
account.account_users.first.destroy!
end
end
end

0 comments on commit 6c9addf

Please sign in to comment.