refactor(autorenewal): clean up imports and simplify error handling
This commit is contained in:
@@ -13,6 +13,7 @@ from misc import utils
|
||||
from repositories.subscriptions import SubscriptionRepository
|
||||
from repositories.users import UserRepository
|
||||
from schemas.subscriptions import InternalSquads, SubscriptionPlan, SubscriptionStatus
|
||||
from services import rw as rw_service
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -48,22 +49,14 @@ class RenewalService:
|
||||
return utils.calculate_price(plan)
|
||||
|
||||
async def _renew_subscription_in_rw(self, subscription: Subscription) -> None:
|
||||
from services import rw as rw_service
|
||||
|
||||
rw_user = await rw_service.get_user_by_telegram_id(self.rw_sdk, subscription.user_id)
|
||||
if not rw_user:
|
||||
raise RenewalServiceError(f"RW user not found for user_id={subscription.user_id}")
|
||||
|
||||
duration = datetime.timedelta(days=subscription.duration_days)
|
||||
squads = [InternalSquads.DEFAULT]
|
||||
if subscription.whitelists:
|
||||
squads.append(InternalSquads.WHITELISTS)
|
||||
|
||||
if rw_user.expire_at > datetime.datetime.now(datetime.UTC):
|
||||
new_expire = rw_user.expire_at + duration
|
||||
else:
|
||||
new_expire = datetime.datetime.now(datetime.UTC) + duration
|
||||
|
||||
await rw_service.add_days(self.rw_sdk, rw_user.uuid, subscription.duration_days)
|
||||
await rw_service.update_squads(self.rw_sdk, rw_user.uuid, squads)
|
||||
await rw_service.set_hwid_limit(self.rw_sdk, rw_user.uuid, subscription.devices)
|
||||
@@ -95,10 +88,7 @@ class RenewalService:
|
||||
await self.subscription_repository.update_subscription_autorenew(
|
||||
session, user_id, False
|
||||
)
|
||||
await self._notify_user(
|
||||
user_id,
|
||||
AUTORENEWAL_INSUFFICIENT_FUNDS
|
||||
)
|
||||
await self._notify_user(user_id, AUTORENEWAL_INSUFFICIENT_FUNDS)
|
||||
return False
|
||||
|
||||
result = await self.user_repository.decrease_user_balance(
|
||||
@@ -143,7 +133,7 @@ class RenewalService:
|
||||
user_id,
|
||||
AUTORENEWAL_SUCCESS.format(
|
||||
price=price,
|
||||
end_date=new_end_date.strftime('%d.%m.%Y'),
|
||||
end_date=new_end_date.strftime("%d.%m.%Y"),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -171,9 +161,7 @@ class RenewalService:
|
||||
else:
|
||||
results["failed"] += 1
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Error processing autorenewal for user %d", subscription.user_id
|
||||
)
|
||||
logger.exception("Error processing autorenewal for user %d", subscription.user_id)
|
||||
results["failed"] += 1
|
||||
|
||||
logger.info(
|
||||
|
||||
Reference in New Issue
Block a user