-
-
Notifications
You must be signed in to change notification settings - Fork 573
4981 adjuments export with item changes #5173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dorner
merged 8 commits into
rubyforgood:main
from
cyang-el:4981-adjuments-export-with-item-changes
May 23, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
caddfdc
Add specs for adjusment csv export
cyang-el b552391
Add service for csv exporting adjustments
cyang-el c8a01d2
Change header to comply with existing documentation
cyang-el a446909
Lint the specs
cyang-el 08c98e0
Move adjustment export format tests into requests spec
cyang-el 2ead900
Adapt adjustments specs to test csv export text and use module method
cyang-el a4c07e2
Hardcode adjustments_requests spec strings
cyang-el b1be134
Merge branch 'main' into 4981-adjuments-export-with-item-changes
dorner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| module Exports | ||
| module ExportAdjustmentsCSVService | ||
| class << self | ||
| def generate_csv(adjustments, organization) | ||
| CSV.generate(headers: true) do |csv| | ||
| generate_csv_data(adjustments, organization).each { |row| csv << row } | ||
| end | ||
| end | ||
|
|
||
| def generate_csv_data(adjustments, organization) | ||
| item_names = get_item_names(organization) | ||
| headers = [ | ||
| "Created date", "Storage Area", | ||
| "Comment", "# of changes" | ||
| ] + item_names | ||
|
|
||
| [headers] + adjustments.map { |adjustment| build_row(adjustment, item_names) } | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def get_item_names(organization) | ||
| organization.items.order(:name).pluck(:name).uniq | ||
| end | ||
|
|
||
| def build_row(adjustment, item_names) | ||
| row = [ | ||
| adjustment.created_at.strftime("%F"), | ||
| adjustment.storage_location.name, | ||
| adjustment.comment, | ||
| adjustment.line_items.count { |item| !item.quantity.eql?(0) } | ||
| ] | ||
|
|
||
| item_quantities = Hash.new(0) | ||
|
|
||
| adjustment.line_items.each do |line_item| | ||
| item_quantities[line_item.item.name] += line_item.quantity | ||
| end | ||
|
|
||
| item_names.each do |item_name| | ||
| row << item_quantities[item_name] | ||
| end | ||
|
|
||
| row | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
spec/services/exports/export_adjustments_csv_service_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| RSpec.describe Exports::ExportAdjustmentsCSVService do | ||
| # Create organization after items to ensure proper associations | ||
| let!(:item1) { create(:item, name: "item1") } | ||
| let!(:item2) { create(:item, name: "item2") } | ||
| let!(:item3) { create(:item, name: "item3") } | ||
| let!(:item4) { create(:item, name: "item4") } | ||
| let!(:item5) { create(:item, :inactive, name: "item5") } | ||
|
|
||
| # Now create organization and associate items with it | ||
| let(:organization) do | ||
| org = create(:organization) | ||
| [item1, item2, item3, item4, item5].each do |item| | ||
| item.update!(organization_id: org.id) | ||
| end | ||
| org | ||
| end | ||
|
|
||
| let(:sorted_item_names) do | ||
| [item1, item2, item3, item4, item5].map(&:name).sort | ||
| end | ||
|
|
||
| let(:storage_location) { create(:storage_location, organization: organization) } | ||
| let(:user) { create(:user, organization: organization) } | ||
|
|
||
| around do |example| | ||
| travel_to Time.zone.local(2024, 12, 25) | ||
| example.run | ||
| travel_back | ||
| end | ||
|
|
||
| describe "#generate_csv_data" do | ||
| subject { described_class.generate_csv(adjustments, organization) } | ||
|
|
||
| context "with multiple adjustments and items" do | ||
| let(:adjustments) do | ||
| [ | ||
| # 1st adjustment with 2 items | ||
| create(:adjustment, | ||
| user_id: user.id, | ||
| storage_location: storage_location, | ||
| organization: organization, | ||
| comment: "adjustment 1", | ||
| line_items_attributes: [ | ||
| {item_id: item1.id, quantity: 10}, | ||
| {item_id: item2.id, quantity: -5} | ||
| ]), | ||
|
|
||
| # 2nd adjustment with 1 item | ||
| create(:adjustment, | ||
| user_id: user.id, | ||
| storage_location: storage_location, | ||
| organization: organization, | ||
| comment: "adjustment 2", | ||
| line_items_attributes: [ | ||
| {item_id: item3.id, quantity: 3} | ||
| ]), | ||
|
|
||
| # 3rd adjustment with the :with_items trait | ||
| create(:adjustment, :with_items, | ||
| user_id: user.id, | ||
| storage_location: storage_location, | ||
| organization: organization, | ||
| comment: "adjustment 3", | ||
| item: item1, | ||
| item_quantity: 7) | ||
| ] | ||
| end | ||
|
|
||
| it "should include the correct adjustment data" do | ||
| csv = <<~CSV | ||
| Created date,Storage Area,Comment,# of changes,item1,item2,item3,item4,item5 | ||
| 2024-12-25,Smithsonian Conservation Center,adjustment 1,2,10,-5,0,0,0 | ||
| 2024-12-25,Smithsonian Conservation Center,adjustment 2,1,0,0,3,0,0 | ||
| 2024-12-25,Smithsonian Conservation Center,adjustment 3,1,7,0,0,0,0 | ||
| CSV | ||
|
|
||
| expect(subject).to eq(csv) | ||
| end | ||
| end | ||
|
|
||
| context "when there are no adjustments" do | ||
| let(:adjustments) { [] } | ||
|
|
||
| it "returns only headers row" do | ||
| csv = <<~CSV | ||
| Created date,Storage Area,Comment,# of changes,item1,item2,item3,item4,item5 | ||
| CSV | ||
|
|
||
| expect(subject).to eq(csv) | ||
| end | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We know the name, can we put the hardcoded value in here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, let me quickly change that :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dorner should I squash this and the last change to a single commit and then force push? to not create too much commit noise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cyang-el no, that's what merge commits are for. 😄 Force push causes havoc with existing comments.