2015-05-04 18:58:28 +00:00
|
|
|
# rubocop:disable Rails/Output
|
2014-07-23 22:20:39 +00:00
|
|
|
module FillDB
|
2015-08-07 09:24:47 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
fill your database with demo records
|
|
|
|
|
2017-10-15 06:19:57 +00:00
|
|
|
FillDB.load(
|
|
|
|
agents: 50,
|
|
|
|
customers: 1000,
|
|
|
|
groups: 20,
|
|
|
|
organizations: 40,
|
|
|
|
tickets: 100
|
|
|
|
)
|
2015-08-07 09:24:47 +00:00
|
|
|
|
2017-10-15 06:19:57 +00:00
|
|
|
or if you only want to create 100 tickets
|
2015-08-07 09:24:47 +00:00
|
|
|
|
2017-10-15 06:19:57 +00:00
|
|
|
FillDB.load(tickets: 100)
|
2015-08-07 09:24:47 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2017-10-15 06:19:57 +00:00
|
|
|
def self.load(params)
|
|
|
|
nice = params[:nice] || 0.5
|
|
|
|
agents = params[:agents] || 0
|
|
|
|
customers = params[:customers] || 0
|
|
|
|
groups = params[:groups] || 0
|
|
|
|
organizations = params[:organizations] || 0
|
|
|
|
tickets = params[:tickets] || 0
|
|
|
|
|
2015-04-27 13:20:16 +00:00
|
|
|
puts 'load db with:'
|
2014-07-23 22:20:39 +00:00
|
|
|
puts " agents:#{agents}"
|
|
|
|
puts " customers:#{customers}"
|
|
|
|
puts " groups:#{groups}"
|
|
|
|
puts " organizations:#{organizations}"
|
|
|
|
puts " tickets:#{tickets}"
|
|
|
|
|
|
|
|
# set current user
|
|
|
|
UserInfo.current_user_id = 1
|
|
|
|
|
|
|
|
# organizations
|
|
|
|
organization_pool = []
|
|
|
|
if organizations && !organizations.zero?
|
2017-10-15 06:19:57 +00:00
|
|
|
(1..organizations).each do
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
organization = Organization.create!(name: "FillOrganization::#{rand(999_999)}", active: true)
|
2015-08-07 09:24:47 +00:00
|
|
|
organization_pool.push organization
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-07 09:24:47 +00:00
|
|
|
end
|
2014-07-23 22:20:39 +00:00
|
|
|
else
|
2015-04-27 13:42:53 +00:00
|
|
|
organization_pool = Organization.where(active: true)
|
2015-08-07 09:30:24 +00:00
|
|
|
puts " take #{organization_pool.length} organizations"
|
2014-07-23 22:20:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# create agents
|
|
|
|
agent_pool = []
|
|
|
|
if agents && !agents.zero?
|
2017-10-15 06:19:57 +00:00
|
|
|
roles = Role.where(name: [ 'Agent'])
|
2014-07-23 22:20:39 +00:00
|
|
|
groups_all = Group.all
|
2015-08-07 09:24:47 +00:00
|
|
|
|
2017-10-15 06:19:57 +00:00
|
|
|
(1..agents).each do
|
|
|
|
ActiveRecord::Base.transaction do
|
2015-08-07 09:24:47 +00:00
|
|
|
suffix = rand(99_999).to_s
|
|
|
|
user = User.create_or_update(
|
|
|
|
login: "filldb-agent-#{suffix}",
|
|
|
|
firstname: "agent #{suffix}",
|
|
|
|
lastname: "agent #{suffix}",
|
|
|
|
email: "filldb-agent-#{suffix}@example.com",
|
|
|
|
password: 'agentpw',
|
|
|
|
active: true,
|
|
|
|
roles: roles,
|
|
|
|
groups: groups_all,
|
|
|
|
)
|
2017-10-15 06:19:57 +00:00
|
|
|
sleep nice
|
2015-08-07 09:24:47 +00:00
|
|
|
agent_pool.push user
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-07 09:24:47 +00:00
|
|
|
end
|
2014-07-23 22:20:39 +00:00
|
|
|
else
|
2015-04-27 13:42:53 +00:00
|
|
|
agent_pool = Role.where(name: 'Agent').first.users.where(active: true)
|
2014-07-23 22:20:39 +00:00
|
|
|
puts " take #{agent_pool.length} agents"
|
|
|
|
end
|
|
|
|
|
|
|
|
# create customer
|
|
|
|
customer_pool = []
|
|
|
|
if customers && !customers.zero?
|
2017-10-15 06:19:57 +00:00
|
|
|
roles = Role.where(name: [ 'Customer'])
|
2014-07-23 22:20:39 +00:00
|
|
|
groups_all = Group.all
|
2015-08-07 09:24:47 +00:00
|
|
|
|
2017-10-15 06:19:57 +00:00
|
|
|
(1..customers).each do
|
|
|
|
ActiveRecord::Base.transaction do
|
2015-08-07 09:24:47 +00:00
|
|
|
suffix = rand(99_999).to_s
|
|
|
|
organization = nil
|
2017-11-23 08:09:44 +00:00
|
|
|
if organization_pool.present? && rand(2) == 1
|
2015-08-07 09:24:47 +00:00
|
|
|
organization = organization_pool[ organization_pool.length - 1 ]
|
|
|
|
end
|
|
|
|
user = User.create_or_update(
|
|
|
|
login: "filldb-customer-#{suffix}",
|
|
|
|
firstname: "customer #{suffix}",
|
|
|
|
lastname: "customer #{suffix}",
|
|
|
|
email: "filldb-customer-#{suffix}@example.com",
|
|
|
|
password: 'customerpw',
|
|
|
|
active: true,
|
|
|
|
organization: organization,
|
|
|
|
roles: roles,
|
|
|
|
)
|
2017-10-15 06:19:57 +00:00
|
|
|
sleep nice
|
2015-08-07 09:24:47 +00:00
|
|
|
customer_pool.push user
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-07 09:24:47 +00:00
|
|
|
end
|
2014-07-23 22:20:39 +00:00
|
|
|
else
|
2015-04-27 13:42:53 +00:00
|
|
|
customer_pool = Role.where(name: 'Customer').first.users.where(active: true)
|
2015-08-07 09:30:24 +00:00
|
|
|
puts " take #{customer_pool.length} customers"
|
2014-07-23 22:20:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# create groups
|
|
|
|
group_pool = []
|
|
|
|
if groups && !groups.zero?
|
2015-08-07 09:24:47 +00:00
|
|
|
|
2017-10-15 06:19:57 +00:00
|
|
|
(1..groups).each do
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
group = Group.create!(name: "FillGroup::#{rand(999_999)}", active: true)
|
2015-08-07 09:24:47 +00:00
|
|
|
group_pool.push group
|
2017-10-01 12:25:52 +00:00
|
|
|
Role.where(name: 'Agent').first.users.where(active: true).each do |user|
|
2015-08-07 09:24:47 +00:00
|
|
|
user_groups = user.groups
|
|
|
|
user_groups.push group
|
|
|
|
user.groups = user_groups
|
2017-10-15 06:19:57 +00:00
|
|
|
user.save!
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2017-10-15 06:19:57 +00:00
|
|
|
sleep nice
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-08-07 09:24:47 +00:00
|
|
|
end
|
2014-07-23 22:20:39 +00:00
|
|
|
else
|
2015-04-27 13:42:53 +00:00
|
|
|
group_pool = Group.where(active: true)
|
2015-08-07 09:30:24 +00:00
|
|
|
puts " take #{group_pool.length} groups"
|
2014-07-23 22:20:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# create tickets
|
|
|
|
priority_pool = Ticket::Priority.all
|
|
|
|
state_pool = Ticket::State.all
|
2015-08-07 09:24:47 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
return if !tickets || tickets.zero?
|
|
|
|
(1..tickets).each do
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
customer = customer_pool[ rand(customer_pool.length - 1) ]
|
|
|
|
agent = agent_pool[ rand(agent_pool.length - 1) ]
|
|
|
|
ticket = Ticket.create!(
|
|
|
|
title: "some title äöüß#{rand(999_999)}",
|
|
|
|
group: group_pool[ rand(group_pool.length - 1) ],
|
|
|
|
customer: customer,
|
|
|
|
owner: agent,
|
|
|
|
state: state_pool[ rand(state_pool.length - 1) ],
|
|
|
|
priority: priority_pool[ rand(priority_pool.length - 1) ],
|
|
|
|
updated_by_id: agent.id,
|
|
|
|
created_by_id: agent.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
# create article
|
|
|
|
article = Ticket::Article.create!(
|
|
|
|
ticket_id: ticket.id,
|
|
|
|
from: customer.email,
|
|
|
|
to: 'some_recipient@example.com',
|
|
|
|
subject: "some subject#{rand(999_999)}",
|
|
|
|
message_id: "some@id-#{rand(999_999)}",
|
|
|
|
body: 'some message ...',
|
|
|
|
internal: false,
|
|
|
|
sender: Ticket::Article::Sender.where(name: 'Customer').first,
|
|
|
|
type: Ticket::Article::Type.where(name: 'phone').first,
|
|
|
|
updated_by_id: agent.id,
|
|
|
|
created_by_id: agent.id,
|
|
|
|
)
|
|
|
|
puts " Ticket #{ticket.number} created"
|
|
|
|
sleep nice
|
2015-08-07 09:24:47 +00:00
|
|
|
end
|
2014-07-23 22:20:39 +00:00
|
|
|
end
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|