2024-04-10 00:33:31 +03:00
|
|
|
from typing import Annotated
|
|
|
|
|
2024-04-08 17:32:50 +03:00
|
|
|
from fastapi import APIRouter, Depends, Request
|
|
|
|
|
2024-04-21 23:46:17 +03:00
|
|
|
from api.application.contracts.company.company_request import (
|
|
|
|
CompanyByOwnerEmail,
|
|
|
|
CreateNewCompany,
|
|
|
|
)
|
2024-04-08 23:55:30 +03:00
|
|
|
from api.application.contracts.company.company_response import CompanyBaseResponse
|
2024-04-10 00:33:31 +03:00
|
|
|
from api.application.protocols.jwt import JwtTokenProcessor
|
2024-04-21 23:46:17 +03:00
|
|
|
from api.application.usecase.company.create_company import CreateCompany
|
2024-04-10 00:33:31 +03:00
|
|
|
from api.application.usecase.company.get_users_company import GetCompaniesByOwnerEmail
|
|
|
|
from api.domain.user.error import UserValidationError
|
|
|
|
from api.infrastructure.dependencies.stub import Stub
|
2024-04-08 17:32:50 +03:00
|
|
|
from api.presentation.auth.fasapi_auth import auth_required
|
|
|
|
|
2024-04-21 23:46:17 +03:00
|
|
|
company_router = APIRouter(
|
|
|
|
prefix="/me",
|
|
|
|
tags=["Company"],
|
|
|
|
dependencies=[Depends(auth_required)],
|
|
|
|
)
|
2024-04-08 17:32:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
@company_router.get(
|
2024-04-21 23:46:17 +03:00
|
|
|
"/companies",
|
|
|
|
response_model=list[CompanyBaseResponse],
|
2024-04-08 17:32:50 +03:00
|
|
|
)
|
2024-04-21 23:46:17 +03:00
|
|
|
async def get_my_companies(
|
2024-04-10 00:33:31 +03:00
|
|
|
request: Request,
|
|
|
|
token_processor: Annotated[JwtTokenProcessor, Depends(Stub(JwtTokenProcessor))],
|
2024-04-21 23:46:17 +03:00
|
|
|
usecase: Annotated[
|
|
|
|
GetCompaniesByOwnerEmail, Depends(Stub(GetCompaniesByOwnerEmail))
|
|
|
|
],
|
2024-04-10 00:33:31 +03:00
|
|
|
) -> list[CompanyBaseResponse]:
|
|
|
|
token_data = token_processor.validate_token(request.scope["auth"])
|
|
|
|
if not token_data:
|
|
|
|
raise UserValidationError("Login required")
|
2024-04-21 23:46:17 +03:00
|
|
|
companies = await usecase.execute(
|
|
|
|
request=CompanyByOwnerEmail(email=token_data[1].value)
|
|
|
|
)
|
2024-04-10 00:33:31 +03:00
|
|
|
return [
|
|
|
|
CompanyBaseResponse(
|
|
|
|
name=c.name,
|
|
|
|
email=c.email,
|
|
|
|
)
|
|
|
|
for c in companies
|
|
|
|
]
|
2024-04-21 23:46:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
@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
|