Skip to content

Commit

Permalink
refactor: Invites::Create and Invites::Destroy services
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellberg committed Apr 11, 2024
1 parent 68dc3ea commit 241b203
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 31 deletions.
52 changes: 28 additions & 24 deletions app/controllers/accounts/invitations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
class Accounts::InvitationsController < ApplicationController
def index
@invitations = current_account.account_invitations.pending
@account = current_account
end
module Accounts
class InvitationsController < KiqrController
def index
@invitations = current_account.account_invitations.pending
@account = current_account
end

def new
@invitation = current_account.account_invitations.new
end
def new
@invitation = current_account.account_invitations.new
end

def create
@invitation = current_account.account_invitations.new(invitation_params)

def create
@invitation = current_account.account_invitations.new(invitation_params)
if @invitation.save
AccountMailer.invitation_email(@invitation).deliver_later
redirect_to invitations_path(account_id: current_account), notice: t(".invitation_sent", email: @invitation.email)
else
render :new, status: :unprocessable_entity
if @invitation.valid?
Kiqr::Services::Invitations::Create.call!(invitation: @invitation, user: current_user)
kiqr_flash_message(:notice, :invitation_sent, email: @invitation.email)
redirect_to invitations_path(account_id: current_account)
else
render :new, status: :unprocessable_entity
end
end
end

def destroy
@invitation = current_account.account_invitations.find_puid!(params[:id])
@invitation.destroy
redirect_to invitations_path, notice: t(".deleted")
end
def destroy
@invitation = current_account.account_invitations.find_puid!(params[:id])
@invitation.destroy
redirect_to invitations_path, notice: t(".deleted")
end

private
private

def invitation_params
params.require(:account_invitation).permit(:email)
def invitation_params
params.require(:account_invitation).permit(:email)
end
end
end
4 changes: 2 additions & 2 deletions gems/kiqr/app/controllers/kiqr_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class KiqrController < ApplicationController
private

def kiqr_flash_message(type, message)
flash[type] = I18n.t("kiqr.flash_messages.#{message}")
def kiqr_flash_message(type, message, **)
flash[type] = I18n.t("kiqr.flash_messages.#{message}", **)
end
end
File renamed without changes.
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 @@ -3,6 +3,7 @@ en:
flash_messages:
account_created: "Your account has been created successfully."
account_updated: "Your account has been updated successfully."
invitation_sent: "An invitation email has been sent to %{email}."
accounts:
new:
title: "Create a new team account"
Expand Down
5 changes: 5 additions & 0 deletions gems/kiqr/lib/kiqr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ module Accounts
autoload :Create, "kiqr/services/accounts/create"
autoload :Update, "kiqr/services/accounts/update"
end

module Invitations
autoload :Create, "kiqr/services/invitations/create"
autoload :Destroy, "kiqr/services/invitations/destroy"
end
end

def self.config
Expand Down
22 changes: 22 additions & 0 deletions gems/kiqr/lib/kiqr/services/invitations/create.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Kiqr
module Services
module Invitations
class Create < Kiqr::ApplicationService
def call(invitation:, user:)
@invitation, @user = invitation, user

invitation.transaction do
invitation.save!
# @todo: AccountMailer.invitation_email(invitation).deliver_later
end

success invitation
end

private

attr_reader :invitation, :user
end
end
end
end
21 changes: 21 additions & 0 deletions gems/kiqr/lib/kiqr/services/invitations/destroy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Kiqr
module Services
module Invitations
class Destroy < Kiqr::ApplicationService
def call(invitation:, user:)
@invitation, @user = invitation, user

invitation.transaction do
invitation.destroy!
end

success invitation
end

private

attr_reader :invitation, :user
end
end
end
end
File renamed without changes.
6 changes: 6 additions & 0 deletions gems/kiqr/test/factories/account_invitation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :account_invitation do
account { create(:account) }
email { Faker::Internet.email }
end
end
4 changes: 2 additions & 2 deletions gems/kiqr/test/services/accounts/create_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def setup
test "creates team account" do
user = create(:user)
account = build(:account)

@service.call(account:, user:, personal: false)

refute_empty account.account_users, "Expected account_users not to be empty"
assert account.account_users.find_by(user: user).owner, "Expected account_users to have owner set to true"
refute account.personal, "Expected account.personal to be false"
Expand All @@ -22,8 +22,8 @@ def setup
test "creates personal account" do
user = create(:user, personal_account: nil)
account = build(:account)

@service.call(account:, user:, personal: true)

assert_empty account.account_users, "Expected account_users to be empty"
assert account.personal, "Expected account.personal to be true"
assert_equal account, user.personal_account, "Expected user to have personal account"
Expand Down
34 changes: 34 additions & 0 deletions gems/kiqr/test/services/invitations/create_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require "test_helper"

module Kiqr
module Services
module Invitations
class CreateTest < ActionDispatch::IntegrationTest
def setup
@service = Kiqr::Services::Invitations::Create.new
end

test "creates invitation" do
account = create(:account)
user = create(:user, with_account: account)
invitation = account.account_invitations.new(email: "[email protected]")
@service.call(invitation: invitation, user: user)

assert invitation.persisted?, "Expected invitation to be saved"
assert_equal invitation.account, account, "Expected invitation to be associated with account"
assert account.account_invitations.find_by(email: "[email protected]"), "Expected to find an invitation with the specified email"
end

test "invitation with invalid attributes raises error" do
account = create(:account)
user = create(:user, with_account: account)
invitation = account.account_invitations.new(email: "foobar")

assert_raises StandardError do
@service.call(invitation: invitation, user: user)
end
end
end
end
end
end
22 changes: 22 additions & 0 deletions gems/kiqr/test/services/invitations/destroy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "test_helper"

module Kiqr
module Services
module Invitations
class DestroyTest < ActionDispatch::IntegrationTest
def setup
@service = Kiqr::Services::Invitations::Destroy.new
end

test "destroys invitation" do
account = create(:account)
user = create(:user, with_account: account)
invitation = create(:account_invitation, account: account)
@service.call(invitation: invitation, user: user)

refute AccountInvitation.find_by_id(invitation.id)
end
end
end
end
end
4 changes: 1 addition & 3 deletions test/controllers/users/invitations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ class Users::InvitationsControllerTest < ActionDispatch::IntegrationTest
invitation = create(:account_invitation)
sign_in user

assert_no_difference -> { user.accounts.count } do
delete user_invitation_path(invitation)
end
delete user_invitation_path(invitation)

assert_redirected_to dashboard_path
assert_nil AccountInvitation.find_by(id: invitation.id)
Expand Down

0 comments on commit 241b203

Please sign in to comment.