base_configuration.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Gql::Mutations
  3. class Channel::Email::BaseConfiguration < BaseMutation
  4. description 'Base class for configuration mutations'
  5. def self.authorize(_obj, ctx)
  6. ctx.current_user.permissions?('admin.channel_email')
  7. end
  8. protected
  9. def map_type_to_config(type)
  10. {
  11. adapter: type.adapter,
  12. options: type.to_h.except(:adapter)
  13. }
  14. end
  15. def map_probe_result(probe_result, field_prefix:)
  16. if probe_result[:result] == 'ok'
  17. return { success: true }.tap do |result|
  18. result[:mailbox_stats] = probe_result.slice(:content_messages, :archive_possible, :archive_possible_is_fallback, :archive_week_range) if probe_result[:content_messages]
  19. end
  20. end
  21. { success: false, errors: map_probe_errors_to_user_errors(probe_result, field_prefix:) }
  22. end
  23. def map_probe_errors_to_user_errors(probe_result, field_prefix:)
  24. error_message = probe_result[:message_human] || probe_result[:message]
  25. # generic error without triggering fields
  26. return [ { message: error_message } ] if probe_result[:invalid_field].blank?
  27. # field specific error(s)
  28. probe_result[:invalid_field].map do |key, _v|
  29. { message: error_message, field: [field_prefix, key].join('.') }
  30. end
  31. end
  32. end
  33. end