from loguru import logger from sqlalchemy.orm import Session from database import get_db from models.Users import Users from exceptions.validation_exception import ValidationException from schemas.ResponseSchemas import UserResponse from models import Clinics from enums.enums import ClinicStatus, UserType from schemas.UpdateSchemas import UserUpdate from exceptions.unauthorized_exception import UnauthorizedException from interface.common_response import CommonResponse from exceptions.business_exception import BusinessValidationException from utils.password_utils import hash_password from schemas.CreateSchemas import UserCreate from exceptions.resource_not_found_exception import ResourceNotFoundException from exceptions.db_exceptions import DBExceptionHandler class UserServices: def __init__(self): self.db: Session = next(get_db()) def create_user(self, user_data: UserCreate): # Start a transaction try: user = user_data.user # Check if user with same username or email exists existing_user = ( self.db.query(Users) .filter(Users.email == user.email.lower()) .first() ) if existing_user: raise ValidationException( "User with same email already exists" ) # Create a new user instance new_user = Users( username=user.username, email=user.email.lower(), password=hash_password(user.password), clinicRole=user.clinicRole, userType=user.userType, mobile=user.mobile ) # Add user to database but don't commit yet self.db.add(new_user) self.db.flush() # Flush to get the user ID without committing # Get clinic data clinic = user_data.clinic # cross verify domain, in db # Convert to lowercase and keep only alphanumeric characters, hyphens, and underscores domain = ''.join(char for char in clinic.name.lower() if char.isalnum() or char == '-' or char == '_') existing_clinic = self.db.query(Clinics).filter(Clinics.domain == domain).first() if existing_clinic: # This will trigger rollback in the exception handler raise ValidationException("Clinic with same domain already exists") # Create clinic instance new_clinic = Clinics( name=clinic.name, address=clinic.address, phone=clinic.phone, email=clinic.email, integration=clinic.integration, pms_id=clinic.pms_id, practice_name=clinic.practice_name, logo=clinic.logo, country=clinic.country, postal_code=clinic.postal_code, city=clinic.city, state=clinic.state, abn_doc=clinic.abn_doc, abn_number=clinic.abn_number, contract_doc=clinic.contract_doc, clinic_phone=clinic.clinic_phone, is_clinic_phone_enabled=clinic.is_clinic_phone_enabled, other_info=clinic.other_info, greeting_msg=clinic.greeting_msg, voice_model=clinic.voice_model, voice_model_provider=clinic.voice_model_provider, voice_model_gender=clinic.voice_model_gender, scenarios=clinic.scenarios, general_info=clinic.general_info, status=ClinicStatus.UNDER_REVIEW, #TODO: change this to PAYMENT_DUE domain=domain, creator_id=new_user.id, # Set the creator_id to link the clinic to the user who created it ) # Add clinic to database self.db.add(new_clinic) # Now commit both user and clinic in a single transaction self.db.commit() return new_user except Exception as e: logger.error(f"Error creating user: {str(e)}") # Rollback the transaction if any error occurs self.db.rollback() # Use the centralized exception handler DBExceptionHandler.handle_exception(e, context="creating user") def get_user(self, user_id) -> UserResponse: try: # Query the user by ID and explicitly load the created clinics relationship from sqlalchemy.orm import joinedload user = self.db.query(Users).options(joinedload(Users.created_clinics)).filter(Users.id == user_id).first() if not user: logger.error("User not found") raise ResourceNotFoundException("User not found") # First convert the user to a dictionary user_dict = {} for column in user.__table__.columns: user_dict[column.name] = getattr(user, column.name) # Convert created clinics to dictionaries if user.created_clinics: clinics_list = [] for clinic in user.created_clinics: clinic_dict = {} for column in clinic.__table__.columns: clinic_dict[column.name] = getattr(clinic, column.name) clinics_list.append(clinic_dict) user_dict['created_clinics'] = clinics_list # Create the user response user_response = UserResponse.model_validate(user_dict) # Return the response as a dictionary return user_response.model_dump() except Exception as e: # Use the centralized exception handler from twillio.exceptions.db_exceptions import DBExceptionHandler DBExceptionHandler.handle_exception(e, context="getting user") def get_users(self, limit:int, offset:int, search:str): query = self.db.query(Users) if search: query = query.filter( or_( Users.username.contains(search), Users.email.contains(search), Users.clinicRole.contains(search), Users.userType.contains(search) ) ) users = query.limit(limit).offset(offset).all() total = self.db.query(Users).count() response = CommonResponse(data=[UserResponse(**user.__dict__.copy()) for user in users], total=total) return response def get_user_by_email(self, email: str) -> UserResponse: user = self.db.query(Users).filter(Users.email == email.lower()).first() if not user: logger.error("User not found") raise ResourceNotFoundException("User not found") user_dict = user.__dict__.copy() user_response = UserResponse(**user_dict) return user_response def update_user(self, admin_id:int|None, user_id: int, user_data: UserUpdate) -> UserResponse: # Check admin authorization if admin_id is provided if admin_id: admin = self.db.query(Users).filter(Users.id == admin_id).first() if not admin: logger.error("Admin not found") raise ResourceNotFoundException("Admin not found") # Only check admin type if admin_id was provided if admin.userType != UserType.SUPER_ADMIN: logger.error("User is not authorized to perform this action") raise UnauthorizedException("User is not authorized to perform this action") # Find the user to update user = self.db.query(Users).filter(Users.id == user_id).first() if not user: logger.error("User not found") raise ResourceNotFoundException("User not found") # Update only the fields that were provided update_data = user_data.model_dump(exclude_unset=True) for key, value in update_data.items(): setattr(user, key, value) self.db.add(user) self.db.commit() self.db.refresh(user) # Return properly serialized response return UserResponse.model_validate(user) def delete_user(self, user_id: int): user = self.db.query(Users).filter(Users.id == user_id).first() if not user: logger.error("User not found") raise ResourceNotFoundException("User not found") # Use the soft_delete method from CustomBase user.soft_delete(self.db) return True