Files
sakhteman/auth/services/auth_db.py
T
2024-02-15 00:17:51 +03:30

46 lines
1.4 KiB
Python

from uuid import UUID
from app.models.customer import CustomerModel
from auth.models import UserInDB
from auth.utils.mongo import use_auth_collection
async def get_user_by_username(username: str) -> UserInDB:
"""
Retrieves user from the database by username.
"""
result = await use_auth_collection('users').find_one({'username': username})
return UserInDB(**result) if result else None
async def get_customer_by_username(username: str) -> UserInDB:
"""
Retrieves user from the database by username.
"""
result = await use_auth_collection('customer').find_one({'$or': [{'username': username}, {'mobile': username}]})
return UserInDB(**result) if result else None
async def get_user_by_id(user_id: str) -> UserInDB | None:
"""
Retrieves user from the database by username.
"""
try:
uuid = UUID(str(user_id))
except ValueError:
return None
result = await use_auth_collection('users').find_one({'_id': uuid})
return UserInDB(**result) if result else None
async def get_customer_by_id(customer_id: str) -> UserInDB | None:
"""
Retrieves user from the database by username.
"""
try:
uuid = UUID(str(customer_id))
except ValueError:
return None
result = await use_auth_collection('customer').find_one({'_id': uuid})
return CustomerModel(**result) if result else None