Commit 7367431d authored by James Lopez's avatar James Lopez

refactor emails service

parent 40c2dcb3
module Emails
class BaseService
def initialize(current_user, user, opts)
@current_user = current_user
def initialize(user, opts)
@user = user
@email = opts[:email]
end
def log_audit_event(options = {})
AuditEventService.new(@current_user, @user, options)
.for_email(@email).security_event
end
end
end
module Emails
class CreateService < ::Emails::BaseService
def execute
email = @user.emails.create(email: @email)
log_audit_event(action: :create)
prepend EE::Emails::CreateService
email
def execute
@user.emails.create(email: @email)
end
end
end
module Emails
class DestroyService < ::Emails::BaseService
prepend EE::Emails::DestroyService
def execute
Email.find_by_email!(@email).destroy && update_secondary_emails!
log_audit_event(action: :destroy)
end
private
......
module EE
module Emails
module BaseService
def initialize(current_user, user, opts)
@current_user = current_user
super(user, opts)
end
private
def log_audit_event(options = {})
AuditEventService.new(@current_user, @user, options)
.for_email(@email).security_event
end
end
end
end
module EE
module Emails
module CreateService
include EE::Emails::BaseService
def execute
email = super
log_audit_event(action: :create) if email.persisted?
email
end
end
end
end
module EE
module Emails
module DestroyService
include EE::Emails::BaseService
def execute
result = super
log_audit_event(action: :destroy)
result
end
end
end
end
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