Set grace period for subscriptions to 14 days
This commit is contained in:
parent
a8c01a1443
commit
430e16d8df
|
@ -57,6 +57,8 @@ from app.utils import (
|
|||
|
||||
Base = declarative_base()
|
||||
|
||||
SUBSCRIPTION_GRACE_DAYS = 14
|
||||
|
||||
|
||||
class TSVector(sa.types.TypeDecorator):
|
||||
impl = TSVECTOR
|
||||
|
@ -751,8 +753,12 @@ class User(Base, ModelMixin, UserMixin, PasswordOracle):
|
|||
sub = Subscription.get_by(user_id=self.id)
|
||||
|
||||
if sub:
|
||||
# sub is active until the next billing_date + 1
|
||||
if sub.next_bill_date >= arrow.now().shift(days=-1).date():
|
||||
# Set grace period to two weeks to allow for failed payment retrials in paddle
|
||||
# sub is active until the next billing_date + SUBSCRIPTION_GRACE_DAYS
|
||||
if (
|
||||
sub.next_bill_date
|
||||
>= arrow.now().shift(days=-SUBSCRIPTION_GRACE_DAYS).date()
|
||||
):
|
||||
return sub
|
||||
# past subscription, user is considered not having a subscription = free plan
|
||||
else:
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
from uuid import UUID
|
||||
import random
|
||||
|
||||
import pytest
|
||||
from sqlalchemy_utils.types.arrow import arrow
|
||||
|
||||
from app.config import EMAIL_DOMAIN, MAX_NB_EMAIL_FREE_PLAN
|
||||
from app.db import Session
|
||||
|
@ -12,6 +14,9 @@ from app.models import (
|
|||
Mailbox,
|
||||
SenderFormatEnum,
|
||||
EnumE,
|
||||
Subscription,
|
||||
PlanEnum,
|
||||
SUBSCRIPTION_GRACE_DAYS,
|
||||
)
|
||||
from tests.utils import login, create_new_user
|
||||
|
||||
|
@ -258,3 +263,26 @@ def test_can_create_new_alias_disabled_user():
|
|||
|
||||
user.disabled = True
|
||||
assert not user.can_create_new_alias()
|
||||
|
||||
|
||||
def test_grace_period_for_subscription(flask_client):
|
||||
user = create_new_user()
|
||||
subscription = Subscription.create(
|
||||
user_id=user.id,
|
||||
cancel_url="https://test.url",
|
||||
update_url="https://test.url",
|
||||
subscription_id=int(random.random() * 1000),
|
||||
event_time=arrow.now(),
|
||||
next_bill_date=arrow.now()
|
||||
.shift(days=-(SUBSCRIPTION_GRACE_DAYS - 1))
|
||||
.date(), # the grace period is 14 days
|
||||
plan=PlanEnum.yearly,
|
||||
commit=True,
|
||||
)
|
||||
|
||||
assert user.get_subscription() is not None
|
||||
|
||||
subscription.next_bill_date = (
|
||||
arrow.now().shift(days=-(SUBSCRIPTION_GRACE_DAYS + 1)).date()
|
||||
)
|
||||
assert user.get_subscription() is None
|
||||
|
|
Loading…
Reference in a new issue