Skip to content

Commit

Permalink
🥗 Tobias: Test issuing Payouts twice doesn't create more `Payment…
Browse files Browse the repository at this point in the history
…s` (#19)

* 🥗 `Tobias`: Issuing `Payouts` doesn't create additional payouts per beneficiary

I thought that this would actually be a problem, but apparently it's
not! Hooray for tests!

* 🥗✨`Tobias`: `Payout#issue` won't create `Payment` for new `Beneficiary`

This one actually needed adjusting! Turns out, `Payout#issue` would
happily create a new `Payment` for a new `Beneficiary`, which is less
than ideal.

Now we have a guard clause that will prevent `Payout#issue` from making
additional `Payments` when there are already `Payments` present.
  • Loading branch information
zspencer authored Jan 29, 2024
1 parent 63f8184 commit 8f5c34a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/furniture/tobias/payout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Payout < ApplicationRecord
monetize :amount_cents

def issue
return if payments.present?

per_beneficiary_amount = (amount / beneficiaries.count)
beneficiaries.each do |beneficiary|
payments.create_with(amount: per_beneficiary_amount).find_or_create_by(beneficiary_id: beneficiary.id)
Expand Down
18 changes: 18 additions & 0 deletions spec/tobias/payout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,23 @@
end
end
end

context "when running twice" do
it "does not issue multiple payouts, even when beneficiaries are added" do
payout = create(:tobias_payout, amount_cents: 100_00)

create_list(:tobias_beneficiary, 2, trust: payout.trust)

payout.issue

create(:tobias_beneficiary, trust: payout.trust)

# ActiveRecord appears to be caching the `payout.beneficiaries` results
# Reload busts that cache.
payout.reload

expect { payout.issue }.not_to(change(payout.payments, :count))
end
end
end
end

0 comments on commit 8f5c34a

Please sign in to comment.