Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def mark_users_updated
redirect_to actions_admin_users_path, notice: 'Incremented unread update count'
end

def force_update_lead
Newflow::CreateOrUpdateSalesforceLead.call(user: get_user)
def force_update_contact
Newflow::CreateOrUpdateSalesforceContact.call(user: get_user)
end

protected
Expand Down
2 changes: 1 addition & 1 deletion app/handlers/newflow/educator_signup/complete_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def handle

transfer_errors_from(@user, {type: :verbatim}, :fail_if_errors)

CreateOrUpdateSalesforceLead.perform_later(user: @user)
CreateOrUpdateSalesforceContact.perform_later(user: @user)

#output the user to the lev handler
outputs.user = @user
Expand Down
2 changes: 1 addition & 1 deletion app/handlers/newflow/educator_signup/sheerid_webhook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def handle(verification_id=nil)
event_data: { verification: verification_details_from_sheerid.inspect })
end

CreateOrUpdateSalesforceLead.perform_later(user: user)
CreateOrUpdateSalesforceContact.perform_later(user: user)


SecurityLog.create!(user: user, event_type: :sheerid_webhook_processed)
Expand Down
3 changes: 3 additions & 0 deletions app/models/security_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ class SecurityLog < ApplicationRecord
attempted_to_add_school_not_cached_yet
school_added_to_user_from_sheerid_webhook
user_lead_id_updated_from_salesforce
starting_salesforce_contact_creation
attempting_to_create_user_contact
created_salesforce_contact
]

json_serialize :event_data, Hash
Expand Down
4 changes: 0 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ class User < ApplicationRecord

attribute :is_not_gdpr_location, :boolean, default: nil

def lead
OpenStax::Salesforce::Remote::Lead.find_by(email: best_email_address_for_salesforce)
end

def contact
OpenStax::Salesforce::Remote::Contact.find(salesforce_contact_id)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Newflow
class CreateOrUpdateSalesforceLead
class CreateOrUpdateSalesforceContact

lev_routine active_job_enqueue_options: { queue: :salesforce }

LEAD_SOURCE = 'Account Creation'
LEAD_SOURCE = 'Account Creation'
DEFAULT_REFERRING_APP_NAME = 'Accounts'

ADOPTION_STATUS_FROM_USER = {
Expand All @@ -24,7 +24,7 @@ def exec(user:)

SecurityLog.create!(
user: user,
event_type: :starting_salesforce_lead_creation
event_type: :starting_salesforce_contact_creation
)

sf_school_id = user.school&.salesforce_id
Expand All @@ -47,7 +47,7 @@ def exec(user:)
adoption_json = build_book_adoption_json_for_salesforce(user)
end

# Check the state of the SheerID response and profile completion to determine faculty status for lead
# Check the state of the SheerID response and profile completion to determine faculty status for contact
sheerid_response = SheeridVerification.find_by(verification_id: user.sheerid_verification_id)
if user.is_profile_complete?
user.faculty_status = :pending_faculty
Expand All @@ -59,47 +59,55 @@ def exec(user:)
user.faculty_status = :incomplete_signup
end


if user.salesforce_lead_id
lead = OpenStax::Salesforce::Remote::Lead.find_by(email: user.best_email_address_for_salesforce)
# Find existing contact or create new one
if user.salesforce_contact_id
contact = OpenStax::Salesforce::Remote::Contact.find_by(id: user.salesforce_contact_id)
else
lead = OpenStax::Salesforce::Remote::Lead.new(email: user.best_email_address_for_salesforce)
# Look for existing contact by email first
contact = OpenStax::Salesforce::Remote::Contact.find_by(email: user.best_email_address_for_salesforce)

# If found, update the user with the contact ID
if contact
user.salesforce_contact_id = contact.id
user.save
else
# Create new contact
contact = OpenStax::Salesforce::Remote::Contact.new(email: user.best_email_address_for_salesforce)
end
end

if lead.nil?
Sentry.capture_message("Lead for user not found #{user.uuid} not found", level: :error)
if contact.nil?
Sentry.capture_message("Contact for user #{user.uuid} not found", level: :error)
return
end

lead.first_name = user.first_name
lead.last_name = user.last_name
lead.phone = user.phone_number
lead.source = LEAD_SOURCE
lead.application_source = DEFAULT_REFERRING_APP_NAME
lead.role = sf_role
lead.position = sf_position
lead.title = user.other_role_name
lead.who_chooses_books = user.who_chooses_books
lead.subject_interest = user.which_books
lead.num_students = user.how_many_students
lead.adoption_status = ADOPTION_STATUS_FROM_USER[user.using_openstax_how]
lead.adoption_json = adoption_json
lead.os_accounts_id = user.id
lead.accounts_uuid = user.uuid
lead.school = user.most_accurate_school_name
lead.city = user.most_accurate_school_city
lead.country = user.most_accurate_school_country
lead.verification_status = user.faculty_status == User::NO_FACULTY_INFO ? nil : user.faculty_status
lead.b_r_i_marketing = user.is_b_r_i_user?
lead.title_1_school = user.title_1_school?
lead.newsletter = user.receive_newsletter?
lead.newsletter_opt_in = user.receive_newsletter?
lead.self_reported_school = user.self_reported_school
lead.sheerid_school_name = user.sheerid_reported_school
lead.account_id = sf_school_id
lead.school_id = sf_school_id
lead.signup_date = user.created_at.strftime("%Y-%m-%dT%T.%L%z")
lead.tracking_parameters = "#{Rails.application.secrets.openstax_url}/accounts/i/signup/"
contact.first_name = user.first_name
contact.last_name = user.last_name
contact.phone = user.phone_number
contact.lead_source = LEAD_SOURCE
contact.role = sf_role
contact.position = sf_position
contact.title = user.other_role_name
contact.who_chooses_books = user.who_chooses_books
contact.subject_interest = user.which_books
contact.num_students = user.how_many_students
contact.adoption_status = ADOPTION_STATUS_FROM_USER[user.using_openstax_how]
contact.adoption_json = adoption_json
contact.os_accounts_id = user.id
contact.accounts_uuid = user.uuid
contact.school = user.most_accurate_school_name
contact.city = user.most_accurate_school_city
contact.country = user.most_accurate_school_country
contact.verification_status = user.faculty_status == User::NO_FACULTY_INFO ? nil : user.faculty_status
contact.b_r_i_marketing = user.is_b_r_i_user?
contact.title_1_school = user.title_1_school?
contact.newsletter = user.receive_newsletter?
contact.newsletter_opt_in = user.receive_newsletter?
contact.self_reported_school = user.self_reported_school
contact.sheerid_school_name = user.sheerid_reported_school
contact.account_id = sf_school_id
contact.signup_date = user.created_at.strftime("%Y-%m-%dT%T.%L%z")
contact.tracking_parameters = "#{Rails.application.secrets.openstax_url}/accounts/i/signup/"

state = user.most_accurate_school_state
unless state.blank?
Expand All @@ -108,40 +116,40 @@ def exec(user:)
unless state.nil?
# Figure out if the State is an abbreviation or the full name
if state == state.upcase
lead.state_code = state
contact.state_code = state
else
lead.state = state
contact.state = state
end
end

SecurityLog.create!(
user: user,
event_type: :attempting_to_create_user_lead,
event_data: { lead_data: lead }
event_type: :attempting_to_create_user_contact,
event_data: { contact_id: contact.id }
)

if lead.save
user.salesforce_lead_id = lead.id
if contact.save
user.salesforce_contact_id = contact.id
if user.save
SecurityLog.create!(
user: user,
event_type: :created_salesforce_lead,
event_data: { lead_id: lead.id.to_s }
event_type: :created_salesforce_contact,
event_data: { contact_id: contact.id.to_s }
)
else
if lead.errors.messages.inspect.include? == 'INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY'
if contact.errors.messages.inspect.include? == 'INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY'
Sentry.capture_message("Invalid school (#{user.school.salesforce_id}) for user (#{user.id})")
end
SecurityLog.create!(
user: user,
event_type: :educator_sign_up_failed,
event_data: { lead_id: lead.id }
event_data: { contact_id: contact.id }
)
Sentry.capture_message("User #{user.id} was not successfully saved with lead #{lead.id}")
Sentry.capture_message("User #{user.id} was not successfully saved with contact #{contact.id}")
end
end

outputs.lead = lead
outputs.contact = contact
outputs.user = user
end

Expand Down Expand Up @@ -174,4 +182,4 @@ def build_book_adoption_json_for_salesforce(user)
adoption_json.to_json
end
end
end
end
75 changes: 0 additions & 75 deletions app/routines/update_user_lead_info.rb

This file was deleted.

2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
get 'js_search', on: :collection
get 'actions', on: :collection
put 'mark_users_updated', on: :collection
post 'force_update_lead', on: :member
post 'force_update_contact', on: :member
delete 'soft_delete', on: :member
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace :accounts do
desc 'Create leads for faculty users that started signup but did not finish so they can be reviewed by customer service'
# Run task with the optional days of leads you want uploaded eg. `rake accounts:create_leads_for_abandoned_user_signups[1]`
desc 'Create contacts for faculty users that started signup but did not finish so they can be reviewed by customer service'
# Run task with the optional days of contacts you want uploaded eg. `rake accounts:create_leads_for_abandoned_user_signups[1]`
# Defaults to two days, with the idea being to run it once per day, allowing it to catch any it might have missed while processing.
task :create_leads_for_abandoned_user_signups, [:day] => [:environment] do |t, args|
args.with_defaults(:day => 2)
Expand All @@ -10,7 +10,7 @@ namespace :accounts do
users.each { |user|
user.faculty_status = :incomplete_signup
user.save!
Newflow::CreateSalesforceLead.perform_later(user: user)
Newflow::CreateOrUpdateSalesforceContact.perform_later(user: user)
}
end
end
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
namespace :accounts do
desc 'Create leads for faculty verified by SheerID and never sent to Salesforce'
desc 'Create contacts for faculty verified by SheerID and never sent to Salesforce'
# rake accounts:create_leads_for_instructors_not_sent_to_sf
task :create_leads_for_instructors_not_sent_to_sf, [:day] => [:environment] do |t, args|
# get all the instructors that don't have lead or contact ids and have a complete profile
users = User.where(salesforce_contact_id: nil, salesforce_lead_id: nil, role: :instructor, is_profile_complete: true)
# get all the instructors that don't have contact ids and have a complete profile
users = User.where(salesforce_contact_id: nil, role: :instructor, is_profile_complete: true)
STDOUT.puts "This will process #{users.count} users. Do you want to continue? (y/n)"

begin
input = STDIN.gets.strip.downcase
end until %w(y n).include?(input)

if input !='y'
STDOUT.puts "Cancelling lead creation for abandoned users."
STDOUT.puts "Cancelling contact creation for abandoned users."
return
end

users.each do |user|
lead = OpenStax::Salesforce::Remote::Lead.select(:id, :verification_status).find_by(accounts_uuid: user.uuid)
contact = OpenStax::Salesforce::Remote::Contact.select(:id, :faculty_verified).find_by(accounts_uuid: user.uuid)

if lead.nil?
Newflow::CreateOrUpdateSalesforceLead.call(user: user)
if contact.nil?
Newflow::CreateOrUpdateSalesforceContact.call(user: user)
else
# set the lead id, this will update their status in UpdateUserLeadInfo
user.salesforce_lead_id = lead.id
# set the contact id, this will update their status in UpdateUserContactInfo
user.salesforce_contact_id = contact.id
user.save
end
end
Expand Down
Loading
Loading