From 7658574d67d4685b2412355b12ac97763528452f Mon Sep 17 00:00:00 2001 From: Travis Semple Date: Fri, 10 Feb 2023 11:33:37 -0800 Subject: [PATCH] =?UTF-8?q?Change=20subquery,=20so=20it=20excludes=20some?= =?UTF-8?q?=20of=20the=20outer=20joins,=20only=20adds=20if=E2=80=A6=20(#11?= =?UTF-8?q?01)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Change subquery, so it excludes some of the outer joins, only adds if necessary. * Remove sbc-common-components --- pay-api/src/pay_api/models/payment.py | 13 ++++++++----- pay-api/src/sbc-common-components | 1 - 2 files changed, 8 insertions(+), 6 deletions(-) delete mode 160000 pay-api/src/sbc-common-components diff --git a/pay-api/src/pay_api/models/payment.py b/pay-api/src/pay_api/models/payment.py index cb8f56e47..8c43ae38c 100644 --- a/pay-api/src/pay_api/models/payment.py +++ b/pay-api/src/pay_api/models/payment.py @@ -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).\ @@ -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) @@ -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 diff --git a/pay-api/src/sbc-common-components b/pay-api/src/sbc-common-components deleted file mode 160000 index 02f90e3a1..000000000 --- a/pay-api/src/sbc-common-components +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 02f90e3a1c9b6eeff424ecec758d58a43d2cfc9b