Commit 1d8e02c4 authored by James Lopez's avatar James Lopez

solved a bunch of issues with mapping members/users

parent b92411a0
...@@ -28,9 +28,10 @@ module Projects ...@@ -28,9 +28,10 @@ module Projects
end end
def assign_member(existing_user, member) def assign_member(existing_user, member)
old_user_id = member['user']['id']
member['user'] = existing_user member['user'] = existing_user
project_member = ProjectMember.new(member_hash(member)) project_member = ProjectMember.new(member_hash(member))
@project_member_map[existing_user.id] = project_member if project_member.save @project_member_map[old_user_id] = project_member.user.id if project_member.save
end end
def member_hash(member) def member_hash(member)
...@@ -42,7 +43,7 @@ module Projects ...@@ -42,7 +43,7 @@ module Projects
@default_project_member ||= @default_project_member ||=
begin begin
default_member = ProjectMember.new(default_project_member_hash) default_member = ProjectMember.new(default_project_member_hash)
default_member if default_member.save default_member.user.id if default_member.save
end end
end end
......
...@@ -3,17 +3,28 @@ module Projects ...@@ -3,17 +3,28 @@ module Projects
module RelationFactory module RelationFactory
extend self extend self
OVERRIDES = { snippets: :project_snippets } OVERRIDES = { snippets: :project_snippets }.freeze
USER_REFERENCES = %w(author_id assignee_id updated_by_id).freeze
def create(relation_sym:, relation_hash:, members_map:) def create(relation_sym:, relation_hash:, members_map:)
relation_sym = parse_relation_sym(relation_sym) relation_sym = parse_relation_sym(relation_sym)
klass = relation_class(relation_sym) klass = relation_class(relation_sym)
relation_hash.delete('id') #screw IDs for now relation_hash.delete('id') #screw IDs for now
update_user_references(relation_hash, members_map)
klass.new(relation_hash) klass.new(relation_hash)
end end
private private
#TODO nice to have, optimize this to only get called for specific models
def update_user_references(relation_hash, members_map)
USER_REFERENCES.each do |reference|
if relation_hash[reference]
relation_hash[reference] = members_map[relation_hash[reference]]
end
end
end
def relation_class(relation_sym) def relation_class(relation_sym)
relation_sym.to_s.classify.constantize relation_sym.to_s.classify.constantize
end end
......
...@@ -6,6 +6,7 @@ describe Projects::ImportExport::MembersMapper, services: true do ...@@ -6,6 +6,7 @@ describe Projects::ImportExport::MembersMapper, services: true do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, :public, name: 'searchable_project') } let(:project) { create(:project, :public, name: 'searchable_project') }
let(:user2) { create(:user) } let(:user2) { create(:user) }
let(:exported_user_id) { 99 }
let(:exported_members) do let(:exported_members) do
[{ [{
"id" => 2, "id" => 2,
...@@ -22,6 +23,7 @@ describe Projects::ImportExport::MembersMapper, services: true do ...@@ -22,6 +23,7 @@ describe Projects::ImportExport::MembersMapper, services: true do
"invite_accepted_at" => nil, "invite_accepted_at" => nil,
"user" => "user" =>
{ {
"id" => exported_user_id,
"email" => user2.email, "email" => user2.email,
"username" => user2.username "username" => user2.username
} }
...@@ -34,15 +36,15 @@ describe Projects::ImportExport::MembersMapper, services: true do ...@@ -34,15 +36,15 @@ describe Projects::ImportExport::MembersMapper, services: true do
end end
it 'maps a project member' do it 'maps a project member' do
expect(project_member_user_id(user2.id)).to eq(user2.id) expect(members_mapper.map[exported_user_id]).to eq(user2.id)
end end
it 'defaults to importer project member if it does not exist' do it 'defaults to importer project member if it does not exist' do
expect(project_member_user_id(-1)).to eq(user.id) expect(members_mapper.map[-1]).to eq(user.id)
end end
end end
def project_member_user_id(id) def project_member_user_id(id)
members_mapper.map[id].user.id members_mapper.map[id]
end end
end end
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