organization.rb 3.6 KB

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