customer.rb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module Import
  3. module OTRS
  4. class Customer
  5. include Import::Helper
  6. include Import::OTRS::Helper
  7. MAPPING = {
  8. ChangeTime: :updated_at,
  9. CreateTime: :created_at,
  10. CreateBy: :created_by_id,
  11. ChangeBy: :updated_by_id,
  12. CustomerCompanyName: :name,
  13. CustomerCompanyComment: :note,
  14. }.freeze
  15. def initialize(customer)
  16. import(customer)
  17. end
  18. def self.by_customer_id(customer_id)
  19. organizations = Import::OTRS::Requester.load('Customer')
  20. result = nil
  21. organizations.each do |organization|
  22. next if customer_id != organization['CustomerID']
  23. result = Organization.find_by(name: organization['CustomerCompanyName'])
  24. break
  25. end
  26. result
  27. end
  28. private
  29. def import(customer)
  30. create_or_update(map(customer))
  31. end
  32. def create_or_update(customer)
  33. return if updated?(customer)
  34. create(customer)
  35. end
  36. def updated?(customer)
  37. @local_customer = Organization.find_by(name: customer[:name])
  38. return false if !@local_customer
  39. log "update Organization.find_by(name: #{customer[:name]})"
  40. @local_customer.update!(customer)
  41. true
  42. end
  43. def create(customer)
  44. log "add Organization.find_by(name: #{customer[:name]})"
  45. @local_customer = Organization.create(customer)
  46. reset_primary_key_sequence('organizations')
  47. end
  48. def map(customer)
  49. {
  50. created_by_id: 1,
  51. updated_by_id: 1,
  52. active: active?(customer),
  53. }
  54. .merge(from_mapping(customer))
  55. end
  56. end
  57. end
  58. end