Fix send request 500: revert to naive datetime.now() per project contract

The QA fix incorrectly changed datetime.now() to datetime.now(timezone.utc),
but the DB uses TIMESTAMP WITHOUT TIME ZONE (naive). Passing a tz-aware
datetime to a naive column causes asyncpg DataError. Also removes the
temporary diagnostic wrapper and builds response before commit.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Kyle 2026-03-05 21:14:39 +08:00
parent ea491a4b89
commit b16bca919f

View File

@ -10,7 +10,7 @@ Security:
"""
import asyncio
import logging
from datetime import date as date_type, datetime, timedelta, timezone
from datetime import date as date_type, datetime, timedelta
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Path, Query, Request
from sqlalchemy import delete, select, func, and_, update
@ -131,16 +131,6 @@ async def send_connection_request(
current_user: User = Depends(get_current_user),
):
"""Send a connection request to another user."""
try:
return await _send_request_impl(body, request, background_tasks, db, current_user)
except HTTPException:
raise
except Exception as exc:
logger.error("send_connection_request UNHANDLED: %s", exc, exc_info=True)
raise HTTPException(status_code=500, detail=f"DEBUG: {type(exc).__name__}: {exc}")
async def _send_request_impl(body, request, background_tasks, db, current_user):
# Resolve target
result = await db.execute(
select(User).where(User.umbral_name == body.umbral_name)
@ -194,7 +184,7 @@ async def _send_request_impl(body, request, background_tasks, db, current_user):
raise HTTPException(status_code=409, detail="A pending request already exists")
# Per-receiver cap: max 5 pending requests within 10 minutes
ten_min_ago = datetime.now(timezone.utc) - timedelta(minutes=10)
ten_min_ago = datetime.now() - timedelta(minutes=10)
pending_count = await db.scalar(
select(func.count())
.select_from(ConnectionRequest)
@ -388,7 +378,7 @@ async def _respond_to_request_inner(
db: AsyncSession,
current_user: User,
) -> RespondAcceptResponse | RespondRejectResponse:
now = datetime.now(timezone.utc)
now = datetime.now()
# Atomic update — only succeeds if status is still 'pending' and receiver is current user
result = await db.execute(
@ -575,7 +565,7 @@ async def cancel_request(
current_user: User = Depends(get_current_user),
):
"""Cancel an outgoing connection request. Atomic via UPDATE...WHERE status='pending'."""
now = datetime.now(timezone.utc)
now = datetime.now()
# Atomic update — only succeeds if sender is current user and status is still pending
result = await db.execute(