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
end
def assign_member(existing_user, member)
old_user_id = member['user']['id']
member['user'] = existing_user
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
def member_hash(member)
......@@ -42,7 +43,7 @@ module Projects
@default_project_member ||=
begin
default_member = ProjectMember.new(default_project_member_hash)
default_member if default_member.save
default_member.user.id if default_member.save
end
end
......
......@@ -3,17 +3,28 @@ module Projects
module RelationFactory
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:)
relation_sym = parse_relation_sym(relation_sym)
klass = relation_class(relation_sym)
relation_hash.delete('id') #screw IDs for now
update_user_references(relation_hash, members_map)
klass.new(relation_hash)
end
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)
relation_sym.to_s.classify.constantize
end
......
......@@ -6,6 +6,7 @@ describe Projects::ImportExport::MembersMapper, services: true do
let(:user) { create(:user) }
let(:project) { create(:project, :public, name: 'searchable_project') }
let(:user2) { create(:user) }
let(:exported_user_id) { 99 }
let(:exported_members) do
[{
"id" => 2,
......@@ -22,6 +23,7 @@ describe Projects::ImportExport::MembersMapper, services: true do
"invite_accepted_at" => nil,
"user" =>
{
"id" => exported_user_id,
"email" => user2.email,
"username" => user2.username
}
......@@ -34,15 +36,15 @@ describe Projects::ImportExport::MembersMapper, services: true do
end
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
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
def project_member_user_id(id)
members_mapper.map[id].user.id
members_mapper.map[id]
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