|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import functools |
| 5 | +import logging |
| 6 | + |
| 7 | +from fastapi import Depends |
| 8 | + |
| 9 | +from app import settings |
| 10 | +from app.core.exceptions import InvitationDoesNotExist, OptScaleAPIResponseError |
| 11 | +from app.optscale_api.invitation_api import OptScaleInvitationAPI |
| 12 | +from app.optscale_api.orgs_api import OptScaleOrgAPI |
| 13 | +from app.optscale_api.users_api import OptScaleUserAPI |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +def validate_invitation(): # noqa: E501 |
| 19 | + def decorator(func): |
| 20 | + @functools.wraps(func) |
| 21 | + async def wrapper(*args, **kwargs): |
| 22 | + email = kwargs.get("email") |
| 23 | + invitation_api = OptScaleInvitationAPI() |
| 24 | + response = await invitation_api.get_list_of_invitations(email=email) |
| 25 | + no_invitations = {"invites": []} |
| 26 | + if response.get("data", {}) == no_invitations: |
| 27 | + # there is no invitation |
| 28 | + raise InvitationDoesNotExist("Invitation not found") |
| 29 | + return await func(*args, **kwargs) |
| 30 | + |
| 31 | + return wrapper |
| 32 | + |
| 33 | + return decorator |
| 34 | + |
| 35 | + |
| 36 | +@validate_invitation() |
| 37 | +async def register_invited_user_on_optscale( |
| 38 | + email: str, |
| 39 | + display_name: str, |
| 40 | + password: str, |
| 41 | + user_api: OptScaleUserAPI, |
| 42 | +) -> dict[str, str] | Exception: |
| 43 | + """ |
| 44 | + Registers invited users to OptScale, without |
| 45 | + verification. |
| 46 | +
|
| 47 | + :param user_api: an instance of OptScaleUserAPI |
| 48 | + :param email: The email of the given user to register |
| 49 | + :param display_name: The display name of the user |
| 50 | + :param password: The password of the user |
| 51 | + :return: dict[str, str] : User information. |
| 52 | + :raises: OptScaleAPIResponseError if any error occurs |
| 53 | + contacting the OptScale APIs |
| 54 | + """ |
| 55 | + try: |
| 56 | + response = await user_api.create_user( |
| 57 | + email=email, |
| 58 | + display_name=display_name, |
| 59 | + password=password, |
| 60 | + admin_api_key=settings.admin_token, |
| 61 | + verified=False, |
| 62 | + ) |
| 63 | + logger.info(f"Invited User successfully registered: {response}") |
| 64 | + return response |
| 65 | + except OptScaleAPIResponseError as error: |
| 66 | + logger.error(f"An error {error} occurred registering the invited user {email}") |
| 67 | + raise |
| 68 | + except InvitationDoesNotExist: |
| 69 | + logger.error(f"There is no invitation for this email {email}") |
| 70 | + raise |
| 71 | + |
| 72 | + |
| 73 | +async def validate_user_delete( |
| 74 | + user_token: str, |
| 75 | + invitation_api: OptScaleInvitationAPI = Depends(), |
| 76 | + org_api: OptScaleOrgAPI = Depends(), |
| 77 | +) -> bool: |
| 78 | + """ |
| 79 | + Validates if a user can be deleted by checking invitations and organizations. |
| 80 | +
|
| 81 | + :param invitation_api: An instance of OptScaleInvitationAPI |
| 82 | + :param org_api: An instance of OptScaleOrgAPI |
| 83 | + :param user_token: The Access Token of the user to be deleted |
| 84 | + :return: True if the user has no invitations and organizations. False, otherwise |
| 85 | + :raises An Exception if an error occurs. |
| 86 | + """ |
| 87 | + try: |
| 88 | + response_invitation, response_organization = await asyncio.gather( |
| 89 | + invitation_api.get_list_of_invitations(user_access_token=user_token), |
| 90 | + org_api.get_user_org_list(user_access_token=user_token), |
| 91 | + ) |
| 92 | + no_org_response = {"organizations": []} |
| 93 | + no_invitations_response = {"invites": []} |
| 94 | + return ( |
| 95 | + response_invitation.get("data", {}) == no_invitations_response |
| 96 | + and response_organization.get("data", {}) == no_org_response |
| 97 | + ) |
| 98 | + except Exception as error: |
| 99 | + logger.error(f"Exception during deletion user validation:{error}") |
| 100 | + return False |
| 101 | + |
| 102 | + |
| 103 | +async def remove_user( |
| 104 | + user_id: str, |
| 105 | + user_access_token: str, |
| 106 | + admin_api_key: str, |
| 107 | + invitation_api: OptScaleInvitationAPI = Depends(), |
| 108 | + org_api: OptScaleOrgAPI = Depends(), |
| 109 | + user_api: OptScaleUserAPI = Depends(), |
| 110 | +) -> bool: |
| 111 | + """ |
| 112 | + Removes a user if they have no invitations or organizations. |
| 113 | + :param admin_api_key: The Secret admin key to add to the Headers |
| 114 | + :param user_id: the user ID to be deleted |
| 115 | + :param user_access_token: The Access Token of the given User |
| 116 | + :param invitation_api: An instance of the OptScaleInvitationAPI |
| 117 | + :param org_api: an instance of the OptScaleOrgAPI |
| 118 | + :param user_api: An instance of OptScaleUserAPI |
| 119 | + :return: True if the user was successfully deleted, False otherwise. |
| 120 | + :raises OptScaleAPIResponseError if any error occurs |
| 121 | + contacting the OptScale APIs |
| 122 | + """ |
| 123 | + validate_delete = await validate_user_delete( |
| 124 | + user_token=user_access_token, invitation_api=invitation_api, org_api=org_api |
| 125 | + ) |
| 126 | + if validate_delete: |
| 127 | + try: |
| 128 | + await user_api.delete_user( |
| 129 | + user_id=user_id, |
| 130 | + admin_api_key=admin_api_key, |
| 131 | + ) |
| 132 | + logger.info(f"The user {user_id} was successfully deleted") |
| 133 | + return True |
| 134 | + except OptScaleAPIResponseError: |
| 135 | + logger.error(f"Error deleting user:{user_id}") |
| 136 | + return False |
| 137 | + logger.info(f"The user {user_id} cannot be deleted.") |
| 138 | + return False |
0 commit comments