refactor: code refactor

feat: added missing update and detele apis
This commit is contained in:
deepvasoya 2025-05-22 14:43:26 +05:30
parent 8a7f034801
commit b3a285293e
5 changed files with 67 additions and 17 deletions

View File

@ -3,9 +3,10 @@ from fastapi import APIRouter, Request
from services.clinicServices import ClinicServices from services.clinicServices import ClinicServices
from schemas.UpdateSchemas import ClinicStatusUpdate from schemas.UpdateSchemas import ClinicStatusUpdate
from schemas.ApiResponse import ApiResponse from schemas.ApiResponse import ApiResponse
from schemas.BaseSchemas import CreateSuperAdmin, MasterAppointmentTypeBase, ClinicOffersBase from schemas.BaseSchemas import MasterAppointmentTypeBase, ClinicOffersBase
from services.authService import AuthService from services.authService import AuthService
from services.masterAppointmentServices import MasterAppointmentServices from services.masterAppointmentServices import MasterAppointmentServices
from schemas.CreateSchemas import CreateSuperAdmin, UpdateSuperAdmin
from utils.constants import DEFAULT_LIMIT, DEFAULT_PAGE from utils.constants import DEFAULT_LIMIT, DEFAULT_PAGE
router = APIRouter() router = APIRouter()
@ -22,6 +23,16 @@ def create_user(req:Request, user_data: CreateSuperAdmin):
AuthService().create_super_admin(req.state.user, user_data) AuthService().create_super_admin(req.state.user, user_data)
return ApiResponse(data="OK", message="User created successfully") return ApiResponse(data="OK", message="User created successfully")
@router.put("/user/{user_id}")
def update_user(req:Request, user_id: int, user_data: UpdateSuperAdmin):
AuthService().update_super_admin(req.state.user, user_id, user_data)
return ApiResponse(data="OK", message="User updated successfully")
@router.delete("/user/{user_id}")
def delete_user(req:Request, user_id: int):
AuthService().delete_super_admin(req.state.user, user_id)
return ApiResponse(data="OK", message="User deleted successfully")
@router.get("/") @router.get("/")
def get_users(req:Request, limit:int = DEFAULT_LIMIT, page:int = DEFAULT_PAGE, search:str = ""): def get_users(req:Request, limit:int = DEFAULT_LIMIT, page:int = DEFAULT_PAGE, search:str = ""):
@ -37,6 +48,18 @@ def create_master_data(appointment_type: MasterAppointmentTypeBase):
return ApiResponse(data="OK", message="Master data created successfully") return ApiResponse(data="OK", message="Master data created successfully")
@router.delete("/master-data/{master_appointment_type_id}")
def delete_master_data(master_appointment_type_id: int):
MasterAppointmentServices().delete_master_appointment_type(master_appointment_type_id)
return ApiResponse(data="OK", message="Master data deleted successfully")
@router.put("/master-data/{master_appointment_type_id}")
def update_master_data(master_appointment_type_id: int, appointment_type: MasterAppointmentTypeBase):
MasterAppointmentServices().update_master_appointment_type(master_appointment_type_id, appointment_type)
return ApiResponse(data="OK", message="Master data updated successfully")
@router.get("/master-data") @router.get("/master-data")
def get_master_data(): def get_master_data():
appointment_types = MasterAppointmentServices().get_master_appointment_types() appointment_types = MasterAppointmentServices().get_master_appointment_types()

View File

@ -37,9 +37,6 @@ class ResetPasswordBase(BaseModel):
token: str token: str
password: str password: str
class CreateSuperAdmin(BaseModel):
username:str
email:EmailStr
# Base schemas (shared attributes for create/read operations) # Base schemas (shared attributes for create/read operations)
class ClinicBase(BaseModel): class ClinicBase(BaseModel):

View File

@ -8,6 +8,13 @@ from enums.enums import AppointmentStatus
class ClinicCreate(ClinicBase): class ClinicCreate(ClinicBase):
pass pass
class CreateSuperAdmin(BaseModel):
username:str
email:EmailStr
class UpdateSuperAdmin(BaseModel):
username:str
class DoctorCreate(DoctorBase): class DoctorCreate(DoctorBase):
pass pass

View File

@ -21,8 +21,8 @@ from exceptions.resource_not_found_exception import ResourceNotFoundException
from models import ResetPasswordTokens from models import ResetPasswordTokens
from utils.constants import generateOTP from utils.constants import generateOTP
from utils.password_utils import generate_reset_password_token, generate_secure_password, hash_password, verify_password from utils.password_utils import generate_reset_password_token, generate_secure_password, hash_password, verify_password
from schemas.CreateSchemas import UserCreate from schemas.CreateSchemas import CreateSuperAdmin, UpdateSuperAdmin, UserCreate
from schemas.BaseSchemas import AuthBase, AuthOTP, CreateSuperAdmin from schemas.BaseSchemas import AuthBase, AuthOTP
from exceptions.unauthorized_exception import UnauthorizedException from exceptions.unauthorized_exception import UnauthorizedException
from database import get_db from database import get_db
@ -190,6 +190,36 @@ class AuthService:
return 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): def forget_password(self, email: str):
user = self.db.query(Users).filter(Users.email == email.lower()).first() user = self.db.query(Users).filter(Users.email == email.lower()).first()

View File

@ -35,7 +35,6 @@ class MasterAppointmentServices:
appointment_type = MasterAppointmentTypes(**appointment_type.model_dump()) appointment_type = MasterAppointmentTypes(**appointment_type.model_dump())
self.db.add(appointment_type) self.db.add(appointment_type)
self.db.commit() self.db.commit()
self.db.refresh(appointment_type)
return return
def delete_master_appointment_type(self, appointment_type_id: int): def delete_master_appointment_type(self, appointment_type_id: int):
@ -49,25 +48,19 @@ class MasterAppointmentServices:
return return
def update_master_appointment_type(self, appointment_type_id: int, appointment_type: MasterAppointmentTypeBase): 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") raise ResourceNotFoundException("Appointment type not found")
appointment_type.type = appointment_type.type.lower()
# get existing appointment type # get existing appointment type
existing_appointment_type = self.is_appointment_type_exists(appointment_type) existing_appointment_type = self.is_appointment_type_exists(appointment_type)
if existing_appointment_type and existing_appointment_type.id != appointment_type_id: if existing_appointment_type and existing_appointment_type.id != appointment_type_id:
raise ResourceNotFoundException("Appointment type already exists") 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(): self.db.add(appointment_type_db)
setattr(appointment_type, key, value)
self.db.add(appointment_type)
self.db.commit() self.db.commit()
self.db.refresh(appointment_type)
return return