refactor: code refactor
feat: added missing update and detele apis
This commit is contained in:
+32
-2
@@ -21,8 +21,8 @@ from exceptions.resource_not_found_exception import ResourceNotFoundException
|
||||
from models import ResetPasswordTokens
|
||||
from utils.constants import generateOTP
|
||||
from utils.password_utils import generate_reset_password_token, generate_secure_password, hash_password, verify_password
|
||||
from schemas.CreateSchemas import UserCreate
|
||||
from schemas.BaseSchemas import AuthBase, AuthOTP, CreateSuperAdmin
|
||||
from schemas.CreateSchemas import CreateSuperAdmin, UpdateSuperAdmin, UserCreate
|
||||
from schemas.BaseSchemas import AuthBase, AuthOTP
|
||||
from exceptions.unauthorized_exception import UnauthorizedException
|
||||
|
||||
from database import get_db
|
||||
@@ -190,6 +190,36 @@ class AuthService:
|
||||
return
|
||||
|
||||
|
||||
def update_super_admin(self, user, user_id: int, data: UpdateSuperAdmin):
|
||||
if user["userType"] != UserType.SUPER_ADMIN:
|
||||
raise UnauthorizedException("User is not authorized to perform this action")
|
||||
|
||||
user = self.db.query(Users).filter(Users.id == user_id).first()
|
||||
|
||||
if not user:
|
||||
raise ResourceNotFoundException("User not found")
|
||||
|
||||
user.username = data.username.lower()
|
||||
|
||||
self.db.add(user)
|
||||
self.db.commit()
|
||||
|
||||
return
|
||||
|
||||
def delete_super_admin(self, user, user_id: int):
|
||||
if user["userType"] != UserType.SUPER_ADMIN:
|
||||
raise UnauthorizedException("User is not authorized to perform this action")
|
||||
|
||||
user = self.db.query(Users).filter(Users.id == user_id).first()
|
||||
|
||||
if not user:
|
||||
raise ResourceNotFoundException("User not found")
|
||||
|
||||
user.soft_delete(self.db)
|
||||
|
||||
return
|
||||
|
||||
|
||||
def forget_password(self, email: str):
|
||||
user = self.db.query(Users).filter(Users.email == email.lower()).first()
|
||||
|
||||
|
||||
@@ -35,7 +35,6 @@ class MasterAppointmentServices:
|
||||
appointment_type = MasterAppointmentTypes(**appointment_type.model_dump())
|
||||
self.db.add(appointment_type)
|
||||
self.db.commit()
|
||||
self.db.refresh(appointment_type)
|
||||
return
|
||||
|
||||
def delete_master_appointment_type(self, appointment_type_id: int):
|
||||
@@ -49,25 +48,19 @@ class MasterAppointmentServices:
|
||||
return
|
||||
|
||||
def update_master_appointment_type(self, appointment_type_id: int, appointment_type: MasterAppointmentTypeBase):
|
||||
appointment_type = self.db.query(MasterAppointmentTypes).filter(MasterAppointmentTypes.id == appointment_type_id).first()
|
||||
appointment_type_db = self.db.query(MasterAppointmentTypes).filter(MasterAppointmentTypes.id == appointment_type_id).first()
|
||||
|
||||
if appointment_type is None:
|
||||
if appointment_type_db is None:
|
||||
raise ResourceNotFoundException("Appointment type not found")
|
||||
|
||||
appointment_type.type = appointment_type.type.lower()
|
||||
|
||||
# get existing appointment type
|
||||
existing_appointment_type = self.is_appointment_type_exists(appointment_type)
|
||||
|
||||
if existing_appointment_type and existing_appointment_type.id != appointment_type_id:
|
||||
raise ResourceNotFoundException("Appointment type already exists")
|
||||
|
||||
update_data = appointment_type.model_dump(exclude_unset=True)
|
||||
appointment_type_db.type = appointment_type.type
|
||||
|
||||
for key, value in update_data.items():
|
||||
setattr(appointment_type, key, value)
|
||||
|
||||
self.db.add(appointment_type)
|
||||
self.db.add(appointment_type_db)
|
||||
self.db.commit()
|
||||
self.db.refresh(appointment_type)
|
||||
return
|
||||
Reference in New Issue
Block a user