feat: add subscription and transaction repositories
- Implemented SubscriptionRepository with methods to get, create, and update subscriptions. - Added BalanceTXRepository for creating and retrieving balance transactions. - Introduced a test script for simulating Pally webhook for local testing.
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import datetime
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from db.models import User
|
||||
from db.models.transactions import BalanceTransaction, BalanceTxType
|
||||
from schemas.users import NewUserDTO
|
||||
|
||||
|
||||
@@ -61,12 +64,60 @@ class UserRepository:
|
||||
return user
|
||||
|
||||
async def increase_user_balance(
|
||||
self, session: AsyncSession, user_id: int, amount: int
|
||||
self,
|
||||
session: AsyncSession,
|
||||
user_id: int,
|
||||
amount: int,
|
||||
tx_type: BalanceTxType,
|
||||
description: str,
|
||||
) -> User | None:
|
||||
user = await self.get_user_by_id(session, user_id=user_id)
|
||||
if not user:
|
||||
return None
|
||||
|
||||
created_at = datetime.datetime.now(datetime.UTC)
|
||||
transaction = BalanceTransaction(
|
||||
user_id=user_id,
|
||||
amount=amount,
|
||||
tx_type=tx_type,
|
||||
balance_before=user.balance,
|
||||
balance_after=user.balance + amount,
|
||||
description=description,
|
||||
created_at=created_at,
|
||||
)
|
||||
|
||||
user.balance += amount
|
||||
|
||||
session.add(transaction)
|
||||
|
||||
await session.commit()
|
||||
return user
|
||||
|
||||
async def decrease_user_balance(
|
||||
self,
|
||||
session: AsyncSession,
|
||||
user_id: int,
|
||||
amount: int,
|
||||
tx_type: BalanceTxType,
|
||||
description: str,
|
||||
) -> User | None:
|
||||
user = await self.get_user_by_id(session, user_id=user_id)
|
||||
if not user or user.balance < amount:
|
||||
return None
|
||||
|
||||
created_at = datetime.datetime.now(datetime.timezone.UTC)
|
||||
transaction = BalanceTransaction(
|
||||
user_id=user_id,
|
||||
amount=amount,
|
||||
tx_type=tx_type,
|
||||
balance_before=user.balance,
|
||||
balance_after=user.balance + amount,
|
||||
description=description,
|
||||
created_at=created_at,
|
||||
)
|
||||
|
||||
user.balance -= amount
|
||||
session.add(transaction)
|
||||
|
||||
await session.commit()
|
||||
return user
|
||||
|
||||
Reference in New Issue
Block a user