updater.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class FormUpdater::Updater
  3. include Mixin::RequiredSubPaths
  4. # Context from GraphQL or possibly other environments.
  5. # It must respond to :current_user and :current_user? for session information (see Gql::Context::CurrentUserAware).
  6. # It may respond to :schema with an object providing :id_for_object to perform ID mappings like in Gql::ZammadSchema.
  7. attr_reader :context, :current_user, :relation_fields, :meta, :data, :id, :object, :result
  8. def initialize(context:, relation_fields:, meta:, data:, id: nil)
  9. @context = context
  10. @meta = meta
  11. @data = data
  12. @id = id
  13. @current_user = context[:current_user]
  14. @result = {}
  15. # Build lookup for relation fields for better usage.
  16. @relation_fields = relation_fields.each_with_object({}) do |relation_field, lookup|
  17. lookup[relation_field[:name]] = relation_field.to_h
  18. end
  19. end
  20. def object_type
  21. raise NotImplementedError
  22. end
  23. def self.updaters
  24. descendants
  25. end
  26. def self.requires_authentication
  27. true
  28. end
  29. def authorized?
  30. # The authorized function needs to be implemented for any updaters which have a `id`.
  31. if id
  32. @object = Gql::ZammadSchema.authorized_object_from_id id, type: object_type, user: current_user
  33. end
  34. true
  35. end
  36. def resolve
  37. validate_workflows if self.class.included_modules.include?(FormUpdater::Concerns::ChecksCoreWorkflow)
  38. resolve_relation_fields if relation_fields.present?
  39. result
  40. end
  41. private
  42. def resolve_relation_fields
  43. relation_fields.each do |name, relation_field|
  44. relation_resolver = get_relation_resolver(relation_field)
  45. result_initialize_field(name)
  46. result[relation_field[:name]][:options] = relation_resolver.options
  47. ensure_field_value_int(result[relation_field[:name]])
  48. end
  49. end
  50. def ensure_field_value_int(field)
  51. return if field[:value].blank?
  52. field[:value] = field[:value].to_i
  53. end
  54. RELATION_CLASS_PREFIX = 'FormUpdater::Relation::'.freeze
  55. def get_relation_resolver(relation_field)
  56. relation_class = "#{RELATION_CLASS_PREFIX}#{relation_field[:name].humanize}".safe_constantize
  57. if !relation_class
  58. relation_class = "#{RELATION_CLASS_PREFIX}#{relation_field[:relation]}".constantize
  59. end
  60. relation_class.new(
  61. context: context,
  62. current_user: current_user,
  63. data: data,
  64. filter_ids: relation_field[:filter_ids],
  65. )
  66. rescue
  67. raise "Cannot resolve relation type #{relation_field[:relation]} (#{relation_field[:name]})."
  68. end
  69. def result_initialize_field(name)
  70. result[name] ||= {}
  71. end
  72. end