-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_dispatch_state_leads
executable file
·68 lines (52 loc) · 1.57 KB
/
import_dispatch_state_leads
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env ruby
require "bundler/setup"
require 'active_support/all'
require 'csv'
require 'dotenv'
require 'pipedrive-ruby'
Dotenv.load
USER_ID = ENV['PIPEDRIVE_USER_ID'].presence || fail('No user ID set.')
PROSPECTING_STAGE_ID = 64
needs_columns = [
"Agency",
"Contact full name",
"Contact email",
"Contact phone",
"Contact title"
]
contents = File.read(ARGV[0])
csv = CSV.parse(contents, headers: true)
headers = csv.headers.to_a
rows = csv.map(&:values_at).to_a
missing_columns = needs_columns - headers
if missing_columns.length > 0
fail "Missing columns: #{missing_columns.join(', ')}"
end
puts "Importing #{rows.length} rows..."
Pipedrive.authenticate(ENV['PIPEDRIVE_TOKEN'])
rows.map { |row| Hash[headers.zip(row)] }.group_by { |row| row['Agency'] }.each do |agency, group|
org = Pipedrive::Organization.find_or_create_by_name(agency)
deal = Pipedrive::Deal.create(
title: "#{agency} deal",
user_id: USER_ID,
org_id: org.id,
stage_id: PROSPECTING_STAGE_ID,
'1f9acce49b24c0b646e94be7d6967d58228d9ee8' => 'Drip Campaign - Email'
)
group.each do |row|
if row['Contact full name'] && row['Contact full name'].length > 0
person = Pipedrive::Person.find_or_create_by_name(
row['Contact full name'],
email: row['Contact email'],
phone: row['Contact phone'],
'962d049d401b8ae67533d4f401c75d6591017702' => row['Contact title'],
org_id: org.id
)
Pipedrive::Base.post(
"/deals/#{deal.id}/participants",
body: { person_id: person.id }
)
end
end
end
puts "Done!"