from fastapi import APIRouter, HTTPException from fastapi.responses import JSONResponse from pydantic import BaseModel from typing import Dict, List, Optional from backend.application.tenant_notification_hub import TenantNotificationHub router = APIRouter() tenant_notification_hub = TenantNotificationHub() class TenantEmailRequest(BaseModel): tenant_id: str template_id: str recipient_emails: List[str] subject_properties: Dict = {} body_properties: Dict = {} region: int sender_email: str = None priority: str = "normal" tracking_enabled: bool = True @router.post( "/send_tenant_email", operation_id="send_tenant_email", summary="Send email using tenant's template and email sender", description="Send email using tenant's selected template and email sender", response_description="Success/failure response in processing the tenant email send request", ) async def send_tenant_email(request: TenantEmailRequest): try: result = await tenant_notification_hub.send_tenant_email( tenant_id=request.tenant_id, template_id=request.template_id, recipient_emails=request.recipient_emails, subject_properties=request.subject_properties, body_properties=request.body_properties, region=request.region, sender_email=request.sender_email, priority=request.priority, tracking_enabled=request.tracking_enabled ) return JSONResponse( content={ "message": "Tenant email queued successfully.", "message_id": result.get("message_id"), "email_ids": result.get("email_ids", []) }, status_code=200 ) except ValueError as e: raise HTTPException(status_code=400, detail=str(e)) except Exception as e: raise HTTPException(status_code=500, detail=f"Failed to send tenant email: {str(e)}") @router.get( "/tenant_email_status/{tenant_id}", operation_id="get_tenant_email_status", summary="Get tenant email status", description="Get the status of a tenant email by email_id or recipient_email", ) async def get_tenant_email_status(tenant_id: str, email_id: str = None, recipient_email: str = None): try: status = await tenant_notification_hub.get_tenant_email_status(tenant_id, email_id, recipient_email) return JSONResponse(content=status, status_code=200) except ValueError as e: raise HTTPException(status_code=400, detail=str(e)) except Exception as e: raise HTTPException(status_code=500, detail=f"Failed to get tenant email status: {str(e)}") @router.get( "/tenant_email_status_list/{tenant_id}", operation_id="get_tenant_email_status_list", summary="Get tenant email status list", description="Get a list of email statuses for a tenant with pagination", ) async def get_tenant_email_status_list( tenant_id: str, limit: int = 50, offset: int = 0 ): try: status_list = await tenant_notification_hub.get_tenant_email_status_list(tenant_id, limit, offset) return JSONResponse(content=status_list, status_code=200) except ValueError as e: raise HTTPException(status_code=400, detail=str(e)) except Exception as e: raise HTTPException(status_code=500, detail=f"Failed to get tenant email status list: {str(e)}") ## TODO: add SendGrid Event Webhook to handle bounce and tracking