Skip to content

Commit 43fbcc4

Browse files
Prevent a finalized audit from being edited
While the validation added prevented a finalized audit to have its status changed, QA showed that it was possible to confirm an edited audit that has already been finalized. With this change, we prevent any edits from being made to a finalized audit and display a message for the user to make the experience better.
1 parent d684758 commit 43fbcc4

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

app/controllers/audits_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ def finalize
3636
end
3737

3838
def update
39+
if @audit.reload.finalized?
40+
redirect_to audit_path(@audit), error: "This audit has been finalized and cannot be edited."
41+
return
42+
end
43+
3944
@audit.line_items.destroy_all
4045
if @audit.update(audit_params)
4146
save_audit_status_and_redirect(params)

spec/requests/audits_requests_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,40 @@
109109
end
110110
end
111111

112+
describe "PUT #update" do
113+
it "confirms the updated audit and redirects to the audit" do
114+
audit = create(:audit, organization: organization, status: :in_progress)
115+
item = create(:item)
116+
audit.line_items << create(:line_item, quantity: 3, item: item)
117+
118+
put audit_path(id: audit.to_param, audit: {
119+
storage_location_id: storage_location.id,
120+
line_items_attributes: {"0" => {"item_id" => item.id, "quantity" => "4"}}
121+
})
122+
123+
expect(response).to redirect_to(audit_path(audit))
124+
expect(flash[:notice]).to include("Audit is confirmed.")
125+
expect(audit.reload).to be_confirmed
126+
end
127+
128+
context "when the audit has already been finalized" do
129+
it "does not allow updates and redirects to the finalized audit" do
130+
finalized_audit = create(:audit, organization: organization, status: :finalized)
131+
item = create(:item)
132+
finalized_audit.line_items << create(:line_item, quantity: 3, item: item)
133+
134+
put audit_path(id: finalized_audit.to_param, audit: {
135+
storage_location_id: storage_location.id,
136+
line_items_attributes: {"0" => {"item_id" => item.id, "quantity" => "4"}}
137+
})
138+
139+
expect(response).to redirect_to(audit_path(finalized_audit))
140+
expect(flash[:error]).to include("This audit has been finalized and cannot be edited.")
141+
expect(finalized_audit.line_items.first.quantity).to eq(3)
142+
end
143+
end
144+
end
145+
112146
describe "POST #create" do
113147
context "with valid params" do
114148
it "creates a new Audit" do

0 commit comments

Comments
 (0)