Files
adminka/src/presentation/routing/organizations.py
2026-06-03 13:52:45 +03:00

103 lines
3.7 KiB
Python

from fastapi import APIRouter, Depends, Query, status
from src.application.commands import (
CreateOrganizationCommand,
CreateOrganizationWalletsCommand,
GetOrganizationCommand,
ListOrganizationsCommand,
UpdateOrganizationCommand,
)
from src.application.domain.dto import AdminAuthContext
from src.presentation.decorators.admin_auth import require_admin_access, require_admin_role
from src.presentation.dependencies.commands import (
get_create_organization_command,
get_create_organization_wallets_command,
get_get_organization_command,
get_list_organizations_command,
get_update_organization_command,
)
from src.presentation.schemas.mappers import organization_to_response, wallet_to_response
from src.presentation.schemas.organization import (
CreateOrganizationRequest,
OrganizationListResponse,
OrganizationResponse,
UpdateOrganizationRequest,
WalletResponse,
)
organizations_router = APIRouter(prefix='/organizations', tags=['organizations'])
@organizations_router.get('', response_model=OrganizationListResponse)
async def list_organizations(
limit: int = Query(default=50, ge=1, le=200),
offset: int = Query(default=0, ge=0),
auth: AdminAuthContext = Depends(require_admin_access),
command: ListOrganizationsCommand = Depends(get_list_organizations_command),
):
items, total = await command(limit=limit, offset=offset)
return OrganizationListResponse(
items=[organization_to_response(x) for x in items],
total=total,
)
@organizations_router.post('', response_model=OrganizationResponse, status_code=status.HTTP_201_CREATED)
async def create_organization(
body: CreateOrganizationRequest,
auth: AdminAuthContext = Depends(require_admin_role('compliance', 'superadmin')),
command: CreateOrganizationCommand = Depends(get_create_organization_command),
):
org = await command(
admin_user_id=auth.admin_user_id,
email=str(body.email),
password=body.password,
name=body.name,
short_name=body.short_name,
inn=body.inn,
ogrn=body.ogrn,
kpp=body.kpp,
legal_address=body.legal_address,
actual_address=body.actual_address,
bank_details=body.bank_details,
contact_person=body.contact_person,
contact_phone=body.contact_phone,
status=body.status,
)
return organization_to_response(org)
@organizations_router.get('/{organization_id}', response_model=OrganizationResponse)
async def get_organization(
organization_id: str,
auth: AdminAuthContext = Depends(require_admin_access),
command: GetOrganizationCommand = Depends(get_get_organization_command),
):
org = await command(organization_id)
return organization_to_response(org)
@organizations_router.patch('/{organization_id}', response_model=OrganizationResponse)
async def update_organization(
organization_id: str,
body: UpdateOrganizationRequest,
auth: AdminAuthContext = Depends(require_admin_role('compliance', 'superadmin')),
command: UpdateOrganizationCommand = Depends(get_update_organization_command),
):
org = await command(organization_id, values=body.model_dump(exclude_unset=True))
return organization_to_response(org)
@organizations_router.post(
'/{organization_id}/wallets/create',
response_model=list[WalletResponse],
status_code=status.HTTP_201_CREATED,
)
async def create_organization_wallets(
organization_id: str,
auth: AdminAuthContext = Depends(require_admin_role('compliance', 'superadmin')),
command: CreateOrganizationWalletsCommand = Depends(get_create_organization_wallets_command),
):
wallets = await command(organization_id=organization_id)
return [wallet_to_response(w) for w in wallets]