From e704f0a54167b6ce32eb0de3aa9291d14f33e3dc Mon Sep 17 00:00:00 2001 From: Dan Ungureanu Date: Wed, 31 Mar 2021 19:19:57 +0300 Subject: [PATCH] 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). --- app/jobs/regular/bulk_invite.rb | 11 +++++++++-- spec/jobs/bulk_invite_spec.rb | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/jobs/regular/bulk_invite.rb b/app/jobs/regular/bulk_invite.rb index 5a4d848d934..3f22282c257 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 f229dbd026f..676d93c8fa6 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