|
2 | 2 |
|
3 | 3 | from uuid import UUID |
4 | 4 |
|
5 | | -from fastapi import APIRouter, Depends, HTTPException, status |
| 5 | +from fastapi import APIRouter, Depends, HTTPException, Query, status |
6 | 6 | from pydantic import BaseModel, ConfigDict |
7 | 7 | from sqlalchemy import select |
8 | 8 | from sqlalchemy.ext.asyncio import AsyncSession |
9 | 9 | from datetime import datetime |
10 | 10 |
|
11 | 11 | from app.api.deps import get_current_user, get_db |
| 12 | +from app.models.organization import OrgMember |
| 13 | +from app.models.project import Project |
12 | 14 | from app.models.run import Run |
13 | 15 | from app.models.user import User |
14 | 16 | from app.schemas.alert import AlertCreate, AlertRead |
15 | 17 | from app.services import alert as alert_service |
16 | 18 |
|
17 | 19 | router = APIRouter(prefix="/runs") |
| 20 | +project_router = APIRouter(prefix="/projects") |
18 | 21 |
|
19 | 22 |
|
20 | 23 | class WebhookDeliveryRead(BaseModel): |
@@ -125,3 +128,62 @@ async def list_alert_deliveries( |
125 | 128 | """ |
126 | 129 | await _verify_run_access(db, run_id, current_user.id) |
127 | 130 | return await alert_service.get_delivery_history(db, alert_id) |
| 131 | + |
| 132 | + |
| 133 | +# --------------------------------------------------------------------------- |
| 134 | +# Project-scope aggregator |
| 135 | +# --------------------------------------------------------------------------- |
| 136 | + |
| 137 | + |
| 138 | +async def _verify_project_membership( |
| 139 | + db: AsyncSession, project_id: UUID, user_id: UUID |
| 140 | +) -> Project: |
| 141 | + """Verify project exists and the caller is a member of its org.""" |
| 142 | + result = await db.execute( |
| 143 | + select(Project).where(Project.id == project_id) |
| 144 | + ) |
| 145 | + project = result.scalar_one_or_none() |
| 146 | + if project is None: |
| 147 | + raise HTTPException( |
| 148 | + status_code=status.HTTP_404_NOT_FOUND, |
| 149 | + detail="Project not found", |
| 150 | + ) |
| 151 | + |
| 152 | + member_result = await db.execute( |
| 153 | + select(OrgMember).where( |
| 154 | + OrgMember.org_id == project.org_id, |
| 155 | + OrgMember.user_id == user_id, |
| 156 | + ) |
| 157 | + ) |
| 158 | + if member_result.scalar_one_or_none() is None: |
| 159 | + raise HTTPException( |
| 160 | + status_code=status.HTTP_403_FORBIDDEN, |
| 161 | + detail="Not a member of the project's organization", |
| 162 | + ) |
| 163 | + return project |
| 164 | + |
| 165 | + |
| 166 | +@project_router.get( |
| 167 | + "/{project_id}/alerts", |
| 168 | + response_model=list[AlertRead], |
| 169 | +) |
| 170 | +async def list_project_alerts( |
| 171 | + project_id: UUID, |
| 172 | + since: datetime | None = Query( |
| 173 | + default=None, |
| 174 | + description="ISO8601 lower bound on Alert.created_at", |
| 175 | + ), |
| 176 | + limit: int = Query(default=20, ge=1, le=100), |
| 177 | + current_user: User = Depends(get_current_user), |
| 178 | + db: AsyncSession = Depends(get_db), |
| 179 | +): |
| 180 | + """List recent alerts across all runs in a project, newest first. |
| 181 | +
|
| 182 | + Aggregator for the FE "Recent alerts (24h)" card. ``Alert`` has no |
| 183 | + resolved-state column, so this returns a flat recent-activity feed |
| 184 | + rather than a status-filtered list. |
| 185 | + """ |
| 186 | + await _verify_project_membership(db, project_id, current_user.id) |
| 187 | + return await alert_service.list_for_project( |
| 188 | + db, project_id, since=since, limit=limit |
| 189 | + ) |
0 commit comments