Use sql datetime comparison for detecting validity of subscription renewal date

- Update the unsubscribe endpoint to use query params
- Use subscription id to process unsubscribe endpoint, rather than the customer id
This commit is contained in:
sabaimran 2023-11-07 19:17:36 -08:00
parent 98cf095b65
commit 609d358b1a
4 changed files with 16 additions and 18 deletions

View file

@ -2,7 +2,7 @@ from typing import Type, TypeVar, List
from datetime import date, datetime, timedelta from datetime import date, datetime, timedelta
import secrets import secrets
from typing import Type, TypeVar, List from typing import Type, TypeVar, List
from datetime import date from datetime import date, timezone
from django.db import models from django.db import models
from django.contrib.sessions.backends.db import SessionStore from django.contrib.sessions.backends.db import SessionStore
@ -116,12 +116,9 @@ async def set_user_subscribed(email: str, type="standard") -> KhojUser:
def is_user_subscribed(email: str, type="standard") -> bool: def is_user_subscribed(email: str, type="standard") -> bool:
user = KhojUser.objects.filter(email=email, subscription_type=type).first() return KhojUser.objects.filter(
if user and user.subscription_renewal_date: email=email, subscription_type=type, subscription_renewal_date__gte=datetime.now(tz=timezone.utc)
is_subscribed = user.subscription_renewal_date > date.today() ).exists()
return is_subscribed
else:
return False
async def get_user_by_token(token: dict) -> KhojUser: async def get_user_by_token(token: dict) -> KhojUser:

View file

@ -184,8 +184,7 @@
</a> </a>
</div> </div>
{% else %} {% else %}
<div class="card-action-row" <div class="card-action-row">
style="display: {% if not current_model_state.github %}none{% endif %}">
<button class="card-button" onclick="unsubscribe()"> <button class="card-button" onclick="unsubscribe()">
Unsubscribe Unsubscribe
</button> </button>
@ -259,14 +258,11 @@
}; };
function unsubscribe() { function unsubscribe() {
fetch('/api/subscription', { fetch('/api/subscription?email=' + '{{username}}', {
method: 'DELETE', method: 'DELETE',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({
"email": "{{ username }}"
})
}) })
} }

View file

@ -776,10 +776,13 @@ async def subscribe(request: Request):
@api.delete("/subscription") @api.delete("/subscription")
@requires(["authenticated"]) @requires(["authenticated"])
async def unsubscribe(request: Request, user_email: str): async def unsubscribe(request: Request, email: str):
customer = stripe.Customer.list(email=user_email).data customer = stripe.Customer.list(email=email).data
if not is_none_or_empty(customer): if not is_none_or_empty(customer):
stripe.Subscription.modify(customer[0].id, cancel_at_period_end=True) customer_id = customer[0].id
for subscription in stripe.Subscription.list(customer=customer_id):
stripe.Subscription.modify(subscription.id, cancel_at_period_end=True)
success = True success = True
else: else:
success = False success = False

View file

@ -1,5 +1,5 @@
# System Packages # System Packages
from datetime import datetime from datetime import datetime, timezone
import json import json
import os import os
@ -119,7 +119,9 @@ def config_page(request: Request):
user: KhojUser = request.user.object user: KhojUser = request.user.object
user_picture = request.session.get("user", {}).get("picture") user_picture = request.session.get("user", {}).get("picture")
user_is_subscribed = is_user_subscribed(user.email) user_is_subscribed = is_user_subscribed(user.email)
days_to_renewal = (user.subscription_renewal_date - datetime.now()).days if user.subscription_renewal_date else 0 days_to_renewal = (
(user.subscription_renewal_date - datetime.now(tz=timezone.utc)).days if user.subscription_renewal_date else 0
)
enabled_content_source = set(EntryAdapters.get_unique_file_source(user).all()) enabled_content_source = set(EntryAdapters.get_unique_file_source(user).all())
successfully_configured = { successfully_configured = {