- Backend: FastAPI app with all models, services, schemas, routes - Admin Panel: Next.js dashboard with all modules (CRM, POS, repairs, warranties, billing, notifications, etc.) - Ecommerce: storefront (submodule) + product catalog - AI Agents: .agents/ skill library for Antigravity IDE - Docs: architecture, billing, notification gateway markdown - Submodules: Backend/OpenWA, Ecommerce/File 2/storefront
118 lines
4.3 KiB
Python
118 lines
4.3 KiB
Python
from fastapi import APIRouter, Depends, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
|
|
from app.core.database.db_session import get_db
|
|
from app.models.UserModel import User
|
|
from app.core.permissions.RoleChecker import PermissionChecker
|
|
from app.schemas.NotificationTemplate import (
|
|
NotificationTemplateCreate,
|
|
NotificationTemplateUpdate,
|
|
NotificationTemplateResponse,
|
|
NotificationTemplatePreviewRequest,
|
|
NotificationTemplatePreviewResponse,
|
|
EventRegistryResponse
|
|
)
|
|
from app.models.NotificationTemplateModel import NotificationTemplate
|
|
from app.services.TemplateService import TemplateService, EVENT_REGISTRY
|
|
|
|
router = APIRouter(prefix="/api/v1/notification-templates", tags=["Notification Templates"])
|
|
|
|
@router.get("", response_model=List[NotificationTemplateResponse])
|
|
def get_templates(
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(PermissionChecker("templates.view"))
|
|
):
|
|
return TemplateService.get_templates(db, tenant_id="tenant_default")
|
|
|
|
@router.post("", response_model=NotificationTemplateResponse, status_code=status.HTTP_201_CREATED)
|
|
def create_template(
|
|
data: NotificationTemplateCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(PermissionChecker("templates.create"))
|
|
):
|
|
return TemplateService.create_template(db, data, current_user.user_id, "tenant_default")
|
|
|
|
@router.put("/{template_id}", response_model=NotificationTemplateResponse)
|
|
def update_template(
|
|
template_id: str,
|
|
data: NotificationTemplateUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(PermissionChecker("templates.update"))
|
|
):
|
|
return TemplateService.update_template(db, template_id, data, current_user.user_id, "tenant_default")
|
|
|
|
@router.patch("/{template_id}/activate", response_model=NotificationTemplateResponse)
|
|
def activate_template(
|
|
template_id: str,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(PermissionChecker("templates.update"))
|
|
):
|
|
return TemplateService.toggle_activation(db, template_id, True, current_user.user_id, "tenant_default")
|
|
|
|
@router.patch("/{template_id}/deactivate", response_model=NotificationTemplateResponse)
|
|
def deactivate_template(
|
|
template_id: str,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(PermissionChecker("templates.update"))
|
|
):
|
|
return TemplateService.toggle_activation(db, template_id, False, current_user.user_id, "tenant_default")
|
|
|
|
@router.post("/{template_id}/preview", response_model=NotificationTemplatePreviewResponse)
|
|
def preview_template_by_id(
|
|
template_id: str,
|
|
data: NotificationTemplatePreviewRequest,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(PermissionChecker("templates.view"))
|
|
):
|
|
from fastapi import HTTPException
|
|
template = db.query(NotificationTemplate).filter(
|
|
NotificationTemplate.template_id == template_id
|
|
).first()
|
|
|
|
if not template:
|
|
raise HTTPException(status_code=404, detail="Template not found")
|
|
|
|
subject, body = TemplateService.preview_template(
|
|
body_content=template.body_content,
|
|
subject=template.subject,
|
|
context=data.context
|
|
)
|
|
return {"subject": subject, "body": body}
|
|
|
|
@router.post("/preview", response_model=NotificationTemplatePreviewResponse)
|
|
def preview_template_raw(
|
|
body_content: str,
|
|
context: dict,
|
|
subject: str = None,
|
|
current_user: User = Depends(PermissionChecker("templates.view"))
|
|
):
|
|
subj, body = TemplateService.preview_template(body_content, subject, context)
|
|
return {"subject": subj, "body": body}
|
|
|
|
@router.get("/events", response_model=List[EventRegistryResponse])
|
|
def get_events(
|
|
current_user: User = Depends(PermissionChecker("templates.view"))
|
|
):
|
|
result = []
|
|
for event_key, data in EVENT_REGISTRY.items():
|
|
result.append({
|
|
"event_key": event_key,
|
|
"description": data["description"],
|
|
"variables": data["variables"]
|
|
})
|
|
return result
|
|
|
|
@router.get("/events/{event_key}/variables")
|
|
def get_event_variables(
|
|
event_key: str,
|
|
current_user: User = Depends(PermissionChecker("templates.view"))
|
|
):
|
|
from fastapi import HTTPException
|
|
if event_key not in EVENT_REGISTRY:
|
|
raise HTTPException(status_code=404, detail="Event not found")
|
|
|
|
return {
|
|
"event_key": event_key,
|
|
"variables": EVENT_REGISTRY[event_key]["variables"]
|
|
}
|