FastAPI + Celery + Next.js + Postgres/Redis app with company monitoring, source collection, LLM-based change analysis, enrichment, and account security (Turnstile, escalating lockout, email verification).
47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
from sqlalchemy import func, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.models.report import Report
|
|
|
|
|
|
class ReportRepository:
|
|
def __init__(self, db: AsyncSession) -> None:
|
|
self.db = db
|
|
|
|
async def get(self, report_id: uuid.UUID) -> Report | None:
|
|
return await self.db.get(Report, report_id)
|
|
|
|
async def get_for_company(self, report_id: uuid.UUID, company_id: uuid.UUID) -> Report | None:
|
|
result = await self.db.execute(
|
|
select(Report).where(Report.id == report_id, Report.company_id == company_id)
|
|
)
|
|
return result.scalar_one_or_none()
|
|
|
|
async def list_for_company(self, company_id: uuid.UUID, limit: int = 50) -> list[Report]:
|
|
result = await self.db.execute(
|
|
select(Report)
|
|
.where(Report.company_id == company_id)
|
|
.order_by(Report.created_at.desc())
|
|
.limit(limit)
|
|
)
|
|
return list(result.scalars().all())
|
|
|
|
async def latest_for_company(self, company_id: uuid.UUID) -> Report | None:
|
|
result = await self.db.execute(
|
|
select(Report)
|
|
.where(Report.company_id == company_id)
|
|
.order_by(Report.created_at.desc())
|
|
.limit(1)
|
|
)
|
|
return result.scalar_one_or_none()
|
|
|
|
async def count_for_company(self, company_id: uuid.UUID) -> int:
|
|
result = await self.db.execute(
|
|
select(func.count()).select_from(Report).where(Report.company_id == company_id)
|
|
)
|
|
return int(result.scalar_one())
|