diff --git a/app/services/exports/export_distributions_csv_service.rb b/app/services/exports/export_distributions_csv_service.rb index 376f5ee64c..0a824bfadd 100644 --- a/app/services/exports/export_distributions_csv_service.rb +++ b/app/services/exports/export_distributions_csv_service.rb @@ -10,10 +10,14 @@ def initialize(distributions:, filters: []) # service object. @distributions = distributions @filters = filters - @organization = @distributions.first.organization + @organization = @distributions.present? ? @distributions.first.organization : nil end def generate_csv + # TODO: we may want some error handling here in case there are none, but I'm not sure what the process is; + # should we do a flash[:error]? + return unless distributions + csv_data = generate_csv_data CSV.generate(headers: true) do |csv| @@ -115,7 +119,7 @@ def base_headers def item_headers return @item_headers if @item_headers - @item_headers = @organization.items.uniq.sort_by(&:created_at).pluck(:name) + @item_headers = @organization.items.order(:created_at).distinct.select([:created_at, :name]).map(&:name) end def build_row_data(distribution) diff --git a/spec/requests/partners_requests_spec.rb b/spec/requests/partners_requests_spec.rb index 8060e6021e..c7472bb914 100644 --- a/spec/requests/partners_requests_spec.rb +++ b/spec/requests/partners_requests_spec.rb @@ -141,7 +141,6 @@ context "csv" do let(:response_format) { 'csv' } - before { create(:distribution, partner: partner) } it { is_expected.to be_successful } end diff --git a/spec/services/exports/export_distributions_csv_service_spec.rb b/spec/services/exports/export_distributions_csv_service_spec.rb index 6066168551..16dd8257d2 100644 --- a/spec/services/exports/export_distributions_csv_service_spec.rb +++ b/spec/services/exports/export_distributions_csv_service_spec.rb @@ -56,7 +56,6 @@ let(:all_org_items) { Item.where(organization:).uniq.sort_by(&:created_at) } let(:total_item_quantities) do - # binding.pry template = all_org_items.pluck(:name).index_with(0) items_lists.map do |items_list|