Skip to content

Commit 8bda42f

Browse files
refactor: Simplify promo activation logic in PromoActivationService
This commit refactors the `PromoActivationService` to simplify its logic and improve readability. Key changes include: - Removed redundant `user_age is None` checks in the age targeting validation, assuming `user_age` is always present. - Streamlined the promo code acquisition process by removing unnecessary conditional branches and `PromoUnavailableError` exceptions. - Eliminated an unreachable `PromoActivationError`, making the code path for activating a promotion more direct.
1 parent ddaeb73 commit 8bda42f

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

promo_code/user/services.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,10 @@ def _validate_targeting(self):
7979
if target.get('country') and user_country != target['country'].lower():
8080
raise TargetingError('Country mismatch.')
8181

82-
if target.get('age_from') and (
83-
user_age is None or user_age < target['age_from']
84-
):
82+
if target.get('age_from') and user_age < target['age_from']:
8583
raise TargetingError('Age mismatch.')
8684

87-
if target.get('age_until') and (
88-
user_age is None or user_age > target['age_until']
89-
):
85+
if target.get('age_until') and user_age > target['age_until']:
9086
raise TargetingError('Age mismatch.')
9187

9288
def _validate_is_active(self):
@@ -127,10 +123,7 @@ def _issue_promo_code(self) -> str:
127123
)
128124
promo_locked.save(update_fields=['used_count'])
129125
promo_code_value = promo_locked.promo_common
130-
else:
131-
raise PromoUnavailableError()
132-
133-
elif promo_locked.mode == business.constants.PROMO_MODE_UNIQUE:
126+
else:
134127
unique_code = promo_locked.unique_codes.filter(
135128
is_used=False,
136129
).first()
@@ -139,8 +132,6 @@ def _issue_promo_code(self) -> str:
139132
unique_code.used_at = django.utils.timezone.now()
140133
unique_code.save(update_fields=['is_used', 'used_at'])
141134
promo_code_value = unique_code.code
142-
else:
143-
raise PromoUnavailableError()
144135

145136
if promo_code_value:
146137
user.models.PromoActivationHistory.objects.create(
@@ -149,7 +140,5 @@ def _issue_promo_code(self) -> str:
149140
)
150141
return promo_code_value
151142

152-
raise PromoActivationError('Invalid promotion type.')
153-
154143
except business.models.Promo.DoesNotExist:
155144
raise PromoActivationError('Promo not found.')

0 commit comments

Comments
 (0)