fill_db.rb 4.8 KB

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