Spaces:
Sleeping
Sleeping
| 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"] | |
| async def find_by_email(email: str): | |
| return await BookMyServiceUserModel.collection.find_one({"email": email}) | |
| async def find_by_mobile(mobile: str): | |
| return await BookMyServiceUserModel.collection.find_one({"mobile": mobile}) | |
| 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 | |
| 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 | |
| async def create(user_data: dict): | |
| result = await BookMyServiceUserModel.collection.insert_one(user_data) | |
| return result.inserted_id | |
| 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 |