73 lines
2.2 KiB
Python
73 lines
2.2 KiB
Python
import uuid
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from models import User, Invite, InviteStatus
|
|
from invite_utils import generate_invite_identifier
|
|
|
|
|
|
def unique_email(prefix: str = "test") -> str:
|
|
"""Generate a unique email for tests sharing the same database."""
|
|
return f"{prefix}-{uuid.uuid4().hex[:8]}@example.com"
|
|
|
|
|
|
async def create_invite_for_godfather(db: AsyncSession, godfather_id: int) -> str:
|
|
"""
|
|
Create an invite for an existing godfather user.
|
|
|
|
Args:
|
|
db: Database session
|
|
godfather_id: ID of the existing user who will be the godfather
|
|
|
|
Returns:
|
|
The invite identifier.
|
|
|
|
Raises:
|
|
ValueError: If the godfather user doesn't exist.
|
|
"""
|
|
# Verify godfather exists
|
|
result = await db.execute(select(User).where(User.id == godfather_id))
|
|
godfather = result.scalar_one_or_none()
|
|
|
|
if not godfather:
|
|
raise ValueError(f"Godfather user with ID {godfather_id} not found")
|
|
|
|
# Create invite
|
|
identifier = generate_invite_identifier()
|
|
invite = Invite(
|
|
identifier=identifier,
|
|
godfather_id=godfather_id,
|
|
status=InviteStatus.READY,
|
|
)
|
|
db.add(invite)
|
|
await db.commit()
|
|
|
|
return identifier
|
|
|
|
|
|
# Backwards-compatible alias that gets a user by email
|
|
async def create_invite_for_registration(db: AsyncSession, godfather_email: str) -> str:
|
|
"""
|
|
Create an invite for an existing godfather user (looked up by email).
|
|
|
|
The godfather must already exist in the database.
|
|
|
|
Args:
|
|
db: Database session
|
|
godfather_email: Email of the existing user who will be the godfather
|
|
|
|
Returns:
|
|
The invite identifier.
|
|
|
|
Raises:
|
|
ValueError: If the godfather user doesn't exist.
|
|
"""
|
|
result = await db.execute(select(User).where(User.email == godfather_email))
|
|
godfather = result.scalar_one_or_none()
|
|
|
|
if not godfather:
|
|
raise ValueError(f"Godfather user with email '{godfather_email}' not found. "
|
|
"Create the user first using create_user_with_roles().")
|
|
|
|
return await create_invite_for_godfather(db, godfather.id)
|