Skip to content

Commit

Permalink
Validate base membership exists for lab access purchase
Browse files Browse the repository at this point in the history
  • Loading branch information
velaco committed May 9, 2024
1 parent 5b933fc commit 1a86311
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
21 changes: 19 additions & 2 deletions api/src/shop/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,12 @@ def payment_success(transaction: Transaction) -> None:

def process_cart(member_id: int, cart: List[CartItem]) -> Tuple[Decimal, List[TransactionContent]]:
contents = []
labaccess_in_cart = False
base_membership_in_cart = False

price_level = get_price_level_for_member(db_session.query(Member).get(member_id))
member = db_session.query(Member).get(member_id)
price_level = get_price_level_for_member(member)
member_has_base_membership = "membership" in [span.type for span in member.spans]

with localcontext() as ctx:
ctx.clear_flags()
Expand All @@ -366,7 +370,8 @@ def process_cart(member_id: int, cart: List[CartItem]) -> Tuple[Decimal, List[Tr
what=NEGATIVE_ITEM_COUNT,
)

if product.get_metadata(MakerspaceMetadataKeys.SUBSCRIPTION_TYPE, None) is not None:
subscription_type = product.get_metadata(MakerspaceMetadataKeys.SUBSCRIPTION_TYPE, None)
if subscription_type is not None:
if count != product.smallest_multiple:
raise BadRequest(
f"Bad count for subscription product {product_id}. Count must be exactly {product.smallest_multiple}, was {count}.",
Expand All @@ -383,6 +388,13 @@ def process_cart(member_id: int, cart: List[CartItem]) -> Tuple[Decimal, List[Tr
if product.filter:
PRODUCT_FILTERS[product.filter](item, member_id)

special_product_id = product.get_metadata(MakerspaceMetadataKeys.SPECIAL_PRODUCT_ID, None)
if subscription_type == SubscriptionType.LAB or special_product_id == "single_labaccess_month":
labaccess_in_cart = True

if subscription_type == SubscriptionType.MEMBERSHIP or special_product_id == "single_membership_year":
base_membership_in_cart = True

discount = get_discount_for_product(product, price_level)

amount = Decimal(product.price) * Decimal(count) * (1 - discount.fraction_off)
Expand All @@ -391,6 +403,11 @@ def process_cart(member_id: int, cart: List[CartItem]) -> Tuple[Decimal, List[Tr
content = TransactionContent(product_id=product_id, count=count, amount=amount)
contents.append(content)

if labaccess_in_cart and not (member_has_base_membership or base_membership_in_cart):
raise BadRequest(
"Could not purchase selected subscription. Please buy the base annual membership before lab access."
)

if ctx.flags[Rounded]:
# This can possibly happen with huge values, I suppose they will be caught below anyway but it's good to
# catch in any case.
Expand Down
6 changes: 6 additions & 0 deletions api/src/systest/api/purchase_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,9 @@ def test_empty_cart_fails_purchase(self):
)

self.post(f"/webshop/pay", purchase.to_dict(), token=self.token).expect(code=400, what="empty_cart")

def test_labaccess_purchase_fails_without_base_membership(self):
pass

def test_labaccess_purchase_allowed_together_with_base_membership(self):
pass

0 comments on commit 1a86311

Please sign in to comment.