| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- """
- Client endpoints for viewing organization devices.
- """
- 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_user
- from app.core.database import get_db
- from app.models.user import User
- from app.schemas.device import DeviceListResponse, DeviceResponse
- from app.services import device_service
- router = APIRouter()
- @router.get("", response_model=DeviceListResponse)
- async def list_organization_devices(
- db: Annotated[AsyncSession, Depends(get_db)],
- current_user: Annotated[User, Depends(get_current_user)],
- 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 devices assigned to current user's organization.
- All authenticated users can view devices in their organization.
- """
- if not current_user.organization_id:
- raise HTTPException(
- status_code=status.HTTP_400_BAD_REQUEST,
- detail="User is not assigned to any organization",
- )
- devices, total = await device_service.list_devices(
- db,
- skip=skip,
- limit=limit,
- organization_id=current_user.organization_id,
- status=status,
- )
- return DeviceListResponse(
- devices=devices,
- total=total,
- )
- @router.get("/{device_id}", response_model=DeviceResponse)
- async def get_organization_device(
- device_id: int,
- db: Annotated[AsyncSession, Depends(get_db)],
- current_user: Annotated[User, Depends(get_current_user)],
- ):
- """
- Get device details from current organization.
- Users can view devices in their organization.
- """
- device = await device_service.get_device(db, device_id)
- if not device:
- raise HTTPException(
- status_code=status.HTTP_404_NOT_FOUND,
- detail="Device not found",
- )
- # Check if device belongs to same organization
- if device.organization_id != current_user.organization_id:
- raise HTTPException(
- status_code=status.HTTP_403_FORBIDDEN,
- detail="Cannot view devices from other organizations",
- )
- return device
|