Commit 17c09916 authored by James Lopez's avatar James Lopez
Browse files

fix and refactor note user mapping

parent f861f1461bcb
......@@ -7,7 +7,6 @@ def initialize(exported_members:, user:, project:)
@exported_members = user.admin? ? exported_members : []
@user = user
@project = project
@missing_author_ids = []
# This needs to run first, as second call would be from #map
# which means project members already exist.
......@@ -39,7 +38,6 @@ def default_user_id
def missing_keys_tracking_hash
Hash.new do |_, key|
@missing_author_ids << key
default_user_id
end
end
......
......@@ -80,17 +80,13 @@ def update_user_references
# is left.
def set_note_author
old_author_id = @relation_hash['author_id']
# Users with admin access can map users
@relation_hash['author_id'] = admin_user? ? @members_mapper.map[old_author_id] : @members_mapper.default_user_id
author = @relation_hash.delete('author')
update_note_for_missing_author(author['name']) if missing_author?(old_author_id)
update_note_for_missing_author(author['name']) unless has_author?(old_author_id)
end
def missing_author?(old_author_id)
!admin_user? || @members_mapper.missing_author_ids.include?(old_author_id)
def has_author?(old_author_id)
admin_user? && !@members_mapper.map.keys.include?(old_author_id)
end
def missing_author_note(updated_at, author_name)
......
......@@ -24,7 +24,7 @@
{
"id" => exported_user_id,
"email" => user2.email,
"username" => user2.username
"username" => 'test'
}
},
{
......@@ -48,6 +48,12 @@
exported_members: exported_members, user: user, project: project)
end
it 'includes the exported user ID in the map' do
members_mapper.map[-1]
expect(members_mapper.map.keys).to include(exported_user_id)
end
it 'maps a project member' do
expect(members_mapper.map[exported_user_id]).to eq(user2.id)
end
......@@ -56,12 +62,6 @@
expect(members_mapper.map[-1]).to eq(user.id)
end
it 'updates missing author IDs on missing project member' do
members_mapper.map[-1]
expect(members_mapper.missing_author_ids.first).to eq(-1)
end
it 'has invited members with no user' do
members_mapper.map
......@@ -86,5 +86,34 @@
expect(members_mapper.map[-1]).to eq(user.id)
end
end
context 'chooses the one with an email first' do
before do
exported_members << {
"id" => 2,
"access_level" => 40,
"source_id" => 14,
"source_type" => "Project",
"user_id" => 19,
"notification_level" => 3,
"created_at" => "2016-03-11T10:21:44.822Z",
"updated_at" => "2016-03-11T10:21:44.822Z",
"created_by_id" => nil,
"invite_email" => nil,
"invite_token" => nil,
"invite_accepted_at" => nil,
"user" =>
{
"id" => exported_user_id,
"email" => 'test@email.com',
"username" => user2.username
}
}
end
it 'maps the project member that has a matching email first' do
expect(members_mapper.map[exported_user_id]).to eq(user2.id)
end
end
end
end
......@@ -128,7 +128,7 @@ class ProjectFooModel < FooModel
let(:new_user) { create(:user) }
let(:exported_member) do
{
"id" => 999,
"id" => 111,
"access_level" => 30,
"source_id" => 1,
"source_type" => "Project",
......@@ -137,7 +137,7 @@ class ProjectFooModel < FooModel
"created_at" => "2016-11-18T09:29:42.634Z",
"updated_at" => "2016-11-18T09:29:42.634Z",
"user" => {
"id" => new_user.id,
"id" => 999,
"email" => new_user.email,
"username" => new_user.username
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment