| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- """
- Superadmin endpoints for organization management.
- """
- from typing import Annotated
- from fastapi import APIRouter, Depends, HTTPException, Query, status
- from sqlalchemy.ext.asyncio import AsyncSession
- from app.api.deps import get_current_superadmin
- from app.core.database import get_db
- from app.models.user import User
- from app.schemas.organization import (
- OrganizationCreate,
- OrganizationListResponse,
- OrganizationResponse,
- OrganizationUpdate,
- )
- from app.services import organization_service
- router = APIRouter()
- @router.get("", response_model=OrganizationListResponse)
- async def list_organizations(
- db: Annotated[AsyncSession, Depends(get_db)],
- current_user: Annotated[User, Depends(get_current_superadmin)],
- skip: int = Query(0, ge=0, description="Number of records to skip"),
- limit: int = Query(100, ge=1, le=1000, description="Max records to return"),
- status: str | None = Query(None, description="Filter by status"),
- ):
- """
- List all organizations (superadmin only).
- Returns paginated list of organizations with optional status filter.
- """
- organizations, total = await organization_service.list_organizations(
- db, skip=skip, limit=limit, status=status
- )
- return OrganizationListResponse(
- organizations=organizations,
- total=total,
- )
- @router.get("/{organization_id}", response_model=OrganizationResponse)
- async def get_organization(
- organization_id: int,
- db: Annotated[AsyncSession, Depends(get_db)],
- current_user: Annotated[User, Depends(get_current_superadmin)],
- ):
- """
- Get organization by ID (superadmin only).
- """
- org = await organization_service.get_organization(db, organization_id)
- if not org:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND,
- detail="Organization not found",
- )
- return org
- @router.post("", response_model=OrganizationResponse, status_code=status.HTTP_201_CREATED)
- async def create_organization(
- data: OrganizationCreate,
- db: Annotated[AsyncSession, Depends(get_db)],
- current_user: Annotated[User, Depends(get_current_superadmin)],
- ):
- """
- Create a new organization (superadmin only).
- Creates an organization with active status and specified product modules.
- """
- org = await organization_service.create_organization(db, data)
- return org
- @router.patch("/{organization_id}", response_model=OrganizationResponse)
- async def update_organization(
- organization_id: int,
- data: OrganizationUpdate,
- db: Annotated[AsyncSession, Depends(get_db)],
- current_user: Annotated[User, Depends(get_current_superadmin)],
- ):
- """
- Update organization (superadmin only).
- Can update organization details, status, and enable/disable product modules.
- """
- org = await organization_service.update_organization(db, organization_id, data)
- if not org:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND,
- detail="Organization not found",
- )
- return org
- @router.delete("/{organization_id}", status_code=status.HTTP_204_NO_CONTENT)
- async def delete_organization(
- organization_id: int,
- db: Annotated[AsyncSession, Depends(get_db)],
- current_user: Annotated[User, Depends(get_current_superadmin)],
- ):
- """
- Delete organization (superadmin only).
- Warning: This will cascade delete all users and devices in the organization.
- """
- deleted = await organization_service.delete_organization(db, organization_id)
- if not deleted:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND,
- detail="Organization not found",
- )
|