This commit is contained in:
xds
2026-03-16 15:43:20 +03:00
parent 002e4cca31
commit 1d76f29244
14 changed files with 546 additions and 89 deletions

View File

@@ -9,6 +9,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from backend.app.core.auth import get_current_rider
from backend.app.core.database import get_session
from backend.app.models.activity import Activity
from backend.app.models.coaching import CoachingChat
from backend.app.models.rider import Rider
from backend.app.models.training import TrainingPlan
@@ -243,11 +244,74 @@ async def get_today(
rider: Rider = Depends(get_current_rider),
session: AsyncSession = Depends(get_session),
):
"""Get today's planned workout."""
"""Get today's planned workout with linked activity if any."""
workout = await get_today_workout(rider, session)
if not workout:
return None
# Check if there's already a linked activity for today
linked_query = (
select(Activity)
.where(Activity.training_plan_id == uuid.UUID(workout["plan_id"]))
.where(Activity.plan_week == workout["week_number"])
.where(Activity.plan_day == workout["day"])
)
linked_result = await session.execute(linked_query)
linked = linked_result.scalar_one_or_none()
workout["linked_activity_id"] = str(linked.id) if linked else None
workout["completed"] = linked is not None
return workout
# --- Activity-Plan linking ---
class LinkRequest(BaseModel):
activity_id: str
plan_id: str
week: int
day: str
@router.post("/link")
async def link_activity(
body: LinkRequest,
rider: Rider = Depends(get_current_rider),
session: AsyncSession = Depends(get_session),
):
"""Manually link an activity to a planned workout day."""
activity = await session.get(Activity, uuid.UUID(body.activity_id))
if not activity or activity.rider_id != rider.id:
raise HTTPException(status_code=404, detail="Activity not found")
plan = await session.get(TrainingPlan, uuid.UUID(body.plan_id))
if not plan or plan.rider_id != rider.id:
raise HTTPException(status_code=404, detail="Plan not found")
activity.training_plan_id = plan.id
activity.plan_week = body.week
activity.plan_day = body.day
await session.commit()
return {"ok": True}
@router.post("/unlink/{activity_id}")
async def unlink_activity(
activity_id: uuid.UUID,
rider: Rider = Depends(get_current_rider),
session: AsyncSession = Depends(get_session),
):
"""Remove link between activity and planned workout."""
activity = await session.get(Activity, activity_id)
if not activity or activity.rider_id != rider.id:
raise HTTPException(status_code=404, detail="Activity not found")
activity.training_plan_id = None
activity.plan_week = None
activity.plan_day = None
await session.commit()
return {"ok": True}
# --- Adjustment chat ---
@router.post("/plan/adjust")