Fix cashflow endpoint: pass date object instead of integer to SQL
asyncpg infers :days as an integer, causing a 'date >= integer' type error in PostgreSQL. Compute the cutoff date in Python and bind it as a date parameter instead. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
4572621f5d
commit
a7e58d6aa1
1 changed files with 5 additions and 2 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import date, timedelta
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from sqlalchemy import text
|
from sqlalchemy import text
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
@ -97,6 +99,7 @@ async def get_portfolio_monthly_returns(db: AsyncSession, user_id: str) -> pd.Da
|
||||||
|
|
||||||
|
|
||||||
async def get_daily_cash_flow(db: AsyncSession, user_id: str, days: int = 90) -> pd.DataFrame:
|
async def get_daily_cash_flow(db: AsyncSession, user_id: str, days: int = 90) -> pd.DataFrame:
|
||||||
|
cutoff = date.today() - timedelta(days=days)
|
||||||
result = await db.execute(text("""
|
result = await db.execute(text("""
|
||||||
SELECT
|
SELECT
|
||||||
t.date::date AS ds,
|
t.date::date AS ds,
|
||||||
|
|
@ -107,10 +110,10 @@ async def get_daily_cash_flow(db: AsyncSession, user_id: str, days: int = 90) ->
|
||||||
AND t.deleted_at IS NULL
|
AND t.deleted_at IS NULL
|
||||||
AND t.status != 'void'
|
AND t.status != 'void'
|
||||||
AND t.type IN ('income', 'expense')
|
AND t.type IN ('income', 'expense')
|
||||||
AND t.date >= CURRENT_DATE - :days
|
AND t.date >= :cutoff
|
||||||
GROUP BY t.date
|
GROUP BY t.date
|
||||||
ORDER BY t.date ASC
|
ORDER BY t.date ASC
|
||||||
"""), {"uid": str(user_id), "days": days})
|
"""), {"uid": str(user_id), "cutoff": cutoff})
|
||||||
rows = result.fetchall()
|
rows = result.fetchall()
|
||||||
if not rows:
|
if not rows:
|
||||||
return pd.DataFrame(columns=["ds", "inflow", "outflow"])
|
return pd.DataFrame(columns=["ds", "inflow", "outflow"])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue