Skip to content

Commit

Permalink
remove stale_id, only update school if lead returns it is bad
Browse files Browse the repository at this point in the history
  • Loading branch information
mwvolo committed Feb 7, 2024
1 parent 5928de8 commit 7c548bd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 32 deletions.
2 changes: 2 additions & 0 deletions app/routines/newflow/create_or_update_salesforce_lead.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ def exec(user:)
try
save_lead(user, lead)
except Restforce::ErrorCode::InsufficientAccessOnCrossReferenceEntity
Sentry.capture_message("Invalid school (#{user.school.salesforce_id}) for user (#{user.id})")
UpdateSchoolSalesforceInfo.cleanup_merged_schools(user.school)
save_lead(user, lead)

outputs.lead = lead
outputs.user = user
Expand Down
40 changes: 15 additions & 25 deletions app/routines/update_school_salesforce_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,37 +80,27 @@ def call
break if sf_schools.length < BATCH_SIZE
end

cleanup_merged_schools

log("Finished updating #{schools_updated} schools")
Sentry.capture_check_in('update-school-salesforce', :ok, check_in_id: check_in_id)
end

def cleanup_merged_schools(school_id=None)
# Loop through stale schools and update users associated with them to prevent sync issues
school_id ? schools = School.where(id: school_id) : schools ||= School.where.not(updated_salesforce_id: nil)
schools&.each do |merged_school|
# Let's make sure it wasn't just created
if School.find_by(salesforce_id: merged_school.updated_salesforce_id)
merged_school.update!(updated_salesforce_id: nil)
next
def cleanup_merged_schools(stale_school_id)
stale_school = School.where(id: stale_school_id)

# Find the new school and update the users attached to it
sf_school = OpenStax::Salesforce::Remote::School.find_by(sheerid_school_name: stale_school.sheerid_reported_name)
if sf_school.nil?
Sentry.capture_message("Stale school (#{stale_school.salesforce_id}) not found by SheerID name (#{stale_school.sheerid_reported_name})")
else
updated_school = School.find_or_create_by(salesforce_id: sf_school.id)
SF_TO_DB_CACHE_COLUMNS_MAP.each do |sf_column, db_column|
updated_school.public_send "#{db_column}=", sf_school.public_send(sf_column)
end
updated_school.save!

# Find the new school and update the users attached to it
sf_school = OpenStax::Salesforce::Remote::School.find_by(name: merged_school.updated_salesforce_id)
if sf_school.nil?
Sentry.capture_message("Possible merged school not found. Original: #{merged_school.salesforce_id} New: #{merged_school.updated_salesforce_id}")
else
updated_school = School.find_or_create_by(salesforce_id: sf_school.id)
SF_TO_DB_CACHE_COLUMNS_MAP.each do |sf_column, db_column|
updated_school.public_send "#{db_column}=", sf_school.public_send(sf_column)
end
updated_school.save!

merged_school.users.update_all(school: updated_school) if merged_school.users.any?
merged_school.reload
merged_school.destroy!
end
stale_school.users.update_all(school: updated_school) if stale_school.users.any?
stale_school.reload
stale_school.destroy!
end
end
end

This file was deleted.

3 changes: 1 addition & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_02_01_190613) do
ActiveRecord::Schema.define(version: 2024_01_23_173834) do

# These are extensions that must be enabled in order to support this database
enable_extension "citext"
Expand Down Expand Up @@ -361,7 +361,6 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "country", default: "United States", null: false
t.text "updated_salesforce_id"
t.index ["name", "city", "state"], name: "index_schools_on_name_and_city_and_state", opclass: :gist_trgm_ops, using: :gist
t.index ["salesforce_id"], name: "index_schools_on_salesforce_id", unique: true
t.index ["sheerid_school_name"], name: "index_schools_on_sheerid_school_name"
Expand Down

0 comments on commit 7c548bd

Please sign in to comment.