Refactor(application): Extract frequently used value into class-level constant

email_sender_hub.py: extract email_sender_manager = EmailSenderManager()
notification_hub.py: extract notification_manager = NotificationManager()
template_message_hub.py: extract template_message_manager = TemplateMessageManager()
template_message_hub.py: add more details in render_template function
This commit is contained in:
YuehuCao 2025-08-11 15:21:10 +08:00
parent cc90c862d7
commit 11c1cc811d
3 changed files with 23 additions and 42 deletions

View File

@ -4,15 +4,14 @@ from backend.business.email_sender_manager import EmailSenderManager
class EmailSenderHub: class EmailSenderHub:
def __init__(self): def __init__(self):
pass self.email_sender_manager = EmailSenderManager()
async def get_email_senders(self, tenant_id: str): async def get_email_senders(self, tenant_id: str):
"""get email senders for tenant""" """get email senders for tenant"""
if not tenant_id: if not tenant_id:
raise ValueError("tenant_id is required") raise ValueError("tenant_id is required")
email_sender_manager = EmailSenderManager() return await self.email_sender_manager.get_email_senders(tenant_id)
return await email_sender_manager.get_email_senders(tenant_id)
async def set_email_senders(self, tenant_id: str, email_senders: List[str]): async def set_email_senders(self, tenant_id: str, email_senders: List[str]):
"""set email senders for tenant""" """set email senders for tenant"""
@ -22,8 +21,7 @@ class EmailSenderHub:
if not email_senders or not isinstance(email_senders, list): if not email_senders or not isinstance(email_senders, list):
raise ValueError("email_senders must be a non-empty list") raise ValueError("email_senders must be a non-empty list")
email_sender_manager = EmailSenderManager() return await self.email_sender_manager.set_email_senders(tenant_id, email_senders)
return await email_sender_manager.set_email_senders(tenant_id, email_senders)
async def add_email_senders(self, tenant_id: str, new_senders: List[str]): async def add_email_senders(self, tenant_id: str, new_senders: List[str]):
"""add email senders to tenant""" """add email senders to tenant"""
@ -33,8 +31,7 @@ class EmailSenderHub:
if not new_senders or not isinstance(new_senders, list): if not new_senders or not isinstance(new_senders, list):
raise ValueError("new_senders must be a non-empty list") raise ValueError("new_senders must be a non-empty list")
email_sender_manager = EmailSenderManager() return await self.email_sender_manager.add_email_senders(tenant_id, new_senders)
return await email_sender_manager.add_email_senders(tenant_id, new_senders)
async def remove_email_senders(self, tenant_id: str, emails_to_remove: List[str]): async def remove_email_senders(self, tenant_id: str, emails_to_remove: List[str]):
"""remove email senders from tenant""" """remove email senders from tenant"""
@ -44,22 +41,19 @@ class EmailSenderHub:
if not emails_to_remove or not isinstance(emails_to_remove, list): if not emails_to_remove or not isinstance(emails_to_remove, list):
raise ValueError("emails_to_remove must be a non-empty list") raise ValueError("emails_to_remove must be a non-empty list")
email_sender_manager = EmailSenderManager() return await self.email_sender_manager.remove_email_senders(tenant_id, emails_to_remove)
return await email_sender_manager.remove_email_senders(tenant_id, emails_to_remove)
async def clear_email_senders(self, tenant_id: str): async def clear_email_senders(self, tenant_id: str):
"""clear email senders for tenant""" """clear email senders for tenant"""
if not tenant_id: if not tenant_id:
raise ValueError("tenant_id is required") raise ValueError("tenant_id is required")
email_sender_manager = EmailSenderManager() return await self.email_sender_manager.clear_email_senders(tenant_id)
return await email_sender_manager.clear_email_senders(tenant_id)
async def delete_email_sender(self, tenant_id: str): async def delete_email_sender(self, tenant_id: str):
"""delete email sender for tenant""" """delete email sender for tenant"""
if not tenant_id: if not tenant_id:
raise ValueError("tenant_id is required") raise ValueError("tenant_id is required")
email_sender_manager = EmailSenderManager() return await self.email_sender_manager.delete_email_sender(tenant_id)
return await email_sender_manager.delete_email_sender(tenant_id)

View File

@ -4,8 +4,8 @@ from backend.models.constants import NotificationChannel
class NotificationHub: class NotificationHub:
# def __init__(self): def __init__(self):
# pass self.notification_manager = NotificationManager()
async def enqueue_notification( async def enqueue_notification(
self, self,
@ -25,10 +25,8 @@ class NotificationHub:
raise ValueError(f"Unsupported notification channel: {channel_int}") raise ValueError(f"Unsupported notification channel: {channel_int}")
# Initialize NotificationManager with sender_id # Initialize NotificationManager with sender_id
notification_manager = NotificationManager()
# Call the enqueue_notification method in NotificationManager # Call the enqueue_notification method in NotificationManager
return await notification_manager.enqueue_notification( return await self.notification_manager.enqueue_notification(
channels=notification_channels, channels=notification_channels,
receiver_id=receiver_id, receiver_id=receiver_id,
subject=subject, subject=subject,

View File

@ -5,15 +5,14 @@ from common.constants.region import UserRegion
class TemplateMessageHub: class TemplateMessageHub:
def __init__(self): def __init__(self):
pass self.template_message_manager = TemplateMessageManager()
async def verify_tenant_access(self, template_id: str, tenant_id: str, region: int): async def verify_tenant_access(self, template_id: str, tenant_id: str, region: int):
"""get template by tenant and template ids with region""" """get template by tenant and template ids with region"""
if not template_id or not tenant_id: if not template_id or not tenant_id:
raise ValueError("template_id and tenant_id are required") raise ValueError("template_id and tenant_id are required")
template_message_manager = TemplateMessageManager() return await self.template_message_manager.verify_tenant_access(template_id, tenant_id, region)
return await template_message_manager.verify_tenant_access(template_id, tenant_id, region)
# ==================== global templates ==================== # ==================== global templates ====================
async def create_global_template( async def create_global_template(
@ -28,8 +27,7 @@ class TemplateMessageHub:
if not template_id or not subject or not body: if not template_id or not subject or not body:
raise ValueError("template_id, subject, and body are required") raise ValueError("template_id, subject, and body are required")
template_message_manager = TemplateMessageManager() return await self.template_message_manager.create_global_template_from_data(
return await template_message_manager.create_global_template_from_data(
template_id=template_id, template_id=template_id,
region=region, region=region,
subject=subject, subject=subject,
@ -50,28 +48,24 @@ class TemplateMessageHub:
if invalid_fields: if invalid_fields:
raise ValueError(f"Invalid update fields: {invalid_fields}") raise ValueError(f"Invalid update fields: {invalid_fields}")
template_message_manager = TemplateMessageManager() return await self.template_message_manager.update_global_template(template_id, data, region)
return await template_message_manager.update_global_template(template_id, data, region)
async def delete_global_template(self, template_id: str): async def delete_global_template(self, template_id: str):
"""delete global template""" """delete global template"""
if not template_id: if not template_id:
raise ValueError("template_id is required") raise ValueError("template_id is required")
template_message_manager = TemplateMessageManager() return await self.template_message_manager.delete_global_template(template_id)
return await template_message_manager.delete_global_template(template_id)
async def list_global_templates(self, region: int): async def list_global_templates(self, region: int):
"""list global templates""" """list global templates"""
template_message_manager = TemplateMessageManager() return await self.template_message_manager.list_global_templates(region)
return await template_message_manager.list_global_templates(region)
# ==================== TENANT templates ==================== # ==================== TENANT templates ====================
async def list_tenant_templates(self, tenant_id: str, region: int): async def list_tenant_templates(self, tenant_id: str, region: int):
"""list tenant templates""" """list tenant templates"""
template_message_manager = TemplateMessageManager() return await self.template_message_manager.list_tenant_templates(tenant_id, region)
return await template_message_manager.list_tenant_templates(tenant_id, region)
async def assign_templates_to_tenant(self, tenant_id: str, template_ids: List[str], region: int): async def assign_templates_to_tenant(self, tenant_id: str, template_ids: List[str], region: int):
"""assign templates to tenant""" """assign templates to tenant"""
@ -81,8 +75,7 @@ class TemplateMessageHub:
if not tenant_id: if not tenant_id:
raise ValueError("tenant_id is required") raise ValueError("tenant_id is required")
template_message_manager = TemplateMessageManager() return await self.template_message_manager.assign_templates_to_tenant(tenant_id, template_ids, region)
return await template_message_manager.assign_templates_to_tenant(tenant_id, template_ids, region)
async def create_tenant_template( async def create_tenant_template(
self, self,
@ -97,8 +90,7 @@ class TemplateMessageHub:
if not template_id or not subject or not body or not tenant_id: if not template_id or not subject or not body or not tenant_id:
raise ValueError("template_id, subject, body, and tenant_id are required") raise ValueError("template_id, subject, body, and tenant_id are required")
template_message_manager = TemplateMessageManager() return await self.template_message_manager.create_tenant_template_from_data(
return await template_message_manager.create_tenant_template_from_data(
template_id=template_id, template_id=template_id,
region=region, region=region,
subject=subject, subject=subject,
@ -121,16 +113,14 @@ class TemplateMessageHub:
if invalid_fields: if invalid_fields:
raise ValueError(f"Invalid update fields: {invalid_fields}") raise ValueError(f"Invalid update fields: {invalid_fields}")
template_message_manager = TemplateMessageManager() return await self.template_message_manager.update_tenant_template(tenant_id, template_id, data, region)
return await template_message_manager.update_tenant_template(tenant_id, template_id, data, region)
async def delete_tenant_template(self, tenant_id: str, template_id: str, region: int): async def delete_tenant_template(self, tenant_id: str, template_id: str, region: int):
"""delete tenant template""" """delete tenant template"""
if not template_id or not tenant_id: if not template_id or not tenant_id:
raise ValueError("template_id and tenant_id are required") raise ValueError("template_id and tenant_id are required")
template_message_manager = TemplateMessageManager() return await self.template_message_manager.delete_tenant_template(tenant_id, template_id, region)
return await template_message_manager.delete_tenant_template(tenant_id, template_id, region)
async def render_template( async def render_template(
self, self,
@ -139,12 +129,11 @@ class TemplateMessageHub:
properties: dict, properties: dict,
region: int region: int
): ):
"""render template""" """Given the content of properties, render template with tenant_id, template_id and region"""
if not template_id or not tenant_id: if not template_id or not tenant_id:
raise ValueError("template_id and tenant_id are required") raise ValueError("template_id and tenant_id are required")
if not properties: if not properties:
raise ValueError("properties cannot be empty") raise ValueError("properties cannot be empty")
template_message_manager = TemplateMessageManager() return await self.template_message_manager.render_template(tenant_id, template_id, properties, region)
return await template_message_manager.render_template(tenant_id, template_id, properties, region)