Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def index

respond_to do |format|
format.html
format.csv { send_data Exports::ExportRequestService.new(@requests).generate_csv, filename: "Requests-#{Time.zone.today}.csv" }
format.csv { send_data Exports::ExportRequestService.new(@requests, current_organization).generate_csv, filename: "Requests-#{Time.zone.today}.csv" }
end
end

Expand Down
14 changes: 12 additions & 2 deletions app/services/exports/export_request_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ module Exports
class ExportRequestService
DELETED_ITEMS_COLUMN_HEADER = '<DELETED_ITEMS>'.freeze

def initialize(requests)
# @param requests [Array<Request>]
# @param organization [Organization]
def initialize(requests, organization)
@requests = requests.includes(:partner, {item_requests: :item})
@organization_items = organization.items.select("DISTINCT ON (LOWER(name)) items.name").order("LOWER(name) ASC")
end

def generate_csv
Expand Down Expand Up @@ -84,11 +87,18 @@ def compute_item_headers
end
end

# Include inactive items or items that are otherwise not in any item_requests
@organization_items.each do |item|
if item_names.exclude?(item.name)
item_names << item.name
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can item_names be a set instead of an array? Then this is automatically handled.

end
end

# Adding this to handle cases in which a requested item
# has been deleted. Normally this wouldn't be necessary,
# but previous versions of the application would cause
# this orphaned data
item_names.sort.uniq << DELETED_ITEMS_COLUMN_HEADER
item_names.to_a.sort_by(&:downcase) << DELETED_ITEMS_COLUMN_HEADER
end

def build_row_data(request)
Expand Down
99 changes: 96 additions & 3 deletions spec/services/exports/export_request_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,26 @@

let(:item_deleted1) { create :item, :inactive, name: "Inactive Diapers1" }
let(:item_deleted2) { create :item, :inactive, name: "Inactive Diapers2" }
let!(:unrequested_item) { create :item, name: "Unrequested Item", organization: org }
let!(:inactive_item) { create :item, name: "Inactive Item", active: false, organization: org }

# Added to ensure sorting is working correctly, otherwise is duplicate behavior
let!(:apple_item) { create :item, name: "apple", organization: org }
let!(:banana_item) { create :item, name: "Banana", organization: org }
let!(:zebra_item) { create :item, name: "Zebra", organization: org }

let!(:partner) { create :partner, organization: org, name: "Howdy Partner" }

let!(:inactive_item_request) do
create(:request,
:started,
:child,
:with_item_requests,
organization: org,
partner: partner,
request_items: [{ item_id: inactive_item.id, quantity: 777 }])
end

let!(:request_3t) do
create(:request,
:started,
Expand Down Expand Up @@ -91,7 +109,7 @@
end

subject do
described_class.new(Request.all).generate_csv_data
described_class.new(Request.all, organization: org).generate_csv_data
end

context "with custom units feature enabled" do
Expand All @@ -110,12 +128,17 @@
"3T Diapers",
"4T Diapers",
"4T Diapers - packs",
"apple",
"Banana",
"Inactive Item",
"Unrequested Item",
"Zebra",
"<DELETED_ITEMS>"
])
end

it "includes rows for each request" do
expect(subject.count).to eq(7)
expect(subject.count).to eq(8)
end

it "has expected data for the 3T Diapers request" do
Expand All @@ -128,6 +151,11 @@
150, # 3T Diapers
0, # 4T Diapers
0, # 4T Diapers - packs
0, # apple
0, # Banana
0, # Inactive Item
0, # Unrequested Item
0, # Zebra
0 # <DELETED_ITEMS>
])
end
Expand All @@ -142,6 +170,11 @@
0, # 3T Diapers
0, # 4T Diapers
0, # 4T Diapers - packs
0, # apple
0, # Banana
0, # Inactive Item
0, # Unrequested Item
0, # Zebra
0 # <DELETED_ITEMS>
])
end
Expand All @@ -156,6 +189,11 @@
0, # 3T Diapers
0, # 4T Diapers
0, # 4T Diapers - packs
0, # apple
0, # Banana
0, # Inactive Item
0, # Unrequested Item
0, # Zebra
400 # <DELETED_ITEMS>
])
end
Expand All @@ -170,6 +208,11 @@
2, # 3T Diapers
0, # 4T Diapers
4, # 4T Diapers - packs
0, # apple
0, # Banana
0, # Inactive Item
0, # Unrequested Item
0, # Zebra
0 # <DELETED_ITEMS>
])
end
Expand All @@ -184,6 +227,11 @@
0, # 3T Diapers
77, # 4T Diapers
0, # 4T Diapers - packs
0, # apple
0, # Banana
0, # Inactive Item
0, # Unrequested Item
0, # Zebra
0 # <DELETED_ITEMS>
])
end
Expand All @@ -198,6 +246,11 @@
0, # 3T Diapers
0, # 4T Diapers
1, # 4T Diapers - packs
0, # apple
0, # Banana
0, # Inactive Item
0, # Unrequested Item
0, # Zebra
0 # <DELETED_ITEMS>
])
end
Expand All @@ -213,6 +266,11 @@
0, # 3T Diapers
0, # 4T Diapers
1, # 4T Diapers - packs
0, # apple
0, # Banana
0, # Inactive Item
0, # Unrequested Item
0, # Zebra
0 # <DELETED_ITEMS>
])
end
Expand All @@ -234,12 +292,17 @@
"2T Diapers -- UPDATED",
"3T Diapers",
"4T Diapers",
"apple",
"Banana",
"Inactive Item",
"Unrequested Item",
"Zebra",
"<DELETED_ITEMS>"
])
end

it "includes rows for each request" do
expect(subject.count).to eq(7)
expect(subject.count).to eq(8)
end

it "has expected data for the 3T Diapers request" do
Expand All @@ -251,6 +314,11 @@
0, # 2T Diapers
150, # 3T Diapers
0, # 4T Diapers
0, # apple
0, # Banana
0, # Inactive Item
0, # Unrequested Item
0, # Zebra
0 # <DELETED_ITEMS>
])
end
Expand All @@ -264,6 +332,11 @@
100, # 2T Diapers
0, # 3T Diapers
0, # 4T Diapers
0, # apple
0, # Banana
0, # Inactive Item
0, # Unrequested Item
0, # Zebra
0 # <DELETED_ITEMS>
])
end
Expand All @@ -277,6 +350,11 @@
0, # 2T Diapers
0, # 3T Diapers
0, # 4T Diapers
0, # apple
0, # Banana
0, # Inactive Item
0, # Unrequested Item
0, # Zebra
400 # <DELETED_ITEMS>
])
end
Expand All @@ -290,6 +368,11 @@
3, # 2T Diapers
2, # 3T Diapers
4, # 4T Diapers
0, # apple
0, # Banana
0, # Inactive Item
0, # Unrequested Item
0, # Zebra
0 # <DELETED_ITEMS>
])
end
Expand All @@ -303,6 +386,11 @@
0, # 2T Diapers
0, # 3T Diapers
77, # 4T Diapers
0, # apple
0, # Banana
0, # Inactive Item
0, # Unrequested Item
0, # Zebra
0 # <DELETED_ITEMS>
])
end
Expand All @@ -316,6 +404,11 @@
0, # 2T Diapers
0, # 3T Diapers
1, # 4T Diapers
0, # apple
0, # Banana
0, # Inactive Item
0, # Unrequested Item
0, # Zebra
0 # <DELETED_ITEMS>
])
end
Expand Down
Loading