summaryrefslogtreecommitdiff
path: root/db/fixtures/development/32_crm.rb
blob: bad2fc56ed3c420ffc9c68153ffb4a1dc0575cc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true

class Gitlab::Seeder::Crm
  attr_reader :group, :organizations_per_group, :contacts_per_group

  def initialize(group, organizations_per_group: 10, contacts_per_group: 40)
    @group = group
    @organizations_per_group = organizations_per_group
    @contacts_per_group = contacts_per_group
  end

  def seed!
    organization_ids = []

    organizations_per_group.times do |index|
      organization_ids << ::CustomerRelations::Organization.create!(
        group_id: group.id,
        name: "#{FFaker::Company.name}-#{index}"
      ).id

      print '.'
    end

    contacts_per_group.times do |index|
      first_name = FFaker::Name.first_name
      last_name = FFaker::Name.last_name
      organization_id = index % 3 == 0 ? organization_ids.sample : nil
      ::CustomerRelations::Contact.create!(
        group_id: group.id,
        first_name: first_name,
        last_name: last_name,
        email: "#{first_name}.#{last_name}@example.org",
        organization_id: organization_id
      )

      print '.'
    end
  end
end

Gitlab::Seeder.quiet do
  puts "\nGenerating group crm organizations and contacts"

  Group.all.find_each do |group|
    Gitlab::Seeder::Crm.new(group).seed!
  end
end