diff --git a/app/controllers/export_controller.rb b/app/controllers/export_controller.rb index ca503b72e..55434009c 100644 --- a/app/controllers/export_controller.rb +++ b/app/controllers/export_controller.rb @@ -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 } @@ -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'], diff --git a/spec/requests/exports_spec.rb b/spec/requests/exports_spec.rb index 442c8d542..b8e6d93b3 100644 --- a/spec/requests/exports_spec.rb +++ b/spec/requests/exports_spec.rb @@ -82,12 +82,16 @@ 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,VIVA-kristian@example.com,kristian@example.com,Kristian,Garza,technical;secondaryTechnical") expect(csv[2]).to start_with("VIVA,VIVA-martin@example.com,martin@example.com,Martin,Fenner,service;secondaryService") expect(csv[3]).to start_with("VIVA,VIVA-robin@example.com,robin@example.com,Robin,Dasler,voting") expect(csv[4]).to start_with("VIVA,VIVA-trisha@example.com,trisha@example.com,Trisha,Cruse,billing;secondaryBilling") + expect(csv[5]).to start_with("UVA,UVA-kristian@example.com,kristian@example.com,Kristian,Garza,technical;secondaryTechnical") + expect(csv[6]).to start_with("UVA,UVA-martin@example.com,martin@example.com,Martin,Fenner,service;secondaryService") + expect(csv[7]).to start_with("UVA,UVA-robin@example.com,robin@example.com,Robin,Dasler,voting") + expect(csv[8]).to start_with("UVA,UVA-trisha@example.com,trisha@example.com,Trisha,Cruse,billing;secondaryBilling") end it 'returns all contacts from date', vcr: false do @@ -95,12 +99,16 @@ 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,VIVA-kristian@example.com,kristian@example.com,Kristian,Garza,technical;secondaryTechnical") expect(csv[2]).to start_with("VIVA,VIVA-martin@example.com,martin@example.com,Martin,Fenner,service;secondaryService") expect(csv[3]).to start_with("VIVA,VIVA-robin@example.com,robin@example.com,Robin,Dasler,voting") expect(csv[4]).to start_with("VIVA,VIVA-trisha@example.com,trisha@example.com,Trisha,Cruse,billing;secondaryBilling") + expect(csv[5]).to start_with("UVA,UVA-kristian@example.com,kristian@example.com,Kristian,Garza,technical;secondaryTechnical") + expect(csv[6]).to start_with("UVA,UVA-martin@example.com,martin@example.com,Martin,Fenner,service;secondaryService") + expect(csv[7]).to start_with("UVA,UVA-robin@example.com,robin@example.com,Robin,Dasler,voting") + expect(csv[8]).to start_with("UVA,UVA-trisha@example.com,trisha@example.com,Trisha,Cruse,billing;secondaryBilling") end it 'returns voting contacts', vcr: false do @@ -108,9 +116,10 @@ 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,VIVA-robin@example.com,robin@example.com,Robin,Dasler,voting") + expect(csv[2]).to start_with("UVA,UVA-robin@example.com,robin@example.com,Robin,Dasler,voting") end it 'returns billing contacts', vcr: false do @@ -118,9 +127,10 @@ 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,VIVA-trisha@example.com,trisha@example.com,Trisha,Cruse,billing;secondaryBilling") + expect(csv[2]).to start_with("UVA,UVA-trisha@example.com,trisha@example.com,Trisha,Cruse,billing;secondaryBilling") end end end