customer.rb 1.7 KB

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