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
Merged
Show file tree
Hide file tree
Changes from 9 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
24 changes: 24 additions & 0 deletions app/routines/update_school_salesforce_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,30 @@ def call
break if sf_schools.length < BATCH_SIZE
end

# Loop through stale schools and update users associated with them to prevent sync issues
School.where.not(updated_salesforce_id: nil) 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
end

# 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.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.

end
end

log("Finished updating #{schools_updated} schools")
Sentry.capture_check_in('update-school-salesforce', :ok, check_in_id: check_in_id)
end
Expand Down
2 changes: 2 additions & 0 deletions app/routines/update_user_contact_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.

SecurityLog.create!(
user: user,
event_type: :attempted_to_add_school_not_cached_yet,
Expand Down
72 changes: 31 additions & 41 deletions app/views/admin/users/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
</div>
</div>

<% if @user.sheerid_verification_id %>
<div class="form-group">
<%= f.label :sheerid_verification_id, class: "col-sm-2 control-label" %>
<div class="col-sm-10">
Expand All @@ -130,6 +131,35 @@
</p>
</div>
</div>
<% end %>

<% if @user.school %>
<div class="form-group">
<%= f.label :school, class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<p class="form-control-static">
<% sf_id = @user.school.salesforce_id %>
<% instance_url = ActiveForce.sfdc_client.instance_url rescue nil %>
<% if instance_url.nil? %>
<%= @user.school.name %>
<% else %>
<%= link_to @user.school.name, instance_url + "/" + sf_id, target: '_blank' %>
<% end %>
</p>
</div>
</div>
<% end %>

<% if @user.which_books %>
<div class="form-group">
<%= f.label :which_books, class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<p class="form-control-static">
<%= @user.which_books %>
</p>
</div>
</div>
<% end %>

<% if @user.sheerid_reported_school %>
<div class="form-group">
Expand All @@ -149,11 +179,7 @@
<% sf_id = @user.salesforce_lead_id %>
<p class="form-control-static">
<% if sf_id.nil? %>
<%= link_to 'Not Set. Force Push?', force_update_lead_admin_user_path(@user),
remote: true, method: :post,
class: 'btn btn-default',
data: {confirm: "Are you sure you want to force sync this lead with Salesforce?"}
%>
No Lead
<% else %>
<% instance_url = ActiveForce.sfdc_client.instance_url rescue nil %>
<% if instance_url.nil? %>
Expand Down Expand Up @@ -323,33 +349,6 @@
</div>
</div>

<div class="form-group">
<%= f.label :is_kip, 'Key Institutional Partner?', class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<div class="checkbox">
<%= f.check_box :is_kip, style: 'margin-left: 0px' %>
</div>
</div>
</div>

<div class="form-group">
<%= f.label :is_b_r_i_user, 'BRI User?', class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<div class="checkbox">
<%= f.check_box :is_b_r_i_user, style: 'margin-left: 0px' %>
</div>
</div>
</div>

<div class="form-group">
<%= f.label :grant_tutor_access, 'Grant Tutor Access?', class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<div class="checkbox">
<%= f.check_box :grant_tutor_access, style: 'margin-left: 0px' %>
</div>
</div>
</div>

<div class="form-group">
<%= f.label :is_administrator, 'Administrator?', class: "col-sm-2 control-label" %>
<div class="col-sm-10">
Expand All @@ -368,15 +367,6 @@
</div>
</div>

<div class="form-group">
<%= f.label :opt_out_of_cookies, 'Opt Out of Cookies?', class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<div class="checkbox">
<%= f.check_box :opt_out_of_cookies, style: 'margin-left: 0px' %>
</div>
</div>
</div>

<% if @user.external_uuids.any? %>
<br>

Expand Down
76 changes: 36 additions & 40 deletions app/views/admin/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
<th>Last Name</th>
<th>Faculty Status</th>
<th>Role</th>
<th>School Type</th>
<th>KIP?</th>
<th>Grant Tutor Access?</th>
<th>School</th>
<th>Salesforce</th>
<th></th>
<th></th>
</tr>
<% @handler_result.outputs[:items].each do |user| %>
Expand All @@ -52,11 +52,38 @@
<td><%= link_to user.id, edit_admin_user_path(user), target: '_blank' %></td>
<td><%= user.first_name || '---' %></td>
<td><%= user.last_name || '---' %></td>
<td><%= user.faculty_status %></td>
<td><%= user.unknown_role? ? "Unknown" : user.role.capitalize %></td>
<td><%= user.school_type %></td>
<td><%= user.is_kip ? 'Yes' : 'No' %></td>
<td><%= user.grant_tutor_access ? 'Yes' : 'No' %></td>
<td><%= user.faculty_status.humanize %></td>
<td><%= user.school_type.humanize %></td>
<td>
<% if user.school %>
<% sf_id = user.school.salesforce_id %>
<% instance_url = ActiveForce.sfdc_client.instance_url rescue nil %>
<% if instance_url.nil? %>
<%= sf_id %>
<% else %>
<%= link_to user.school.name, instance_url + "/" + sf_id, target: '_blank' %>
<% end %>
<% end %>
</td>
<td>
<% if !user.salesforce_contact_id.nil? %>
<% sf_id = user.salesforce_contact_id %>
<% sf_type = 'Contact' %>
<% elsif !user.salesforce_lead_id.nil? %>
<% sf_id = user.salesforce_lead_id%>
<% sf_type = 'Lead' %>
<% end %>

<% if !sf_id.nil? %>
<% instance_url = ActiveForce.sfdc_client.instance_url rescue nil %>
<% if instance_url.nil? %>
<%= sf_id %>
<% else %>
<%= link_to sf_type, instance_url + "/" + sf_id, target: '_blank' %>
<% end %>
<% end %>
</td>
<td><%= link_to admin_security_log_path(search: { query: "user_id:#{user.id}" }) do %><i class="fa fa-history"></i><% end %></td>
<td>
<%= link_to become_admin_user_path(user),
method: :post,
Expand All @@ -67,16 +94,14 @@
</tr>
<tr class="<%= zebra %> details">
<td></td>
<td colspan=6>
<td colspan=8>
<div>
<div style="float:right">
<span class="created"><i>Created:</i> <%= user.created_at.in_time_zone('Central Time (US & Canada)') %></span>
<br/>
<span class="updated"><i>Updated:</i> <%= user.updated_at.in_time_zone('Central Time (US & Canada)') %></span>
</div>

<span class="full-name"><%= user.full_name || '(no name)' %></span> |

<% if user.is_administrator %>
<span class="admin">Administrator</span> |
<% end %>
Expand All @@ -85,39 +110,10 @@
<span class="test">Test User</span> |
<% end %>

<% sf_id = user.salesforce_contact_id %>

<% if sf_id.present? %>
<span class="sf">
<i>Salesforce:</i>

<% instance_url = ActiveForce.sfdc_client.instance_url rescue nil %>
<% if instance_url.nil? %>
<%= sf_id %>
<% else %>
<%= link_to sf_id, instance_url + "/" + sf_id, target: '_blank' %>
<% end %>
</span> |
<% end %>

<% security_log_params = { query: "user_id:#{user.id}" } %>

<span class="security_log">
<%= link_to "Security Log", admin_security_log_path(search: security_log_params), target: '_blank' %>
</span> |

<span class="state">
<i>State:</i> <%= user.state %>
</span> |

<span class="uuid">
<i>UUID:</i> <%= user.uuid %>
</span>

<span class="support_identifier">
<i>Support Identifier:</i> <%= user.support_identifier || '(no support_identifier)' %>
</span>

<% if user.self_reported_school.present? %>
| <span class="school">
<i>Self-reported school:</i> <%= user.self_reported_school %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUpdatedSalesforceIdToSchools < ActiveRecord::Migration[5.2]
def change
add_column :schools, :updated_salesforce_id, :text
end
end
3 changes: 2 additions & 1 deletion 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_01_23_173834) do
ActiveRecord::Schema.define(version: 2024_02_01_190613) do

# These are extensions that must be enabled in order to support this database
enable_extension "citext"
Expand Down Expand Up @@ -361,6 +361,7 @@
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
3 changes: 1 addition & 2 deletions spec/features/admin/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

page.all(:css, '.expand').each(&:click)

expect(page).to have_content("#{@admin_user.full_name} | Administrator |")
expect(page).to have_content("#{@sf_user.full_name} | Salesforce: booyah")
expect(page).to have_content("Administrator |")
end

it "can bring up the edit page without exploding" do
Expand Down
Loading