health-apps-backend/services/dashboardService.py

88 lines
3.2 KiB
Python

from database import get_db
from services.clinicDoctorsServices import ClinicDoctorsServices
from services.clinicServices import ClinicServices
from schemas.BaseSchemas import SignupPricingMasterBase
from schemas.ResponseSchemas import SignupPricingMasterResponse
from models.SignupPricingMaster import SignupPricingMaster
from exceptions import UnauthorizedException
from enums.enums import UserType
from exceptions import ResourceNotFoundException
from loguru import logger
class DashboardService:
def __init__(self):
self.db = next(get_db())
self.clinicDoctorsServices = ClinicDoctorsServices()
self.clinicServices = ClinicServices()
self.logger = logger
async def get_dashboard_counts(self, isSuperAdmin: bool):
if isSuperAdmin:
clinicCounts = await self.clinicServices.get_clinic_count()
return clinicCounts
else:
clinicDoctorsCount = await self.clinicDoctorsServices.get_doctor_status_count()
return clinicDoctorsCount
async def update_signup_pricing_master(
self, user, pricing_data: SignupPricingMasterBase
):
try:
if user["userType"] != UserType.SUPER_ADMIN:
raise UnauthorizedException(
"You are not authorized to update signup pricing master"
)
existing_pricing = self.db.query(SignupPricingMaster).first()
if existing_pricing is None:
# Create new record
new_pricing = SignupPricingMaster(
**pricing_data.model_dump()
)
self.db.add(new_pricing)
self.db.commit()
self.db.refresh(new_pricing)
response = SignupPricingMasterResponse(
**new_pricing.__dict__.copy()
)
return response
else:
# Update existing record with values from the request
existing_pricing.setup_fees = pricing_data.setup_fees
existing_pricing.subscription_fees = pricing_data.subscription_fees
existing_pricing.per_call_charges = pricing_data.per_call_charges
self.db.commit()
self.db.refresh(existing_pricing)
response = SignupPricingMasterResponse(
**existing_pricing.__dict__.copy()
)
return response
except Exception as e:
self.logger.error(f"Error updating signup pricing master: {e}")
raise e
finally:
self.db.close()
async def get_signup_pricing_master(self):
try:
signup_pricing_master = self.db.query(SignupPricingMaster).first()
if signup_pricing_master is None:
raise ResourceNotFoundException("Signup pricing master not found")
response = SignupPricingMasterResponse(
**signup_pricing_master.__dict__.copy()
)
return response
except Exception as e:
self.logger.error(f"Error getting signup pricing master: {e}")
raise e
finally:
self.db.close()