organization.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Enrichment
  3. module Clearbit
  4. class Organization
  5. def initialize(user:, payload:)
  6. @user = user
  7. @payload = payload
  8. @source = 'clearbit'
  9. @config = Setting.get('clearbit_config')
  10. @object = 'Organization'
  11. end
  12. def synced?
  13. return false if !@config
  14. UserInfo.current_user_id = 1
  15. return false if !mapping?
  16. return false if !changes?
  17. # create new organization
  18. return organization_created? if !remote_id? || !external_found?
  19. # update existing organization
  20. organization = existing_organization
  21. return true if @user.organization_id
  22. # assign new organization to user
  23. update_user(organization)
  24. end
  25. private
  26. def mapping?
  27. @mapping = @config['organization_sync'].dup
  28. return false if @mapping.blank?
  29. # TODO: Refactoring:
  30. # Currently all target keys are prefixed with
  31. # organization.
  32. # which is not necessary since the target object
  33. # is always an organization
  34. @mapping.transform_values! { |value| value.sub('organization.', '') }
  35. true
  36. end
  37. def changes?
  38. @current_changes = ExternalSync.map(
  39. mapping: @mapping,
  40. source: @payload
  41. )
  42. @current_changes.present?
  43. end
  44. def remote_id?
  45. return if !@payload['company']
  46. @remote_id = @payload['company']['id']
  47. end
  48. def external_found?
  49. return true if @external_organization
  50. @external_organization = ExternalSync.find_by(
  51. source: @source,
  52. source_id: @remote_id,
  53. object: @object,
  54. )
  55. @external_organization.present?
  56. end
  57. def organization_created?
  58. # if organization is already assigned, do not create a new one
  59. return false if @user.organization_id
  60. # can't create organization without name
  61. return false if @current_changes[:name].blank?
  62. organization = create_current
  63. # assign new organization to user
  64. update_user(organization)
  65. end
  66. def create_current
  67. organization = ::Organization.find_by(name: @current_changes[:name])
  68. return organization if organization
  69. organization = ::Organization.new(
  70. shared: @config['organization_shared'],
  71. )
  72. return organization if !ExternalSync.changed?(
  73. object: organization,
  74. current_changes: @current_changes,
  75. )
  76. organization.save!
  77. ExternalSync.create(
  78. source: @source,
  79. source_id: @remote_id,
  80. object: @object,
  81. o_id: organization.id,
  82. last_payload: @payload,
  83. )
  84. organization
  85. end
  86. def load_previous_changes
  87. last_payload = @external_organization.last_payload
  88. return if !last_payload
  89. @previous_changes = ExternalSync.map(
  90. mapping: @mapping,
  91. source: last_payload
  92. )
  93. end
  94. def existing_organization
  95. load_previous_changes
  96. organization = ::Organization.find(@external_organization[:o_id])
  97. return organization if !ExternalSync.changed?(
  98. object: organization,
  99. previous_changes: @previous_changes,
  100. current_changes: @current_changes,
  101. )
  102. organization.updated_by_id = 1
  103. organization.save!
  104. @external_organization.last_payload = @payload
  105. @external_organization.save!
  106. organization
  107. end
  108. def update_user(organization)
  109. @user.organization_id = organization.id
  110. true
  111. end
  112. end
  113. end
  114. end