FIX: Autocorrect values for dropdown imported user fields (#12572)

FIX: Autocorrect values for dropdown imported user fields (#12572)

When bulk inviting, the uploaded CSV file may contain wrong values for the user fields. This tries to automatically correct them by finding the most similar option (by ignoring the case).

diff --git a/app/jobs/regular/bulk_invite.rb b/app/jobs/regular/bulk_invite.rb
index 5a4d848..3f22282 100644
--- a/app/jobs/regular/bulk_invite.rb
+++ b/app/jobs/regular/bulk_invite.rb
@@ -93,8 +93,15 @@ module Jobs
       user_fields = {}
 
       fields.each do |key, value|
-        @user_fields[key] ||= UserField.where('name ILIKE ?', key).pluck_first(:id) || :nil
-        user_fields[@user_fields[key]] = value if @user_fields[key] != :nil
+        @user_fields[key] ||= UserField.includes(:user_field_options).where('name ILIKE ?', key).first || :nil
+        next if @user_fields[key] == :nil
+
+        # Automatically correct user field value
+        if @user_fields[key].field_type == "dropdown"
+          value = @user_fields[key].user_field_options.find { |ufo| ufo.value.casecmp?(value) }&.value
+        end
+
+        user_fields[@user_fields[key].id] = value
       end
 
       user_fields
diff --git a/spec/jobs/bulk_invite_spec.rb b/spec/jobs/bulk_invite_spec.rb
index f229dbd..676d93c 100644
--- a/spec/jobs/bulk_invite_spec.rb
+++ b/spec/jobs/bulk_invite_spec.rb
@@ -83,12 +83,17 @@ describe Jobs::BulkInvite do
 
     it 'can create staged users and prepulate user fields' do
       user_field = Fabricate(:user_field, name: "Location")
+      user_field_color = Fabricate(:user_field, field_type: "dropdown", name: "Color")
+      user_field_color.user_field_options.create!(value: "Red")
+      user_field_color.user_field_options.create!(value: "Green")
+      user_field_color.user_field_options.create!(value: "Blue")
+
       described_class.new.execute(
         current_user_id: admin.id,
         invites: [
           { email: 'test@discourse.org' }, # new user without user fields
-          { email: user.email, location: 'value 1' }, # existing user with user fields
-          { email: staged_user.email, location: 'value 2' }, # existing staged user with user fields
+          { email: user.email, location: 'value 1', color: 'blue' }, # existing user with user fields
+          { email: staged_user.email, location: 'value 2', color: 'redd' }, # existing staged user with user fields
           { email: 'test2@discourse.org', location: 'value 3' } # new staged user with user fields
         ]
       )
@@ -96,7 +101,9 @@ describe Jobs::BulkInvite do
       expect(Invite.count).to eq(3)
       expect(User.where(staged: true).find_by_email('test@discourse.org')).to eq(nil)
       expect(user.user_fields[user_field.id.to_s]).to eq('value 1')
+      expect(user.user_fields[user_field_color.id.to_s]).to eq('Blue')
       expect(staged_user.user_fields[user_field.id.to_s]).to eq('value 2')
+      expect(staged_user.user_fields[user_field_color.id.to_s]).to eq(nil)
       new_staged_user = User.where(staged: true).find_by_email('test2@discourse.org')
       expect(new_staged_user.user_fields[user_field.id.to_s]).to eq('value 3')
     end

GitHub sha: e704f0a5

This commit appears in #12572 which was approved by markvanlan. It was merged by udan11.