73 lines
2.3 KiB
Python
73 lines
2.3 KiB
Python
from typing import List, Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.crud.base import CRUDBase
|
|
from app.models.withdrawal import Withdrawal, WithdrawalStatus
|
|
from app.schemas.withdrawal import WithdrawalCreate, WithdrawalUpdate
|
|
|
|
|
|
class CRUDWithdrawal(CRUDBase[Withdrawal, WithdrawalCreate, WithdrawalUpdate]):
|
|
def get_by_user(
|
|
self, db: Session, *, user_id: int, skip: int = 0, limit: int = 100
|
|
) -> List[Withdrawal]:
|
|
return (
|
|
db.query(Withdrawal)
|
|
.filter(Withdrawal.user_id == user_id)
|
|
.order_by(Withdrawal.created_at.desc())
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
def get_by_status(
|
|
self, db: Session, *, status: str, skip: int = 0, limit: int = 100
|
|
) -> List[Withdrawal]:
|
|
return (
|
|
db.query(Withdrawal)
|
|
.filter(Withdrawal.status == status)
|
|
.order_by(Withdrawal.created_at.desc())
|
|
.offset(skip)
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
def get_all_pending(
|
|
self, db: Session, *, skip: int = 0, limit: int = 100
|
|
) -> List[Withdrawal]:
|
|
return self.get_by_status(db, status=WithdrawalStatus.PENDING, skip=skip, limit=limit)
|
|
|
|
def approve(
|
|
self, db: Session, *, db_obj: Withdrawal, transaction_hash: str, admin_notes: Optional[str] = None
|
|
) -> Withdrawal:
|
|
update_data = {
|
|
"status": WithdrawalStatus.APPROVED,
|
|
"transaction_hash": transaction_hash
|
|
}
|
|
if admin_notes:
|
|
update_data["admin_notes"] = admin_notes
|
|
|
|
return super().update(db, db_obj=db_obj, obj_in=update_data)
|
|
|
|
def reject(
|
|
self, db: Session, *, db_obj: Withdrawal, admin_notes: str
|
|
) -> Withdrawal:
|
|
update_data = {
|
|
"status": WithdrawalStatus.REJECTED,
|
|
"admin_notes": admin_notes
|
|
}
|
|
|
|
return super().update(db, db_obj=db_obj, obj_in=update_data)
|
|
|
|
|
|
withdrawal = CRUDWithdrawal(Withdrawal)
|
|
|
|
|
|
# Aliases for convenience
|
|
get_withdrawal = withdrawal.get
|
|
create_withdrawal = withdrawal.create
|
|
get_withdrawals_by_user = withdrawal.get_by_user
|
|
get_withdrawals_by_status = withdrawal.get_by_status
|
|
get_all_pending_withdrawals = withdrawal.get_all_pending
|
|
approve_withdrawal = withdrawal.approve
|
|
reject_withdrawal = withdrawal.reject |