Обновлены функции для получения данных с учетом текущего аккаунта в различных эндпоинтах.
This commit is contained in:
parent
8a8b111f40
commit
5d14969f82
52
main.py
52
main.py
@ -162,6 +162,24 @@ def get_db():
|
||||
with Session(AUTH_DB_ENGINE) as session:
|
||||
yield session
|
||||
|
||||
def get_current_account(token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)):
|
||||
credentials_exception = HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Could not validate credentials",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
try:
|
||||
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||
login: str = payload.get("sub")
|
||||
if login is None:
|
||||
raise credentials_exception
|
||||
except InvalidTokenError:
|
||||
raise credentials_exception
|
||||
account = get_account_by_login(db, login)
|
||||
if account is None:
|
||||
raise credentials_exception
|
||||
return account
|
||||
|
||||
# Авторизация
|
||||
async def get_current_tg_agent(request: Request, db: Session = Depends(get_db)):
|
||||
credentials_exception = HTTPException(
|
||||
@ -313,7 +331,7 @@ def get_stat(current_tg_agent: TgAgent = Depends(get_current_tg_agent), db: Sess
|
||||
}
|
||||
|
||||
@app.get("/dashboard/cards", tags=["bff"])
|
||||
def get_dashboard_cards(db: Session = Depends(get_db)):
|
||||
def get_dashboard_cards(current_account: Account = Depends(get_current_account), db: Session = Depends(get_db)):
|
||||
# 1. Общий доход - сумма всех Sale.cost
|
||||
total_revenue = db.exec(select(Sale)).all()
|
||||
totalRevenue = sum(sale.cost for sale in total_revenue)
|
||||
@ -341,7 +359,7 @@ def get_dashboard_cards(db: Session = Depends(get_db)):
|
||||
}
|
||||
|
||||
@app.get("/dashboard/chart/total", tags=["bff"])
|
||||
def get_dashboard_chart_total(db: Session = Depends(get_db)):
|
||||
def get_dashboard_chart_total(current_account: Account = Depends(get_current_account), db: Session = Depends(get_db)):
|
||||
# Группируем продажи по дате (день)
|
||||
result = db.exec(
|
||||
select(
|
||||
@ -359,7 +377,7 @@ def get_dashboard_chart_total(db: Session = Depends(get_db)):
|
||||
return JSONResponse(content=data)
|
||||
|
||||
@app.get("/dashboard/chart/agent", tags=["bff"])
|
||||
def get_dashboard_chart_agent(db: Session = Depends(get_db)):
|
||||
def get_dashboard_chart_agent(current_account: Account = Depends(get_current_account), db: Session = Depends(get_db)):
|
||||
# Получаем всех агентов
|
||||
agents = db.exec(select(TgAgent)).all()
|
||||
result = []
|
||||
@ -390,6 +408,7 @@ def get_agents_stat(
|
||||
db: Session = Depends(get_db),
|
||||
date_start: str = Query(None),
|
||||
date_end: str = Query(None),
|
||||
current_account: Account = Depends(get_current_account),
|
||||
):
|
||||
agents_query = select(TgAgent)
|
||||
if date_start:
|
||||
@ -429,6 +448,7 @@ def get_referrals_stat(
|
||||
db: Session = Depends(get_db),
|
||||
date_start: str = Query(None),
|
||||
date_end: str = Query(None),
|
||||
current_account: Account = Depends(get_current_account),
|
||||
):
|
||||
refs_query = select(Ref)
|
||||
if date_start:
|
||||
@ -456,6 +476,7 @@ def get_sales_stat(
|
||||
db: Session = Depends(get_db),
|
||||
date_start: str = Query(None),
|
||||
date_end: str = Query(None),
|
||||
current_account: Account = Depends(get_current_account),
|
||||
):
|
||||
sales_query = select(Sale)
|
||||
if date_start:
|
||||
@ -483,7 +504,7 @@ def get_sales_stat(
|
||||
return JSONResponse(content=result)
|
||||
|
||||
@app.get("/billing/cards", tags=["bff"])
|
||||
def get_billing_cards(db: Session = Depends(get_db)):
|
||||
def get_billing_cards(current_account: Account = Depends(get_current_account), db: Session = Depends(get_db)):
|
||||
# 1. cost - Общий заработок (сумма всех Sale.cost)
|
||||
sales = db.exec(select(Sale)).all()
|
||||
cost = sum(sale.cost for sale in sales)
|
||||
@ -507,6 +528,7 @@ def get_billing_payouts_transactions(
|
||||
db: Session = Depends(get_db),
|
||||
date_start: str = Query(None),
|
||||
date_end: str = Query(None),
|
||||
current_account: Account = Depends(get_current_account),
|
||||
):
|
||||
# Используем AgentTransaction вместо Transaction
|
||||
# Явно выбираем обе модели для корректной распаковки
|
||||
@ -533,7 +555,7 @@ def get_billing_payouts_transactions(
|
||||
return result
|
||||
|
||||
@app.get("/billing/chart/stat", tags=["bff"])
|
||||
def get_billing_chart_stat(db: Session = Depends(get_db)):
|
||||
def get_billing_chart_stat(current_account: Account = Depends(get_current_account), db: Session = Depends(get_db)):
|
||||
# Группируем агентские транзакции по дате (день) и статусу
|
||||
result = db.exec(
|
||||
select(
|
||||
@ -555,7 +577,7 @@ def get_billing_chart_stat(db: Session = Depends(get_db)):
|
||||
return JSONResponse(content=data)
|
||||
|
||||
@app.get("/billing/chart/pie", tags=["bff"])
|
||||
def get_billing_chart_pie(db: Session = Depends(get_db)):
|
||||
def get_billing_chart_pie(current_account: Account = Depends(get_current_account), db: Session = Depends(get_db)):
|
||||
# Группируем агентские транзакции по статусу
|
||||
result = db.exec(
|
||||
select(
|
||||
@ -588,23 +610,7 @@ def get_account_by_login(db: Session, login: str) -> Optional[Account]:
|
||||
statement = select(Account).where(Account.login == login)
|
||||
return db.exec(statement).first()
|
||||
|
||||
def get_current_account(token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)):
|
||||
credentials_exception = HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Could not validate credentials",
|
||||
headers={"WWW-Authenticate": "Bearer"},
|
||||
)
|
||||
try:
|
||||
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||
login: str = payload.get("sub")
|
||||
if login is None:
|
||||
raise credentials_exception
|
||||
except InvalidTokenError:
|
||||
raise credentials_exception
|
||||
account = get_account_by_login(db, login)
|
||||
if account is None:
|
||||
raise credentials_exception
|
||||
return account
|
||||
|
||||
|
||||
@app.get("/account", tags=["bff"])
|
||||
def get_account(current_account: Account = Depends(get_current_account)):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user