freeleaps-service-hub/app/notification/webapi/routes/send_notification.py

52 lines
1.8 KiB
Python

from fastapi import APIRouter, Depends, HTTPException
import traceback
from fastapi.responses import JSONResponse
from starlette.status import HTTP_500_INTERNAL_SERVER_ERROR
from app.notification.backend.application.notification_hub import NotificationHub
from pydantic import BaseModel
from app.notification.backend.models.constants import NotificationChannel
from typing import Dict
router = APIRouter()
# Define the request body schema
class NotificationRequest(BaseModel):
sender_id: str
channels: list[str]
receiver_id: str
subject: str
event: str
properties: Dict
# Web API
# Send notification to user for a certain channel
@router.post(
"/send_notification",
operation_id="send_notification",
summary="Send notification to user for a certain channel",
description="Send notification to user for a channel (e.g., sms, email, in-app, etc.)",
response_description="Success/failure response in processing the notification send request",
)
# API route to enqueue notifications
@router.post("/send_notification")
async def send_notification(request: NotificationRequest):
try:
notification_hub = NotificationHub()
await notification_hub.enqueue_notification(
sender_id="freeleaps@freeleaps.com",
channels=request.channels,
receiver_id=request.receiver_id,
subject=request.subject,
event=request.event,
properties=request.properties,
)
return JSONResponse(
content={"message": "Notifications queued successfully."}, 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 enqueue notification")