Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(export): parse Prisma raw timestamptz strings in copilot weekly usage (#12964)

Zamil Majdyfe601430f0c4
diff --git a/autogpt_platform/backend/backend/api/features/admin/credit_admin_routes.py b/autogpt_platform/backend/backend/api/features/admin/credit_admin_routes.py
index c442b801c5..6af41c80fd 100644
--- a/autogpt_platform/backend/backend/api/features/admin/credit_admin_routes.py
+++ b/autogpt_platform/backend/backend/api/features/admin/credit_admin_routes.py
@@ -121,7 +121,7 @@ async def export_credit_transactions(
),
admin_user_id: str = Security(get_user_id),
) -> CreditTransactionsExportResponse:
- """Export CreditTransaction rows in [start, end] for finance reporting.
+ """Export CreditTransaction rows in [start, end].
Capped at CREDIT_EXPORT_MAX_DAYS days and CREDIT_EXPORT_MAX_ROWS rows;
over-cap requests fail fast with 400 so callers narrow the window
diff --git a/autogpt_platform/backend/backend/data/platform_cost.py b/autogpt_platform/backend/backend/data/platform_cost.py
index b5b5f415b1..635dbc1d18 100644
--- a/autogpt_platform/backend/backend/data/platform_cost.py
+++ b/autogpt_platform/backend/backend/data/platform_cost.py
@@ -818,11 +818,17 @@ async def get_copilot_weekly_usage_for_export(
out: list[CopilotWeeklyUsageRow] = []
for r in rows:
- week_start: datetime = r["week_start"]
+ # Prisma's query_raw returns timestamptz columns as ISO strings, not
+ # datetimes — parse defensively so either shape works.
+ raw_week_start = r["week_start"]
+ if isinstance(raw_week_start, str):
+ week_start = datetime.fromisoformat(raw_week_start.replace("Z", "+00:00"))
+ else:
+ week_start = raw_week_start
if week_start.tzinfo is None:
week_start = week_start.replace(tzinfo=timezone.utc)
- # Inclusive end-of-week (Sunday 23:59:59.999999 UTC) — matches finance
- # conventions where "the week" is Mon–Sun, not Mon–next Mon.
+ # Inclusive end-of-week (Sunday 23:59:59.999999 UTC); "the week" is
+ # Mon–Sun, not Mon–next Mon.
week_end = week_start + timedelta(days=7, microseconds=-1)
cost = int(r.get("cost_microdollars") or 0)
tier_str = r.get("tier") or DEFAULT_TIER.value
diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.json
index 3356b0af30..35219ee77f 100644
--- a/autogpt_platform/frontend/src/app/api/openapi.json
+++ b/autogpt_platform/frontend/src/app/api/openapi.json
@@ -3131,7 +3131,7 @@
"get": {
"tags": ["v2", "admin", "credits", "admin"],
"summary": "Export Credit Transactions",
- "description": "Export CreditTransaction rows in [start, end] for finance reporting.\n\nCapped at CREDIT_EXPORT_MAX_DAYS days and CREDIT_EXPORT_MAX_ROWS rows;\nover-cap requests fail fast with 400 so callers narrow the window\ninstead of receiving silently truncated data.",
+ "description": "Export CreditTransaction rows in [start, end].\n\nCapped at CREDIT_EXPORT_MAX_DAYS days and CREDIT_EXPORT_MAX_ROWS rows;\nover-cap requests fail fast with 400 so callers narrow the window\ninstead of receiving silently truncated data.",
"operationId": "getV2Export credit transactions",
"security": [{ "HTTPBearerJWT": [] }],
"parameters": [