freeleaps-service-hub/apps/notification/webapi/routes/email_sender.py

213 lines
8.0 KiB
Python

from fastapi import APIRouter, HTTPException, Depends
import traceback
from fastapi.responses import JSONResponse
from starlette.status import HTTP_500_INTERNAL_SERVER_ERROR
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from backend.application.email_sender_hub import EmailSenderHub
from common.token.token_manager import TokenManager
from pydantic import BaseModel
from typing import List
router = APIRouter()
security = HTTPBearer()
token_manager = TokenManager()
email_sender_hub = EmailSenderHub()
# Define the request body schema
class EmailSenderSetRequest(BaseModel):
email_senders: List[str]
class EmailSenderAddRequest(BaseModel):
new_senders: List[str]
class EmailSenderRemoveRequest(BaseModel):
emails_to_remove: List[str]
# check credentials for admin and tenant
def admin_only(credentials: HTTPAuthorizationCredentials = Depends(security)):
try:
payload = token_manager.decode_token(credentials.credentials)
# handle token generated by structure Authentication service
if "subject" in payload:
# Authentication format: payload.subject.role
role = payload.get("subject", {}).get("role")
else:
# local generated format: payload.role
role = payload.get("role")
# according to AdministrativeRole enum defined in authentication service, ADMINISTRATOR = 8
if role not in ["admin", 8]:
raise HTTPException(status_code=403, detail="Admin access required")
return payload
except Exception:
raise HTTPException(status_code=401, detail="Invalid token")
def tenant_only(credentials: HTTPAuthorizationCredentials = Depends(security)):
try:
payload = token_manager.decode_token(credentials.credentials)
# handle token generated by structure Authentication service
if "subject" in payload:
tenant_id = payload.get("subject", {}).get("id")
user_id = payload.get("subject", {}).get("id")
role = payload.get("subject", {}).get("role")
else:
tenant_id = payload.get("tenant_id")
user_id = payload.get("id")
role = payload.get("role")
if not tenant_id:
raise HTTPException(status_code=403, detail="Tenant access required")
# according to AdministrativeRole enum defined in authentication service, BUSINESS = 2
if role not in [2, "tenant"]:
raise HTTPException(status_code=403, detail="Tenant access required")
payload["tenant_id"] = tenant_id
payload["user_id"] = user_id
payload["is_admin"] = False
return payload
except Exception:
raise HTTPException(status_code=401, detail="Invalid token")
# Web API
# Get email senders for tenant
@router.get(
"/email_senders/get",
dependencies=[Depends(tenant_only)],
operation_id="get_email_senders",
summary="Get email senders for tenant",
description="Retrieve the list of email senders configured for the current tenant",
response_description="List of email sender addresses"
)
async def get_email_senders(payload: dict = Depends(tenant_only)):
try:
tenant_id = payload.get("tenant_id")
result = await email_sender_hub.get_email_senders(tenant_id)
return JSONResponse(
content={"success": True, "email_senders": result},
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="Failed to get email senders")
# Set email senders for tenant
@router.post(
"/email_senders/set",
dependencies=[Depends(tenant_only)],
operation_id="set_email_senders",
summary="Set email senders for tenant",
description="Set the complete list of email senders for the specified tenant",
response_description="Success/failure response in setting email senders"
)
async def set_email_senders(request: EmailSenderSetRequest, payload: dict = Depends(tenant_only)):
try:
tenant_id = payload.get("tenant_id")
result = await email_sender_hub.set_email_senders(tenant_id, request.email_senders)
return JSONResponse(
content=result,
status_code=200
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
import traceback
traceback.print_exc()
raise HTTPException(status_code=500, detail=f"Failed to set email senders: {str(e)}")
# Add email senders to tenant
@router.post(
"/email_senders/add",
dependencies=[Depends(tenant_only)],
operation_id="add_email_senders",
summary="Add email senders to tenant",
description="Add new email senders to the existing list for the specified tenant",
response_description="Success/failure response in adding email senders"
)
async def add_email_senders(request: EmailSenderAddRequest, payload: dict = Depends(tenant_only)):
try:
tenant_id = payload.get("tenant_id")
result = await email_sender_hub.add_email_senders(tenant_id, request.new_senders)
return JSONResponse(
content=result,
status_code=200
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
import traceback
traceback.print_exc()
raise HTTPException(status_code=500, detail=f"Failed to add email senders: {str(e)}")
# Remove email senders from tenant
@router.delete(
"/email_senders/remove",
dependencies=[Depends(tenant_only)],
operation_id="remove_email_senders",
summary="Remove email senders from tenant",
description="Remove specific email senders from the tenant's list",
response_description="Success/failure response in removing email senders"
)
async def remove_email_senders(request: EmailSenderRemoveRequest, payload: dict = Depends(tenant_only)):
try:
tenant_id = payload.get("tenant_id")
result = await email_sender_hub.remove_email_senders(tenant_id, request.emails_to_remove)
return JSONResponse(
content=result,
status_code=200
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
import traceback
traceback.print_exc()
raise HTTPException(status_code=500, detail=f"Failed to remove email senders: {str(e)}")
# Clear all email senders for tenant
@router.delete(
"/email_senders/clear",
dependencies=[Depends(tenant_only)],
operation_id="clear_email_senders",
summary="Clear all email senders for tenant",
description="Remove all email senders from the current tenant's list",
response_description="Success/failure response in clearing email senders"
)
async def clear_email_senders(payload: dict = Depends(tenant_only)):
try:
tenant_id = payload.get("tenant_id")
result = await email_sender_hub.clear_email_senders(tenant_id)
return JSONResponse(
content=result,
status_code=200
)
except Exception as e:
raise HTTPException(status_code=500, detail="Failed to clear email senders")
# Delete email sender configuration for tenant
@router.delete(
"/email_senders/delete/{tenant_id}",
dependencies=[Depends(admin_only)],
operation_id="delete_email_sender",
summary="Delete email sender configuration for tenant",
description="Completely delete the email sender configuration for the specified tenant",
response_description="Success/failure response in deleting email sender configuration"
)
async def delete_email_sender(tenant_id: str):
try:
result = await email_sender_hub.delete_email_sender(tenant_id)
return JSONResponse(
content=result,
status_code=200
)
except Exception as e:
raise HTTPException(status_code=500, detail="Failed to delete email sender")