Email template management
This commit is contained in:
98
backend/app/api/v1/email_templates.py
Normal file
98
backend/app/api/v1/email_templates.py
Normal file
@@ -0,0 +1,98 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
|
||||
from ...core.database import get_db
|
||||
from ...models.models import EmailTemplate
|
||||
from ...schemas import (
|
||||
EmailTemplateCreate, EmailTemplateUpdate, EmailTemplateResponse, MessageResponse
|
||||
)
|
||||
from ...api.dependencies import get_super_admin_user
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/", response_model=List[EmailTemplateResponse])
|
||||
async def list_email_templates(
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
current_user = Depends(get_super_admin_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""List all email templates (super admin only)"""
|
||||
templates = db.query(EmailTemplate).offset(skip).limit(limit).all()
|
||||
return templates
|
||||
|
||||
|
||||
@router.get("/{template_key}", response_model=EmailTemplateResponse)
|
||||
async def get_email_template(
|
||||
template_key: str,
|
||||
current_user = Depends(get_super_admin_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Get email template by key (super admin only)"""
|
||||
template = db.query(EmailTemplate).filter(EmailTemplate.template_key == template_key).first()
|
||||
|
||||
if not template:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Email template not found"
|
||||
)
|
||||
|
||||
return template
|
||||
|
||||
|
||||
@router.put("/{template_key}", response_model=EmailTemplateResponse)
|
||||
async def update_email_template(
|
||||
template_key: str,
|
||||
template_update: EmailTemplateUpdate,
|
||||
current_user = Depends(get_super_admin_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Update email template (super admin only)"""
|
||||
template = db.query(EmailTemplate).filter(EmailTemplate.template_key == template_key).first()
|
||||
|
||||
if not template:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Email template not found"
|
||||
)
|
||||
|
||||
update_data = template_update.model_dump(exclude_unset=True)
|
||||
|
||||
for field, value in update_data.items():
|
||||
setattr(template, field, value)
|
||||
|
||||
db.commit()
|
||||
db.refresh(template)
|
||||
|
||||
return template
|
||||
|
||||
|
||||
@router.post("/seed-defaults", response_model=MessageResponse)
|
||||
async def seed_default_templates(
|
||||
current_user = Depends(get_super_admin_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""Seed database with default email templates (super admin only)"""
|
||||
from ...services.email_service import get_default_templates
|
||||
|
||||
default_templates = get_default_templates()
|
||||
created_count = 0
|
||||
|
||||
for template_data in default_templates:
|
||||
# Check if template already exists
|
||||
existing = db.query(EmailTemplate).filter(
|
||||
EmailTemplate.template_key == template_data["template_key"]
|
||||
).first()
|
||||
|
||||
if not existing:
|
||||
template = EmailTemplate(**template_data)
|
||||
db.add(template)
|
||||
created_count += 1
|
||||
|
||||
if created_count > 0:
|
||||
db.commit()
|
||||
return {"message": f"Created {created_count} default email templates"}
|
||||
else:
|
||||
return {"message": "All default templates already exist"}
|
||||
Reference in New Issue
Block a user