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).
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.models.user import User
|
|
|
|
|
|
class UserRepository:
|
|
def __init__(self, db: AsyncSession) -> None:
|
|
self.db = db
|
|
|
|
async def get_by_id(self, user_id: uuid.UUID) -> User | None:
|
|
return await self.db.get(User, user_id)
|
|
|
|
async def get_by_email(self, email: str) -> User | None:
|
|
result = await self.db.execute(select(User).where(User.email == email.lower()))
|
|
return result.scalar_one_or_none()
|
|
|
|
async def list_admin_emails(self) -> list[str]:
|
|
result = await self.db.execute(select(User.email).where(User.is_admin.is_(True)))
|
|
return list(result.scalars().all())
|
|
|
|
async def create(
|
|
self,
|
|
*,
|
|
email: str,
|
|
password_hash: str | None,
|
|
display_name: str,
|
|
timezone: str,
|
|
user_id: uuid.UUID | None = None,
|
|
is_admin: bool = False,
|
|
email_verified: bool = False,
|
|
) -> User:
|
|
user = User(
|
|
id=user_id or uuid.uuid4(),
|
|
email=email.lower(),
|
|
password_hash=password_hash,
|
|
display_name=display_name,
|
|
timezone=timezone,
|
|
is_admin=is_admin,
|
|
email_verified=email_verified,
|
|
)
|
|
self.db.add(user)
|
|
await self.db.flush()
|
|
return user
|