diff --git a/app/controllers/product_drives_controller.rb b/app/controllers/product_drives_controller.rb index 33826f3a79..874957378f 100644 --- a/app/controllers/product_drives_controller.rb +++ b/app/controllers/product_drives_controller.rb @@ -74,6 +74,14 @@ def show @selected_name_filter = filter_params[:by_name] @selected_item_category = filter_params[:by_item_category_id] @product_drive = current_organization.product_drives.includes(:donations).find(params[:id]) + + respond_to do |format| + format.html + format.csv do + send_data Exports::ExportProductDriveParticipantsCSVService.generate_csv(@product_drive), + filename: "Product-Drive-Participants-#{@product_drive.name}-#{Time.zone.today}.csv" + end + end end def update diff --git a/app/services/exports/export_product_drive_participants_csv_service.rb b/app/services/exports/export_product_drive_participants_csv_service.rb new file mode 100644 index 0000000000..f3810ea3cc --- /dev/null +++ b/app/services/exports/export_product_drive_participants_csv_service.rb @@ -0,0 +1,31 @@ +module Exports + class ExportProductDriveParticipantsCSVService + HEADERS = ["Donation Id", "Product Drive Participant", "Storage Location", "Quantity", "In Kind Value"].freeze + + class << self + include ItemsHelper + + def generate_csv(product_drive) + CSV.generate do |csv| + csv << HEADERS + + product_drive.donations.includes(:product_drive_participant).find_each do |donation| + csv << generate_row(donation) + end + end + end + + private + + def generate_row(donation) + [ + donation.id.to_s, + donation.product_drive_participant_id ? donation.product_drive_participant.business_name : nil, + donation.storage_location.name, + donation.line_items.count(&:quantity), + dollar_value(donation.value_per_itemizable) + ] + end + end + end +end diff --git a/app/views/product_drives/show.html.erb b/app/views/product_drives/show.html.erb index d82a6fa2b3..f3c2b18223 100644 --- a/app/views/product_drives/show.html.erb +++ b/app/views/product_drives/show.html.erb @@ -95,6 +95,18 @@ To add additional donations to this product drive, please edit that donation and select this product drive from the appropriate dropdown.

+ diff --git a/spec/requests/product_drives_requests_spec.rb b/spec/requests/product_drives_requests_spec.rb index f8d07e5e4b..5f7c812ad8 100644 --- a/spec/requests/product_drives_requests_spec.rb +++ b/spec/requests/product_drives_requests_spec.rb @@ -291,6 +291,34 @@ expect(response.body).to include("4862167") end + + context "csv" do + it 'is successful' do + product_drive = create(:product_drive, organization: organization) + + get product_drive_path(id: product_drive.id, format: :csv) + + expect(response).to be_successful + expect(response.header['Content-Type']).to include 'text/csv' + + expected_headers = Exports::ExportProductDriveParticipantsCSVService::HEADERS + expect(response.body.chomp.split(",")).to eq(expected_headers) + end + + it 'returns ONLY the associated product drive participants' do + product_drive = create(:product_drive, organization: organization) + participant = create(:product_drive_participant, business_name: "Associated Participant") + create(:donation, product_drive: product_drive, product_drive_participant: participant) + + other_participant = create(:product_drive_participant, business_name: "Unassociated Participant") + create(:donation, product_drive: create(:product_drive, organization: organization), product_drive_participant: other_participant) + + get product_drive_path(id: product_drive.id, format: :csv) + + expect(response.body).to include("Associated Participant") + expect(response.body).not_to include("Unassociated Participant") + end + end end describe "DELETE #destroy" do