Skip to content

Commit

Permalink
Added sendgrid sync job
Browse files Browse the repository at this point in the history
  • Loading branch information
chtzvt committed Mar 12, 2024
1 parent 2f75bb3 commit a837581
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ gem "rails-i18n"
gem "redcarpet"
gem "rexml", ">= 3.2.5"
gem "scout_apm"
gem "sendgrid-ruby"
gem "sidekiq"
gem "sitemap_generator"
gem "stripe"
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,16 @@ GEM
ruby-vips (2.1.4)
ffi (~> 1.12)
ruby2_keywords (0.0.5)
ruby_http_client (3.5.5)
rubyzip (2.3.2)
scout_apm (5.1.1)
parser
selenium-webdriver (4.7.1)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
sendgrid-ruby (6.7.0)
ruby_http_client (~> 3.4)
sidekiq (7.1.3)
concurrent-ruby (< 2)
connection_pool (>= 2.3.0)
Expand Down Expand Up @@ -520,6 +523,7 @@ DEPENDENCIES
rexml (>= 3.2.5)
scout_apm
selenium-webdriver
sendgrid-ruby
sidekiq
sitemap_generator
sprockets-rails
Expand Down
39 changes: 39 additions & 0 deletions lib/tasks/sendgrid_sync.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace :users do
desc "Sync registered user data to Sendgrid"
task sync_sendgrid: :environment do
users = User.all
contacts = []

users.each do |u|
next if u.nil?

contact = {
first_name: u.ap_first_name,
last_name: u.ap_last_name,
email: u.ap_email,
custom_fields: {
e47_T: u.ap_types.to_s
}
}

if u.developer
contact[:custom_fields]["e16_T"] = u.developer.linkedin
contact[:custom_fields]["e18_T"] = u.developer.github
contact[:custom_fields]["e53_T"] = u.developer.codeboxx_student
contact[:custom_fields]["e15_T"] = u.developer.website
end

contacts << contact
end

sg = SendGrid::API.new(api_key: ENV["SENDGRID_API_KEY"])

response = sg.client._("marketing/contacts").put(request_body: {contacts: contacts})

if response.status_code != 202
Rails.logger.error "Failed to update SendGrid contacts with error #{response.status_code} #{response.body}"
else
Rails.logger.info "Updated #{contacts.length} contacts(s)"
end
end
end

0 comments on commit a837581

Please sign in to comment.