updater.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. if self.class.included_modules.include?(FormUpdater::Concerns::ChecksCoreWorkflow)
  38. validate_workflows
  39. end
  40. if relation_fields.present?
  41. resolve_relation_fields
  42. end
  43. result
  44. end
  45. private
  46. def resolve_relation_fields
  47. relation_fields.each do |name, relation_field|
  48. relation_resolver = get_relation_resolver(relation_field)
  49. result_initialize_field(name)
  50. result[relation_field[:name]][:options] = relation_resolver.options
  51. ensure_field_value_int(result[relation_field[:name]])
  52. end
  53. end
  54. def ensure_field_value_int(field)
  55. return if field[:value].blank?
  56. field[:value] = field[:value].to_i
  57. end
  58. RELATION_CLASS_PREFIX = 'FormUpdater::Relation::'.freeze
  59. def get_relation_resolver(relation_field)
  60. relation_class = "#{RELATION_CLASS_PREFIX}#{relation_field[:name].humanize}".safe_constantize
  61. if !relation_class
  62. relation_class = "#{RELATION_CLASS_PREFIX}#{relation_field[:relation]}".constantize
  63. end
  64. relation_class.new(
  65. context: context,
  66. current_user: current_user,
  67. data: data,
  68. filter_ids: relation_field[:filter_ids],
  69. )
  70. rescue
  71. raise "Cannot resolve relation type #{relation_field[:relation]} (#{relation_field[:name]})."
  72. end
  73. def result_initialize_field(name)
  74. result[name] ||= {}
  75. end
  76. end