for both template message and email senders, their workflow will follow: api->application->business->service->infra
217 lines
7.9 KiB
Python
217 lines
7.9 KiB
Python
from typing import Optional, List, Dict
|
|
from backend.services.template_message_service import TemplateMessageService
|
|
from backend.models.models import MessageTemplateDoc
|
|
from common.log.module_logger import ModuleLogger
|
|
from datetime import datetime, timezone
|
|
from common.constants.region import UserRegion
|
|
|
|
|
|
class TemplateMessageManager:
|
|
def __init__(self):
|
|
self.template_message_service = TemplateMessageService()
|
|
self.module_logger = ModuleLogger(sender_id="TemplateMessageManager")
|
|
|
|
async def verify_tenant_access(self, template_id: str, tenant_id: str, region: int) -> Optional[MessageTemplateDoc]:
|
|
"""get template by tenant and template ids with region"""
|
|
if not template_id or not tenant_id:
|
|
raise ValueError("template_id and tenant_id are required")
|
|
|
|
template = await self.template_message_service.verify_tenant_access(template_id, tenant_id, region)
|
|
await self.module_logger.log_info(
|
|
info="Template retrieved by tenant and ID",
|
|
properties={"template_id": template_id, "tenant_id": tenant_id, "region": region}
|
|
)
|
|
|
|
return template
|
|
# ==================== global templates ====================
|
|
async def create_global_template_from_data(
|
|
self,
|
|
template_id: str,
|
|
region: int,
|
|
subject: str,
|
|
body: str,
|
|
is_active: bool = True
|
|
):
|
|
"""create global template from data"""
|
|
template = MessageTemplateDoc(
|
|
template_id=template_id,
|
|
tenant_id=None,
|
|
region=region,
|
|
subject=subject,
|
|
body=body,
|
|
is_active=is_active,
|
|
created_at=datetime.now(timezone.utc)
|
|
)
|
|
|
|
result = await self.template_message_service.create_global_template(template)
|
|
|
|
await self.module_logger.log_info(
|
|
info="Global template created",
|
|
properties={"template_id": template_id, "region": region}
|
|
)
|
|
|
|
return result
|
|
|
|
async def update_global_template(self, template_id: str, data: dict, region: int):
|
|
"""update global template"""
|
|
if not data.get("subject") and not data.get("body"):
|
|
raise ValueError("At least subject or body must be provided")
|
|
|
|
result = await self.template_message_service.update_global_template(template_id, data, region)
|
|
|
|
await self.module_logger.log_info(
|
|
info="Global template updated",
|
|
properties={"template_id": template_id, "updated_fields": list(data.keys())}
|
|
)
|
|
|
|
return result
|
|
|
|
async def delete_global_template(self, template_id: str):
|
|
"""delete global template"""
|
|
|
|
result = await self.template_message_service.delete_global_template(template_id)
|
|
|
|
await self.module_logger.log_info(
|
|
info="Global template deleted",
|
|
properties={"template_id": template_id}
|
|
)
|
|
|
|
return result
|
|
|
|
async def list_global_templates(self, region: int):
|
|
"""list global templates"""
|
|
templates = await self.template_message_service.list_global_templates(region)
|
|
|
|
formatted_templates = []
|
|
for template in templates:
|
|
formatted_templates.append({
|
|
"id": str(template.id),
|
|
"template_id": template.template_id,
|
|
"subject": template.subject,
|
|
"region": template.region.value if hasattr(template.region, 'value') else template.region,
|
|
"is_active": template.is_active,
|
|
"created_at": template.created_at.isoformat() if template.created_at else None
|
|
})
|
|
|
|
return formatted_templates
|
|
|
|
# ==================== TENANT templates ====================
|
|
|
|
async def list_tenant_templates(self, tenant_id: str, region: int):
|
|
"""list tenant templates"""
|
|
|
|
templates = await self.template_message_service.list_tenant_templates(tenant_id, region)
|
|
|
|
formatted_templates = []
|
|
for template in templates:
|
|
formatted_templates.append({
|
|
"id": str(template.id),
|
|
"template_id": template.template_id,
|
|
"subject": template.subject,
|
|
"region": template.region.value if hasattr(template.region, 'value') else template.region,
|
|
"is_active": template.is_active,
|
|
"created_at": template.created_at.isoformat() if template.created_at else None
|
|
})
|
|
|
|
return formatted_templates
|
|
|
|
async def assign_templates_to_tenant(self, tenant_id: str, template_ids: List[str], region: int):
|
|
"""assign templates to tenant"""
|
|
if not template_ids:
|
|
raise ValueError("Template IDs cannot be empty")
|
|
|
|
results = await self.template_message_service.assign_template_to_tenant(tenant_id, template_ids, region)
|
|
|
|
success_count = sum(1 for r in results if r.get("success"))
|
|
await self.module_logger.log_info(
|
|
info="Templates assigned to tenant",
|
|
properties={
|
|
"tenant_id": tenant_id,
|
|
"region": region,
|
|
"total_requested": len(template_ids),
|
|
"success_count": success_count
|
|
}
|
|
)
|
|
|
|
return results
|
|
|
|
async def create_tenant_template_from_data(
|
|
self,
|
|
template_id: str,
|
|
region: int,
|
|
subject: str,
|
|
body: str,
|
|
tenant_id: str,
|
|
is_active: bool = True
|
|
):
|
|
"""create tenant template from data"""
|
|
template = MessageTemplateDoc(
|
|
template_id=template_id,
|
|
tenant_id=tenant_id,
|
|
region=region,
|
|
subject=subject,
|
|
body=body,
|
|
is_active=is_active,
|
|
created_at=datetime.now(timezone.utc)
|
|
)
|
|
|
|
result = await self.template_message_service.create_tenant_template(tenant_id, template)
|
|
|
|
await self.module_logger.log_info(
|
|
info="Tenant template created",
|
|
properties={
|
|
"tenant_id": tenant_id,
|
|
"template_id": template_id,
|
|
"region": region
|
|
}
|
|
)
|
|
|
|
return result
|
|
|
|
async def update_tenant_template(self, tenant_id: str, template_id: str, data: dict, region: int):
|
|
"""update tenant template"""
|
|
if not data.get("subject") and not data.get("body"):
|
|
raise ValueError("At least subject or body must be provided")
|
|
|
|
result = await self.template_message_service.update_tenant_template(tenant_id, template_id, data, region)
|
|
|
|
await self.module_logger.log_info(
|
|
info="Tenant template updated",
|
|
properties={
|
|
"tenant_id": tenant_id,
|
|
"template_id": template_id,
|
|
"region": region,
|
|
"updated_fields": list(data.keys())
|
|
}
|
|
)
|
|
|
|
return result
|
|
async def delete_tenant_template(self, tenant_id: str, template_id: str, region: int):
|
|
"""delete tenant template"""
|
|
result = await self.template_message_service.delete_tenant_template(tenant_id, template_id, region)
|
|
|
|
await self.module_logger.log_info(
|
|
info="Tenant template deleted",
|
|
properties={
|
|
"tenant_id": tenant_id,
|
|
"template_id": template_id
|
|
}
|
|
)
|
|
|
|
return result
|
|
|
|
async def render_template(self, tenant_id: str, template_id: str, properties: dict, region: int):
|
|
"""render template"""
|
|
result = await self.template_message_service.render_template(tenant_id, template_id, properties, region)
|
|
|
|
await self.module_logger.log_info(
|
|
info="Template rendered",
|
|
properties={
|
|
"tenant_id": tenant_id,
|
|
"template_id": template_id,
|
|
"properties_count": len(properties)
|
|
}
|
|
)
|
|
|
|
return result
|
|
|