Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: filter payments based on visible invoices #3102

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
14 changes: 9 additions & 5 deletions spec/models/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading