Skip to content

Commit

Permalink
cleanup users with changed school ids from salesforce
Browse files Browse the repository at this point in the history
  • Loading branch information
mwvolo committed Jan 31, 2024
1 parent d573c19 commit 09b8f63
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app/routines/update_school_salesforce_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@ def call
School.where(salesforce_id: deleted_salesforce_ids).delete_all
end

# Loop through stale schools and update users associated with them to prevent sync issues
School.find_by(stale_in_salesforce: true) do |stale_school|
sf_school = OpenStax::Salesforce::Remote::School.find(school.salesforce_id)
unless sf_school.nil?
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!

stale_school.users do |user|
user.update!(school: updated_school)
end
stale_school.destroy!
end
end


# Go through all SF Schools and cache their information, if it changed
schools_updated = 0
last_id = nil
Expand Down
3 changes: 3 additions & 0 deletions app/routines/update_user_contact_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def call
sf_contact = contacts_by_uuid[user.uuid]
school = schools_by_salesforce_id[sf_contact.school_id]

# Mark the school as stale if it's not in the database
school.stale_in_salesforce = school.nil?

previous_contact_id = user.salesforce_contact_id
user.salesforce_contact_id = sf_contact.id

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStaleInSalesforceToSchools < ActiveRecord::Migration[5.2]
def change
add_column :schools, :stale_in_salesforce, :boolean
end
end

0 comments on commit 09b8f63

Please sign in to comment.