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).
This commit is contained in:
Dan Ungureanu 2021-03-31 19:19:57 +03:00 committed by GitHub
parent 5fe1d1f84f
commit e704f0a541
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 4 deletions

View File

@ -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

View File

@ -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