"""Logic for identity.""" import uuid from ddtrace import tracer from owsresponse import response from account.constants import error, header from account.models import ows_users, subaccount as subaccount_model, vendor as vendor_model def not_found_error(): """Helper to format a not found response.""" return response.create_error_response( code=error.ERROR_CODE_NOT_FOUND, message=error.ERROR_MESSAGE_NOT_FOUND, status=404, ) def get_identity(account_type, account_id): """Get info about a vendor/subaccount when given account type and id. Args: account_type (str): either `vendor` or `subaccount`. account_id (int): the id of the vendor or sub-account. Returns: response.Response: contains dict of account information. """ if not account_type or not account_id: return not_found_error() if account_type == header.GRASS_ACCOUNT_TYPE_SUBACCOUNT: subaccount_result = subaccount_model.get_subaccount(account_id) if not subaccount_result: return not_found_error() return response.Response( { 'account_type': account_type, 'account_id': subaccount_result.message.get('subaccount_id'), 'account_name': subaccount_result.message.get('subaccount_name'), 'country_id': subaccount_result.message.get('country_id'), } ) elif account_type == header.GRASS_ACCOUNT_TYPE_VENDOR: vendor_result = vendor_model.get_vendor(account_id) if not vendor_result: return not_found_error() return response.Response( { 'account_type': account_type, 'account_id': vendor_result.message.get('vendor_id'), 'account_name': vendor_result.message.get('name'), 'country_id': vendor_result.message.get('country_id'), } ) else: return not_found_error() # We should never get here. def get_user_details(orchard_user_id): """Get info about a user when given Orchard user id. Args: orchard_user_id (str): Orchard User id of the format oa:123/alw:123. Returns: response.Response: contains dict of user information. """ return ows_users.get_user_details(orchard_user_id) @tracer.wrap() def get_oa_user_id(identity_id: uuid.UUID) -> int | None: """Get Orchard User id when given identity id. Args: identity_id (str): identity id of the format of a UUID. Returns: response.Response: contains dict of user information. """ profiles = ows_users.get_user_profiles_by_identity_id(identity_id) user_id = None for profile in profiles: if profile.profile_type == 'OrchAdminProfile': user_id = profile.profile_id return user_id