bookmyservice-ums / app /models /user_model.py
MukeshKapoor25's picture
Add initial implementation of User Management Service
b407a42
raw
history blame
1.76 kB
from fastapi import HTTPException
from app.core.nosql_client import db
from app.utils.common_utils import is_email # Assumes utility exists
class BookMyServiceUserModel:
collection = db["book_my_service_users"]
@staticmethod
async def find_by_email(email: str):
return await BookMyServiceUserModel.collection.find_one({"email": email})
@staticmethod
async def find_by_mobile(mobile: str):
return await BookMyServiceUserModel.collection.find_one({"mobile": mobile})
@staticmethod
async def find_by_identifier(identifier: str):
if is_email(identifier):
user = await BookMyServiceUserModel.find_by_email(identifier)
else:
user = await BookMyServiceUserModel.find_by_mobile(identifier)
if not user:
raise HTTPException(status_code=404, detail="User not found with this email or mobile")
return user
@staticmethod
async def exists_by_email_or_phone(email: str, phone: str) -> bool:
return await BookMyServiceUserModel.collection.find_one({
"$or": [{"email": email}, {"mobile": phone}]
}) is not None
@staticmethod
async def create(user_data: dict):
result = await BookMyServiceUserModel.collection.insert_one(user_data)
return result.inserted_id
@staticmethod
async def update_by_identifier(identifier: str, update_fields: dict):
query = {"email": identifier} if is_email(identifier) else {"mobile": identifier}
result = await BookMyServiceUserModel.collection.update_one(query, {"$set": update_fields})
if result.matched_count == 0:
raise HTTPException(status_code=404, detail="User not found")
return result.modified_count > 0