Commit 785cbb79 authored by James Lopez's avatar James Lopez

refactor emails service

parent 0c8e7f49
......@@ -136,7 +136,7 @@ class Admin::UsersController < Admin::ApplicationController
# restore username to keep form action url.
user.username = params[:id]
format.html { render "edit" }
format.json { render json: result[:message], status: result[:status] }
format.json { render json: [result[:message]], status: result[:status] }
end
end
end
......@@ -152,11 +152,7 @@ class Admin::UsersController < Admin::ApplicationController
def remove_email
email = user.emails.find(params[:email_id])
Emails::DestroyService.new(current_user, self, email: email.email).execute
result = Users::UpdateService.new(current_user, @user).execute do |user|
user.update_secondary_emails!
end
Emails::DestroyService.new(current_user, user, email: email.email).execute
respond_to do |format|
if result[:status] == :success
......
......@@ -18,9 +18,7 @@ class Profiles::EmailsController < Profiles::ApplicationController
def destroy
@email = current_user.emails.find(params[:id])
Emails::DestroyService.new(self, self, email: @email.email).execute
Users::UpdateService.new(current_user, current_user).execute { |user| user.update_secondary_emails! }
Emails::DestroyService.new(current_user, current_user, email: @email.email).execute
respond_to do |format|
format.html { redirect_to profile_emails_url, status: 302 }
......
......@@ -496,8 +496,6 @@ class User < ActiveRecord::Base
if primary_email_record
Emails::DestroyService.new(self, self, email: email).execute
Emails::CreateService.new(self, self, email: email_was).execute
update_secondary_emails!
end
end
......
......@@ -3,7 +3,17 @@ module Emails
def execute(skip_authorization: false)
raise Gitlab::Access::AccessDeniedError unless skip_authorization || can_manage_emails?
Email.find_by_email(@email).destroy
Email.find_by_email(@email).destroy && update_secondary_emails!
end
private
def update_secondary_emails!
result = ::Users::UpdateService.new(@current_user, @current_user).execute do |user|
user.update_secondary_emails!
end
result[:status] == 'success'
end
end
end
......@@ -275,10 +275,6 @@ module API
not_found!('Email') unless email
Emails::DestroyService.new(current_user, user, email: email.email).execute(skip_authorization: true)
::Users::UpdateService.new(current_user, user).execute do |user|
user.update_secondary_emails!
end
end
desc 'Delete a user. Available only for admins.' do
......@@ -509,10 +505,6 @@ module API
not_found!('Email') unless email
Emails::DestroyService.new(current_user, current_user, email: email.email).execute
::Users::UpdateService.new(current_user, current_user).execute do |user|
user.update_secondary_emails!
end
end
desc 'Get a list of user activities'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment