fix get_user_bonuses
This commit is contained in:
@ -83,29 +83,27 @@ class BonusService:
|
|||||||
return {"bonuses": [BonusType(**bonus) for bonus in bonuses]}
|
return {"bonuses": [BonusType(**bonus) for bonus in bonuses]}
|
||||||
|
|
||||||
async def get_user_bonuses(self, username: str):
|
async def get_user_bonuses(self, username: str):
|
||||||
"""Получить активные бонусы пользователя"""
|
"""Получить бонусы пользователя (активные и неактивные)"""
|
||||||
from app.db.database import users_collection
|
from app.db.database import users_collection
|
||||||
|
|
||||||
user = await users_collection.find_one({"username": username})
|
user = await users_collection.find_one({"username": username})
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(status_code=404, detail="Пользователь не найден")
|
raise HTTPException(status_code=404, detail="Пользователь не найден")
|
||||||
|
|
||||||
# Находим активные бонусы с учетом бесконечных (expires_at = null) или действующих
|
user_bonuses = await user_bonuses_collection.find({
|
||||||
active_bonuses = await user_bonuses_collection.find({
|
|
||||||
"user_id": str(user["_id"]),
|
"user_id": str(user["_id"]),
|
||||||
"is_active": True,
|
|
||||||
}).to_list(50)
|
}).to_list(50)
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
for bonus in active_bonuses:
|
for bonus in user_bonuses:
|
||||||
bonus_type = await bonus_types_collection.find_one({"id": bonus["bonus_type_id"]})
|
bonus_type = await bonus_types_collection.find_one({"id": bonus["bonus_type_id"]})
|
||||||
if bonus_type:
|
if bonus_type:
|
||||||
# Рассчитываем итоговое значение эффекта с учетом уровня
|
|
||||||
level = bonus.get("level", 1)
|
level = bonus.get("level", 1)
|
||||||
effect_value = bonus_type["base_effect_value"] + (level - 1) * bonus_type["effect_increment"]
|
effect_value = bonus_type["base_effect_value"] + (level - 1) * bonus_type["effect_increment"]
|
||||||
|
|
||||||
bonus_data = {
|
bonus_data = {
|
||||||
"id": bonus["id"],
|
"id": bonus["id"],
|
||||||
|
"bonus_type_id": bonus["bonus_type_id"],
|
||||||
"name": bonus_type["name"],
|
"name": bonus_type["name"],
|
||||||
"description": bonus_type["description"],
|
"description": bonus_type["description"],
|
||||||
"effect_type": bonus_type["effect_type"],
|
"effect_type": bonus_type["effect_type"],
|
||||||
@ -115,12 +113,13 @@ class BonusService:
|
|||||||
"can_upgrade": bonus_type["max_level"] == 0 or level < bonus_type["max_level"],
|
"can_upgrade": bonus_type["max_level"] == 0 or level < bonus_type["max_level"],
|
||||||
"upgrade_price": bonus_type["upgrade_price"],
|
"upgrade_price": bonus_type["upgrade_price"],
|
||||||
"image_url": bonus_type.get("image_url"),
|
"image_url": bonus_type.get("image_url"),
|
||||||
|
"is_active": bonus.get("is_active", True),
|
||||||
}
|
}
|
||||||
|
|
||||||
# Для временных бонусов добавляем срок
|
|
||||||
if bonus.get("expires_at"):
|
if bonus.get("expires_at"):
|
||||||
bonus_data["expires_at"] = bonus["expires_at"].isoformat()
|
bonus_data["expires_at"] = bonus["expires_at"].isoformat()
|
||||||
bonus_data["time_left"] = (bonus["expires_at"] - datetime.utcnow()).total_seconds()
|
bonus_data["time_left"] = (bonus["expires_at"] - datetime.utcnow()).total_seconds()
|
||||||
|
bonus_data["is_permanent"] = False
|
||||||
else:
|
else:
|
||||||
bonus_data["is_permanent"] = True
|
bonus_data["is_permanent"] = True
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user