fill_db.rb 4.4 KB

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