mirror of
https://github.com/simple-login/app.git
synced 2025-10-19 19:56:20 +08:00
Fix: if there's an expired manual sub, extend it (#2393)
This commit is contained in:
parent
a908e1cd18
commit
7e77afa4fc
2 changed files with 37 additions and 7 deletions
|
@ -71,13 +71,18 @@ def redeem_coupon(coupon_code: str, user: User) -> Optional[Coupon]:
|
|||
else:
|
||||
sub.end_at = arrow.now().shift(years=coupon.nb_year, days=1)
|
||||
else:
|
||||
sub = ManualSubscription.create(
|
||||
user_id=user.id,
|
||||
end_at=arrow.now().shift(years=coupon.nb_year, days=1),
|
||||
comment="using coupon code",
|
||||
is_giveaway=coupon.is_giveaway,
|
||||
commit=True,
|
||||
)
|
||||
# There may be an expired manual subscription
|
||||
sub = ManualSubscription.get_by(user_id=user.id)
|
||||
end_at = arrow.now().shift(years=coupon.nb_year, days=1)
|
||||
if sub:
|
||||
sub.end_at = end_at
|
||||
else:
|
||||
sub = ManualSubscription.create(
|
||||
user_id=user.id,
|
||||
end_at=end_at,
|
||||
comment="using coupon code",
|
||||
is_giveaway=coupon.is_giveaway,
|
||||
)
|
||||
emit_user_audit_log(
|
||||
user=user,
|
||||
action=UserAuditLogAction.Upgrade,
|
||||
|
|
|
@ -66,6 +66,31 @@ def test_use_coupon_extend_manual_sub():
|
|||
assert left.days > 364
|
||||
|
||||
|
||||
def test_use_coupon_extend_expired_manual_sub():
|
||||
user = create_new_user()
|
||||
initial_end = arrow.now().shift(days=-15)
|
||||
ManualSubscription.create(
|
||||
user_id=user.id,
|
||||
end_at=initial_end,
|
||||
flush=True,
|
||||
)
|
||||
code = random_string(10)
|
||||
Coupon.create(code=code, nb_year=1, commit=True)
|
||||
|
||||
coupon = redeem_coupon(code, user)
|
||||
assert coupon
|
||||
|
||||
coupon = Coupon.get_by(code=code)
|
||||
assert coupon
|
||||
assert coupon.used
|
||||
assert coupon.used_by_user_id == user.id
|
||||
|
||||
sub = user.get_active_subscription()
|
||||
assert isinstance(sub, ManualSubscription)
|
||||
left = sub.end_at - initial_end
|
||||
assert left.days > 364
|
||||
|
||||
|
||||
def test_coupon_with_subscription():
|
||||
user = create_new_user()
|
||||
end_at = arrow.utcnow().shift(days=1).replace(hour=0, minute=0, second=0)
|
||||
|
|
Loading…
Add table
Reference in a new issue