diff --git a/app/models/payment.rb b/app/models/payment.rb index 3285eab53f2..0fd5220147d 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -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? diff --git a/spec/models/payment_spec.rb b/spec/models/payment_spec.rb index e1717b70b82..1e3dcd06c72 100644 --- a/spec/models/payment_spec.rb +++ b/spec/models/payment_spec.rb @@ -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