From 6c9addf360e457183f4fd93612ce4572ad6da7f7 Mon Sep 17 00:00:00 2001 From: Rasmus Kjellberg <2277443+kjellberg@users.noreply.github.com> Date: Thu, 6 Jun 2024 12:37:28 +0200 Subject: [PATCH] chore: error classes & rescue from DeleteTeamOwnerError --- .../app/controllers/kiqr/account_users_controller.rb | 3 +++ gems/kiqr/app/controllers/kiqr/invitations_controller.rb | 2 +- gems/kiqr/app/models/account_user.rb | 2 +- gems/kiqr/config/locales/kiqr/en.yml | 1 + gems/kiqr/lib/kiqr.rb | 4 +++- gems/kiqr/lib/kiqr/errors/delete_team_owner_error.rb | 9 +++++++++ gems/kiqr/lib/kiqr/errors/invitation_expired_error.rb | 9 +++++++++ gems/kiqr/lib/kiqr/services/invitations/accept.rb | 2 +- test/models/account_user_test.rb | 4 ++-- 9 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 gems/kiqr/lib/kiqr/errors/delete_team_owner_error.rb create mode 100644 gems/kiqr/lib/kiqr/errors/invitation_expired_error.rb diff --git a/gems/kiqr/app/controllers/kiqr/account_users_controller.rb b/gems/kiqr/app/controllers/kiqr/account_users_controller.rb index bb48436..aad8a50 100644 --- a/gems/kiqr/app/controllers/kiqr/account_users_controller.rb +++ b/gems/kiqr/app/controllers/kiqr/account_users_controller.rb @@ -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 diff --git a/gems/kiqr/app/controllers/kiqr/invitations_controller.rb b/gems/kiqr/app/controllers/kiqr/invitations_controller.rb index 02d1e25..2f44f2e 100644 --- a/gems/kiqr/app/controllers/kiqr/invitations_controller.rb +++ b/gems/kiqr/app/controllers/kiqr/invitations_controller.rb @@ -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 diff --git a/gems/kiqr/app/models/account_user.rb b/gems/kiqr/app/models/account_user.rb index 6e71c4a..d4331ed 100644 --- a/gems/kiqr/app/models/account_user.rb +++ b/gems/kiqr/app/models/account_user.rb @@ -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 diff --git a/gems/kiqr/config/locales/kiqr/en.yml b/gems/kiqr/config/locales/kiqr/en.yml index 2a03b99..37d0e04 100644 --- a/gems/kiqr/config/locales/kiqr/en.yml +++ b/gems/kiqr/config/locales/kiqr/en.yml @@ -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." diff --git a/gems/kiqr/lib/kiqr.rb b/gems/kiqr/lib/kiqr.rb index 143baec..c4be727 100644 --- a/gems/kiqr/lib/kiqr.rb +++ b/gems/kiqr/lib/kiqr.rb @@ -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 diff --git a/gems/kiqr/lib/kiqr/errors/delete_team_owner_error.rb b/gems/kiqr/lib/kiqr/errors/delete_team_owner_error.rb new file mode 100644 index 0000000..ac1fc3e --- /dev/null +++ b/gems/kiqr/lib/kiqr/errors/delete_team_owner_error.rb @@ -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 diff --git a/gems/kiqr/lib/kiqr/errors/invitation_expired_error.rb b/gems/kiqr/lib/kiqr/errors/invitation_expired_error.rb new file mode 100644 index 0000000..1cd49df --- /dev/null +++ b/gems/kiqr/lib/kiqr/errors/invitation_expired_error.rb @@ -0,0 +1,9 @@ +module Kiqr + module Errors + class InvitationExpiredError < StandardError + def initialize(msg = "Invitation has already expired") + super + end + end + end +end diff --git a/gems/kiqr/lib/kiqr/services/invitations/accept.rb b/gems/kiqr/lib/kiqr/services/invitations/accept.rb index 1f159a2..b327340 100644 --- a/gems/kiqr/lib/kiqr/services/invitations/accept.rb +++ b/gems/kiqr/lib/kiqr/services/invitations/accept.rb @@ -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 diff --git a/test/models/account_user_test.rb b/test/models/account_user_test.rb index 52d0257..81e1642 100644 --- a/test/models/account_user_test.rb +++ b/test/models/account_user_test.rb @@ -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