Skip to content

Commit

Permalink
Merge pull request #528 from datacite/contact-export
Browse files Browse the repository at this point in the history
Allow export of multiple contacts with same email but different provider id
  • Loading branch information
Martin Fenner authored May 19, 2020
2 parents 1bd76be + 0232fcb commit b31a661
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
20 changes: 11 additions & 9 deletions app/controllers/export_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,25 @@ def contacts
csv = headers.to_csv

# Use a hashmap for the contacts to avoid duplicated
contacts = Hash.new
contacts = {}

add_contact = Proc.new { |contacts, email, id, firstname, lastname, type|
if email
unless contacts.has_key?(email)
contacts[email] = {
fabrica_id = id + "-" + email
unless contacts.has_key?(fabrica_id)
contacts[fabrica_id] = {
'fabricaAccountId' => id,
'fabricaId' => id + "-" + email,
'fabricaId' => fabrica_id,
'email' => email,
'firstName' => firstname,
'lastName' => lastname.present? ? lastname : email,
}
end

if contacts[email].has_key?('type')
contacts[email]['type'] += ";" + type
if contacts[fabrica_id].has_key?('type')
contacts[fabrica_id]['type'] += ";" + type
else
contacts[email]['type'] = type
contacts[fabrica_id]['type'] = type
end
end
}
Expand All @@ -90,11 +92,11 @@ def contacts
end
end

contacts.each do |email, contact|
contacts.each do |_, contact|
csv += CSV.generate_line [
contact['fabricaAccountId'],
contact['fabricaId'],
email,
contact['email'],
contact['firstName'],
contact['lastName'],
contact['type'],
Expand Down
18 changes: 14 additions & 4 deletions spec/requests/exports_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,45 +82,55 @@

expect(last_response.status).to eq(200)
csv = last_response.body.lines
expect(csv.length).to eq(5)
expect(csv.length).to eq(9)
expect(csv[0]).to eq("fabricaAccountId,fabricaId,email,firstName,lastName,type\n")
expect(csv[1]).to start_with("VIVA,[email protected],[email protected],Kristian,Garza,technical;secondaryTechnical")
expect(csv[2]).to start_with("VIVA,[email protected],[email protected],Martin,Fenner,service;secondaryService")
expect(csv[3]).to start_with("VIVA,[email protected],[email protected],Robin,Dasler,voting")
expect(csv[4]).to start_with("VIVA,[email protected],[email protected],Trisha,Cruse,billing;secondaryBilling")
expect(csv[5]).to start_with("UVA,[email protected],[email protected],Kristian,Garza,technical;secondaryTechnical")
expect(csv[6]).to start_with("UVA,[email protected],[email protected],Martin,Fenner,service;secondaryService")
expect(csv[7]).to start_with("UVA,[email protected],[email protected],Robin,Dasler,voting")
expect(csv[8]).to start_with("UVA,[email protected],[email protected],Trisha,Cruse,billing;secondaryBilling")
end

it 'returns all contacts from date', vcr: false do
get "/export/contacts?from-date=#{Date.today}", nil, admin_headers

expect(last_response.status).to eq(200)
csv = last_response.body.lines
expect(csv.length).to eq(5)
expect(csv.length).to eq(9)
expect(csv[0]).to eq("fabricaAccountId,fabricaId,email,firstName,lastName,type\n")
expect(csv[1]).to start_with("VIVA,[email protected],[email protected],Kristian,Garza,technical;secondaryTechnical")
expect(csv[2]).to start_with("VIVA,[email protected],[email protected],Martin,Fenner,service;secondaryService")
expect(csv[3]).to start_with("VIVA,[email protected],[email protected],Robin,Dasler,voting")
expect(csv[4]).to start_with("VIVA,[email protected],[email protected],Trisha,Cruse,billing;secondaryBilling")
expect(csv[5]).to start_with("UVA,[email protected],[email protected],Kristian,Garza,technical;secondaryTechnical")
expect(csv[6]).to start_with("UVA,[email protected],[email protected],Martin,Fenner,service;secondaryService")
expect(csv[7]).to start_with("UVA,[email protected],[email protected],Robin,Dasler,voting")
expect(csv[8]).to start_with("UVA,[email protected],[email protected],Trisha,Cruse,billing;secondaryBilling")
end

it 'returns voting contacts', vcr: false do
get "/export/contacts?type=voting", nil, admin_headers

expect(last_response.status).to eq(200)
csv = last_response.body.lines
expect(csv.length).to eq(2)
expect(csv.length).to eq(3)
expect(csv[0]).to eq("fabricaAccountId,fabricaId,email,firstName,lastName,type\n")
expect(csv[1]).to start_with("VIVA,[email protected],[email protected],Robin,Dasler,voting")
expect(csv[2]).to start_with("UVA,[email protected],[email protected],Robin,Dasler,voting")
end

it 'returns billing contacts', vcr: false do
get "/export/contacts?type=billing", nil, admin_headers

expect(last_response.status).to eq(200)
csv = last_response.body.lines
expect(csv.length).to eq(2)
expect(csv.length).to eq(3)
expect(csv[0]).to eq("fabricaAccountId,fabricaId,email,firstName,lastName,type\n")
expect(csv[1]).to start_with("VIVA,[email protected],[email protected],Trisha,Cruse,billing;secondaryBilling")
expect(csv[2]).to start_with("UVA,[email protected],[email protected],Trisha,Cruse,billing;secondaryBilling")
end
end
end

0 comments on commit b31a661

Please sign in to comment.