Skip to content

Commit

Permalink
filter contact download by type
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Mar 9, 2020
1 parent 4c7a262 commit c9bcb0b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
24 changes: 16 additions & 8 deletions app/controllers/export_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,21 @@ def contacts
}

providers.each do |provider|
add_contact.call(contacts, provider.technical_contact.email, provider.symbol, provider.technical_contact.given_name, provider.technical_contact.family_name, 'technical') if provider.technical_contact.present?
add_contact.call(contacts, provider.secondary_technical_contact.email, provider.symbol, provider.secondary_technical_contact.given_name, provider.secondary_technical_contact.family_name, 'secondaryTechnical') if provider.secondary_technical_contact.present?
add_contact.call(contacts, provider.service_contact.email, provider.symbol, provider.service_contact.given_name, provider.service_contact.family_name, 'service') if provider.service_contact.present?
add_contact.call(contacts, provider.secondary_service_contact.email, provider.symbol, provider.secondary_service_contact.given_name, provider.secondary_service_contact.family_name, 'secondaryService') if provider.secondary_service_contact.present?
add_contact.call(contacts, provider.voting_contact.email, provider.symbol, provider.voting_contact.given_name, provider.voting_contact.family_name, 'voting') if provider.voting_contact.present?
add_contact.call(contacts, provider.billing_contact.email, provider.symbol, provider.billing_contact.given_name, provider.billing_contact.family_name, 'billing') if provider.billing_contact.present?
add_contact.call(contacts, provider.secondary_billing_contact.email, provider.symbol, provider.secondary_billing_contact.given_name, provider.secondary_billing_contact.family_name, 'secondaryBilling') if provider.secondary_billing_contact.present?
if params[:type].blank? || params[:type] == "technical"
add_contact.call(contacts, provider.technical_contact.email, provider.symbol, provider.technical_contact.given_name, provider.technical_contact.family_name, 'technical') if provider.technical_contact.present?
add_contact.call(contacts, provider.secondary_technical_contact.email, provider.symbol, provider.secondary_technical_contact.given_name, provider.secondary_technical_contact.family_name, 'secondaryTechnical') if provider.secondary_technical_contact.present?
end
if params[:type].blank? || params[:type] == "service"
add_contact.call(contacts, provider.service_contact.email, provider.symbol, provider.service_contact.given_name, provider.service_contact.family_name, 'service') if provider.service_contact.present?
add_contact.call(contacts, provider.secondary_service_contact.email, provider.symbol, provider.secondary_service_contact.given_name, provider.secondary_service_contact.family_name, 'secondaryService') if provider.secondary_service_contact.present?
end
if params[:type].blank? || params[:type] == "voting"
add_contact.call(contacts, provider.voting_contact.email, provider.symbol, provider.voting_contact.given_name, provider.voting_contact.family_name, 'voting') if provider.voting_contact.present?
end
if params[:type].blank? || params[:type] == "billing"
add_contact.call(contacts, provider.billing_contact.email, provider.symbol, provider.billing_contact.given_name, provider.billing_contact.family_name, 'billing') if provider.billing_contact.present?
add_contact.call(contacts, provider.secondary_billing_contact.email, provider.symbol, provider.secondary_billing_contact.given_name, provider.secondary_billing_contact.family_name, 'secondaryBilling') if provider.secondary_billing_contact.present?
end
end

contacts.each do |email, contact|
Expand All @@ -93,7 +101,7 @@ def contacts
]
end

send_data csv, filename: "contacts-#{Date.today}.csv"
send_data csv, filename: "contacts-#{params.fetch(:type, "all")}-#{Date.today}.csv"
rescue StandardError, Elasticsearch::Transport::Transport::Errors::BadRequest => exception
Raven.capture_exception(exception)

Expand Down
22 changes: 21 additions & 1 deletion spec/requests/exports_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
sleep 1
end

it 'returns contacts', vcr: false do
it 'returns all contacts', vcr: false do
get "/export/contacts", nil, admin_headers

expect(last_response.status).to eq(200)
Expand All @@ -66,5 +66,25 @@
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")
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[0]).to eq("fabricaAccountId,fabricaId,email,firstName,lastName,type\n")
expect(csv[1]).to start_with("VIVA,[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[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")
end
end
end

0 comments on commit c9bcb0b

Please sign in to comment.