feat(points): 新增积分赠送
This commit is contained in:
@@ -13,7 +13,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from core.base.schema import ProductCategory
|
||||
from core.db.crud import CRUD
|
||||
from core.db.engine import get_async_session
|
||||
from core.db.models import User, Order, Product
|
||||
from core.db.models import User, Order, Product, PointsTransaction
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -30,7 +30,7 @@ async def actions_after_payment(
|
||||
logger.error(f'Order not found -> {order_id}')
|
||||
return
|
||||
|
||||
product: Product = await db_crud.get(model=Product, filters={'product_id': order.product_id})
|
||||
product: Product = await db_crud.get(model=Product, filters={'id': order.product_id})
|
||||
if not product:
|
||||
logger.warning(f'Product not found -> {order.product_id}')
|
||||
return
|
||||
@@ -40,30 +40,62 @@ async def actions_after_payment(
|
||||
logger.warning(f'User not found -> {order.order_id}')
|
||||
return
|
||||
|
||||
if product.action == ProductCategory.VIP:
|
||||
vip_expire_date = (
|
||||
user.vip_expire_date or datetime.now()
|
||||
) + timedelta(days=product.number)
|
||||
await db_crud.update(
|
||||
model=User,
|
||||
data={
|
||||
'vip_expire_date': vip_expire_date.date()
|
||||
},
|
||||
filters={'user_id': order.user_id}
|
||||
await actions_handler(db_crud, product, user)
|
||||
if product.gift_id:
|
||||
gift_product = await db_crud.get(
|
||||
model=Product, filters={
|
||||
'id': product.gift_id,
|
||||
'category': ProductCategory.GIFT
|
||||
}
|
||||
)
|
||||
elif product.action == ProductCategory.POINTS:
|
||||
points = (
|
||||
user.points or 0
|
||||
) + product.number
|
||||
await db_crud.update(
|
||||
model=User,
|
||||
data={
|
||||
'points': points
|
||||
},
|
||||
filters={'user_id': order.user_id}
|
||||
)
|
||||
else:
|
||||
logger.error(f'Unknown action -> {product.action}')
|
||||
await actions_handler(db_crud, gift_product, user)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f'Payment Actions Exception occurred -> {e}')
|
||||
logger.exception(e)
|
||||
logger.exception(e)
|
||||
|
||||
|
||||
async def actions_handler(db_crud: CRUD, product: Product, user: User):
|
||||
if product.action == ProductCategory.VIP:
|
||||
vip_expire_date = (
|
||||
user.vip_expire_date or datetime.now()
|
||||
) + timedelta(days=product.number)
|
||||
await db_crud.update(
|
||||
model=User,
|
||||
data={
|
||||
'vip_expire_date': vip_expire_date.date()
|
||||
},
|
||||
filters={'user_id': user.user_id}
|
||||
)
|
||||
elif product.action == ProductCategory.POINTS:
|
||||
points = (
|
||||
user.points or 0
|
||||
) + product.number
|
||||
await db_crud.update(
|
||||
model=User,
|
||||
data={
|
||||
'points': points
|
||||
},
|
||||
filters={'user_id': user.user_id}
|
||||
)
|
||||
|
||||
if product.category.value == 'gift':
|
||||
transaction_type = 3
|
||||
else:
|
||||
transaction_type = 1
|
||||
|
||||
await db_crud.update(
|
||||
model=PointsTransaction,
|
||||
data={
|
||||
'user_id': user.user_id,
|
||||
'transaction_type': transaction_type,
|
||||
'points': product.number,
|
||||
'product_id': product.id,
|
||||
'balance': points,
|
||||
'description': f'{product.name} - {product.number}'
|
||||
},
|
||||
filters={'id': -1}
|
||||
)
|
||||
|
||||
else:
|
||||
logger.error(f'Unknown action -> {product.action}')
|
||||
|
||||
Reference in New Issue
Block a user