adds_metadata_origin_by_id.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # Adds origin_by_id field (if missing) when creating articles.
  3. module Ticket::Article::AddsMetadataOriginById
  4. extend ActiveSupport::Concern
  5. included do
  6. before_create :ticket_article_add_metadata_origin_by_id
  7. end
  8. private
  9. def ticket_article_add_metadata_origin_by_id
  10. return if !neither_importing_nor_postmaster?
  11. return if origin_by_id.present?
  12. return if ticket&.customer_id.blank?
  13. return if !sender || sender.name != 'Customer'
  14. return if %w[phone note web].exclude? type.name
  15. return if metadata_origin_by_customer_and_creator_colleagues?
  16. self.origin_by_id = ticket.customer_id
  17. end
  18. def neither_importing_nor_postmaster?
  19. # return if we run import mode
  20. return if Setting.get('import_mode')
  21. # only do fill origin_by_id if article got created via application_server (e. g. not
  22. # if article and sender type is set via *.postmaster)
  23. return if ApplicationHandleInfo.postmaster?
  24. true
  25. end
  26. def metadata_origin_by_customer_and_creator_colleagues?
  27. created_by_organizations = created_by.all_organizations.where(shared: true)
  28. customer_organizations = ticket.customer.all_organizations.where(shared: true)
  29. created_by_organizations.ids.intersect? customer_organizations.ids
  30. end
  31. end