Skip to content

Commit

Permalink
fixes shitty bug where existing users were getting attributed incorre…
Browse files Browse the repository at this point in the history
…ctly to events when an email was present but unique identifier was not
  • Loading branch information
CollinSchneider committed Apr 29, 2024
1 parent b668dc4 commit 7fee72e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ def provided_user_properties
def pre_existing_user_profile_for_provided_user_data
return @pre_existing_user_profile_for_provided_user_data if defined?(@pre_existing_user_profile_for_provided_user_data)
@pre_existing_user_profile_for_provided_user_data ||= begin
return if !provided_unique_user_identifier.present? && !provided_user_properties['email'].present?
profile_by_identifier = workspace.analytics_user_profiles.find_by(user_unique_identifier: provided_unique_user_identifier)
return profile_by_identifier if profile_by_identifier.present? || provided_user_properties['email'].blank?
workspace.analytics_user_profiles.find_by(user_unique_identifier: nil, email: provided_user_properties['email'])
if provided_unique_user_identifier.present?
user_by_identifier = workspace.analytics_user_profiles.find_by(user_unique_identifier: provided_unique_user_identifier)
return user_by_identifier if user_by_identifier.present?
end
if provided_user_properties['email'].present?
user_by_email = workspace.analytics_user_profiles.find_by(email: provided_user_properties['email'], user_unique_identifier: nil)
return user_by_email if user_by_email.present?
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,18 @@ def parsed_event(swishjam_api_key:, name: 'some_random_event', timestamp: 10.min
anonymous_user_profile.reload
expect(anonymous_user_profile.merged_into_analytics_user_profile_id).to eq(existing_identified_user_profile.id)
end

it 'creates a new user profile if just an email is provided and there are no matching users with that email' do
existing_resend_user = FactoryBot.create(:analytics_user_profile, workspace: @workspace, user_unique_identifier: nil, email: 'resend@user.com')
user = described_class.new(
parsed_event(swishjam_api_key: @public_key, name: 'update_user', properties: { device_identifier: 'foo', user: { email: 'new@email.com' }})
).get_user_profile_and_associate_to_device_if_necessary!

expect(@workspace.analytics_user_profiles.count).to be(2)
expect(user.email).to eq('new@email.com')
expect(user.user_unique_identifier).to be(nil)

expect(existing_resend_user.reload.email).to eq('resend@user.com')
end
end
end

0 comments on commit 7fee72e

Please sign in to comment.