201 előtti mentés
This commit is contained in:
@@ -18,6 +18,52 @@ from app.schemas.asset import AssetResponse, AssetCreate
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/vehicles", response_model=List[AssetResponse])
|
||||
async def get_user_vehicles(
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
"""
|
||||
Get all vehicles/assets belonging to the current user or their organization.
|
||||
|
||||
This endpoint returns a paginated list of vehicles that the authenticated user
|
||||
has access to (either as owner or through organization membership).
|
||||
"""
|
||||
# Query assets where user is owner or organization member
|
||||
from sqlalchemy import or_
|
||||
|
||||
# First, get user's organization memberships
|
||||
from app.models.marketplace.organization import OrganizationMember
|
||||
org_stmt = select(OrganizationMember.organization_id).where(
|
||||
OrganizationMember.user_id == current_user.id
|
||||
)
|
||||
org_result = await db.execute(org_stmt)
|
||||
user_org_ids = [row[0] for row in org_result.all()]
|
||||
|
||||
# Build query: assets owned by user OR assets in user's organizations
|
||||
stmt = (
|
||||
select(Asset)
|
||||
.where(
|
||||
or_(
|
||||
Asset.owner_person_id == current_user.id,
|
||||
Asset.owner_org_id.in_(user_org_ids) if user_org_ids else False,
|
||||
Asset.operator_person_id == current_user.id,
|
||||
Asset.operator_org_id.in_(user_org_ids) if user_org_ids else False
|
||||
)
|
||||
)
|
||||
.order_by(Asset.created_at.desc())
|
||||
.offset(skip)
|
||||
.limit(limit)
|
||||
.options(selectinload(Asset.catalog))
|
||||
)
|
||||
|
||||
result = await db.execute(stmt)
|
||||
assets = result.scalars().all()
|
||||
|
||||
return assets
|
||||
|
||||
@router.get("/{asset_id}/financial-summary", response_model=Dict[str, Any])
|
||||
async def get_asset_financial_report(
|
||||
asset_id: uuid.UUID,
|
||||
|
||||
Reference in New Issue
Block a user