Skip to content
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

[#161990] Change Project ID to Project Name in bulk order import #4062

Merged
merged 2 commits into from
Feb 21, 2024
Merged
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
5 changes: 5 additions & 0 deletions app/services/order_row_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def validate_fields
validate_product
validate_account
validate_existing_order
validate_custom_attributes
end
end

Expand Down Expand Up @@ -293,4 +294,8 @@ def validate_existing_order
add_error(:order_user_mismatch)
end
end

# Projects adds a custom attribute and overrides this method to validate it
def validate_custom_attributes
end
end
2 changes: 1 addition & 1 deletion public/templates/bulk_import_template.csv
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Netid / Email,Chart String,Product Name,Quantity,Order Date,Fulfillment Date,Note,Order,Reference ID
Netid / Email,Chart String,Product Name,Quantity,Order Date,Fulfillment Date,Note,Order,Reference ID,Project Name
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@ module Projects

module OrderRowImporterExtension

def validate_custom_attributes
if field(:project_name).present? && project.nil?
add_error(:project_not_found)
end
end

private

def project
facility.projects.active.find_by name: field(:project_name)
end

def custom_attributes
{ project_id: field(:project_id) }
{ project_id: project&.id }
end

end
Expand Down
4 changes: 3 additions & 1 deletion vendor/engines/projects/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ en:

order_row_importer:
headers:
project_id: Project ID
project_name: Project Name
errors:
project_not_found: Project not found
projects:
projects:
edit:
Expand Down
2 changes: 1 addition & 1 deletion vendor/engines/projects/lib/projects/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Engine < Rails::Engine
OrdersController.send :include, Projects::OrdersControllerExtension
Reservation.send :include, Projects::ReservationExtension
OrderRowImporter.send :prepend, Projects::OrderRowImporterExtension
OrderRowImporter.order_import_headers.concat [:project_id]
OrderRowImporter.order_import_headers.insert(-2, :project_name)

ViewHook.add_hook "reservations.account_field",
"after_account",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
let!(:account_api_record) { create(Settings.testing.api_account_factory.to_sym, account_number: account.account_number) } if Settings.testing.api_account_factory
let(:facility) { create(:setup_facility) }
let(:project) { create(:project, facility:) }
let(:project_name) { project.name }
let(:order_import) { build(:order_import, creator: user, facility:) }
let(:service) { create(:setup_service, facility:) }
let(:user) { create(:user) }
Expand All @@ -30,7 +31,7 @@
"Fulfillment Date" => fulfillment_date,
"Note" => "This is a note",
"Reference ID" => reference_id,
"Project ID" => project.id,
"Project Name" => project_name,
}
CSV::Row.new(ref.keys, ref.values)
end
Expand All @@ -40,10 +41,31 @@
end

describe "#import" do
it "creates an order detail with a project" do
subject.import
expect(subject.errors).to be_empty
expect(OrderDetail.last.project_id).to eq project.id
context "with existing project name" do
it "creates an order detail with a project" do
subject.import
expect(subject.errors).to be_empty
expect(OrderDetail.last.project_id).to eq project.id
end
end

context "with non-existing project name" do
let(:project_name) { "Non-existing project" }

it "produces and error" do
subject.import
expect(subject.errors).to eq ["Project not found"]
end
end

context "with an empty string as a project name" do
let(:project_name) { "" }

it "creates an order detail with no project" do
subject.import
expect(subject.errors).to be_empty
expect(OrderDetail.last.project_id).to be_nil
end
end
end

Expand All @@ -59,8 +81,8 @@
"Note",
"Order",
"Reference ID",
"Project Name",
"Errors",
"Project ID",
],
)
end
Expand Down
Loading