service_man/api/presentation/routers/company.py

37 lines
1.3 KiB
Python

from typing import Annotated
from fastapi import APIRouter, Depends, Request
from api.application.contracts.company.company_request import CompanyByOwnerEmail
from api.application.contracts.company.company_response import CompanyBaseResponse
from api.application.protocols.jwt import JwtTokenProcessor
from api.application.usecase.company.get_users_company import GetCompaniesByOwnerEmail
from api.domain.user.error import UserValidationError
from api.infrastructure.dependencies.stub import Stub
from api.presentation.auth.fasapi_auth import auth_required
company_router = APIRouter(prefix="/company", tags=["Company"])
@company_router.get(
"/",
response_model=None,
dependencies=[Depends(auth_required)],
)
async def get_companies(
request: Request,
token_processor: Annotated[JwtTokenProcessor, Depends(Stub(JwtTokenProcessor))],
usecase: Annotated[GetCompaniesByOwnerEmail, Depends(Stub(GetCompaniesByOwnerEmail))],
) -> list[CompanyBaseResponse]:
token_data = token_processor.validate_token(request.scope["auth"])
if not token_data:
raise UserValidationError("Login required")
companies = await usecase.execute(request=CompanyByOwnerEmail(email=token_data[1].value))
return [
CompanyBaseResponse(
name=c.name,
email=c.email,
)
for c in companies
]