2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-16 09:06:14 -04:00
|
|
|
module Emails
|
2017-06-19 08:51:46 -04:00
|
|
|
class DestroyService < ::Emails::BaseService
|
2017-10-01 11:07:26 -04:00
|
|
|
def execute(email)
|
2021-11-03 20:12:36 -04:00
|
|
|
raise StandardError, 'Cannot delete primary email' if email.user_primary_email?
|
|
|
|
|
2021-08-19 05:10:34 -04:00
|
|
|
email.destroy && update_secondary_emails!(email.email)
|
2017-06-22 02:55:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-08-19 05:10:34 -04:00
|
|
|
def update_secondary_emails!(deleted_email)
|
2017-09-27 05:48:33 -04:00
|
|
|
result = ::Users::UpdateService.new(@current_user, user: @user).execute do |user|
|
2021-08-19 05:10:34 -04:00
|
|
|
user.unset_secondary_emails_matching_deleted_email!(deleted_email)
|
2017-06-22 02:55:07 -04:00
|
|
|
end
|
|
|
|
|
2020-04-21 11:21:10 -04:00
|
|
|
result[:status] == :success
|
2017-06-16 09:06:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Emails::DestroyService.prepend_mod_with('Emails::DestroyService')
|