Skip to content

Commit

Permalink
Fix bug when form is required for cross core order (#4506)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeticiaErrandonea authored Aug 16, 2024
1 parent ce51952 commit 90e8c89
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
20 changes: 18 additions & 2 deletions app/controllers/order_detail_stored_files_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ def template_results

# GET /orders/:order_id/order_details/:order_detail_id/order_file
def order_file
@original_order_id = params[:original_order_id]
raise ActiveRecord::RecordNotFound if @order_detail.product.stored_files.template.empty?
@file = @order_detail.stored_files.new(file_type: "template_result")
end

# POST /orders/:order_id/order_details/:order_detail_id/upload_order_file
def upload_order_file
@original_order_id = params[:original_order_id]
@file = @order_detail.stored_files.new(params[:stored_file]&.permit(:file))
@file.file_type = "template_result"
@file.name = "Order File"
Expand All @@ -45,9 +47,9 @@ def upload_order_file

if @order_detail.order.to_be_merged?
@order_detail.save! # trigger the OrderDetailObserver callbacks
redirect_to facility_order_path(@order_detail.facility, @order_detail.order.merge_order || @order_detail.order)
redirect_to_facility_order(consider_merge_order: true)
else
redirect_to(order_path(@order))
redirect_to_facility_order
end
else
flash.now[:error] = text("upload_order_file.error")
Expand Down Expand Up @@ -81,4 +83,18 @@ def ability_resource
@order_detail
end

def redirect_to_facility_order(consider_merge_order: false)
if SettingsHelper.feature_on?(:cross_core_projects) && @original_order_id.present?
original_order = Order.find(@original_order_id)

order = consider_merge_order ? original_order.merge_order || original_order : original_order

redirect_to facility_order_path(original_order.facility, order)
else
order = consider_merge_order ? @order_detail.order.merge_order || @order_detail.order : @order_detail.order

redirect_to facility_order_path(@order_detail.facility, order)
end
end

end
8 changes: 4 additions & 4 deletions app/controllers/order_management/order_details_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ class OrderManagement::OrderDetailsController < ApplicationController

load_resource :facility, find_by: :url_name

load_resource :order, through: :facility, except: [:files]
load_resource :order_detail, through: :order, except: [:files]
load_resource :order, through: :facility, except: [:files, :template_results]
load_resource :order_detail, through: :order, except: [:files, :template_results]
# We can't load through the facility because of cross-core orders
before_action :init_order_detail, only: [:files]
before_action :init_order_detail, only: [:files, :template_results]

helper_method :edit_disabled?

before_action :authorize_order_detail, except: %i(sample_results template_results)
before_action :authorize_order_detail, except: %i(sample_results)
before_action :load_accounts, only: [:edit, :update]
before_action :load_order_statuses, only: [:edit, :update]

Expand Down
2 changes: 1 addition & 1 deletion app/lib/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ def cross_core_abilities(user, resource, controller)
project = resource.order.cross_core_project

if project.present?
can [:add_accessories, :new, :show, :update, :cancel], OrderDetail if user.facility_staff_or_manager_of?(project.facility)
can [:add_accessories, :new, :show, :update, :cancel, :template_results], OrderDetail if user.facility_staff_or_manager_of?(project.facility)
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/views/order_detail_stored_files/order_file.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

%h2= @order_detail.product

= form_for(@file, url: order_order_detail_upload_order_file_path, html: { multipart: true, method: :post }) do |f|
= form_for(@file, url: order_order_detail_upload_order_file_path(original_order_id: @original_order_id), html: { multipart: true, method: :post }) do |f|
= f.error_messages
%table.table.table-striped.table-hover
%tbody
Expand All @@ -31,5 +31,5 @@
%b Upload the completed order form
%p= f.file_field :file
%ul.inline
%li= f.submit('Upload', :disable_with => "Uploading...", :class => 'btn')
%li= f.submit('Upload', disable_with: "Uploading...", class: 'btn')
%li= link_to('Cancel', order_path(@order))
2 changes: 1 addition & 1 deletion app/views/orders/_service_desc.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- if order_detail.product.stored_files.template.count > 0
.order-action
- if order_detail.stored_files.template_result.empty?
= link_to "Upload Order Form", order_order_detail_order_file_path(order_detail.order, order_detail), :class => "btn"
= link_to "Upload Order Form", order_order_detail_order_file_path(order_detail.order, order_detail, original_order_id: @order.id), class: "btn"
- else
= link_to "Uploaded Order Form", order_detail_first_template_result_path(order_detail)
= " (#{link_to("Delete", order_order_detail_remove_order_file_path(order_detail.order, order_detail))})".html_safe
4 changes: 1 addition & 3 deletions spec/system/admin/adding_to_a_cross_core_order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@
click_button "Upload"
end

# Skipping because it fails with a JS error. It's redirected to 404.
# Will get fixed after actions are in a modal, so user is no longer redirected.
xit "sets the expected attributes", :aggregate_failures do
it "sets the expected attributes", :aggregate_failures do
project = order.reload.cross_core_project

expect(project).to be_present
Expand Down

0 comments on commit 90e8c89

Please sign in to comment.