From b56f77e4f613da9806e6464467a03d8598c8b1e6 Mon Sep 17 00:00:00 2001 From: Muhammad Noyan Aziz Date: Thu, 12 Dec 2024 15:20:41 +0500 Subject: [PATCH] chore: first time discount check based on lms --- .../apps/commercetools/clients.py | 25 +++++++++++-------- .../apps/commercetools/pipeline.py | 5 ++-- commerce_coordinator/apps/lms/filters.py | 5 ++-- commerce_coordinator/apps/lms/views.py | 7 +++--- commerce_coordinator/settings/base.py | 2 -- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/commerce_coordinator/apps/commercetools/clients.py b/commerce_coordinator/apps/commercetools/clients.py index 02ab46151..564641e6a 100644 --- a/commerce_coordinator/apps/commercetools/clients.py +++ b/commerce_coordinator/apps/commercetools/clients.py @@ -604,31 +604,34 @@ def retire_customer_anonymize_fields(self, customer_id: str, customer_version: i f"error correlation id {err.correlation_id} and error/s: {err.errors}") raise err - def is_first_time_discount_eligible(self, email: str) -> bool: + def is_first_time_discount_eligible(self, email: str, code: str) -> bool: """ Check if a user is eligible for a first time discount Args: email (str): Email of the user + code (str): First time discount code Returns (bool): True if the user is eligible for a first time discount """ try: - discounts = self.base_client.discount_codes.query( - where="code in :discountCodes", - predicate_var={'discountCodes': settings.COMMERCETOOLS_FIRST_TIME_DISCOUNTS} - ) - discount_ids = [discount.id for discount in discounts.results] - discounted_orders = self.base_client.orders.query( where=[ "customerEmail=:email", "orderState=:orderState", - "discountCodes(discountCode(id in :discountIds))" + "discountCodes(discountCode is defined)" ], - predicate_var={'email': email, 'discountIds': discount_ids, 'orderState': 'Complete'} + predicate_var={'email': email, 'orderState': 'Complete'}, + expand=["discountCodes[*].discountCode"] ) - if discounted_orders.total > 0: - return False + if discounted_orders.total < 1: + return True + + discounted_orders = discounted_orders.results + + for order in discounted_orders: + discount_code = order.discount_codes[0].discount_code.obj.code + if discount_code == code: + return False return True except CommercetoolsError as err: # pragma no cover diff --git a/commerce_coordinator/apps/commercetools/pipeline.py b/commerce_coordinator/apps/commercetools/pipeline.py index 4a67b6f1c..f0ba111a0 100644 --- a/commerce_coordinator/apps/commercetools/pipeline.py +++ b/commerce_coordinator/apps/commercetools/pipeline.py @@ -415,11 +415,12 @@ class CheckCommercetoolsDiscountEligibility(PipelineStep): """ Checks if a user is eligible for a first time discount in Commercetools. """ - def run_filter(self, email): # pylint: disable=arguments-differ + def run_filter(self, email, code): # pylint: disable=arguments-differ """ Execute a filter with the signature specified. Arguments: email: Email of the user + code: First time discount code kwargs: The keyword arguments passed through from the filter Returns: is_eligible (bool): True if the user is eligible for a first time discount @@ -428,7 +429,7 @@ def run_filter(self, email): # pylint: disable=arguments-differ try: ct_api_client = CommercetoolsAPIClient() - is_eligible = ct_api_client.is_first_time_discount_eligible(email) + is_eligible = ct_api_client.is_first_time_discount_eligible(email, code) return { 'is_eligible': is_eligible diff --git a/commerce_coordinator/apps/lms/filters.py b/commerce_coordinator/apps/lms/filters.py index ef1288f1f..91d35d0a9 100644 --- a/commerce_coordinator/apps/lms/filters.py +++ b/commerce_coordinator/apps/lms/filters.py @@ -74,12 +74,13 @@ class CheckFirstTimeDiscountEligibility(OpenEdxPublicFilter): filter_type = "org.edx.coordinator.lms.check.first.time.discount.eligibility.v1" @classmethod - def run_filter(cls, email): + def run_filter(cls, email, code): """ Call the PipelineStep(s) defined for this filter. Arguments: email: Email of the user + code: First time discount code Returns: is_eligible (bool): True if the user is eligible for a first time discount """ - return super().run_pipeline(email=email) + return super().run_pipeline(email=email, code=code) diff --git a/commerce_coordinator/apps/lms/views.py b/commerce_coordinator/apps/lms/views.py index bd21d04ac..769a97127 100644 --- a/commerce_coordinator/apps/lms/views.py +++ b/commerce_coordinator/apps/lms/views.py @@ -345,11 +345,12 @@ class FirstTimeDiscountEligibleView(APIView): def get(self, request): """Return True if user is eligible for a first time discount.""" email = request.query_params.get('email') + code = request.query_params.get('code') - if not email: # pragma: no cover - raise PermissionDenied(detail="Could not detect user email.") + if not email or not code: # pragma: no cover + raise PermissionDenied(detail="Could not detect user email or discount code.") - result = CheckFirstTimeDiscountEligibility.run_filter(email=email) + result = CheckFirstTimeDiscountEligibility.run_filter(email=email, code=code) output = { "is_eligible": result.get('is_eligible', True) diff --git a/commerce_coordinator/settings/base.py b/commerce_coordinator/settings/base.py index 8b6e6bbdf..a5aaa301a 100644 --- a/commerce_coordinator/settings/base.py +++ b/commerce_coordinator/settings/base.py @@ -460,8 +460,6 @@ def root(*path_fragments): # Checkout view urls COMMERCETOOLS_FRONTEND_URL = 'http://localhost:3000/SET-ME' -COMMERCETOOLS_FIRST_TIME_DISCOUNTS = ('EDXWELCOME', 'NEW2EDX') - COMMERCETOOLS_MERCHANT_CENTER_ORDERS_PAGE_URL = \ f'https://mc.{_COMMERCETOOLS_CONFIG_GEO}.commercetools.com/{COMMERCETOOLS_CONFIG["projectKey"]}/orders'