Skip to content

Commit

Permalink
fix: filter payments based on visible invoices (#3102)
Browse files Browse the repository at this point in the history
## Context

This changes the scope `for_organization` in the `Payment` model,
allowing payments only for visible invoices to be considered.

## Description
only let the Invoices with statuses defined in
`Invoice::VISIBLE_STATUS`.
brunomiguelpinto authored Jan 24, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 035cea3 commit 3881346
Showing 2 changed files with 13 additions and 7 deletions.
6 changes: 4 additions & 2 deletions app/models/payment.rb
Original file line number Diff line number Diff line change
@@ -32,14 +32,16 @@ class Payment < ApplicationRecord
ON invoices.id = payments.payable_id
AND payments.payable_type = 'Invoice'
AND invoices.organization_id = :org_id
AND invoices.status IN (:visible_statuses)
LEFT JOIN payment_requests
ON payment_requests.id = payments.payable_id
AND payments.payable_type = 'PaymentRequest'
AND payment_requests.organization_id = :org_id
SQL
{org_id: organization.id}
{org_id: organization.id, visible_statuses: Invoice::VISIBLE_STATUS.values}
])
joins(payables_join).where('invoices.id IS NOT NULL OR payment_requests.id IS NOT NULL')
joins(payables_join)
.where('invoices.id IS NOT NULL OR payment_requests.id IS NOT NULL')
}

def should_sync_payment?
14 changes: 9 additions & 5 deletions spec/models/payment_spec.rb
Original file line number Diff line number Diff line change
@@ -286,28 +286,32 @@
subject(:result) { described_class.for_organization(organization) }

let(:organization) { create(:organization) }
let(:invoice) { create(:invoice, organization:) }
let(:visible_invoice) { create(:invoice, organization:, status: Invoice::VISIBLE_STATUS[:finalized]) }
let(:invisible_invoice) { create(:invoice, organization:, status: Invoice::INVISIBLE_STATUS[:generating]) }
let(:payment_request) { create(:payment_request, organization:) }
let(:other_org_payment_request) { create(:payment_request) }

let(:invoice_payment) { create(:payment, payable: invoice) }
let(:visible_invoice_payment) { create(:payment, payable: visible_invoice) }
let(:invisible_invoice_payment) { create(:payment, payable: invisible_invoice) }
let(:payment_request_payment) { create(:payment, payable: payment_request) }
let(:other_org_invoice_payment) { create(:payment) }
let(:other_org_payment_request_payment) { create(:payment, payable: other_org_payment_request) }

before do
invoice_payment
visible_invoice_payment
invisible_invoice_payment
payment_request_payment

other_org_invoice_payment
other_org_payment_request_payment
end

it "returns organization's payments" do
it "returns payments and payment requests for the organization's visible invoices" do
payments = subject

expect(payments).to include(invoice_payment)
expect(payments).to include(visible_invoice_payment)
expect(payments).to include(payment_request_payment)
expect(payments).not_to include(invisible_invoice_payment)
expect(payments).not_to include(other_org_invoice_payment)
expect(payments).not_to include(other_org_payment_request_payment)
end

0 comments on commit 3881346

Please sign in to comment.