Skip to content

Commit

Permalink
Adds query for all an organization's items to the CSV export service,…
Browse files Browse the repository at this point in the history
… updates spec to test new behavior
  • Loading branch information
katelovescode committed Jan 31, 2024
1 parent dc8561d commit f48bd71
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
13 changes: 4 additions & 9 deletions app/services/exports/export_distributions_csv_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def initialize(distributions:, filters: [])
# service object.
@distributions = distributions
@filters = filters
@organization = @distributions.first.organization
end

def generate_csv
Expand Down Expand Up @@ -114,15 +115,7 @@ def base_headers
def item_headers
return @item_headers if @item_headers

item_names = Set.new

distributions.each do |distribution|
distribution.line_items.each do |line_item|
item_names.add(line_item.item.name)
end
end

@item_headers = item_names.sort
@item_headers = @organization.items.uniq.sort_by(&:created_at).pluck(:name)
end

def build_row_data(distribution)
Expand All @@ -133,6 +126,8 @@ def build_row_data(distribution)
distribution.line_items.each do |line_item|
item_name = line_item.item.name
item_column_idx = headers_with_indexes[item_name]
next unless item_column_idx

row[item_column_idx] += line_item.quantity
end

Expand Down
1 change: 1 addition & 0 deletions spec/requests/partners_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@

context "csv" do
let(:response_format) { 'csv' }
before { create(:distribution, partner: partner) }

it { is_expected.to be_successful }
end
Expand Down
53 changes: 45 additions & 8 deletions spec/services/exports/export_distributions_csv_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,13 @@
let(:item_id) { distributions.flatten.first.line_items.first.item_id }
let(:filters) { {by_item_id: item_id} }
let(:item_name) { Item.find(item_id).name }
let(:organization) { distributions.first.organization }

let(:all_org_items) { Item.where(organization:).uniq.sort_by(&:created_at) }

let(:total_item_quantities) do
template = item_names.index_with(0)
# binding.pry
template = all_org_items.pluck(:name).index_with(0)

items_lists.map do |items_list|
row = template.dup
Expand All @@ -64,7 +68,7 @@
end
end

let(:expected_headers) do
let(:non_item_headers) do
[
"Partner",
"Date of Distribution",
Expand All @@ -76,14 +80,10 @@
"State",
"Agency Representative",
"Comments"
] + expected_item_headers
]
end

let(:expected_item_headers) do
expect(item_names).not_to be_empty

item_names
end
let(:expected_headers) { non_item_headers + all_org_items.pluck(:name) }

it 'should match the expected content for the csv' do
expect(subject[0]).to eq(expected_headers)
Expand All @@ -107,5 +107,42 @@
expect(subject[idx + 1]).to eq(row)
end
end

context 'when a new item is added' do
let!(:original_columns_count) { organization.items.size + non_item_headers.size }
let(:new_item_name) { "new item" }
before do
create(:item, name: new_item_name, organization:)
end

it 'should add it to the end of the row' do
expect(subject[0].uniq).to eq(expected_headers)
.and end_with(new_item_name)
.and have_attributes(size: original_columns_count + 1)
end

it 'should show up with a 0 quantity if there are no distributions' do
distributions.zip(total_item_quantities).each_with_index do |(distribution, total_item_quantity), idx|
row = [
distribution.partner.name,
distribution.issued_at.strftime("%m/%d/%Y"),
distribution.storage_location.name,
distribution.line_items.where(item_id: item_id).total,
distribution.cents_to_dollar(distribution.line_items.total_value),
distribution.delivery_method,
"$#{distribution.shipping_cost.to_f}",
distribution.state,
distribution.agency_rep,
distribution.comment
]

row += total_item_quantity

expect(subject[idx + 1]).to eq(row)
.and end_with(0)
.and have_attributes(size: original_columns_count + 1)
end
end
end
end
end

0 comments on commit f48bd71

Please sign in to comment.