fill_db.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # rubocop:disable Rails/Output
  2. module FillDB
  3. def self.load( agents, customers, groups, organizations, tickets )
  4. puts 'load db with:'
  5. puts " agents:#{agents}"
  6. puts " customers:#{customers}"
  7. puts " groups:#{groups}"
  8. puts " organizations:#{organizations}"
  9. puts " tickets:#{tickets}"
  10. # set current user
  11. UserInfo.current_user_id = 1
  12. # organizations
  13. organization_pool = []
  14. if organizations && !organizations.zero?
  15. (1..organizations).each {
  16. organization = Organization.create( name: 'FillOrganization::' + rand(999_999).to_s, active: true )
  17. organization_pool.push organization
  18. }
  19. else
  20. organization_pool = Organization.where(active: true)
  21. end
  22. # create agents
  23. agent_pool = []
  24. if agents && !agents.zero?
  25. roles = Role.where( name: [ 'Agent'] )
  26. groups_all = Group.all
  27. (1..agents).each {
  28. suffix = rand(99_999).to_s
  29. user = User.create_or_update(
  30. login: "filldb-agent-#{suffix}",
  31. firstname: "agent #{suffix}",
  32. lastname: "agent #{suffix}",
  33. email: "filldb-agent-#{suffix}@example.com",
  34. password: 'agentpw',
  35. active: true,
  36. roles: roles,
  37. groups: groups_all,
  38. )
  39. agent_pool.push user
  40. }
  41. else
  42. agent_pool = Role.where(name: 'Agent').first.users.where(active: true)
  43. puts " take #{agent_pool.length} agents"
  44. end
  45. # create customer
  46. customer_pool = []
  47. if customers && !customers.zero?
  48. roles = Role.where( name: [ 'Customer'] )
  49. groups_all = Group.all
  50. (1..customers).each {
  51. suffix = rand(99_999).to_s
  52. organization = nil
  53. if !organization_pool.empty? && rand(2) == 1
  54. organization = organization_pool[ organization_pool.length - 1 ]
  55. end
  56. user = User.create_or_update(
  57. login: "filldb-customer-#{suffix}",
  58. firstname: "customer #{suffix}",
  59. lastname: "customer #{suffix}",
  60. email: "filldb-customer-#{suffix}@example.com",
  61. password: 'customerpw',
  62. active: true,
  63. organization: organization,
  64. roles: roles,
  65. )
  66. customer_pool.push user
  67. }
  68. else
  69. customer_pool = Role.where(name: 'Customer').first.users.where(active: true)
  70. end
  71. # create groups
  72. group_pool = []
  73. if groups && !groups.zero?
  74. puts "1..#{groups}"
  75. (1..groups).each {
  76. group = Group.create( name: 'FillGroup::' + rand(999_999).to_s, active: true )
  77. group_pool.push group
  78. Role.where(name: 'Agent').first.users.where(active: true).each {|user|
  79. user_groups = user.groups
  80. user_groups.push group
  81. user.groups = user_groups
  82. user.save
  83. }
  84. }
  85. else
  86. group_pool = Group.where(active: true)
  87. end
  88. # create tickets
  89. priority_pool = Ticket::Priority.all
  90. state_pool = Ticket::State.all
  91. if tickets && !tickets.zero?
  92. (1..tickets).each {
  93. customer = customer_pool[ rand(customer_pool.length - 1) ]
  94. agent = agent_pool[ rand(agent_pool.length - 1) ]
  95. ticket = Ticket.create(
  96. title: 'some title äöüß' + rand(999_999).to_s,
  97. group: group_pool[ rand(group_pool.length - 1) ],
  98. customer: customer,
  99. owner: agent,
  100. state: state_pool[ rand(state_pool.length - 1) ],
  101. priority: priority_pool[ rand(priority_pool.length - 1) ],
  102. updated_by_id: agent.id,
  103. created_by_id: agent.id,
  104. )
  105. # create article
  106. article = Ticket::Article.create(
  107. ticket_id: ticket.id,
  108. from: customer.email,
  109. to: 'some_recipient@example.com',
  110. subject: 'some subject' + rand(999_999).to_s,
  111. message_id: 'some@id-' + rand(999_999).to_s,
  112. body: 'some message ...',
  113. internal: false,
  114. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  115. type: Ticket::Article::Type.where(name: 'phone').first,
  116. updated_by_id: agent.id,
  117. created_by_id: agent.id,
  118. )
  119. }
  120. end
  121. end
  122. end