organization.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. # TODO
  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 allways 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].empty?
  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