59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
from typing import Annotated
|
|
|
|
from fastapi import APIRouter, Depends, Request
|
|
|
|
from api.application.contracts.company.company_request import (
|
|
CompanyByOwnerEmail,
|
|
CreateNewCompany,
|
|
)
|
|
from api.application.contracts.company.company_response import CompanyBaseResponse
|
|
from api.application.protocols.jwt import JwtTokenProcessor
|
|
from api.application.usecase.company.create_company import CreateCompany
|
|
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="/me",
|
|
tags=["Company"],
|
|
dependencies=[Depends(auth_required)],
|
|
)
|
|
|
|
|
|
@company_router.get(
|
|
"/companies",
|
|
response_model=list[CompanyBaseResponse],
|
|
)
|
|
async def get_my_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
|
|
]
|
|
|
|
|
|
@company_router.post("/companies/create", response_model=CompanyBaseResponse)
|
|
async def create_new_company(
|
|
request: Request,
|
|
request_data: CreateNewCompany,
|
|
token_processor: Annotated[JwtTokenProcessor, Depends(Stub(JwtTokenProcessor))],
|
|
usecase: Annotated[CreateCompany, Depends(Stub(CreateCompany))],
|
|
) -> CompanyBaseResponse:
|
|
token_data = token_processor.validate_token(request.scope["auth"])
|
|
if not token_data:
|
|
raise UserValidationError("Login required")
|
|
|
|
company = await usecase.execute(request=request_data)
|
|
return company
|