Skip to content

Commit

Permalink
payment requests: block if there are duplicated validated PFMPs
Browse files Browse the repository at this point in the history
  • Loading branch information
freesteph committed Apr 4, 2024
1 parent 2c5e1fa commit cf261a6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
6 changes: 6 additions & 0 deletions app/models/asp/payment_request_state_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class PaymentRequestStateMachine
request.schooling.attributive_decision.attached?
end

guard_transition(to: :ready) do |request|
request.pfmp.duplicates.none? do |pfmp|
pfmp.in_state?(:validated)
end
end

guard_transition(from: :ready, to: :sent) do |request|
request.asp_request.present?
end
Expand Down
6 changes: 6 additions & 0 deletions app/models/pfmp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,10 @@ def can_be_modified?
def payment_due?
day_count.present?
end

def duplicates
student.pfmps.excluding(self).select do |other|
other.start_date == start_date && other.end_date == end_date
end
end
end
30 changes: 27 additions & 3 deletions spec/models/asp/payment_request_state_machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,36 @@
end

context "when the attributive decision has not been attached" do
before do
asp_payment_request.pfmp.schooling.attributive_decision.detach
end
before { asp_payment_request.pfmp.schooling.attributive_decision.detach }

it_behaves_like "a blocked request"
end

context "when there is another duplicated PFMP" do
let(:duplicate) do
pfmp = asp_payment_request.pfmp

create(
:pfmp,
schooling: pfmp.schooling,
start_date: pfmp.start_date,
end_date: pfmp.end_date,
day_count: pfmp.day_count
)
end

context "when it is validated" do
before { duplicate.validate! }

it_behaves_like "a blocked request"
end

context "when it's not validated" do
it "allows the transition" do
expect { asp_payment_request.mark_ready! }.not_to raise_error
end
end
end
end

describe "mark_as_sent!" do
Expand Down

0 comments on commit cf261a6

Please sign in to comment.