Skip to content

Commit

Permalink
[NU-57] Bulk orders import event log (#5079)
Browse files Browse the repository at this point in the history
* Fix order import active tab

* Create log event on create order import
  • Loading branch information
joaquinco authored Mar 7, 2025
1 parent 456df60 commit 93d3bfe
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 20 deletions.
23 changes: 16 additions & 7 deletions app/controllers/order_imports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,22 @@ def create_params
end

def create_order_import!
OrderImport.create!(
create_params.merge(
created_by: session_user.id,
upload_file: stored_file,
facility: @current_facility,
),
)
OrderImport.transaction do
order_import = OrderImport.create!(
create_params.merge(
created_by: session_user.id,
upload_file: stored_file,
facility: @current_facility,
),
)
LogEvent.log(
order_import,
:created,
session_user,
)

order_import
end
end

def import_exception_alert(exception)
Expand Down
6 changes: 5 additions & 1 deletion app/models/order_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,15 @@ def error_mode?
@in_error_mode
end

def to_s
upload_file.name
end

private

def create_order_from_imported_row!(row_importer)
Order.create!(
facility: facility,
facility:,
account: row_importer.account,
user: row_importer.user,
created_by_user: creator,
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/shared/_tabnav_order.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
- if current_ability.can?(:show_problems, Order)
= display_tab text("problem_orders"), show_problems_facility_orders_path, action: :show_problems
- if current_ability.can?(:new, OrderImport)
= tab text("add_new"), new_facility_order_import_path(current_facility), (controller.controller_name == 'facility_orders_import')
= tab text("add_new"), new_facility_order_import_path(current_facility), (controller.controller_name == "order_imports")
2 changes: 2 additions & 0 deletions config/locales/views/admin/en.log_events.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ en:
price_change: Order's price was manually changed
resolve_from_problem_queue: Problem order resolved
updated_fulfilled_at: Fulfilled date updated on completed order
order_import:
created: Bulk Order Import
product_user:
create: User added to access list
delete: User removed from access list
Expand Down
55 changes: 50 additions & 5 deletions spec/controllers/order_imports_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def fixture_file(filename)
@params = { facility_id: facility.url_name }
end

context "starting an import" do
describe "new" do
before :each do
@action = :new
@method = :get
Expand All @@ -34,19 +34,64 @@ def fixture_file(filename)
end
end

context "importing orders" do
describe "create" do
before :each do
@action = :create
@method = :post
@params.merge!(
order_import: {
upload_file: upload_file,
fail_on_error: fail_on_error,
send_receipts: send_receipts,
upload_file:,
fail_on_error:,
send_receipts:,
},
)
end

describe "log event creation" do
let(:user) { @admin }
let(:send_receipts) { false }
let(:fail_on_error) { false }

before { sign_in user }

context "on success" do
let(:upload_file) { fixture_file("blank.csv") }

it "creates an order import" do
expect { do_request }.to(
change { OrderImport.count }.by(1)
)
end

it "creates a log event on success" do
expect { do_request }.to(
change do
LogEvent.where(
user:,
event_type: :created,
).count
end.by(1)
)
end
end

context "on error" do
let(:upload_file) { nil }

it "does not create an order import" do
expect { do_request }.to_not(
change { OrderImport.count }
)
end

it "does not create a log event" do
expect { do_request }.to_not(
change { LogEvent.count }
)
end
end
end

context "when the file is blank" do
let(:upload_file) { fixture_file("blank.csv") }
let(:fail_on_error) { false }
Expand Down
17 changes: 11 additions & 6 deletions spec/models/order_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require "stringio"

RSpec.describe OrderImport, :time_travel do

CSV_HEADERS = [
I18n.t("order_row_importer.headers.user"),
I18n.t("Chart_string"),
Expand All @@ -28,14 +27,14 @@ def nucore_format_date(date)
OrderImport.create!(
created_by: director.id,
upload_file: stored_file,
facility: facility,
facility:,
)
end

let(:account) do
create(:nufs_account,
description: "dummy account",
account_users_attributes: account_users_attributes,
account_users_attributes:,
)
end

Expand All @@ -54,7 +53,7 @@ def nucore_format_date(date)
order_import.error_file.read_attached_file.split("\n").count
end
let(:facility) { create(:facility) }
let(:facility_account) { create(:facility_account, facility: facility) }
let(:facility_account) { create(:facility_account, facility:) }
let(:fiscal_year_beginning) { SettingsHelper.fiscal_year_beginning }
let(:guest) { @guest }
let(:guest2) { create(:user, username: "guest2") }
Expand Down Expand Up @@ -85,8 +84,8 @@ def nucore_format_date(date)
before :each do
grant_role(director, facility)

price_group = FactoryBot.create(:price_group, facility: facility)
create(:account_price_group_member, account: account, price_group: price_group)
price_group = FactoryBot.create(:price_group, facility:)
create(:account_price_group_member, account:, price_group:)
item.item_price_policies.create!(attributes_for(:item_price_policy,
price_group_id: price_group.id,
start_date: fiscal_year_beginning,
Expand All @@ -97,6 +96,12 @@ def nucore_format_date(date)
))
end

describe "#to_s" do
it "is the filename" do
expect(subject.to_s).to eq(stored_file.name)
end
end

shared_examples_for "it does not send notifications" do
it "does not send notifications" do
expect(PurchaseNotifier).to receive(:order_receipt).never
Expand Down

0 comments on commit 93d3bfe

Please sign in to comment.