diff --git a/app/representers/api/v1/user_representer.rb b/app/representers/api/v1/user_representer.rb index eb327d631d..3280c38cf5 100644 --- a/app/representers/api/v1/user_representer.rb +++ b/app/representers/api/v1/user_representer.rb @@ -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, diff --git a/app/routines/update_user_contact_info.rb b/app/routines/update_user_contact_info.rb index c78a56d1cd..02accd2d69 100644 --- a/app/routines/update_user_contact_info.rb +++ b/app/routines/update_user_contact_info.rb @@ -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 diff --git a/db/migrate/20231004175741_add_adopter_status_to_user.rb b/db/migrate/20231004175741_add_adopter_status_to_user.rb new file mode 100644 index 0000000000..8426252468 --- /dev/null +++ b/db/migrate/20231004175741_add_adopter_status_to_user.rb @@ -0,0 +1,5 @@ +class AddAdopterStatusToUser < ActiveRecord::Migration[5.2] + def change + add_column :users, :adopter_status, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index ce96944d85..b3f068c00a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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" diff --git a/lib/tasks/accounts/update_adopter_status.rake b/lib/tasks/accounts/update_adopter_status.rake new file mode 100644 index 0000000000..b1e0611518 --- /dev/null +++ b/lib/tasks/accounts/update_adopter_status.rake @@ -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) + + 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 + + break if users.length < 250 + end + end +end