67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
"""Admin router for administrative functions."""
|
|
|
|
import secrets
|
|
from datetime import datetime, timedelta, UTC
|
|
from typing import Annotated
|
|
from fastapi import APIRouter, Depends
|
|
from pydantic import BaseModel
|
|
|
|
from baby_monitor.routers.auth import verify_admin
|
|
from baby_monitor.repositories.dependencies.get_invitation_repository import (
|
|
get_invitation_repository,
|
|
)
|
|
from baby_monitor.repositories.interfaces import (
|
|
InvitationRepositoryInterface,
|
|
)
|
|
|
|
router = APIRouter(prefix="/api/admin", tags=["admin"])
|
|
|
|
|
|
class InvitationResponse(BaseModel):
|
|
"""Response model for invitation link generation."""
|
|
|
|
token: str
|
|
expires_at: str
|
|
message: str
|
|
|
|
|
|
@router.post("/generate-invitation", response_model=InvitationResponse)
|
|
async def generate_invitation_link(
|
|
user_id: Annotated[int, Depends(verify_admin)],
|
|
invitation_repository: Annotated[
|
|
InvitationRepositoryInterface,
|
|
Depends(get_invitation_repository),
|
|
],
|
|
) -> InvitationResponse:
|
|
"""Generate a new invitation link for user registration.
|
|
|
|
The invitation token is valid for 24 hours and can be used once.
|
|
Requires admin role.
|
|
|
|
Args:
|
|
user_id: Admin user ID (from verify_admin)
|
|
invitation_repository: Invitation storage backend
|
|
|
|
Returns:
|
|
InvitationResponse with token and expiration details
|
|
"""
|
|
|
|
# Generate secure random token
|
|
invitation_token = secrets.token_urlsafe(32)
|
|
|
|
# Calculate expiration (24 hours from now)
|
|
expires_at = datetime.now(UTC) + timedelta(hours=24)
|
|
|
|
# Store invitation token in database
|
|
invitation_repository.create_invitation(
|
|
token=invitation_token,
|
|
created_by_user_id=user_id,
|
|
expires_at=expires_at,
|
|
)
|
|
|
|
return InvitationResponse(
|
|
token=invitation_token,
|
|
expires_at=expires_at.isoformat(),
|
|
message="Invitation link generated successfully. Valid for 24 hours.",
|
|
)
|