Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from a limit of 10 tickets per month to an annual subscription with a limit of 120 tickets per year to relieve users from pressure per month, allowing them to relax during off-peak times. #454

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions services/stripe/customer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def get_subscription(customer_id: str) -> stripe.ListObject[stripe.Subscription]


@handle_exceptions(default_return_value=FREE_TIER_REQUEST_AMOUNT, raise_on_error=False)
def get_request_count_from_product_id_metadata(product_id: str):
def get_base_request_limit(product_id: str):
"""
https://docs.stripe.com/api/products/retrieve?lang=python
https://dashboard.stripe.com/test/products/prod_PqZFpCs1Jq6X4E
"""
price = stripe.Product.retrieve(product_id)
return int(price["metadata"]["request_count"])
product = stripe.Product.retrieve(product_id)
return int(product["metadata"]["request_count"])
34 changes: 23 additions & 11 deletions services/supabase/users_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
)
from services.stripe.customer import (
get_subscription,
get_request_count_from_product_id_metadata,
get_base_request_limit,
subscribe_to_free_plan,
)
from utils.handle_exceptions import handle_exceptions
Expand Down Expand Up @@ -45,8 +45,8 @@ def parse_subscription_object(
customer_id: str,
owner_id: int,
owner_name: str,
) -> tuple[int, int, str]:
"""Parsing stripe subscription object to get the start date, end date and product id of either a paid or free tier customer subscription"""
) -> tuple[int, int, str, str]:
"""Parsing stripe subscription object to get the start date, end date, product id and interval"""
if len(subscription.data) > 2:
msg = "There are more than 2 active subscriptions for this customer. This is a check when we move to multiple paid subscriptions."
logging.error(msg)
Expand All @@ -55,6 +55,8 @@ def parse_subscription_object(
free_tier_start_date = 0
free_tier_end_date = 0
free_tier_product_id = ""
free_tier_interval = "month" # Default interval

# return the first paid subscription if found, if not return the free one found
for sub in subscription.data:
# Iterate over the items, there should only be one item, but we do this just in case
Expand All @@ -65,12 +67,14 @@ def parse_subscription_object(
free_tier_start_date = sub.current_period_start
free_tier_end_date = sub.current_period_end
free_tier_product_id = item["price"]["product"]
free_tier_interval = item["price"]["recurring"]["interval"]
continue

return (
sub["current_period_start"],
sub["current_period_end"],
item["price"]["product"],
item["price"]["recurring"]["interval"],
)

if (
Expand All @@ -94,7 +98,12 @@ def parse_subscription_object(
owner_name=owner_name,
)
# Return from Free Tier Subscription if there is no paid subscription object
return free_tier_start_date, free_tier_end_date, free_tier_product_id
return (
free_tier_start_date,
free_tier_end_date,
free_tier_product_id,
free_tier_interval,
)

@handle_exceptions(default_return_value=(1, 1, DEFAULT_TIME), raise_on_error=False)
def get_how_many_requests_left_and_cycle(
Expand All @@ -119,7 +128,7 @@ def get_how_many_requests_left_and_cycle(

# Get subscription object and extract start date, end date and product id
subscription = get_subscription(customer_id=stripe_customer_id)
start_date_seconds, end_date_seconds, product_id = (
start_date_seconds, end_date_seconds, product_id, interval = (
self.parse_subscription_object(
subscription=subscription,
installation_id=installation_id,
Expand All @@ -129,8 +138,12 @@ def get_how_many_requests_left_and_cycle(
)
)

# Get request count from product id metadata
request_count = get_request_count_from_product_id_metadata(product_id)
# Get base request count from product id metadata
base_request_limit = get_base_request_limit(product_id)
request_limit = (
base_request_limit * 12 if interval == "year" else base_request_limit
)

start_date = datetime.fromtimestamp(timestamp=start_date_seconds, tz=TZ)
end_date = datetime.fromtimestamp(timestamp=end_date_seconds, tz=TZ)

Expand All @@ -145,10 +158,9 @@ def get_how_many_requests_left_and_cycle(
)

# Process unique_issue_id in Python
unique_issue_ids = set(record['unique_issue_id'] for record in data[1])
requests_left = request_count - len(unique_issue_ids)

return (requests_left, request_count, end_date)
unique_issue_ids = set(record["unique_issue_id"] for record in data[1])
requests_left = request_limit - len(unique_issue_ids)
return (requests_left, request_limit, end_date)

@handle_exceptions(default_return_value=None, raise_on_error=False)
def get_user(self, user_id: int):
Expand Down
2 changes: 1 addition & 1 deletion tests/services/supabase/test_users_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def test_parse_subscription_object() -> None:

def assertion_test(customer_id: str, product_id: str):
subscription = get_subscription(customer_id=customer_id)
_, _, product_id_output = supabase_manager.parse_subscription_object(
_, _, product_id_output, _ = supabase_manager.parse_subscription_object(
subscription=subscription,
installation_id=INSTALLATION_ID,
customer_id=customer_id,
Expand Down
Loading