|
4 | 4 |
|
5 | 5 | from fastapi import Body, Depends, Path, Query, Security |
6 | 6 |
|
7 | | -from api.dependencies import create_user_use_case_factory, get_one_user_use_case_factory, get_request_context, get_users_use_case_factory |
| 7 | +from api.dependencies import ( |
| 8 | + create_user_use_case_factory, |
| 9 | + delete_user_use_case_factory, |
| 10 | + get_one_user_use_case_factory, |
| 11 | + get_request_context, |
| 12 | + get_users_use_case_factory, |
| 13 | +) |
8 | 14 | from api.domain import SortOrder |
9 | 15 | from api.domain.organization.errors import OrganizationNotFoundError |
10 | 16 | from api.domain.role.errors import RoleNotFoundError |
11 | 17 | from api.domain.user.entities import UserSortField |
12 | | -from api.domain.user.errors import UserAlreadyExistsError, UserExpiredError, UserIsNotAdminError, UserNotFoundError |
| 18 | +from api.domain.user.errors import ( |
| 19 | + DeleteUserWithProvidersError, |
| 20 | + DeleteUserWithRoutersError, |
| 21 | + UserAlreadyExistsError, |
| 22 | + UserExpiredError, |
| 23 | + UserIsNotAdminError, |
| 24 | + UserNotFoundError, |
| 25 | +) |
13 | 26 | from api.infrastructure.fastapi.access import get_current_key |
14 | 27 | from api.infrastructure.fastapi.context import RequestContext |
15 | 28 | from api.infrastructure.fastapi.documentation import get_documentation_responses |
16 | 29 | from api.infrastructure.fastapi.endpoints.admin import router |
17 | 30 | from api.infrastructure.fastapi.endpoints.exceptions import ( |
18 | 31 | AccountExpiredHTTPException, |
| 32 | + DeleteUserWithProvidersHTTPException, |
| 33 | + DeleteUserWithRoutersHTTPException, |
19 | 34 | InternalServerHTTPException, |
20 | 35 | NotAdminUserHTTPException, |
21 | 36 | OrganizationNotFoundHTTPException, |
|
28 | 43 | CreateUserCommand, |
29 | 44 | CreateUserUseCase, |
30 | 45 | CreateUserUseCaseSuccess, |
| 46 | + DeleteUserCommand, |
| 47 | + DeleteUserUseCase, |
| 48 | + DeleteUserUseCaseSuccess, |
31 | 49 | GetOneUserCommand, |
32 | 50 | GetOneUserUseCase, |
33 | 51 | GetOneUserUseCaseSuccess, |
@@ -184,3 +202,53 @@ async def get_users( |
184 | 202 | raise AccountExpiredHTTPException() |
185 | 203 | case _ as unreachable: |
186 | 204 | assert_never(unreachable) |
| 205 | + |
| 206 | + |
| 207 | +@router.delete( |
| 208 | + path=EndpointRoute.ADMIN_USERS + "/{user_id}", |
| 209 | + dependencies=[Security(dependency=get_current_key)], |
| 210 | + status_code=200, |
| 211 | + responses=get_documentation_responses( |
| 212 | + [ |
| 213 | + UserNotFoundHTTPException, |
| 214 | + DeleteUserWithRoutersHTTPException, |
| 215 | + DeleteUserWithProvidersHTTPException, |
| 216 | + ] |
| 217 | + ), |
| 218 | +) |
| 219 | +async def delete_user( |
| 220 | + user_id: int = Path(description="The ID of the user to delete."), |
| 221 | + delete_user_use_case: DeleteUserUseCase = Depends(delete_user_use_case_factory), |
| 222 | + request_context: ContextVar[RequestContext] = Depends(get_request_context), |
| 223 | +) -> UserResponse: |
| 224 | + command = DeleteUserCommand( |
| 225 | + authenticated_user_id=request_context.get().user_id, |
| 226 | + user_id=user_id, |
| 227 | + ) |
| 228 | + try: |
| 229 | + result = await delete_user_use_case.execute(command) |
| 230 | + except Exception as e: |
| 231 | + logger.exception( |
| 232 | + "Unexpected error while executing delete_user use case", |
| 233 | + extra={ |
| 234 | + "authenticated_user_id": command.authenticated_user_id, |
| 235 | + "user_id": command.user_id, |
| 236 | + "error_type": type(e).__name__, |
| 237 | + }, |
| 238 | + ) |
| 239 | + raise InternalServerHTTPException() |
| 240 | + match result: |
| 241 | + case DeleteUserUseCaseSuccess(user=user): |
| 242 | + return UserResponse.model_validate(user, from_attributes=True) |
| 243 | + case UserNotFoundError(id=not_found_id): |
| 244 | + raise UserNotFoundHTTPException(user_id=not_found_id) |
| 245 | + case DeleteUserWithRoutersError(router_ids=router_ids): |
| 246 | + raise DeleteUserWithRoutersHTTPException(router_ids=router_ids) |
| 247 | + case DeleteUserWithProvidersError(provider_ids=provider_ids): |
| 248 | + raise DeleteUserWithProvidersHTTPException(provider_ids=provider_ids) |
| 249 | + case UserIsNotAdminError(): |
| 250 | + raise NotAdminUserHTTPException() |
| 251 | + case UserExpiredError(): |
| 252 | + raise AccountExpiredHTTPException() |
| 253 | + case _ as unreachable: |
| 254 | + assert_never(unreachable) |
0 commit comments