diff --git a/app/controllers/export_controller.rb b/app/controllers/export_controller.rb index 7971a313f..6e0852529 100644 --- a/app/controllers/export_controller.rb +++ b/app/controllers/export_controller.rb @@ -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| @@ -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) diff --git a/spec/requests/exports_spec.rb b/spec/requests/exports_spec.rb index ed19526fc..11e4df5bb 100644 --- a/spec/requests/exports_spec.rb +++ b/spec/requests/exports_spec.rb @@ -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) @@ -66,5 +66,25 @@ 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") 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,VIVA-robin@example.com,robin@example.com,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,VIVA-trisha@example.com,trisha@example.com,Trisha,Cruse,billing;secondaryBilling") + end end end