Skip to content

Commit

Permalink
Change subquery, so it excludes some of the outer joins, only adds if… (
Browse files Browse the repository at this point in the history
#1101)

* Change subquery, so it excludes some of the outer joins, only adds if necessary.

* Remove sbc-common-components
  • Loading branch information
seeker25 authored Feb 10, 2023
1 parent 11e9d7c commit 7658574
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
13 changes: 8 additions & 5 deletions pay-api/src/pay_api/models/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ def search_purchase_history(cls, # noqa:E501; pylint:disable=too-many-arguments
if not return_all:
count = cls.get_count(auth_account_id, search_filter)
# Add pagination
sub_query = query.with_entities(Invoice.id).\
sub_query = db.session.query(Invoice) \
.outerjoin(PaymentAccount, Invoice.payment_account_id == PaymentAccount.id)
sub_query = cls.filter(sub_query, auth_account_id, search_filter, add_outer_joins=True).\
with_entities(Invoice.id).\
group_by(Invoice.id).\
order_by(Invoice.id.desc()). \
limit(limit).\
Expand Down Expand Up @@ -238,12 +241,12 @@ def get_count(cls, auth_account_id: str, search_filter: Dict):
# We need to exclude the outer joins for performance here, they get re-added in filter.
query = db.session.query(Invoice) \
.outerjoin(PaymentAccount, Invoice.payment_account_id == PaymentAccount.id)
query = cls.filter(query, auth_account_id, search_filter, is_count=True)
query = cls.filter(query, auth_account_id, search_filter, add_outer_joins=True)
count = query.group_by(Invoice.id).with_entities(func.count()).count()
return count

@classmethod
def filter(cls, query, auth_account_id: str, search_filter: Dict, is_count=False):
def filter(cls, query, auth_account_id: str, search_filter: Dict, add_outer_joins=False):
"""For filtering queries."""
if auth_account_id:
query = query.filter(PaymentAccount.auth_account_id == auth_account_id)
Expand All @@ -269,13 +272,13 @@ def filter(cls, query, auth_account_id: str, search_filter: Dict, is_count=False

if invoice_number := search_filter.get('invoiceNumber', None):
# could have multiple invoice reference rows, but is handled in sub_query below (group by)
if is_count:
if add_outer_joins:
query = query.outerjoin(InvoiceReference, InvoiceReference.invoice_id == Invoice.id)
query = query.filter(InvoiceReference.invoice_number.ilike(f'%{invoice_number}%'))

query = cls.filter_corp_type(query, search_filter)
query = cls.filter_payment(query, search_filter)
query = cls.filter_details(query, search_filter, is_count)
query = cls.filter_details(query, search_filter, add_outer_joins)
query = cls.filter_date(query, search_filter)
return query

Expand Down
1 change: 0 additions & 1 deletion pay-api/src/sbc-common-components
Submodule sbc-common-components deleted from 02f90e

0 comments on commit 7658574

Please sign in to comment.