From 5d14969f822ea592c8d01ff1a6fe9bee4aa5ecb0 Mon Sep 17 00:00:00 2001 From: Redsandyg Date: Sat, 7 Jun 2025 12:41:51 +0300 Subject: [PATCH] =?UTF-8?q?=20=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D1=8B=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85=20=D1=81=20?= =?UTF-8?q?=D1=83=D1=87=D0=B5=D1=82=D0=BE=D0=BC=20=D1=82=D0=B5=D0=BA=D1=83?= =?UTF-8?q?=D1=89=D0=B5=D0=B3=D0=BE=20=D0=B0=D0=BA=D0=BA=D0=B0=D1=83=D0=BD?= =?UTF-8?q?=D1=82=D0=B0=20=D0=B2=20=D1=80=D0=B0=D0=B7=D0=BB=D0=B8=D1=87?= =?UTF-8?q?=D0=BD=D1=8B=D1=85=20=D1=8D=D0=BD=D0=B4=D0=BF=D0=BE=D0=B8=D0=BD?= =?UTF-8?q?=D1=82=D0=B0=D1=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 52 +++++++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/main.py b/main.py index 4ec9a6c..5b5fbb1 100644 --- a/main.py +++ b/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)):