fix: Lint fixes of services
All checks were successful
Backend CI / test (push) Successful in 3m59s

This commit is contained in:
JSC
2025-08-01 01:27:47 +02:00
parent 95ccb76233
commit a10111793c
12 changed files with 237 additions and 160 deletions

View File

@@ -76,14 +76,12 @@ class CreditService:
self,
user_id: int,
action_type: CreditActionType,
metadata: dict[str, Any] | None = None,
) -> tuple[User, CreditAction]:
"""Validate user has sufficient credits and optionally reserve them.
Args:
user_id: The user ID
action_type: The type of action
metadata: Optional metadata to store with transaction
Returns:
Tuple of (user, credit_action)
@@ -118,6 +116,7 @@ class CreditService:
self,
user_id: int,
action_type: CreditActionType,
*,
success: bool = True,
metadata: dict[str, Any] | None = None,
) -> CreditTransaction:
@@ -139,18 +138,26 @@ class CreditService:
"""
action = get_credit_action(action_type)
# Only deduct if action requires success and was successful, or doesn't require success
should_deduct = (action.requires_success and success) or not action.requires_success
# Only deduct if action requires success and was successful,
# or doesn't require success
should_deduct = (
action.requires_success and success
) or not action.requires_success
if not should_deduct:
logger.info(
"Skipping credit deduction for user %s: action %s failed and requires success",
"Skipping credit deduction for user %s: "
"action %s failed and requires success",
user_id,
action_type.value,
)
# Still create a transaction record for auditing
return await self._create_transaction_record(
user_id, action, 0, success, metadata,
user_id,
action,
0,
success=success,
metadata=metadata,
)
session = self.db_session_factory()
@@ -204,14 +211,19 @@ class CreditService:
"action_type": action_type.value,
"success": success,
}
await socket_manager.send_to_user(str(user_id), "user_credits_changed", event_data)
await socket_manager.send_to_user(
str(user_id),
"user_credits_changed",
event_data,
)
logger.info("Emitted user_credits_changed event for user %s", user_id)
except Exception:
logger.exception(
"Failed to emit user_credits_changed event for user %s", user_id,
"Failed to emit user_credits_changed event for user %s",
user_id,
)
return transaction
else:
return transaction
except Exception:
await session.rollback()
@@ -292,14 +304,19 @@ class CreditService:
"description": description,
"success": True,
}
await socket_manager.send_to_user(str(user_id), "user_credits_changed", event_data)
await socket_manager.send_to_user(
str(user_id),
"user_credits_changed",
event_data,
)
logger.info("Emitted user_credits_changed event for user %s", user_id)
except Exception:
logger.exception(
"Failed to emit user_credits_changed event for user %s", user_id,
"Failed to emit user_credits_changed event for user %s",
user_id,
)
return transaction
else:
return transaction
except Exception:
await session.rollback()
@@ -312,6 +329,7 @@ class CreditService:
user_id: int,
action: CreditAction,
amount: int,
*,
success: bool,
metadata: dict[str, Any] | None = None,
) -> CreditTransaction:
@@ -342,19 +360,22 @@ class CreditService:
amount=amount,
balance_before=user.credits,
balance_after=user.credits,
description=f"{action.description} (failed)" if not success else action.description,
description=(
f"{action.description} (failed)"
if not success
else action.description
),
success=success,
metadata_json=json.dumps(metadata) if metadata else None,
)
session.add(transaction)
await session.commit()
return transaction
except Exception:
await session.rollback()
raise
else:
return transaction
finally:
await session.close()