Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

School info in admin, Sentry message on school error during lead creation #1236

Merged
merged 14 commits into from
Feb 7, 2024

Conversation

mwvolo
Copy link
Member

@mwvolo mwvolo commented Jan 31, 2024

We have a few users that are signing up and getting associated with a school that has likely been merged in Salesforce, or somehow had it's salesforce_id updated. Because there are users associated with it, it doesn't delete when updating schools from Salesforce.
This code will throw a Sentry message for more investigation into what's going on.

I've also added some administrative abilities to see the school associated with the user.

@mwvolo mwvolo requested a review from Dantemss February 1, 2024 20:08
Copy link
Member

@Dantemss Dantemss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking instead of this entire PR all you need is maybe another check in update_user_contact_info.rb for this additional case:
if !school.nil? && !sf_school.nil?
In this case you want to check that school.salesforce_id == sf_school.id and if not, then update the user's school?

updated_school.save!

merged_school.users.update_all(school: updated_school) if merged_school.users.any?
merged_school.destroy!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd call reload before destroy!, just in case... otherwise it could try to dissociate the users.

@@ -121,6 +121,8 @@ def call
user.grant_tutor_access = sf_contact.grant_tutor_access

if school.nil? && !sf_school.nil?
# Add the possible updated school id to the record, we check for this in UpdateSchoolSalesforceInfo
user.school.updated_salesforce_id = sf_school.id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user.school is maybe not always set?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this code would only be relevant when the school is in SF but not in Accounts, which hopefully only happens sometimes in the first sync after CS adds a school in SF.

@Dantemss
Copy link
Member

Dantemss commented Feb 1, 2024

Nvm, what you actually what is compare user.school with school somewhere. school in update_user_contact_info.rb is local but being loaded via Salesforce (via the school id in the contact and the salesforce_id in School). user.school is just a foreign key in the Accounts postgres DB.

@Dantemss
Copy link
Member

Dantemss commented Feb 1, 2024

Alternatively, change the user.school association (and school.users) to always load via the salesforce keys and remove/ignore the user.school_id key in Accounts. You can set the primary_key and foreign_key options in the belongs_to and has_many associations to accomplish this. You'd have to check nothing else is using user.school_id directly though.

@mwvolo
Copy link
Member Author

mwvolo commented Feb 7, 2024

You certainly helped me see that I was on the wrong path in trying to fix the error. The error is popping up when the lead gets created first, which is where it should be caught - then update the users affected so it doesn't impact future syncs with user/school combo.

See if you think I still need to modify the foreign key to accomplish what I am trying to do. There might be another place in the application that I should check if the school is 'stale'.

@mwvolo
Copy link
Member Author

mwvolo commented Feb 7, 2024

@Dantemss decided to just throw a message to Sentry for now, this also adds some admin info that will be helpful.
I'll address cleaning up stale schools in another PR.

@mwvolo mwvolo changed the title cleanup users with changed school ids from salesforce School info in admin, Sentry message on school error during lead creation Feb 7, 2024
@mwvolo mwvolo requested a review from Dantemss February 7, 2024 21:18
@mwvolo mwvolo added accounts To help tag product on project boards and removed bug labels Feb 7, 2024
Copy link
Member

@Dantemss Dantemss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can still have the case where sf_contact.school_id != user.school.salesforce_id. If user.school is supposed to reflect what's in Salesforce, I think we just need to set user.school in app/routines/update_user_contact_info.rb to match whatever is in the contact? Basically where it says school = just change to user.school =

@@ -121,31 +121,19 @@ def exec(user:)
event_data: { lead_id: lead.id.to_s }
)
else
if lead.errors.messages.inspect.include? == 'INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY'
Sentry.capture_message("Invalid school (#{user.school.salesforce_id}) for user (#{user.id})")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me that's what the message means but if you are sure from what you've seen in SF go for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This took a bit of command line testing to figure out what SF is spitting out as an error - but yeah, this is the one's that need catching with the ID of that school so it can at least be manually cleaned up (maybe this can move to the admin or some automated process in the future).

@Dantemss
Copy link
Member

Dantemss commented Feb 7, 2024

@mwvolo
Copy link
Member Author

mwvolo commented Feb 7, 2024

Nvm, I see this line https://github.com/openstax/accounts/blob/main/app/routines/update_user_contact_info.rb#L132 is supposed to do that

Ah, this might be where I need to put something to update all the users with that old school id!
Again, future PR. Thanks for all the pointers!

@mwvolo mwvolo merged commit 920c524 into main Feb 7, 2024
8 checks passed
@mwvolo mwvolo deleted the stale-salesforce-school branch February 7, 2024 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accounts To help tag product on project boards enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants