Skip to content

Commit

Permalink
chore: first time discount check based on lms
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Noyan Aziz authored and Muhammad Noyan Aziz committed Dec 12, 2024
1 parent 184427c commit b56f77e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
25 changes: 14 additions & 11 deletions commerce_coordinator/apps/commercetools/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions commerce_coordinator/apps/commercetools/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions commerce_coordinator/apps/lms/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 4 additions & 3 deletions commerce_coordinator/apps/lms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions commerce_coordinator/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down

0 comments on commit b56f77e

Please sign in to comment.