Skip to content

Commit

Permalink
fix: reset usage bug (#1424)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImMohammad20000 authored Nov 9, 2024
1 parent b76dc2b commit 5700dce
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions app/db/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,17 +511,17 @@ def update_user(db: Session, dbuser: User, modify: UserModify) -> User:

if modify.on_hold_expire_duration is not None:
dbuser.on_hold_expire_duration = modify.on_hold_expire_duration

if modify.next_plan is not None:
dbuser.next_plan=NextPlan(
dbuser.next_plan = NextPlan(
data_limit=modify.next_plan.data_limit,
expire=modify.next_plan.expire,
add_remaining_traffic=modify.next_plan.add_remaining_traffic,
fire_on_either=modify.next_plan.fire_on_either,
)
elif dbuser.next_plan is not None:
db.delete(dbuser.next_plan)

dbuser.edit_at = datetime.utcnow()

db.commit()
Expand Down Expand Up @@ -550,9 +550,10 @@ def reset_user_data_usage(db: Session, dbuser: User) -> User:
dbuser.node_usages.clear()
if dbuser.status not in (UserStatus.expired or UserStatus.disabled):
dbuser.status = UserStatus.active.value

db.delete(dbuser.next_plan)
dbuser.next_plan = None

if dbuser.next_plan:
db.delete(dbuser.next_plan)
dbuser.next_plan = None
db.add(dbuser)

db.commit()
Expand All @@ -571,10 +572,10 @@ def reset_user_by_next(db: Session, dbuser: User) -> User:
Returns:
User: The updated user object.
"""

if (dbuser.next_plan is None):
return

usage_log = UserUsageResetLogs(
user=dbuser,
used_traffic_at_reset=dbuser.used_traffic,
Expand All @@ -583,10 +584,11 @@ def reset_user_by_next(db: Session, dbuser: User) -> User:

dbuser.node_usages.clear()
dbuser.status = UserStatus.active.value

dbuser.data_limit = dbuser.next_plan.data_limit + (0 if dbuser.next_plan.add_remaining_traffic else dbuser.data_limit - dbuser.used_traffic)

dbuser.data_limit = dbuser.next_plan.data_limit + \
(0 if dbuser.next_plan.add_remaining_traffic else dbuser.data_limit - dbuser.used_traffic)
dbuser.expire = dbuser.next_plan.expire

dbuser.used_traffic = 0
db.delete(dbuser.next_plan)
dbuser.next_plan = None
Expand Down

0 comments on commit 5700dce

Please sign in to comment.