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

add adopter status to user as string #1207

Merged
merged 7 commits into from
Oct 25, 2023
Merged
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
6 changes: 6 additions & 0 deletions app/representers/api/v1/user_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ class UserRepresenter < Roar::Decorator
readable: true,
writeable: false

property :adopter_status,
if: ->(user_options:, **) { user_options.try(:fetch, :include_private_data, false) },
type: String,
readable: true,
writeable: false

property :salesforce_contact_id,
type: String,
readable: true,
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 @@ -98,10 +98,12 @@ def call
:unknown_school_location
end

# TODO: This can be removed once OSWeb is migated to using the new adopter_status field for renewal forms
unless sf_contact.adoption_status.blank?
user.using_openstax = ADOPTION_STATUSES[sf_contact.adoption_status]
end

user.adopter_status = sf_contact.adoption_status
user.is_kip = sf_school&.is_kip || sf_school&.is_child_of_kip
user.grant_tutor_access = sf_contact.grant_tutor_access
user.is_b_r_i_user = sf_contact.b_r_i_marketing
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20231004175741_add_adopter_status_to_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAdopterStatusToUser < ActiveRecord::Migration[5.2]
def change
add_column :users, :adopter_status, :string
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: 2023_08_10_185800) do
ActiveRecord::Schema.define(version: 2023_10_04_175741) do

# These are extensions that must be enabled in order to support this database
enable_extension "citext"
Expand Down Expand Up @@ -483,6 +483,7 @@
t.boolean "needs_sync"
t.boolean "sheer_id_webhook_received"
t.jsonb "books_used_details"
t.string "adopter_status"
t.index "lower((first_name)::text)", name: "index_users_on_first_name"
t.index "lower((last_name)::text)", name: "index_users_on_last_name"
t.index "lower((username)::text)", name: "index_users_on_username_case_insensitive"
Expand Down
29 changes: 29 additions & 0 deletions lib/tasks/accounts/update_adopter_status.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace :accounts do
desc 'Update newly created field for adopter status from Salesforce'
# rake accounts:update_adopter_status
task update_adopter_status: [:environment] do
loop do
users = User.where(adopter_status: nil).where.not(salesforce_contact_id: nil).limit(250)

contacts = OpenStax::Salesforce::Remote::Contact.select(
:id,
:adoption_status,
:accounts_uuid
)
.where(id: users.map(&:salesforce_contact_id))
.to_a
.index_by(&:id)
Copy link
Member Author

Choose a reason for hiding this comment

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

@Dantemss it looks like index_by is not supported by ActiveForce. I'll see what I can do to fix this.

Copy link
Member

Choose a reason for hiding this comment

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

Try .to_a before .index_by and it should work, since index_by is an array method.


updated_users = users.map do |user|
contact = contacts[user.salesforce_contact_id]
user.adopter_status = contact.adoption_status
end

updated_users.transaction do
updated_users.each(&:save!)
end
Copy link
Member

Choose a reason for hiding this comment

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

You still need to break out of the loop when there are less than 250 users.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was wondering why this was running for so long locally! Fixed


break if users.length < 250
end
end
end
Loading