2022-08-31 19:01:43 +00:00
|
|
|
## Class to generate sample data for a chatwoot test @Account.
|
2021-11-26 19:18:46 +00:00
|
|
|
############################################################
|
|
|
|
### Usage #####
|
|
|
|
#
|
|
|
|
# # Seed an account with all data types in this class
|
2022-08-31 19:01:43 +00:00
|
|
|
# Seeders::AccountSeeder.new(account: @Account.find(1)).perform!
|
2021-11-26 19:18:46 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
############################################################
|
|
|
|
|
|
|
|
class Seeders::AccountSeeder
|
2022-08-31 19:01:43 +00:00
|
|
|
def initialize(account:)
|
2022-09-01 15:53:57 +00:00
|
|
|
raise 'Account Seeding is not allowed.' unless ENV.fetch('ENABLE_ACCOUNT_SEEDING', !Rails.env.production?)
|
2021-11-26 19:18:46 +00:00
|
|
|
|
2022-08-31 19:01:43 +00:00
|
|
|
@account_data = HashWithIndifferentAccess.new(YAML.safe_load(File.read(Rails.root.join('lib/seeders/seed_data.yml'))))
|
|
|
|
@account = account
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform!
|
|
|
|
set_up_account
|
|
|
|
seed_teams
|
|
|
|
set_up_users
|
|
|
|
seed_labels
|
2021-11-26 19:18:46 +00:00
|
|
|
seed_canned_responses
|
2022-05-11 08:03:24 +00:00
|
|
|
seed_inboxes
|
2022-08-31 19:01:43 +00:00
|
|
|
seed_contacts
|
2021-11-26 19:18:46 +00:00
|
|
|
end
|
|
|
|
|
2022-08-31 19:01:43 +00:00
|
|
|
def set_up_account
|
|
|
|
@account.teams.destroy_all
|
|
|
|
@account.conversations.destroy_all
|
|
|
|
@account.labels.destroy_all
|
|
|
|
@account.inboxes.destroy_all
|
|
|
|
@account.contacts.destroy_all
|
2021-11-26 19:18:46 +00:00
|
|
|
end
|
2022-05-11 08:03:24 +00:00
|
|
|
|
2022-08-31 19:01:43 +00:00
|
|
|
def seed_teams
|
|
|
|
@account_data['teams'].each do |team_name|
|
|
|
|
@account.teams.create!(name: team_name)
|
|
|
|
end
|
2022-05-11 08:03:24 +00:00
|
|
|
end
|
|
|
|
|
2022-08-31 19:01:43 +00:00
|
|
|
def seed_labels
|
|
|
|
@account_data['labels'].each do |label|
|
|
|
|
@account.labels.create!(label)
|
|
|
|
end
|
2022-05-11 08:03:24 +00:00
|
|
|
end
|
|
|
|
|
2022-08-31 19:01:43 +00:00
|
|
|
def set_up_users
|
|
|
|
@account_data['users'].each do |user|
|
|
|
|
user_record = User.create_with(name: user['name'], password: 'Password1!.').find_or_create_by!(email: (user['email']).to_s)
|
|
|
|
user_record.skip_confirmation!
|
|
|
|
user_record.save!
|
|
|
|
Avatar::AvatarFromUrlJob.perform_later(user_record, "https://xsgames.co/randomusers/avatar.php?g=#{user['gender']}")
|
|
|
|
AccountUser.create_with(role: (user['role'] || 'agent')).find_or_create_by!(account_id: @account.id, user_id: user_record.id)
|
|
|
|
next if user['team'].blank?
|
2022-05-11 08:03:24 +00:00
|
|
|
|
2022-08-31 19:01:43 +00:00
|
|
|
add_user_to_teams(user: user_record, teams: user['team'])
|
|
|
|
end
|
2022-05-11 08:03:24 +00:00
|
|
|
end
|
|
|
|
|
2022-08-31 19:01:43 +00:00
|
|
|
def add_user_to_teams(user:, teams:)
|
|
|
|
teams.each do |team|
|
|
|
|
team_record = @account.teams.where('name LIKE ?', "%#{team.downcase}%").first if team.present?
|
|
|
|
TeamMember.find_or_create_by!(team_id: team_record.id, user_id: user.id) unless team_record.nil?
|
|
|
|
end
|
2022-05-11 08:03:24 +00:00
|
|
|
end
|
|
|
|
|
2022-08-31 19:01:43 +00:00
|
|
|
def seed_canned_responses(count: 50)
|
|
|
|
count.times do
|
|
|
|
@account.canned_responses.create(content: Faker::Quote.fortune_cookie, short_code: Faker::Alphanumeric.alpha(number: 10))
|
|
|
|
end
|
2022-05-11 08:03:24 +00:00
|
|
|
end
|
|
|
|
|
2022-08-31 19:01:43 +00:00
|
|
|
def seed_contacts
|
|
|
|
@account_data['contacts'].each do |contact_data|
|
|
|
|
contact = @account.contacts.create!(contact_data.slice('name', 'email'))
|
|
|
|
Avatar::AvatarFromUrlJob.perform_later(contact, "https://xsgames.co/randomusers/avatar.php?g=#{contact_data['gender']}")
|
|
|
|
contact_data['conversations'].each do |conversation_data|
|
|
|
|
inbox = @account.inboxes.find_by(channel_type: conversation_data['channel'])
|
|
|
|
contact_inbox = inbox.contact_inboxes.create!(contact: contact, source_id: (conversation_data['source_id'] || SecureRandom.hex))
|
|
|
|
create_conversation(contact_inbox: contact_inbox, conversation_data: conversation_data)
|
|
|
|
end
|
|
|
|
end
|
2022-05-11 08:03:24 +00:00
|
|
|
end
|
|
|
|
|
2022-08-31 19:01:43 +00:00
|
|
|
def create_conversation(contact_inbox:, conversation_data:)
|
|
|
|
assignee = User.find_by(email: conversation_data['assignee']) if conversation_data['assignee'].present?
|
|
|
|
conversation = contact_inbox.conversations.create!(account: contact_inbox.inbox.account, contact: contact_inbox.contact,
|
|
|
|
inbox: contact_inbox.inbox, assignee: assignee)
|
|
|
|
create_messages(conversation: conversation, messages: conversation_data['messages'])
|
2022-05-11 08:03:24 +00:00
|
|
|
end
|
|
|
|
|
2022-08-31 19:01:43 +00:00
|
|
|
def create_messages(conversation:, messages:)
|
|
|
|
messages.each do |message_data|
|
|
|
|
sender = User.find_by(email: message_data['sender']) if message_data['sender'].present?
|
|
|
|
conversation.messages.create!(message_data.slice('content', 'message_type').merge(account: conversation.inbox.account, sender: sender,
|
|
|
|
inbox: conversation.inbox))
|
|
|
|
end
|
2022-05-11 08:03:24 +00:00
|
|
|
end
|
|
|
|
|
2022-08-31 19:01:43 +00:00
|
|
|
def seed_inboxes
|
|
|
|
Seeders::InboxSeeder.new(account: @account, company_data: @account_data[:company]).perform!
|
2022-05-11 08:03:24 +00:00
|
|
|
end
|
2021-11-26 19:18:46 +00:00
|
|
|
end
|