create.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::Channel::Email::Create < Service::Base
  3. def execute(inbound_configuration:, outbound_configuration:, group:, email_address:, email_realname:)
  4. ::Channel.create!(
  5. area: 'Email::Account',
  6. options: {
  7. inbound: inbound_configuration,
  8. outbound: outbound_configuration,
  9. },
  10. group: group,
  11. last_log_in: nil,
  12. last_log_out: nil,
  13. status_in: 'ok',
  14. status_out: 'ok',
  15. active: true,
  16. ).tap do |channel|
  17. set_email_address(channel:, email_address:, email_realname:)
  18. end
  19. end
  20. private
  21. def set_email_address(channel:, email_address:, email_realname:)
  22. address = if ::Channel.one?
  23. # on initial setup, use placeholder email address
  24. EmailAddress.first
  25. else
  26. # remember address && set channel for email address
  27. EmailAddress.find_by(email: email_address)
  28. end
  29. address ||= EmailAddress.new
  30. address.name = email_realname
  31. address.email = email_address
  32. address.active = true
  33. address.channel = channel
  34. address.save!
  35. end
  36. end