Skip to content

Commit 819e933

Browse files
authored
Merge pull request #5334 from GreenGogh47/fix_request_fulfilled_issue
Fix request fulfilled issue
2 parents 0ee1fc3 + 6ea3394 commit 819e933

File tree

5 files changed

+116
-3
lines changed

5 files changed

+116
-3
lines changed

app/controllers/requests_controller.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ def show
3737
# pre-filled distribution containing all the requested items.
3838
def start
3939
request = Request.find(params[:id])
40-
request.status_started!
41-
flash[:notice] = "Request started"
42-
redirect_to new_distribution_path(request_id: request.id)
40+
begin
41+
request.status_started!
42+
flash[:notice] = "Request started"
43+
redirect_to new_distribution_path(request_id: request.id)
44+
rescue ActiveRecord::RecordInvalid
45+
flash[:alert] = request.errors.full_messages.to_sentence
46+
redirect_to request_path(request)
47+
end
4348
end
4449

4550
def print_picklist

app/models/request.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Request < ApplicationRecord
3737
validates :distribution_id, uniqueness: true, allow_nil: true
3838
validate :item_requests_uniqueness_by_item_id
3939
validate :not_completely_empty
40+
validate :cannot_change_status_once_fulfilled, on: :update
4041

4142
after_validation :sanitize_items_data
4243

@@ -86,4 +87,10 @@ def not_completely_empty
8687
errors.add(:base, "completely empty request")
8788
end
8889
end
90+
91+
def cannot_change_status_once_fulfilled
92+
if status_changed? && status_was == "fulfilled"
93+
errors.add(:status, "cannot be changed once fulfilled")
94+
end
95+
end
8996
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class BackfillFulfilledRequests < ActiveRecord::Migration[8.0]
2+
def up
3+
# This migration fixes requests that should be marked as "fulfilled"
4+
# but are currently marked as "started"
5+
# A request should be "fulfilled" if its associated distribution is "complete"
6+
Request.joins(:distribution)
7+
.where(status: "started", distributions: { state: "complete" })
8+
.update_all(status: "fulfilled")
9+
end
10+
11+
def down
12+
# This is a data correction migration, so we make it irreversible
13+
# to prevent accidental data corruption
14+
raise ActiveRecord::IrreversibleMigration
15+
end
16+
end
17+
18+
# Migration Context:
19+
# Previously, fulfilled requests could be incorrectly changed back to "started" status.
20+
# This has been fixed in the Request model with validation, but this migration
21+
# corrects existing data that may be in an inconsistent state.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
require "rails_helper"
2+
require Rails.root.join("db/migrate/20250826223940_backfill_fulfilled_requests")
3+
4+
RSpec.describe BackfillFulfilledRequests, type: :migration do
5+
let(:migration) { described_class.new }
6+
let(:organization) { create(:organization, :with_items) }
7+
let(:partner) { create(:partner, organization: organization) }
8+
9+
it "updates started requests with complete distributions to fulfilled" do
10+
# Create a distribution with complete state
11+
distribution = create(:distribution, organization: organization, partner: partner, state: "complete")
12+
13+
# Create a request with started status and link it to the distribution
14+
request = create(:request, :started, organization: organization, partner: partner, distribution: distribution)
15+
16+
expect {
17+
migration.up
18+
}.to change { request.reload.status }.from("started").to("fulfilled")
19+
end
20+
21+
it "does not change requests without complete distributions" do
22+
# Create a distribution with scheduled state (not complete)
23+
distribution = create(:distribution, organization: organization, partner: partner, state: "scheduled")
24+
25+
# Create a request with started status and link it to the distribution
26+
request = create(:request, :started, organization: organization, partner: partner, distribution: distribution)
27+
28+
expect {
29+
migration.up
30+
}.not_to change { request.reload.status }
31+
end
32+
33+
it "does not change requests without any distribution" do
34+
# Create a request with started status but no distribution
35+
request = create(:request, :started, organization: organization, partner: partner, distribution: nil)
36+
37+
expect {
38+
migration.up
39+
}.not_to change { request.reload.status }
40+
end
41+
42+
it "does not change fulfilled requests even if they have complete distributions" do
43+
# Create a distribution with complete state
44+
distribution = create(:distribution, organization: organization, partner: partner, state: "complete")
45+
46+
# Create a request that's already fulfilled
47+
request = create(:request, :fulfilled, organization: organization, partner: partner, distribution: distribution)
48+
49+
expect {
50+
migration.up
51+
}.not_to change { request.reload.status }
52+
end
53+
54+
it "does not change pending requests even if they have complete distributions" do
55+
# Create a distribution with complete state
56+
distribution = create(:distribution, organization: organization, partner: partner, state: "complete")
57+
58+
# Create a request with pending status
59+
request = create(:request, :pending, organization: organization, partner: partner, distribution: distribution)
60+
61+
expect {
62+
migration.up
63+
}.not_to change { request.reload.status }
64+
end
65+
end

spec/models/request_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@
2929
expect(Request.status_started).to eq([request_started])
3030
expect(Request.status_fulfilled).to eq([request_fulfilled])
3131
end
32+
33+
it "does not regress from fulfilled to started" do
34+
expect { request_fulfilled.status_started! }
35+
.to raise_error(ActiveRecord::RecordInvalid, /cannot be changed once fulfilled/)
36+
end
37+
38+
it "does not regress from fulfilled to pending" do
39+
expect { request_fulfilled.status_pending! }
40+
.to raise_error(ActiveRecord::RecordInvalid, /cannot be changed once fulfilled/)
41+
end
42+
43+
it "allows normal transitions" do
44+
expect { request_pending.status_started! }.not_to raise_error
45+
expect { request_started.status_fulfilled! }.not_to raise_error
46+
end
3247
end
3348
end
3449

0 commit comments

Comments
 (0)