diff --git a/apis/endpoints/admin.py b/apis/endpoints/admin.py index ddcd398..87419ec 100644 --- a/apis/endpoints/admin.py +++ b/apis/endpoints/admin.py @@ -3,9 +3,10 @@ from fastapi import APIRouter, Request from services.clinicServices import ClinicServices from schemas.UpdateSchemas import ClinicStatusUpdate 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.masterAppointmentServices import MasterAppointmentServices +from schemas.CreateSchemas import CreateSuperAdmin, UpdateSuperAdmin from utils.constants import DEFAULT_LIMIT, DEFAULT_PAGE router = APIRouter() @@ -22,6 +23,16 @@ def create_user(req:Request, user_data: CreateSuperAdmin): AuthService().create_super_admin(req.state.user, user_data) 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("/") 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") +@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") def get_master_data(): appointment_types = MasterAppointmentServices().get_master_appointment_types() diff --git a/schemas/BaseSchemas.py b/schemas/BaseSchemas.py index 5484d0d..e354117 100644 --- a/schemas/BaseSchemas.py +++ b/schemas/BaseSchemas.py @@ -37,9 +37,6 @@ class ResetPasswordBase(BaseModel): token: str password: str -class CreateSuperAdmin(BaseModel): - username:str - email:EmailStr # Base schemas (shared attributes for create/read operations) class ClinicBase(BaseModel): diff --git a/schemas/CreateSchemas.py b/schemas/CreateSchemas.py index d37a232..a9d4362 100644 --- a/schemas/CreateSchemas.py +++ b/schemas/CreateSchemas.py @@ -8,6 +8,13 @@ from enums.enums import AppointmentStatus class ClinicCreate(ClinicBase): pass +class CreateSuperAdmin(BaseModel): + username:str + email:EmailStr + + +class UpdateSuperAdmin(BaseModel): + username:str class DoctorCreate(DoctorBase): pass diff --git a/services/authService.py b/services/authService.py index e5d36b2..0430d0a 100644 --- a/services/authService.py +++ b/services/authService.py @@ -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() diff --git a/services/masterAppointmentServices.py b/services/masterAppointmentServices.py index 0b992e7..71f06d3 100644 --- a/services/masterAppointmentServices.py +++ b/services/masterAppointmentServices.py @@ -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 \ No newline at end of file