Добавлен новый эндпоинт для запроса на вывод средств для Telegram-агентов в integration_api.py. Обновлены теги для существующих эндпоинтов, изменив их с "partner-tg" на "agent-tg". В integration_models.py добавлены модели WithdrawRequest и WithdrawResponse для обработки запросов на вывод средств. Улучшена логика обработки транзакций и проверок баланса.
This commit is contained in:
parent
7045d6790a
commit
3973d6404d
@ -6,7 +6,7 @@ import hashlib
|
|||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from sql_models import Company, IntegrationToken, Ref, Sale, AgentTransaction, PartnerTransaction, AgentBalance, TgAgent, CompanyBalance
|
from sql_models import Company, IntegrationToken, Ref, Sale, AgentTransaction, PartnerTransaction, AgentBalance, TgAgent, CompanyBalance
|
||||||
from integration_models import Token, SaleCreateRequest, SaleCreateResponse, TransactionStatus
|
from integration_models import Token, SaleCreateRequest, SaleCreateResponse, TransactionStatus, WithdrawRequest, WithdrawResponse
|
||||||
from bff_models import RegisterResponse, TgAuthResponse
|
from bff_models import RegisterResponse, TgAuthResponse
|
||||||
from tg_models import RefAddRequest, RefResponse, RefAddResponse, RefStatResponse, RegisterRequest, StatResponse
|
from tg_models import RefAddRequest, RefResponse, RefAddResponse, RefStatResponse, RegisterRequest, StatResponse
|
||||||
from helpers_bff import AUTH_DB_ENGINE, get_integration_db, create_integration_jwt_token, get_current_company_from_jwt, get_tg_agent_by_tg_id, get_current_tg_agent
|
from helpers_bff import AUTH_DB_ENGINE, get_integration_db, create_integration_jwt_token, get_current_company_from_jwt, get_tg_agent_by_tg_id, get_current_tg_agent
|
||||||
@ -53,7 +53,7 @@ async def get_token_for_api_key(
|
|||||||
jwt_token = create_integration_jwt_token(integration_token_db.company_id)
|
jwt_token = create_integration_jwt_token(integration_token_db.company_id)
|
||||||
return {"access_token": jwt_token, "token_type": "bearer"}
|
return {"access_token": jwt_token, "token_type": "bearer"}
|
||||||
|
|
||||||
@app.get("/ref", response_model=List[RefResponse], tags=["partner-tg"])
|
@app.get("/ref", response_model=List[RefResponse], tags=["agent-tg"])
|
||||||
def get_refs(current_tg_agent: TgAgent = Depends(get_current_tg_agent), db: Session = Depends(get_integration_db)):
|
def get_refs(current_tg_agent: TgAgent = Depends(get_current_tg_agent), db: Session = Depends(get_integration_db)):
|
||||||
"""
|
"""
|
||||||
Возвращает список реферальных ссылок текущего Telegram-агента.
|
Возвращает список реферальных ссылок текущего Telegram-агента.
|
||||||
@ -61,7 +61,7 @@ def get_refs(current_tg_agent: TgAgent = Depends(get_current_tg_agent), db: Sess
|
|||||||
refs = db.exec(select(Ref).where(Ref.tg_agent_id == current_tg_agent.id)).all()
|
refs = db.exec(select(Ref).where(Ref.tg_agent_id == current_tg_agent.id)).all()
|
||||||
return [RefResponse(ref=r.ref, description=r.description or "") for r in refs]
|
return [RefResponse(ref=r.ref, description=r.description or "") for r in refs]
|
||||||
|
|
||||||
@app.post("/ref/add", tags=["partner-tg"], response_model=RefAddResponse)
|
@app.post("/ref/add", tags=["agent-tg"], response_model=RefAddResponse)
|
||||||
def add_ref(req: RefAddRequest, current_tg_agent: TgAgent = Depends(get_current_tg_agent), db: Session = Depends(get_integration_db)):
|
def add_ref(req: RefAddRequest, current_tg_agent: TgAgent = Depends(get_current_tg_agent), db: Session = Depends(get_integration_db)):
|
||||||
"""
|
"""
|
||||||
Добавляет новую реферальную ссылку для текущего Telegram-агента.
|
Добавляет новую реферальную ссылку для текущего Telegram-агента.
|
||||||
@ -76,7 +76,7 @@ def add_ref(req: RefAddRequest, current_tg_agent: TgAgent = Depends(get_current_
|
|||||||
db.refresh(new_ref)
|
db.refresh(new_ref)
|
||||||
return {"ref": new_ref.ref}
|
return {"ref": new_ref.ref}
|
||||||
|
|
||||||
@app.get("/ref/stat", tags=["partner-tg"], response_model=RefStatResponse)
|
@app.get("/ref/stat", tags=["agent-tg"], response_model=RefStatResponse)
|
||||||
def get_ref_stat(current_tg_agent: TgAgent = Depends(get_current_tg_agent), db: Session = Depends(get_integration_db)):
|
def get_ref_stat(current_tg_agent: TgAgent = Depends(get_current_tg_agent), db: Session = Depends(get_integration_db)):
|
||||||
"""
|
"""
|
||||||
Возвращает статистику по реферальным ссылкам текущего Telegram-агента.
|
Возвращает статистику по реферальным ссылкам текущего Telegram-агента.
|
||||||
@ -96,7 +96,7 @@ def get_ref_stat(current_tg_agent: TgAgent = Depends(get_current_tg_agent), db:
|
|||||||
})
|
})
|
||||||
return {"refData": result}
|
return {"refData": result}
|
||||||
|
|
||||||
@app.get("/stat", tags=["partner-tg"], response_model=StatResponse)
|
@app.get("/stat", tags=["agent-tg"], response_model=StatResponse)
|
||||||
def get_stat(current_tg_agent: TgAgent = Depends(get_current_tg_agent), db: Session = Depends(get_integration_db)):
|
def get_stat(current_tg_agent: TgAgent = Depends(get_current_tg_agent), db: Session = Depends(get_integration_db)):
|
||||||
"""
|
"""
|
||||||
Возвращает общую статистику для текущего Telegram-агента.
|
Возвращает общую статистику для текущего Telegram-агента.
|
||||||
@ -118,7 +118,7 @@ def get_stat(current_tg_agent: TgAgent = Depends(get_current_tg_agent), db: Sess
|
|||||||
"availableWithdrawal": availableWithdrawal
|
"availableWithdrawal": availableWithdrawal
|
||||||
}
|
}
|
||||||
|
|
||||||
@app.post("/tg_auth", tags=["partner-tg"], response_model=TgAuthResponse)
|
@app.post("/tg_auth", tags=["agent-tg"], response_model=TgAuthResponse)
|
||||||
def tg_auth(hash: str = Body(..., embed=True), db: Session = Depends(get_integration_db)):
|
def tg_auth(hash: str = Body(..., embed=True), db: Session = Depends(get_integration_db)):
|
||||||
"""
|
"""
|
||||||
Авторизует Telegram-агента по хешу.
|
Авторизует Telegram-агента по хешу.
|
||||||
@ -128,6 +128,56 @@ def tg_auth(hash: str = Body(..., embed=True), db: Session = Depends(get_integra
|
|||||||
raise HTTPException(status_code=401, detail="Hash not found")
|
raise HTTPException(status_code=401, detail="Hash not found")
|
||||||
return {"msg": "Auth success", "tg_id": tg_agent.tg_id}
|
return {"msg": "Auth success", "tg_id": tg_agent.tg_id}
|
||||||
|
|
||||||
|
@app.post("/withdraw", tags=["agent-tg"], response_model=WithdrawResponse)
|
||||||
|
async def withdraw_funds(
|
||||||
|
req: WithdrawRequest,
|
||||||
|
db: Session = Depends(get_integration_db)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Запрос на вывод средств для Telegram-агента.
|
||||||
|
"""
|
||||||
|
tg_agent = db.exec(select(TgAgent).where(TgAgent.tg_id == req.tg_id)).first()
|
||||||
|
if not tg_agent:
|
||||||
|
raise HTTPException(status_code=404, detail="Telegram-агент не найден")
|
||||||
|
|
||||||
|
company = db.exec(select(Company).where(Company.id == tg_agent.company_id)).first()
|
||||||
|
if not company:
|
||||||
|
raise HTTPException(status_code=404, detail="Компания не найдена для агента")
|
||||||
|
|
||||||
|
if req.amount <= 0:
|
||||||
|
raise HTTPException(status_code=400, detail="Сумма для вывода должна быть положительной")
|
||||||
|
|
||||||
|
agent_balance = db.exec(select(AgentBalance).where(AgentBalance.tg_agent_id == tg_agent.id)).first()
|
||||||
|
if not agent_balance or agent_balance.available_balance < req.amount:
|
||||||
|
raise HTTPException(status_code=400, detail="Недостаточно средств на балансе для вывода")
|
||||||
|
|
||||||
|
# Определяем статус транзакции
|
||||||
|
transaction_status = TransactionStatus.WAITING
|
||||||
|
if company.auto_approve_transactions:
|
||||||
|
transaction_status = TransactionStatus.NEW
|
||||||
|
|
||||||
|
# Создаем запись AgentTransaction
|
||||||
|
new_agent_transaction = AgentTransaction(
|
||||||
|
tg_agent_id=tg_agent.id,
|
||||||
|
amount=req.amount,
|
||||||
|
status=transaction_status.value,
|
||||||
|
transaction_group=uuid.uuid4()
|
||||||
|
)
|
||||||
|
db.add(new_agent_transaction)
|
||||||
|
|
||||||
|
|
||||||
|
# Обновляем баланс агента
|
||||||
|
agent_balance.available_balance -= req.amount
|
||||||
|
if transaction_status == TransactionStatus.WAITING: # Если автоматически одобряется, переводим на замороженный баланс компании (т.е. компания должна выплатить)
|
||||||
|
agent_balance.frozen_balance += req.amount # Удерживаем средства, пока они не будут выведены
|
||||||
|
db.add(agent_balance)
|
||||||
|
|
||||||
|
db.commit()
|
||||||
|
db.refresh(new_agent_transaction)
|
||||||
|
db.refresh(agent_balance)
|
||||||
|
|
||||||
|
return {"msg": "Запрос на вывод средств успешно создан", "transaction_id": new_agent_transaction.transaction_group}
|
||||||
|
|
||||||
@app.post("/sale", tags=["integration"], response_model=SaleCreateResponse)
|
@app.post("/sale", tags=["integration"], response_model=SaleCreateResponse)
|
||||||
async def create_sale(
|
async def create_sale(
|
||||||
req: SaleCreateRequest,
|
req: SaleCreateRequest,
|
||||||
@ -221,7 +271,7 @@ async def create_sale(
|
|||||||
"crediting": new_sale.crediting
|
"crediting": new_sale.crediting
|
||||||
}
|
}
|
||||||
|
|
||||||
@app.post("/register", tags=["partner-tg"], response_model=RegisterResponse)
|
@app.post("/register", tags=["agent-tg"], response_model=RegisterResponse)
|
||||||
def register(req: RegisterRequest, db: Session = Depends(get_integration_db)):
|
def register(req: RegisterRequest, db: Session = Depends(get_integration_db)):
|
||||||
"""
|
"""
|
||||||
Регистрирует нового Telegram-агента в системе.
|
Регистрирует нового Telegram-агента в системе.
|
||||||
|
|||||||
@ -25,6 +25,14 @@ class SaleCreateResponse(BaseModel):
|
|||||||
sale_id: str
|
sale_id: str
|
||||||
crediting: float
|
crediting: float
|
||||||
|
|
||||||
|
class WithdrawRequest(BaseModel):
|
||||||
|
tg_id: int
|
||||||
|
amount: float
|
||||||
|
|
||||||
|
class WithdrawResponse(BaseModel):
|
||||||
|
msg: str
|
||||||
|
transaction_id: uuid.UUID
|
||||||
|
|
||||||
class TransactionStatus(str, Enum):
|
class TransactionStatus(str, Enum):
|
||||||
NEW = "new"
|
NEW = "new"
|
||||||
PROCESS = "process"
|
PROCESS = "process"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user