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

refactor emails service

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